Skip to content

Instantly share code, notes, and snippets.

@ahonor
Created April 14, 2014 18:24
Show Gist options
  • Save ahonor/10671542 to your computer and use it in GitHub Desktop.
Save ahonor/10671542 to your computer and use it in GitHub Desktop.
/*
* CreateInstanceStep.java
*
* User: Alex Honor <a href="mailto:[email protected]">[email protected]</a>
* Created: 04/14/14 9:27 AM
*
*/
package org.rundeck.plugin.openstack;
import com.dtolabs.rundeck.core.execution.workflow.steps.FailureReason;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.core.plugins.configuration.Describable;
import com.dtolabs.rundeck.core.plugins.configuration.Description;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import com.dtolabs.rundeck.plugins.step.StepPlugin;
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;
import com.dtolabs.rundeck.plugins.util.PropertyBuilder;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.compute.domain.TemplateBuilder;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import java.util.Map;
import java.util.Properties;
/**
*/
@Plugin(name = CreateInstanceStep.SERVICE_PROVIDER_NAME, service = ServiceNameConstants.WorkflowStep)
public class CreateInstanceStep implements StepPlugin, Describable {
/**
* Define a name used to identify your plugin. It is a good idea to use a fully qualified package-style name.
*/
public static final String SERVICE_PROVIDER_NAME = "org.rundeck.plugin.openstack.CreateInstanceStep";
private String endpoint;
private String identity;
private String password;
private String provider;
private String osFamily;
private String instanceName;
private String flavor;
private String imageName;
private String groupName;
CreateInstanceStep(Properties properties) {
endpoint = properties.getProperty("endpoint");
identity = properties.getProperty("identity");
password = properties.getProperty("password");
provider = properties.getProperty("provider");
osFamily = properties.getProperty("osFamily");
instanceName = properties.getProperty("instanceName");
groupName = properties.getProperty("groupName");
flavor = properties.getProperty("flavor");
imageName = properties.getProperty("imageName");
}
/**
*
*/
public Description getDescription() {
return DescriptionBuilder.builder()
.name(SERVICE_PROVIDER_NAME)
.title("Create Instance Step")
.description("Creates a new instance")
.property(PropertyBuilder.builder()
.select("provider")
.title("Provider")
.description("Compute service provider")
.required(false)
.values("openstack-nova")
.build()
)
.property(PropertyBuilder.builder()
.string("endpoint")
.title("API endpoint")
.description("This is the Keystone endpoint to connect. eg, http://172.16.0.1:5000/v2.0/")
.required(true)
.build()
)
.property(PropertyBuilder.builder()
.string("identity")
.title("Identity")
.description("use the tenant name and user name with a colon between them instead of just a user name")
.required(true)
.build()
)
.property(PropertyBuilder.builder()
.string("password")
.title("Password")
.description("the password")
.required(true)
.build()
)
.property(PropertyBuilder.builder()
.string("flavor")
.title("Flavor")
.description("The hardware flavor")
.required(true)
.build()
)
.property(PropertyBuilder.builder()
.booleanType("os64Bit")
.title("64 bit")
.description("Operating System 64bit?")
.required(false)
.defaultValue("false")
.build()
)
.property(PropertyBuilder.builder()
.freeSelect("osFamily")
.title("OS Family")
.description("The OS Family")
.required(false)
.defaultValue("ubuntu")
.values("ubuntu", "centos")
.build()
)
.property(PropertyBuilder.builder()
.string("instanceName")
.title("Instance Name")
.description("The instance name")
.required(true)
.build()
)
.property(PropertyBuilder.builder()
.string("groupName")
.title("Group Name")
.description("The group name")
.required(true)
.build()
)
.build();
}
/**
* This enum lists the known reasons this plugin might fail
*/
static enum Reason implements FailureReason {
ExampleReason
}
/**
* Here is the meat of the plugin implementation, which should perform the appropriate logic for your plugin.
* <p/>
* The {@link PluginStepContext} provides access to the appropriate Nodes, the configuration of the plugin, and
* details about the step number and context.
*/
public void executeStep(final PluginStepContext context, final Map<String, Object> configuration) throws
StepException {
ComputeService compute = initComputeService(provider, endpoint, identity, password);
// note this will create a user with the same name as you on the
// node. ex. you can connect via ssh publicip
//Statement bootInstructions = AdminAccess.standard();
TemplateBuilder templateBuilder = compute.templateBuilder();
try {
compute.createNodesInGroup(groupName, 1, templateBuilder.build());
} catch (Exception e) {
throw new StepException(e.getMessage(),Reason.ExampleReason);
}
}
private static ComputeService initComputeService(String provider, String endpoint, String identity, String password) {
Properties properties = new Properties();
ContextBuilder builder = ContextBuilder.newBuilder(provider)
.endpoint(endpoint)
.credentials(identity, password)
.overrides(properties);
System.out.printf(">> initializing %s%n", builder.getApiMetadata());
return builder.buildView(ComputeServiceContext.class).getComputeService();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment