Search in sources :

Example 1 with LoginCredentials

use of org.jclouds.domain.LoginCredentials 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;
}
Also used : FirewallManager(io.fabric8.service.jclouds.firewall.FirewallManager) ExecResponse(org.jclouds.compute.domain.ExecResponse) AuthorizationException(org.jclouds.rest.AuthorizationException) FirewallNotSupportedOnProviderException(io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException) IOException(java.io.IOException) SshException(org.jclouds.ssh.SshException) Properties(java.util.Properties) URL(java.net.URL) AuthorizationException(org.jclouds.rest.AuthorizationException) FirewallNotSupportedOnProviderException(io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException) IOException(java.io.IOException) SshException(org.jclouds.ssh.SshException) LoginCredentials(org.jclouds.domain.LoginCredentials) Rule(io.fabric8.service.jclouds.firewall.Rule) HashSet(java.util.HashSet)

Example 2 with LoginCredentials

use of org.jclouds.domain.LoginCredentials 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);
        }
    }
}
Also used : RunScriptOptions(org.jclouds.compute.options.RunScriptOptions) Instance(org.apache.whirr.Cluster.Instance) ExecResponse(org.jclouds.compute.domain.ExecResponse) ClusterActionEvent(org.apache.whirr.service.ClusterActionEvent) ComputeMetadata(org.jclouds.compute.domain.ComputeMetadata) LoginCredentials(org.jclouds.domain.LoginCredentials) Builder.overrideLoginCredentials(org.jclouds.compute.options.RunScriptOptions.Builder.overrideLoginCredentials) ExecutionException(java.util.concurrent.ExecutionException) Cluster(org.apache.whirr.Cluster) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext) ClusterSpec(org.apache.whirr.ClusterSpec) IOException(java.io.IOException) ComputeService(org.jclouds.compute.ComputeService) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) StatementBuilder(org.apache.whirr.service.jclouds.StatementBuilder) Future(java.util.concurrent.Future) InstanceTemplate(org.apache.whirr.InstanceTemplate)

Example 3 with LoginCredentials

use of org.jclouds.domain.LoginCredentials in project whirr by apache.

the class ClusterController method runScriptOnNodesMatching.

public Map<? extends NodeMetadata, ExecResponse> runScriptOnNodesMatching(ClusterSpec spec, Predicate<NodeMetadata> condition, Statement statement, RunScriptOptions options) throws IOException, RunScriptOnNodesException {
    LoginCredentials credentials = LoginCredentials.builder().user(spec.getClusterUser()).privateKey(spec.getPrivateKey()).build();
    if (options == null) {
        options = defaultRunScriptOptionsForSpec(spec);
    } else if (options.getLoginUser() == null) {
        options = options.overrideLoginCredentials(credentials);
    }
    condition = Predicates.and(runningInGroup(spec.getClusterName()), condition);
    ComputeServiceContext context = getCompute().apply(spec);
    return context.getComputeService().runScriptOnNodesMatching(condition, statement, options);
}
Also used : LoginCredentials(org.jclouds.domain.LoginCredentials) Builder.overrideLoginCredentials(org.jclouds.compute.options.RunScriptOptions.Builder.overrideLoginCredentials) ComputeServiceContext(org.jclouds.compute.ComputeServiceContext)

Example 4 with LoginCredentials

use of org.jclouds.domain.LoginCredentials in project SimianArmy by Netflix.

the class ChaosInstance method connectSsh.

/**
 * Connect to the instance over SSH.
 *
 * @return {@link SshClient} for connection
 */
public SshClient connectSsh() {
    if (!sshConfig.isEnabled()) {
        throw new IllegalStateException();
    }
    LoginCredentials credentials = sshConfig.getCredentials();
    SshClient ssh = cloudClient.connectSsh(instanceId, credentials);
    return ssh;
}
Also used : LoginCredentials(org.jclouds.domain.LoginCredentials) SshClient(org.jclouds.ssh.SshClient)

Example 5 with LoginCredentials

