use of io.fabric8.service.jclouds.CreateJCloudsContainerMetadata 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 io.fabric8.service.jclouds.CreateJCloudsContainerMetadata in project fabric8 by jboss-fuse.
the class JcloudsContainerProvider method destroy.
@Override
public void destroy(Container container) {
assertValid();
CreateContainerMetadata metadata = container.getMetadata();
if (!(metadata instanceof CreateJCloudsContainerMetadata)) {
throw new IllegalStateException("Container doesn't have valid create container metadata type");
} else {
container.setProvisionResult(Container.PROVISION_DELETING);
CreateJCloudsContainerMetadata jCloudsContainerMetadata = (CreateJCloudsContainerMetadata) metadata;
CreateJCloudsContainerOptions options = jCloudsContainerMetadata.getCreateOptions();
String nodeId = jCloudsContainerMetadata.getNodeId();
ComputeService computeService = getOrCreateComputeService(options);
computeService.destroyNode(nodeId);
}
}
use of io.fabric8.service.jclouds.CreateJCloudsContainerMetadata 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 io.fabric8.service.jclouds.CreateJCloudsContainerMetadata in project fabric8 by jboss-fuse.
the class JcloudsContainerProvider method create.
@Override
public CreateJCloudsContainerMetadata create(CreateJCloudsContainerOptions input, CreationStateListener listener) throws MalformedURLException, RunNodesException, URISyntaxException, InterruptedException {
assertValid();
CreateJCloudsContainerOptions options = input.updateComputeService(getOrCreateComputeService(input));
listener.onStateChange("Looking up for compute service.");
ComputeService computeService = getOrCreateComputeService(options);
if (computeService == null) {
throw new IllegalStateException("Compute service could not be found or created.");
}
Template template = ToTemplate.apply(options);
listener.onStateChange(String.format(OVERVIEW_FORMAT, 1, options.getContextName()));
try {
Set<? extends NodeMetadata> metadata = computeService.createNodesInGroup(options.getGroup(), 1, template);
if (metadata == null || metadata.size() != 1) {
throw new IllegalStateException("JClouds created " + metadata.size() + " containers instead of 1");
}
NodeMetadata nodeMetadata = metadata.iterator().next();
switch(nodeMetadata.getStatus()) {
case RUNNING:
listener.onStateChange(String.format(NODE_CREATED_FORMAT, nodeMetadata.getName()));
break;
default:
listener.onStateChange(String.format(NODE_ERROR_FORMAT, nodeMetadata.getStatus()));
}
CloudContainerInstallationTask installationTask = new CloudContainerInstallationTask(options.getName(), nodeMetadata, options, computeService, firewallManagerFactory.get(), template.getOptions(), listener);
return installationTask.install();
} catch (Throwable ex) {
CreateJCloudsContainerMetadata failureMetadata = new CreateJCloudsContainerMetadata();
failureMetadata.setCreateOptions(options);
failureMetadata.setFailure(ex);
return failureMetadata;
}
}
use of io.fabric8.service.jclouds.CreateJCloudsContainerMetadata in project fabric8 by jboss-fuse.
the class CloudFirewallEdit method doExecute.
@Override
protected Object doExecute() throws Exception {
if (validateArguments()) {
ComputeService computeService = findTargetComputeService();
if (computeService == null) {
return null;
}
Set<String> sourceCidrs = collectCirds();
FirewallManager firewallManager = firewallManagerFactory.getFirewallManager(computeService);
NodeMetadata node = null;
if (!Strings.isNullOrEmpty(targetContainerName) && getCurator().getZookeeperClient().isConnected() && fabricService != null) {
CreateJCloudsContainerMetadata metadata = getContainerCloudMetadata(targetContainerName);
if (metadata != null && !Strings.isNullOrEmpty(metadata.getNodeId())) {
targetNodeId = metadata.getNodeId();
}
}
if (!Strings.isNullOrEmpty(targetNodeId)) {
node = computeService.getNodeMetadata(targetNodeId);
}
if (node == null) {
System.err.println("Could not find target node. Make sure you specified either --target-node-id or --target-container using a valid cloud container.");
return null;
}
if (flush) {
firewallManager.addRule(Rule.create().destination(node).flush());
return null;
}
for (String cidr : sourceCidrs) {
Rule rule = Rule.create().destination(node).source(cidr);
if (port != null && port.length > 0) {
rule = rule.ports(port);
}
if (revoke) {
firewallManager.addRule(rule.revoke());
} else {
firewallManager.addRule(rule);
}
}
}
return null;
}
Aggregations