Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit c62e0cd794912add7e1025c6eaf7b46eb4e4560a 0 parents
Vincent Behar authored February 20, 2011
4  .gitignore
... ...
@@ -0,0 +1,4 @@
  1
+.classpath
  2
+.project
  3
+.settings
  4
+target
43  README
... ...
@@ -0,0 +1,43 @@
  1
+Nexus RunDeck plugin
  2
+https://github.com/vbehar/nexus-rundeck-plugin
  3
+
  4
+This is a Nexus plugin that provides a new REST API meant to be used by RunDeck as an "Option provider".
  5
+See http://rundeck.org/docs/RunDeck-Guide.html#option-model-provider
  6
+
  7
+
  8
+
  9
+Use
  10
+
  11
+The plugin provides the following new resources :
  12
+- NEXUS_HOST/service/local/rundeck/options/versions : a json array with the versions of the matching artifacts.
  13
+  Parameters (all optional) :
  14
+  - r : repository ID to search in (null for searching in all indexed repositories)
  15
+  - g : groupId of the artifacts to match
  16
+  - a : artifactId of the artifacts to match
  17
+  - p : packaging of the artifacts to match ('jar', 'war', etc)
  18
+  - c : classifier of the artifacts to match ('sources', 'javadoc', etc)
  19
+  - l : limit - max number of results to return
  20
+  - includeLatest : if true, will include the special "LATEST" version before all versions
  21
+  - includeRelease : if true, will include the special "RELEASE" version before all versions
  22
+
  23
+
  24
+
  25
+How to install
  26
+
  27
+Unzip the nexus-rundeck-plugin-VERSION-bundle.zip into the plugin-repository directory (located in $NEXUS_HOME/runtime/apps/nexus/plugin-repository)
  28
+You should have a nexus-rundeck-plugin-VERSION directory with a nexus-rundeck-plugin-VERSION.jar file inside it.
  29
+Then, you just need to restart Nexus.
  30
+
  31
+
  32
+
  33
+Change Log
  34
+
  35
+- Version 1.0 : compatible with Nexus 1.9 and RunDeck 1.1
  36
+
  37
+
  38
+
  39
+Resources
  40
+
  41
+* Nexus : http://nexus.sonatype.org
  42
+* RunDeck : http://www.rundeck.org 
  43
+
69  pom.xml
... ...
@@ -0,0 +1,69 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
  3
+  xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4
+  <modelVersion>4.0.0</modelVersion>
  5
+  <parent>
  6
+    <groupId>org.sonatype.nexus.plugins</groupId>
  7
+    <artifactId>nexus-plugins-parent</artifactId>
  8
+    <version>8</version>
  9
+  </parent>
  10
+
  11
+  <groupId>org.sonatype.nexus.plugins</groupId>
  12
+  <artifactId>nexus-rundeck-plugin</artifactId>
  13
+  <version>1.0-SNAPSHOT</version>
  14
+  <packaging>nexus-plugin</packaging>
  15
+
  16
+  <name>Nexus RunDeck Plugin</name>
  17
+  <description>Nexus plugin providing support for RunDeck options.</description>
  18
+  <url>https://github.com/vbehar/nexus-rundeck-plugin</url>
  19
+  <scm>
  20
+    <connection>scm:git:git://github.com/vbehar/nexus-rundeck-plugin.git</connection>
  21
+    <developerConnection>scm:git:git@github.com:vbehar/nexus-rundeck-plugin.git</developerConnection>
  22
+  </scm>
  23
+
  24
+  <developers>
  25
+    <developer>
  26
+      <id>vbehar</id>
  27
+      <name>Vincent Behar</name>
  28
+    </developer>
  29
+  </developers>
  30
+
  31
+  <properties>
  32
+    <nexus.version>1.9</nexus.version>
  33
+  </properties>
  34
+
  35
+  <dependencies>
  36
+	<!-- Nexus API -->
  37
+    <dependency>
  38
+      <groupId>org.sonatype.nexus</groupId>
  39
+      <artifactId>nexus-plugin-api</artifactId>
  40
+    </dependency>
  41
+    <dependency>
  42
+      <groupId>org.sonatype.nexus</groupId>
  43
+      <artifactId>nexus-proxy</artifactId>
  44
+      <version>${nexus.version}</version>
  45
+      <scope>provided</scope>
  46
+    </dependency>
  47
+    <dependency>
  48
+      <groupId>org.sonatype.nexus</groupId>
  49
+      <artifactId>nexus-rest-api</artifactId>
  50
+      <version>${nexus.version}</version>
  51
+      <scope>provided</scope>
  52
+      <exclusions>
  53
+        <exclusion>
  54
+          <groupId>commons-logging</groupId>
  55
+          <artifactId>commons-logging-api</artifactId>
  56
+        </exclusion>
  57
+      </exclusions>
  58
+    </dependency>
  59
+  	<!-- Nexus Plugins -->
  60
+    <dependency>
  61
+      <groupId>org.sonatype.nexus.plugins</groupId>
  62
+      <artifactId>nexus-indexer-lucene-plugin</artifactId>
  63
+      <version>${nexus.version}</version>
  64
+      <scope>provided</scope>
  65
+      <type>nexus-plugin</type>
  66
+    </dependency>
  67
+  </dependencies>
  68
+
  69
+</project>
121  src/main/java/org/sonatype/nexus/plugins/rundeck/RundeckVersionOptionProvider.java
... ...
@@ -0,0 +1,121 @@
  1
