Search in sources :

Example 1 with PasswordDataAndPrivateKey

use of org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey in project legacy-jclouds-examples by jclouds.

the class WindowsInstanceStarter method run.

public void run() {
    final String region = arguments.getRegion();
    // Build a template
    Template template = computeService.templateBuilder().locationId(region).imageNameMatches(arguments.getImageNamePattern()).hardwareId(arguments.getInstanceType()).build();
    logger.info("Selected AMI is: %s", template.getImage().toString());
    template.getOptions().inboundPorts(3389);
    // Create the node
    logger.info("Creating node and waiting for it to become available");
    Set<? extends NodeMetadata> nodes = null;
    try {
        nodes = computeService.createNodesInGroup("basic-ami", 1, template);
    } catch (RunNodesException e) {
        logger.error(e, "Unable to start nodes; aborting");
        return;
    }
    NodeMetadata node = Iterables.getOnlyElement(nodes);
    // Wait for the administrator password
    logger.info("Waiting for administrator password to become available");
    // This predicate will call EC2's API to get the Windows Administrator
    // password, and returns true if there is password data available.
    Predicate<String> passwordReady = new Predicate<String>() {

        @Override
        public boolean apply(@Nullable String s) {
            if (Strings.isNullOrEmpty(s))
                return false;
            PasswordData data = ec2Client.getWindowsServices().getPasswordDataInRegion(region, s);
            if (data == null)
                return false;
            return !Strings.isNullOrEmpty(data.getPasswordData());
        }
    };
    // Now wait, using RetryablePredicate
    final int maxWait = 600;
    final int period = 10;
    final TimeUnit timeUnit = TimeUnit.SECONDS;
    RetryablePredicate<String> passwordReadyRetryable = new RetryablePredicate<String>(passwordReady, maxWait, period, timeUnit);
    boolean isPasswordReady = passwordReadyRetryable.apply(node.getProviderId());
    if (!isPasswordReady) {
        logger.error("Password is not ready after %s %s - aborting and shutting down node", maxWait, timeUnit.toString());
        computeService.destroyNode(node.getId());
        return;
    }
    // Now we can get the password data, decrypt it, and get a LoginCredentials instance
    PasswordDataAndPrivateKey dataAndKey = new PasswordDataAndPrivateKey(ec2Client.getWindowsServices().getPasswordDataInRegion(region, node.getProviderId()), node.getCredentials().getPrivateKey());
    WindowsLoginCredentialsFromEncryptedData f = context.getUtils().getInjector().getInstance(WindowsLoginCredentialsFromEncryptedData.class);
    LoginCredentials credentials = f.apply(dataAndKey);
    // Send to the log the details you need to log in to the instance with RDP
    String publicIp = Iterables.getFirst(node.getPublicAddresses(), null);
    logger.info("IP address: %s", publicIp);
    logger.info("Login name: %s", credentials.getUser());
    logger.info("Password:   %s", credentials.getPassword());
    // Wait for Enter on the console
    logger.info("Hit Enter to shut down the node.");
    InputStreamReader converter = new InputStreamReader(System.in);
    BufferedReader in = new BufferedReader(converter);
    try {
        in.readLine();
    } catch (IOException e) {
        logger.error(e, "IOException while reading console input");
    }
    // Tidy up
    logger.info("Shutting down");
    computeService.destroyNode(node.getId());
}
Also used : RetryablePredicate(org.jclouds.predicates.RetryablePredicate) WindowsLoginCredentialsFromEncryptedData(org.jclouds.ec2.compute.functions.WindowsLoginCredentialsFromEncryptedData) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Template(org.jclouds.compute.domain.Template) Predicate(com.google.common.base.Predicate) RetryablePredicate(org.jclouds.predicates.RetryablePredicate) NodeMetadata(org.jclouds.compute.domain.NodeMetadata) LoginCredentials(org.jclouds.domain.LoginCredentials) RunNodesException(org.jclouds.compute.RunNodesException) PasswordData(org.jclouds.ec2.domain.PasswordData) BufferedReader(java.io.BufferedReader) TimeUnit(java.util.concurrent.TimeUnit) PasswordDataAndPrivateKey(org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey) Nullable(javax.annotation.Nullable)

Aggregations

Predicate (com.google.common.base.Predicate)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 TimeUnit (java.util.concurrent.TimeUnit)1 Nullable (javax.annotation.Nullable)1 RunNodesException (org.jclouds.compute.RunNodesException)1 NodeMetadata (org.jclouds.compute.domain.NodeMetadata)1 Template (org.jclouds.compute.domain.Template)1 LoginCredentials (org.jclouds.domain.LoginCredentials)1 PasswordDataAndPrivateKey (org.jclouds.ec2.compute.domain.PasswordDataAndPrivateKey)1 WindowsLoginCredentialsFromEncryptedData (org.jclouds.ec2.compute.functions.WindowsLoginCredentialsFromEncryptedData)1 PasswordData (org.jclouds.ec2.domain.PasswordData)1 RetryablePredicate (org.jclouds.predicates.RetryablePredicate)1