use of org.jclouds.compute.domain.ExecResponse in project fabric8 by jboss-fuse.
the class CloudContainerInstallationTask method install.
public CreateJCloudsContainerMetadata install() {
LoginCredentials credentials = nodeMetadata.getCredentials();
// For some cloud providers return do not allow shell access to root, so the user needs to be overrided.
if (!Strings.isNullOrEmpty(options.getUser()) && credentials != null) {
credentials = credentials.toBuilder().user(options.getUser()).build();
} else {
credentials = nodeMetadata.getCredentials();
}
String id = nodeMetadata.getId();
Set<String> publicAddresses = nodeMetadata.getPublicAddresses();
// Make a copy of the addresses, because we don't want to return back a guice implementation of Set.
Set<String> copyOfPublicAddresses = new HashSet<String>();
for (String publicAddress : publicAddresses) {
copyOfPublicAddresses.add(publicAddress);
}
CreateJCloudsContainerMetadata jCloudsContainerMetadata = new CreateJCloudsContainerMetadata();
jCloudsContainerMetadata.setCreateOptions(options);
jCloudsContainerMetadata.setNodeId(nodeMetadata.getId());
jCloudsContainerMetadata.setContainerName(containerName);
jCloudsContainerMetadata.setPublicAddresses(copyOfPublicAddresses);
jCloudsContainerMetadata.setHostname(nodeMetadata.getHostname());
if (credentials != null) {
jCloudsContainerMetadata.setIdentity(credentials.identity);
jCloudsContainerMetadata.setCredential(credentials.credential);
}
String publicAddress = "";
Properties addresses = new Properties();
if (publicAddresses != null && !publicAddresses.isEmpty()) {
publicAddress = publicAddresses.iterator().next();
addresses.put(ZkDefs.PUBLIC_IP, publicAddress);
}
options.getSystemProperties().put(ContainerProviderUtils.ADDRESSES_PROPERTY_KEY, addresses);
options.getMetadataMap().put(containerName, jCloudsContainerMetadata);
// Setup firwall for node
try {
FirewallManager firewallManager = firewallManagerFactory.getFirewallManager(computeService);
if (firewallManager.isSupported()) {
listener.onStateChange("Configuring firewall.");
String source = getOriginatingIp();
Rule httpRule = Rule.create().source("0.0.0.0/0").destination(nodeMetadata).port(8181);
firewallManager.addRules(httpRule);
if (source != null) {
Rule jmxRule = Rule.create().source(source).destination(nodeMetadata).ports(44444, 1099);
Rule sshRule = Rule.create().source(source).destination(nodeMetadata).port(8101);
Rule zookeeperRule = Rule.create().source(source).destination(nodeMetadata).port(2181);
firewallManager.addRules(jmxRule, sshRule, zookeeperRule);
}
// where firewall configuration is shared among nodes of the same groups, e.g. EC2.
if (!Strings.isNullOrEmpty(publicAddress)) {
Rule zookeeperFromTargetRule = Rule.create().source(publicAddress + "/32").destination(nodeMetadata).port(2181);
firewallManager.addRule(zookeeperFromTargetRule);
}
} else {
listener.onStateChange(String.format("Skipping firewall configuration. Not supported for provider %s", options.getProviderName()));
}
} catch (FirewallNotSupportedOnProviderException e) {
LOGGER.warn("Firewall manager not supported. Firewall will have to be manually configured.");
} catch (IOException e) {
LOGGER.warn("Could not lookup originating ip. Firewall will have to be manually configured.", e);
} catch (Throwable t) {
LOGGER.warn("Failed to setup firewall", t);
}
try {
String script = buildInstallAndStartScript(containerName, options);
listener.onStateChange(String.format("Installing fabric agent on container %s. It may take a while...", containerName));
ExecResponse response = null;
String uploadPath = "/tmp/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip";
URL distributionURL = options.getProxyUri().resolve("io/fabric8/fabric8-karaf/" + FabricConstants.FABRIC_VERSION + "/fabric8-karaf-" + FabricConstants.FABRIC_VERSION + ".zip").toURL();
try {
if (options.doUploadDistribution()) {
uploadToNode(computeService.getContext(), nodeMetadata, credentials, distributionURL, uploadPath);
}
if (credentials != null) {
response = computeService.runScriptOnNode(id, script, templateOptions.overrideLoginCredentials(credentials).runAsRoot(false));
} else {
response = computeService.runScriptOnNode(id, script, templateOptions);
}
} catch (AuthorizationException ex) {
throw new Exception("Failed to connect to the container via ssh.");
} catch (SshException ex) {
throw new Exception("Failed to connect to the container via ssh.");
}
if (response != null && response.getOutput() != null) {
if (response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
}
String overridenResolverValue = ContainerProviderUtils.parseResolverOverride(response.getOutput());
if (overridenResolverValue != null) {
jCloudsContainerMetadata.setOverridenResolver(overridenResolverValue);
listener.onStateChange("Overriding resolver to " + overridenResolverValue + ".");
}
} else {
jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
}
} catch (Throwable t) {
jCloudsContainerMetadata.setFailure(t);
}
// Cleanup addresses.
options.getSystemProperties().clear();
return jCloudsContainerMetadata;
}
use of org.jclouds.compute.domain.ExecResponse in project fabric8 by jboss-fuse.
the class JcloudsContainerProvider method stop.
@Override
public void stop(Container container) {
assertValid();
CreateContainerMetadata metadata = container.getMetadata();
if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
throw new IllegalStateException("Container doesn't have valid create container metadata type");
} else {
CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
try {
ComputeService computeService = getOrCreateComputeService(options);
String nodeId = jCloudsContainerMetadata.getNodeId();
Optional<RunScriptOptions> runScriptOptions = ToRunScriptOptions.withComputeService(computeService).apply(jCloudsContainerMetadata);
String script = buildStopScript(container.getId(), options);
ExecResponse response;
container.setProvisionResult(Container.PROVISION_STOPPING);
if (runScriptOptions.isPresent()) {
response = computeService.runScriptOnNode(nodeId, script, runScriptOptions.get());
} else {
response = computeService.runScriptOnNode(nodeId, script);
}
if (response == null) {
jCloudsContainerMetadata.setFailure(new Exception("No response received for fabric install script."));
} else if (response.getOutput() != null && response.getOutput().contains(ContainerProviderUtils.FAILURE_PREFIX)) {
jCloudsContainerMetadata.setFailure(new Exception(ContainerProviderUtils.parseScriptFailure(response.getOutput())));
}
} catch (Throwable t) {
container.setProvisionResult(Container.PROVISION_STOPPED);
jCloudsContainerMetadata.setFailure(t);
}
}
}
use of org.jclouds.compute.domain.ExecResponse in project SimianArmy by Netflix.
the class ScriptChaosType method apply.
/**
* Runs the script.
*/
@Override
public void apply(ChaosInstance instance) {
LOGGER.info("Running script for {} on instance {}", getKey(), instance.getInstanceId());
SshClient ssh = instance.connectSsh();
String filename = getKey().toLowerCase() + ".sh";
URL url = Resources.getResource(ScriptChaosType.class, "/scripts/" + filename);
String script;
try {
script = Resources.toString(url, Charsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("Error reading script resource", e);
}
ssh.put("/tmp/" + filename, script);
ExecResponse response = ssh.exec("/bin/bash /tmp/" + filename);
if (response.getExitStatus() != 0) {
LOGGER.warn("Got non-zero output from running script: {}", response);
}
ssh.disconnect();
}
use of org.jclouds.compute.domain.ExecResponse in project whirr by apache.
the class MahoutServiceTest method testBinMahout.
@Test(timeout = TestConstants.ITEST_TIMEOUT)
public void testBinMahout() throws Exception {
Statement binMahout = Statements.exec("source /etc/profile; $MAHOUT_HOME/bin/mahout");
Cluster.Instance mahoutInstance = findMahoutInstance();
Predicate<NodeMetadata> mahoutClientRole = and(alwaysTrue(), withIds(mahoutInstance.getId()));
Map<? extends NodeMetadata, ExecResponse> responses = controller.runScriptOnNodesMatching(clusterSpec, mahoutClientRole, binMahout);
LOG.info("Responses for Statement: " + binMahout);
for (Map.Entry<? extends NodeMetadata, ExecResponse> entry : responses.entrySet()) {
LOG.info("Node[" + entry.getKey().getId() + "]: " + entry.getValue());
}
assertResponsesContain(responses, binMahout, "Running on hadoop");
}
use of org.jclouds.compute.domain.ExecResponse in project whirr by apache.
the class ByonClusterAction method doAction.
@Override
protected void doAction(Map<InstanceTemplate, ClusterActionEvent> eventMap) throws IOException, InterruptedException {
final Collection<Future<ExecResponse>> futures = Sets.newHashSet();
List<NodeMetadata> nodes = Lists.newArrayList();
List<NodeMetadata> usedNodes = Lists.newArrayList();
int numberAllocated = 0;
Set<Instance> allInstances = Sets.newLinkedHashSet();
for (Entry<InstanceTemplate, ClusterActionEvent> entry : eventMap.entrySet()) {
final ClusterSpec clusterSpec = entry.getValue().getClusterSpec();
final StatementBuilder statementBuilder = entry.getValue().getStatementBuilder();
if (statementBuilder.isEmpty()) {
// skip
continue;
}
final ComputeServiceContext computeServiceContext = getCompute().apply(clusterSpec);
final ComputeService computeService = computeServiceContext.getComputeService();
LoginCredentials credentials = LoginCredentials.builder().user(clusterSpec.getClusterUser()).privateKey(clusterSpec.getPrivateKey()).build();
final RunScriptOptions options = overrideLoginCredentials(credentials);
if (numberAllocated == 0) {
for (ComputeMetadata compute : computeService.listNodes()) {
if (!(compute instanceof NodeMetadata)) {
throw new IllegalArgumentException("Not an instance of NodeMetadata: " + compute);
}
nodes.add((NodeMetadata) compute);
}
}
int num = entry.getKey().getNumberOfInstances();
Predicate<NodeMetadata> unused = not(in(usedNodes));
// TODO: This seems very fragile and a bug. It is not required that someone passes a hardware id,
// so this is likely to break badly. Even if there was, why do we assume it is splittable?!
// this logic should be refactored or removed ASAP
Predicate<NodeMetadata> instancePredicate = Predicates.alwaysTrue();
if (entry.getKey().getTemplate() != null) {
String hardwareId = entry.getKey().getTemplate().getHardwareId();
if (hardwareId != null)
instancePredicate = new TagsPredicate(StringUtils.split(hardwareId));
}
List<NodeMetadata> templateNodes = Lists.newArrayList(filter(nodes, and(unused, instancePredicate)));
if (templateNodes.size() < num) {
LOG.warn("Not enough nodes available for template " + StringUtils.join(entry.getKey().getRoles(), "+"));
}
templateNodes = templateNodes.subList(0, num);
usedNodes.addAll(templateNodes);
numberAllocated = usedNodes.size();
Set<Instance> templateInstances = getInstances(credentials, entry.getKey().getRoles(), templateNodes);
allInstances.addAll(templateInstances);
for (final Instance instance : templateInstances) {
futures.add(runStatementOnInstanceInCluster(statementBuilder, instance, clusterSpec, options));
}
}
for (Future<ExecResponse> future : futures) {
try {
future.get();
} catch (ExecutionException e) {
throw new IOException(e.getCause());
}
}
if (action.equals(ClusterActionHandler.BOOTSTRAP_ACTION)) {
Cluster cluster = new Cluster(allInstances);
for (ClusterActionEvent event : eventMap.values()) {
event.setCluster(cluster);
}
}
}
Aggregations