use of org.jclouds.compute.domain.Template in project whirr by apache.
the class ZooKeeperService method launchCluster.
@Override
public ZooKeeperCluster launchCluster(ClusterSpec clusterSpec) throws IOException {
ComputeServiceContext computeServiceContext = ComputeServiceContextBuilder.build(clusterSpec);
ComputeService computeService = computeServiceContext.getComputeService();
byte[] bootScript = RunUrlBuilder.runUrls("sun/java/install", "apache/zookeeper/install");
TemplateBuilder templateBuilder = computeService.templateBuilder().osFamily(UBUNTU).options(runScript(bootScript).installPrivateKey(clusterSpec.readPrivateKey()).authorizePublicKey(clusterSpec.readPublicKey()));
// TODO extract this logic elsewhere
if (clusterSpec.getProvider().equals("ec2"))
templateBuilder.imageNameMatches(".*10\\.?04.*").osDescriptionMatches("^ubuntu-images.*").architecture(Architecture.X86_32);
Template template = templateBuilder.build();
InstanceTemplate instanceTemplate = clusterSpec.getInstanceTemplate(ZOOKEEPER_ROLE);
checkNotNull(instanceTemplate);
int ensembleSize = instanceTemplate.getNumberOfInstances();
Set<? extends NodeMetadata> nodeMap;
try {
nodeMap = computeService.runNodesWithTag(clusterSpec.getClusterName(), ensembleSize, template);
} catch (RunNodesException e) {
// TODO: can we do better here - proceed if ensemble is big enough?
throw new IOException(e);
}
FirewallSettings.authorizeIngress(computeServiceContext, nodeMap, clusterSpec, CLIENT_PORT);
List<NodeMetadata> nodes = Lists.newArrayList(nodeMap);
// Pass list of all servers in ensemble to configure script.
// Position is significant: i-th server has id i.
String servers = Joiner.on(' ').join(getPrivateIps(nodes));
byte[] configureScript = RunUrlBuilder.runUrls("apache/zookeeper/post-configure " + servers);
try {
computeService.runScriptOnNodesMatching(runningWithTag(clusterSpec.getClusterName()), configureScript);
} catch (RunScriptOnNodesException e) {
// TODO: retry
throw new IOException(e);
}
String hosts = Joiner.on(',').join(getHosts(nodes));
return new ZooKeeperCluster(getInstances(nodes), hosts);
}
use of org.jclouds.compute.domain.Template in project hive by apache.
the class TestCloudExecutionContextProvider method setup.
@Before
public void setup() throws Exception {
dataDir = baseDir.newFolder().getAbsolutePath();
workingDir = baseDir.newFolder().getAbsolutePath();
cloudComputeService = mock(CloudComputeService.class);
sshCommandExecutor = new MockSSHCommandExecutor(LOG);
node1 = mock(NodeMetadata.class);
node2 = mock(NodeMetadata.class);
node3 = mock(NodeMetadata.class);
template = mock(Template.class);
when(template.getLocation()).thenReturn(mock(Location.class));
when(template.getImage()).thenReturn(mock(Image.class));
when(template.getHardware()).thenReturn(mock(Hardware.class));
when(node1.getHostname()).thenReturn("node1");
when(node1.getPublicAddresses()).thenReturn(Collections.singleton("1.1.1.1"));
when(node2.getHostname()).thenReturn("node2");
when(node2.getPublicAddresses()).thenReturn(Collections.singleton("1.1.1.2"));
when(node3.getHostname()).thenReturn("node3");
when(node3.getPublicAddresses()).thenReturn(Collections.singleton("1.1.1.3"));
runNodesException = new RunNodesException("", 2, template, Collections.singleton(node1), Collections.<String, Exception>emptyMap(), Collections.singletonMap(node2, new Exception("For testing")));
}
use of org.jclouds.compute.domain.Template in project legacy-jclouds-examples by jclouds.
the class MainApp method main.
public static void main(String[] args) {
if (args.length < PARAMETERS)
throw new IllegalArgumentException(INVALID_SYNTAX);
// Args
String accesskeyid = args[0];
String secretkey = args[1];
String group = args[2];
String command = args[3];
// Init
ComputeService compute = new ComputeServiceContextFactory().createContext("aws-ec2", accesskeyid, secretkey).getComputeService();
// wait up to 60 seconds for ssh to be accessible
RetryablePredicate<IPSocket> socketTester = new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 60, 1, 1, TimeUnit.SECONDS);
try {
if (command.equals("create")) {
Template template = compute.templateBuilder().build();
template.getOptions().as(AWSEC2TemplateOptions.class).spotPrice(0.03f).authorizePublicKey(Files.toString(new File(System.getProperty("user.home") + "/.ssh/id_rsa.pub"), Charsets.UTF_8));
System.out.printf(">> running one spot node type(%s) with ami(%s) in group(%s)%n", template.getHardware().getProviderId(), template.getImage().getId(), group);
// run only a single node
NodeMetadata node = Iterables.getOnlyElement(compute.createNodesInGroup(group, 1, template));
System.out.printf("<< running node(%s)%n", node.getId());
IPSocket socket = new IPSocket(Iterables.get(node.getPublicAddresses(), 0), node.getLoginPort());
if (socketTester.apply(socket)) {
System.out.printf("<< socket ready [%s] node(%s)%n", socket, node.getId());
System.out.printf("ssh to node with the following command:%n ssh %s@%s%n", node.getCredentials().identity, socket.getAddress());
System.exit(0);
} else {
System.out.printf("<< socket not ready [%s] node(%s)%n", socket, node.getId());
}
} else if (command.equals("destroy")) {
System.out.printf(">> destroying nodes in group(%s)%n", group);
Set<? extends NodeMetadata> destroyed = compute.destroyNodesMatching(NodePredicates.inGroup(group));
System.out.printf("<< destroyed(%d)%n", destroyed.size());
System.exit(0);
} else {
System.err.println(INVALID_SYNTAX);
System.exit(1);
}
} catch (RunNodesException e) {
System.err.println(e.getMessage());
for (NodeMetadata node : e.getNodeErrors().keySet()) compute.destroyNode(node.getId());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
} finally {
compute.getContext().close();
}
}
use of org.jclouds.compute.domain.Template in project legacy-jclouds-examples by jclouds.
the class CreateVolumeAndAttach method createServer.
private NodeMetadata createServer() throws RunNodesException, TimeoutException {
Template template = compute.templateBuilder().locationId(Constants.ZONE).osDescriptionMatches(".*CentOS 6.2.*").minRam(512).build();
System.out.println("Create Server");
Set<? extends NodeMetadata> nodes = compute.createNodesInGroup(Constants.NAME, 1, template);
NodeMetadata nodeMetadata = nodes.iterator().next();
String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
// We set the password to something we know so we can login in the DetachVolume example
nova.getApi().getServerApiForZone(Constants.ZONE).changeAdminPass(nodeMetadata.getProviderId(), Constants.PASSWORD);
System.out.println(" " + nodeMetadata);
System.out.println(" Login: ssh " + nodeMetadata.getCredentials().getUser() + "@" + publicAddress);
System.out.println(" Password: " + Constants.PASSWORD);
return nodeMetadata;
}
use of org.jclouds.compute.domain.Template in project legacy-jclouds-examples by jclouds.
the class CreateServer method createServer.
/**
* Create a server based on a Template. This method uses Template.fromHardware() and Template.fromImage() to
* also demonstrate iterating through Hardware and Images. Alternatively you do the same without iterating
* using the following Template.
*
* Template template = compute.templateBuilder()
* .locationId(getLocationId())
* .osFamily(OsFamily.UBUNTU)
* .osVersionMatches("12.04")
* .minRam(512)
* .build();
*/
private void createServer() throws RunNodesException, TimeoutException {
Template template = compute.templateBuilder().locationId(getLocationId()).fromHardware(getHardware()).fromImage(getImage()).build();
System.out.println("Create Server");
// This method will continue to poll for the server status and won't return until this server is ACTIVE
// If you want to know what's happening during the polling, enable logging. See
// /jclouds-example/rackspace/src/main/java/org/jclouds/examples/rackspace/Logging.java
Set<? extends NodeMetadata> nodes = compute.createNodesInGroup(Constants.NAME, 1, template);
NodeMetadata nodeMetadata = nodes.iterator().next();
String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
System.out.println(" " + nodeMetadata);
System.out.println(" Login: ssh " + nodeMetadata.getCredentials().getUser() + "@" + publicAddress);
System.out.println(" Password: " + nodeMetadata.getCredentials().getPassword());
}
Aggregations