Permalink
Browse files

initial commit

  • Loading branch information...
vbehar committed Feb 20, 2011
0 parents commit c62e0cd794912add7e1025c6eaf7b46eb4e4560a
Showing with 237 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +43 −0 README
  3. +69 −0 pom.xml
  4. +121 −0 src/main/java/org/sonatype/nexus/plugins/rundeck/RundeckVersionOptionProvider.java
@@ -0,0 +1,4 @@
+.classpath
+.project
+.settings
+target
43 README
@@ -0,0 +1,43 @@
+Nexus RunDeck plugin
+https://github.com/vbehar/nexus-rundeck-plugin
+
+This is a Nexus plugin that provides a new REST API meant to be used by RunDeck as an "Option provider".
+See http://rundeck.org/docs/RunDeck-Guide.html#option-model-provider
+
+
+
+Use
+
+The plugin provides the following new resources :
+- NEXUS_HOST/service/local/rundeck/options/versions : a json array with the versions of the matching artifacts.
+ Parameters (all optional) :
+ - r : repository ID to search in (null for searching in all indexed repositories)
+ - g : groupId of the artifacts to match
+ - a : artifactId of the artifacts to match
+ - p : packaging of the artifacts to match ('jar', 'war', etc)
+ - c : classifier of the artifacts to match ('sources', 'javadoc', etc)
+ - l : limit - max number of results to return
+ - includeLatest : if true, will include the special "LATEST" version before all versions
+ - includeRelease : if true, will include the special "RELEASE" version before all versions
+
+
+
+How to install
+
+Unzip the nexus-rundeck-plugin-VERSION-bundle.zip into the plugin-repository directory (located in $NEXUS_HOME/runtime/apps/nexus/plugin-repository)
+You should have a nexus-rundeck-plugin-VERSION directory with a nexus-rundeck-plugin-VERSION.jar file inside it.
+Then, you just need to restart Nexus.
+
+
+
+Change Log
+
+- Version 1.0 : compatible with Nexus 1.9 and RunDeck 1.1
+
+
+
+Resources
+
+* Nexus : http://nexus.sonatype.org
+* RunDeck : http://www.rundeck.org
+
69 pom.xml
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+ xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.sonatype.nexus.plugins</groupId>
+ <artifactId>nexus-plugins-parent</artifactId>
+ <version>8</version>
+ </parent>
+
+ <groupId>org.sonatype.nexus.plugins</groupId>
+ <artifactId>nexus-rundeck-plugin</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>nexus-plugin</packaging>
+
+ <name>Nexus RunDeck Plugin</name>
+ <description>Nexus plugin providing support for RunDeck options.</description>
+ <url>https://github.com/vbehar/nexus-rundeck-plugin</url>
+ <scm>
+ <connection>scm:git:git://github.com/vbehar/nexus-rundeck-plugin.git</connection>
+ <developerConnection>scm:git:git@github.com:vbehar/nexus-rundeck-plugin.git</developerConnection>
+ </scm>
+
+ <developers>
+ <developer>
+ <id>vbehar</id>
+ <name>Vincent Behar</name>
+ </developer>
+ </developers>
+
+ <properties>
+ <nexus.version>1.9</nexus.version>
+ </properties>
+
+ <dependencies>
+ <!-- Nexus API -->
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-plugin-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-proxy</artifactId>
+ <version>${nexus.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.nexus</groupId>
+ <artifactId>nexus-rest-api</artifactId>
+ <version>${nexus.version}</version>
+ <scope>provided</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <!-- Nexus Plugins -->
+ <dependency>
+ <groupId>org.sonatype.nexus.plugins</groupId>
+ <artifactId>nexus-indexer-lucene-plugin</artifactId>
+ <version>${nexus.version}</version>
+ <scope>provided</scope>
+ <type>nexus-plugin</type>
+ </dependency>
+ </dependencies>
+
+</project>
@@ -0,0 +1,121 @@
+package org.sonatype.nexus.plugins.rundeck;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.inject.Inject;
+import javax.inject.Named;
+import org.apache.maven.index.ArtifactInfo;
+import org.apache.maven.index.IteratorSearchResponse;
+import org.apache.maven.index.SearchType;
+import org.codehaus.plexus.component.annotations.Component;
+import org.restlet.Context;
+import org.restlet.data.Form;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.ResourceException;
+import org.restlet.resource.Variant;
+import org.sonatype.nexus.index.Searcher;
+import org.sonatype.nexus.proxy.NoSuchRepositoryException;
+import org.sonatype.nexus.proxy.maven.metadata.operations.VersionComparator;
+import org.sonatype.plexus.rest.resource.AbstractPlexusResource;
+import org.sonatype.plexus.rest.resource.PathProtectionDescriptor;
+import org.sonatype.plexus.rest.resource.PlexusResource;
+
+/**
+ * Option provider for RunDeck - see http://rundeck.org/docs/RunDeck-Guide.html#option-model-provider<br>
+ * Provider for versions of artifacts presents in the Nexus repository, and matching the request.
+ *
+ * @author Vincent Behar
+ */
+@Component(role = PlexusResource.class, hint = "RundeckVersionOptionProvider")
+public class RundeckVersionOptionProvider extends AbstractPlexusResource {
+
+ @Inject
+ @Named("mavenCoordinates")
+ private Searcher searcher;
+
+ @Override
+ public String getResourceUri() {
+ return "/rundeck/options/versions";
+ }
+
+ @Override
+ public Object get(Context context, Request request, Response response, Variant variant) throws ResourceException {
+ // retrieve main parameters (r, g, a, v, p, c)
+ Form form = request.getResourceRef().getQueryAsForm();
+ String repositoryId = form.getFirstValue("r", null);
+ Map<String, String> terms = new HashMap<String, String>(form.getValuesMap());
+
+ // search
+ IteratorSearchResponse searchResponse;
+ try {
+ searchResponse = searcher.flatIteratorSearch(terms,
+ repositoryId,
+ null,
+ null,
+ null,
+ false,
+ SearchType.EXACT,
+ null);
+ } catch (NoSuchRepositoryException e) {
+ throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "No repository at " + repositoryId, e);
+ }
+
+ // retrieve unique versions and sort them from newest to oldest
+ List<String> versions = new ArrayList<String>();
+ for (ArtifactInfo aInfo : searchResponse.getResults()) {
+ String version = aInfo.getArtifactVersion().toString();
+ if (!versions.contains(version)) {
+ versions.add(version);
+ }
+ }
+ Comparator<String> reverseVersionComparator = Collections.reverseOrder(new VersionComparator());
+ Collections.sort(versions, reverseVersionComparator);
+
+ // optionally add "special" versions
+ if (Boolean.parseBoolean(form.getFirstValue("includeLatest", null))) {
+ versions.add(0, "LATEST");
+ }
+ if (Boolean.parseBoolean(form.getFirstValue("includeRelease", null))) {
+ versions.add(0, "RELEASE");
+ }
+
+ // optionally limit the number of versions returned
+ Integer limit;
+ try {
+ limit = Integer.parseInt(form.getFirstValue("l", null));
+ } catch (NumberFormatException e) {
+ limit = null;
+ }
+ if (limit != null && limit > 0 && versions.size() > limit) {
+ versions = new ArrayList<String>(versions.subList(0, limit));
+ }
+
+ return versions;
+ }
+
+ @Override
+ public List<Variant> getVariants() {
+ return Arrays.asList(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public PathProtectionDescriptor getResourceProtection() {
+ // should be new PathProtectionDescriptor(this.getResourceUri(), "anon");
+ // BUT https://issues.sonatype.org/browse/NEXUS-3951
+ return new PathProtectionDescriptor(getResourceUri(), "authcBasic");
+ }
+
+ @Override
+ public Object getPayloadInstance() {
+ return null;
+ }
+
+}

0 comments on commit c62e0cd

Please sign in to comment.