+package org.sonatype.nexus.plugins.rundeck;
  2
+
  3
+import java.util.ArrayList;
  4
+import java.util.Arrays;
  5
+import java.util.Collections;
  6
+import java.util.Comparator;
  7
+import java.util.HashMap;
  8
+import java.util.List;
  9
+import java.util.Map;
  10
+import javax.inject.Inject;
  11
+import javax.inject.Named;
  12
+import org.apache.maven.index.ArtifactInfo;
  13
+import org.apache.maven.index.IteratorSearchResponse;
  14
+import org.apache.maven.index.SearchType;
  15
+import org.codehaus.plexus.component.annotations.Component;
  16
+import org.restlet.Context;
  17
+import org.restlet.data.Form;
  18
+import org.restlet.data.MediaType;
  19
+import org.restlet.data.Request;
  20
+import org.restlet.data.Response;
  21
+import org.restlet.data.Status;
  22
+import org.restlet.resource.ResourceException;
  23
+import org.restlet.resource.Variant;
  24
+import org.sonatype.nexus.index.Searcher;
  25
+import org.sonatype.nexus.proxy.NoSuchRepositoryException;
  26
+import org.sonatype.nexus.proxy.maven.metadata.operations.VersionComparator;
  27
+import org.sonatype.plexus.rest.resource.AbstractPlexusResource;
  28
+import org.sonatype.plexus.rest.resource.PathProtectionDescriptor;
  29
+import org.sonatype.plexus.rest.resource.PlexusResource;
  30
+
  31
+/**
  32
+ * Option provider for RunDeck - see http://rundeck.org/docs/RunDeck-Guide.html#option-model-provider<br>
  33
+ * Provider for versions of artifacts presents in the Nexus repository, and matching the request.
  34
+ * 
  35
+ * @author Vincent Behar
  36
+ */
  37
+@Component(role = PlexusResource.class, hint = "RundeckVersionOptionProvider")
  38
+public class RundeckVersionOptionProvider extends AbstractPlexusResource {
  39
+
  40
+    @Inject
  41
+    @Named("mavenCoordinates")
  42
+    private Searcher searcher;
  43
+
  44
+    @Override
  45
+    public String getResourceUri() {
  46
+        return "/rundeck/options/versions";
  47
+    }
  48
+
  49
+    @Override
  50
+    public Object get(Context context, Request request, Response response, Variant variant) throws ResourceException {
  51
+        // retrieve main parameters (r, g, a, v, p, c)
  52
+        Form form = request.getResourceRef().getQueryAsForm();
  53
+        String repositoryId = form.getFirstValue("r", null);
  54
+        Map<String, String> terms = new HashMap<String, String>(form.getValuesMap());
  55
+
  56
+        // search
  57
+        IteratorSearchResponse searchResponse;
  58
+        try {
  59
+            searchResponse = searcher.flatIteratorSearch(terms,
  60
+                                                         repositoryId,
  61
+                                                         null,
  62
+                                                         null,
  63
+                                                         null,
  64
+                                                         false,
  65
+                                                         SearchType.EXACT,
  66
+                                                         null);
  67
+        } catch (NoSuchRepositoryException e) {
  68
+            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No repository at " + repositoryId, e);
  69
+        }
  70
+
  71
+        // retrieve unique versions and sort them from newest to oldest
  72
+        List<String> versions = new ArrayList<String>();
  73
+        for (ArtifactInfo aInfo : searchResponse.getResults()) {
  74
+            String version = aInfo.getArtifactVersion().toString();
  75
+            if (!versions.contains(version)) {
  76
+                versions.add(version);
  77
+            }
  78
+        }
  79
+        Comparator<String> reverseVersionComparator = Collections.reverseOrder(new VersionComparator());
  80
+        Collections.sort(versions, reverseVersionComparator);
  81
+
  82
+        // optionally add "special" versions
  83
+        if (Boolean.parseBoolean(form.getFirstValue("includeLatest", null))) {
  84
+            versions.add(0, "LATEST");
  85
+        }
  86
+        if (Boolean.parseBoolean(form.getFirstValue("includeRelease", null))) {
  87
+            versions.add(0, "RELEASE");
  88
+        }
  89
+
  90
+        // optionally limit the number of versions returned
  91
+        Integer limit;
  92
+        try {
  93
+            limit = Integer.parseInt(form.getFirstValue("l", null));
  94
+        } catch (NumberFormatException e) {
  95
+            limit = null;
  96
+        }
  97
+        if (limit != null && limit > 0 && versions.size() > limit) {
  98
+            versions = new ArrayList<String>(versions.subList(0, limit));
  99
+        }
  100
+
  101
+        return versions;
  102
+    }
  103
+
  104
+    @Override
  105
+    public List<Variant> getVariants() {
  106
+        return Arrays.asList(new Variant(MediaType.APPLICATION_JSON));
  107
+    }
  108
+
  109
+    @Override
  110
+    public PathProtectionDescriptor getResourceProtection() {
  111
+        // should be new PathProtectionDescriptor(this.getResourceUri(), "anon");
  112
+        // BUT https://issues.sonatype.org/browse/NEXUS-3951
  113
+        return new PathProtectionDescriptor(getResourceUri(), "authcBasic");
  114
+    }
  115
+
  116
+    @Override
  117
+    public Object getPayloadInstance() {
  118
+        return null;
  119
+    }
  120
+
  121
+}

0 notes on commit c62e0cd

Please sign in to comment.
Something went wrong with that request. Please try again.