use of org.jclouds.domain.LoginCredentials 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);
    }
    String provider = args[0];
    String identity = args[1];
    String credential = args[2];
    String groupName = args[3];
    Action action = Action.valueOf(args[4].toUpperCase());
    if ((action == Action.CHEF || action == Action.SOLO) && args.length < PARAMETERS + 1) {
        throw new IllegalArgumentException("please provide the list of recipes to install, separated by commas");
    }
    String recipes = action == Action.CHEF || action == Action.SOLO ? args[5] : "apache2";
    String minRam = System.getProperty("minRam");
    // note that you can check if a provider is present ahead of time
    checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys);
    LoginCredentials login = action != Action.DESTROY ? getLoginForCommandExecution(action) : null;
    ComputeService compute = initComputeService(provider, identity, credential);
    try {
        switch(action) {
            case ADD:
                System.out.printf(">> adding node to group %s%n", groupName);
                // Default template chooses the smallest size on an operating
                // system that tested to work with java, which tends to be Ubuntu
                // or CentOS
                TemplateBuilder templateBuilder = compute.templateBuilder();
                // can just tweak minRam
                if (minRam != null) {
                    templateBuilder.minRam(Integer.parseInt(minRam));
                }
                // 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();
                // to run commands as root, we use the runScript option in the
                // template.
                templateBuilder.options(runScript(bootInstructions));
                NodeMetadata node = getOnlyElement(compute.createNodesInGroup(groupName, 1, templateBuilder.build()));
                System.out.printf("<< node %s: %s%n", node.getId(), concat(node.getPrivateAddresses(), node.getPublicAddresses()));
            case SOLO:
                System.out.printf(">> installing [%s] on group %s as %s%n", recipes, groupName, login.identity);
                Iterable<String> recipeList = Splitter.on(',').split(recipes);
                ImmutableList.Builder<Statement> bootstrapBuilder = ImmutableList.builder();
                bootstrapBuilder.add(new InstallGit());
                // Clone community cookbooks into the node
                for (String recipe : recipeList) {
                    bootstrapBuilder.add(CloneGitRepo.builder().repository("git://github.com/opscode-cookbooks/" + recipe + ".git").directory(// 
                    "/var/chef/cookbooks/" + recipe).build());
                }
                // Configure Chef Solo to bootstrap the selected recipes
                bootstrapBuilder.add(InstallRuby.builder().build());
                bootstrapBuilder.add(InstallRubyGems.builder().build());
                bootstrapBuilder.add(// 
                ChefSolo.builder().cookbookPath(// 
                "/var/chef/cookbooks").runlist(// 
                RunList.builder().recipes(recipeList).build()).build());
                // Build the statement that will perform all the operations above
                StatementList bootstrap = new StatementList(bootstrapBuilder.build());
                // Run the script in the nodes of the group
                runScriptOnGroup(compute, login, groupName, bootstrap);
                break;
            case CHEF:
                // Create the connection to the Chef server
                ChefService chef = initChefService(System.getProperty("chef.client"), System.getProperty("chef.validator"));
                // Build the runlist for the deployed nodes
                System.out.println("Configuring node runlist in the Chef server...");
                List<String> runlist = new RunListBuilder().addRecipes(recipes.split(",")).build();
                chef.updateRunListForGroup(runlist, groupName);
                Statement chefServerBootstrap = chef.createBootstrapScriptForGroup(groupName);
                // Run the script in the nodes of the group
                System.out.printf(">> installing [%s] on group %s as %s%n", recipes, groupName, login.identity);
                runScriptOnGroup(compute, login, groupName, chefServerBootstrap);
                break;
            case DESTROY:
                System.out.printf(">> destroying nodes in group %s%n", groupName);
                // you can use predicates to select which nodes you wish to
                // destroy.
                Set<? extends NodeMetadata> destroyed = // 
                compute.destroyNodesMatching(Predicates.<NodeMetadata>and(not(TERMINATED), inGroup(groupName)));
                System.out.printf("<< destroyed nodes %s%n", destroyed);
                break;
        }
    } catch (RunNodesException e) {
        System.err.println("error adding node to group " + groupName + ": " + e.getMessage());
        error = 1;
    } catch (RunScriptOnNodesException e) {
        System.err.println("error installing " + recipes + " on group " + groupName + ": " + e.getMessage());
        error = 1;
    } catch (Exception e) {
        System.err.println("error: " + e.getMessage());
        error = 1;
    } finally {
        compute.getContext().close();
        System.exit(error);
    }
}
Also used : Statement(org.jclouds.scriptbuilder.domain.Statement) ImmutableList(com.google.common.collect.ImmutableList) TemplateBuilder(org.jclouds.compute.domain.TemplateBuilder) InstallGit(org.jclouds.scriptbuilder.statements.git.InstallGit) ChefService(org.jclouds.chef.ChefService) ComputeService(org.jclouds.compute.ComputeService) RunNodesException(org.jclouds.compute.RunNodesException) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) Builder.overrideLoginCredentials(org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials) LoginCredentials(org.jclouds.domain.LoginCredentials) RunNodesException(org.jclouds.compute.RunNodesException) StatementList(org.jclouds.scriptbuilder.domain.StatementList) RunListBuilder(org.jclouds.chef.util.RunListBuilder) RunScriptOnNodesException(org.jclouds.compute.RunScriptOnNodesException)

Aggregations

LoginCredentials (org.jclouds.domain.LoginCredentials)9 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)5 ExecResponse (org.jclouds.compute.domain.ExecResponse)4 IOException (java.io.IOException)3 ComputeService (org.jclouds.compute.ComputeService)3 RunNodesException (org.jclouds.compute.RunNodesException)3 ComputeServiceContext (org.jclouds.compute.ComputeServiceContext)2 RunScriptOnNodesException (org.jclouds.compute.RunScriptOnNodesException)2 TemplateBuilder (org.jclouds.compute.domain.TemplateBuilder)2 Builder.overrideLoginCredentials (org.jclouds.compute.options.RunScriptOptions.Builder.overrideLoginCredentials)2 Builder.overrideLoginCredentials (org.jclouds.compute.options.TemplateOptions.Builder.overrideLoginCredentials)2 Statement (org.jclouds.scriptbuilder.domain.Statement)2 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 CreateJCloudsContainerOptions (io.fabric8.service.jclouds.CreateJCloudsContainerOptions)1 FirewallManager (io.fabric8.service.jclouds.firewall.FirewallManager)1 FirewallNotSupportedOnProviderException (io.fabric8.service.jclouds.firewall.FirewallNotSupportedOnProviderException)1 Rule (io.fabric8.service.jclouds.firewall.Rule)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1