Skip to content

Commit

Permalink
Merge pull request #11 from rundeck/java-orchestrator
Browse files Browse the repository at this point in the history
Adding orchestrator java plugin
  • Loading branch information
ltamaster committed Mar 18, 2019
2 parents c844507 + 8e2e2ac commit 443a7b5
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Existing service plugins enabled on boostrap-plugin
* WorkflowNodeStep
* LogFilter
* NodeExecutor
* Orchestrator

#### for Script Plugins:
* ResourceModelSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JavaPluginTemplateGenerator extends AbstractTemplateGenerator {
private static final String TEMPLATE_BASE = "templates/java-plugin/"
private static final String JAVA_STRUCTURE = "java-plugin.structure"

private static final List ALLOWED_TEMPLATES = ["ResourceModelSource","Notification","WorkflowStep","WorkflowNodeStep","LogFilter","NodeExecutor"]
private static final List ALLOWED_TEMPLATES = ["ResourceModelSource","Notification","WorkflowStep","WorkflowNodeStep","LogFilter","NodeExecutor","Orchestrator"]

@Override
Map makeTemplateProperties(final String pluginName, final String providedService) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.plugin.${javaPluginClass.toLowerCase()};

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.workflow.StepExecutionContext;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.orchestrator.Orchestrator;
import com.dtolabs.rundeck.plugins.orchestrator.OrchestratorPlugin;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

@Plugin(service=ServiceNameConstants.Orchestrator,name="${sanitizedPluginName}")
@PluginDescription(title="${pluginName}", description="My Orchestrator plugin description")
public class ${javaPluginClass} implements OrchestratorPlugin{

@PluginProperty(title = "Count", description = "Number of nodes to select from the pool", defaultValue = "1")
protected int count;

@Override
public Orchestrator createOrchestrator(StepExecutionContext context, Collection<INodeEntry> nodes) {
return new ${javaPluginClass}Orchestrator(count, context, nodes);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.plugin.${javaPluginClass.toLowerCase()};

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.execution.workflow.StepExecutionContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.node.NodeStepResult;
import com.dtolabs.rundeck.plugins.orchestrator.Orchestrator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

/**
* Selects a random subset of the nodes
*/
public class ${javaPluginClass}Orchestrator implements Orchestrator {

Random random;
final int count;
List<INodeEntry> nodes;

public ${javaPluginClass}Orchestrator(
int count,
StepExecutionContext context,
Collection<INodeEntry> nodes
)
{
this.random = new Random();
this.count = count;
this.nodes = select(count, nodes);
}

/**
* Select count random items from the input nodes, or if nodes is smaller than count, reorders them
* @param count number of nodes
* @param nodes input nodes
* @return list of count nodes
*/
private List<INodeEntry> select(final int count, final Collection<INodeEntry> nodes) {
List<INodeEntry> source = new ArrayList<>(nodes);
List<INodeEntry> selected = new ArrayList<>();
int total = Math.min(count, nodes.size());
for (int i = 0; i < total; i++) {
selected.add(source.remove(random.nextInt(source.size())));
}
return selected;
}


@Override
public INodeEntry nextNode() {
if (nodes.size() > 0) {
return nodes.remove(0);
} else {
return null;
}
}

@Override
public void returnNode( final INodeEntry node, final boolean success, final NodeStepResult result)
{

}

@Override
public boolean isComplete() {
return nodes.size() == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.plugin.${javaPluginClass.toLowerCase()}

import com.dtolabs.rundeck.core.common.INodeEntry
import com.dtolabs.rundeck.core.common.NodeEntryImpl
import com.dtolabs.rundeck.core.execution.workflow.StepExecutionContext
import spock.lang.Specification

class ${javaPluginClass}Spec extends Specification {

private INodeEntry create(String hostname, String rankAttr=null, String rankValue=null){
INodeEntry iNodeEntry = NodeEntryImpl.create(hostname, hostname);

if(null!=rankAttr) {
iNodeEntry.getAttributes().put(rankAttr, rankValue);
}
return iNodeEntry;
}

def "simple test"() {

given:
INodeEntry node1 = create("Centos6", "memory", "1048");
INodeEntry node2 = create("Centos7", "memory", "2048");
INodeEntry node3 = create("Windows2016", "memory", "10240");
INodeEntry node4 = create("Windows10", "memory", "4096");

List<INodeEntry> nodes = Arrays.asList(node1, node2, node3, node4);

def count=1

StepExecutionContext context=Mock(StepExecutionContext)

when:
def orchestrator=new ${javaPluginClass}Orchestrator(count,context,nodes)

then:
orchestrator.nextNode()?.hostname!=null
orchestrator.nextNode()?.hostname==null

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ${pluginName} Rundeck Plugin

This is a orchestrator plugin.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version = '0.1.0'
defaultTasks 'clean','build'
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
sourceCompatibility = 1.8
ext.rundeckPluginVersion= '2.0'
ext.rundeckVersion= '${rundeckVersion}'


repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compile 'org.rundeck:rundeck-core:3.0.1+'

testCompile 'junit:junit:4.12'
testCompile "org.codehaus.groovy:groovy-all:2.4.15"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}

ext.pluginClassNames='com.plugin.${sanitizedPluginName}.${javaPluginClass}'
jar {
manifest {
attributes 'Rundeck-Plugin-Classnames': pluginClassNames
attributes 'Rundeck-Plugin-File-Version': version
attributes 'Rundeck-Plugin-Name': '${pluginName}'
attributes 'Rundeck-Plugin-Description': 'Provide a short description of your plugin here.'
attributes 'Rundeck-Plugin-Rundeck-Compatibility-Version': '3.x'
attributes 'Rundeck-Plugin-Tags': 'java,orchestrator'
attributes 'Rundeck-Plugin-Version': rundeckPluginVersion, 'Rundeck-Plugin-Archive': 'true'
}
}

wrapper {
gradleVersion = '4.4.1'
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build.gradle.template->build.gradle
README.md.template->README.md
icon.png->src/main/resources/resources/icon.png
Plugin.java.template->src/main/java/com/plugin/${javaPluginClass.toLowerCase()}/${javaPluginClass}.java
PluginOrchestrator.java.template->src/main/java/com/plugin/${javaPluginClass.toLowerCase()}/${javaPluginClass}Orchestrator.java
PluginSpec.groovy.template->src/test/groovy/com/plugin/${javaPluginClass.toLowerCase()}/${javaPluginClass}Spec.groovy

0 comments on commit 443a7b5

Please sign in to comment.