Search in sources :

Example 1 with EC2Api

use of org.jclouds.ec2.EC2Api in project acceptance-test-harness by jenkinsci.

the class Ec2Provider method postStartupSetup.

@Override
public void postStartupSetup(NodeMetadata node) {
    String host = node.getPublicAddresses().iterator().next();
    try (Ssh ssh = new Ssh(host)) {
        authenticator().authenticate(ssh.getConnection());
        ssh.getConnection().exec(String.format("pkill -u $(id -u %s)", config.getUser()), System.out);
        // set instance initiated shutdown behavior to terminate. This means any shutdown will result in to instance
        // termination
        EC2Api client = contextBuilder.buildApi(EC2Api.class);
        client.getInstanceApi().get().setInstanceInitiatedShutdownBehaviorForInstanceInRegion(config.getRegion(), node.getProviderId(), Volume.InstanceInitiatedShutdownBehavior.TERMINATE);
        copyAutoterminateScript(host);
    } catch (IOException | InterruptedException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : EC2Api(org.jclouds.ec2.EC2Api) Ssh(org.jenkinsci.test.acceptance.Ssh) IOException(java.io.IOException)

Example 2 with EC2Api

use of org.jclouds.ec2.EC2Api in project whirr by apache.

the class FirewallManagerTest method testFirewallAuthorizationIsIdempotent.

@Test(timeout = TestConstants.ITEST_TIMEOUT)
public void testFirewallAuthorizationIsIdempotent() throws IOException {
    if (EC2ComputeService.class.isInstance(context.getComputeService())) {
        EC2Api api = context.unwrapApi(EC2Api.class);
        String groupName = "jclouds#" + clusterSpec.getClusterName();
        api.getSecurityGroupApi().get().createSecurityGroupInRegion(region, groupName, "group description");
        try {
            manager.addRule(Rule.create().destination(instances).port(23344));
            /* The second call should not throw an exception. */
            manager.addRule(Rule.create().destination(instances).port(23344));
            manager.authorizeAllRules();
        } finally {
            api.getSecurityGroupApi().get().deleteSecurityGroupInRegion(region, groupName);
        }
    }
}
Also used : EC2Api(org.jclouds.ec2.EC2Api) Test(org.junit.Test)

Example 3 with EC2Api

use of org.jclouds.ec2.EC2Api in project acceptance-test-harness by jenkinsci.

the class TSREc2Provider method postStartupSetup.

@Override
public void postStartupSetup(NodeMetadata node) {
    String host = node.getPublicAddresses().iterator().next();
    try (Ssh ssh = new Ssh(host)) {
        authenticator().authenticate(ssh.getConnection());
        ssh.getConnection().exec(String.format("pkill -u $(id -u %s)", config.getUser()), System.out);
        // set instance initiated shutdown behavior to terminate. This means any shutdown will result in to instance
        // termination
        EC2Api client = contextBuilder.buildApi(EC2Api.class);
        client.getInstanceApi().get().setInstanceInitiatedShutdownBehaviorForInstanceInRegion(config.getRegion(), node.getProviderId(), Volume.InstanceInitiatedShutdownBehavior.TERMINATE);
        copyAutoterminateScript(host);
    } catch (IOException | InterruptedException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : EC2Api(org.jclouds.ec2.EC2Api) Ssh(org.jenkinsci.test.acceptance.Ssh) IOException(java.io.IOException)

Example 4 with EC2Api

use of org.jclouds.ec2.EC2Api in project acceptance-test-harness by jenkinsci.

the class TSREc2Provider method getTemplate.

@Override
public Template getTemplate() throws IOException {
    if (config.getSecurityGroups().size() > 0) {
        EC2Api client = contextBuilder.buildApi(EC2Api.class);
        for (String sg : config.getSecurityGroups()) {
            try {
                client.getSecurityGroupApi().get().createSecurityGroupInRegion(config.getRegion(), sg, sg);
                client.getSecurityGroupApi().get().authorizeSecurityGroupIngressInRegion(config.getRegion(), sg, IpProtocol.TCP, config.getInboundPorts()[0], config.getInboundPorts()[config.getInboundPorts().length - 1], "0.0.0.0/0");
                client.getSecurityGroupApi().get().authorizeSecurityGroupIngressInRegion(config.getRegion(), sg, IpProtocol.TCP, 22, 22, "0.0.0.0/0");
            } catch (IllegalStateException e) {
                // Lets ignore it, most likely its due to existing security roles, it might fail
                logger.error("Failed to create and authorize IP ports in security group" + e.getMessage());
            }
        }
    }
    Template template = computeService.templateBuilder().imageId(config.getRegion() + "/" + config.getImageId()).locationId(config.getRegion()).hardwareId(config.getInstanceType()).build();
    String publicKey = keyPair.readPublicKey();
    EC2TemplateOptions options = template.getOptions().as(EC2TemplateOptions.class);
    options.authorizePublicKey(publicKey).securityGroups(config.getSecurityGroups()).inboundPorts(config.getInboundPorts()).overrideLoginUser(config.getUser());
    // fixme: the key pair name we set here just doesn't get used at all. as of 1.6.0
    // in CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions
    // #createNewKeyPairUnlessUserSpecifiedOtherwise
    // it goes through "and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential))"
    // check, which evaluates to true (because doesntNeedSshAfterImportingPublicKey is true), and it ends up going
    // through importExistingKeyPair.apply(...) that doesn't look at the key pair name we prefer.
    String kn = config.getKeyPairName();
    if (kn == null) {
        try {
            kn = "jenkins-test-" + keyPair.getFingerprint().substring(0, 11);
        } catch (GeneralSecurityException e) {
            throw new IOException("Failed to compute key fingerprint of", e);
        }
    }
    options.keyPair(kn);
    return template;
}
Also used : EC2Api(org.jclouds.ec2.EC2Api) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) EC2TemplateOptions(org.jclouds.ec2.compute.options.EC2TemplateOptions) Template(org.jclouds.compute.domain.Template)

Example 5 with EC2Api

use of org.jclouds.ec2.EC2Api in project acceptance-test-harness by jenkinsci.

the class Ec2Provider method getTemplate.

@Override
public Template getTemplate() throws IOException {
    if (config.getSecurityGroups().size() > 0) {
        EC2Api client = contextBuilder.buildApi(EC2Api.class);
        for (String sg : config.getSecurityGroups()) {
            try {
                client.getSecurityGroupApi().get().createSecurityGroupInRegion(config.getRegion(), sg, sg);
                client.getSecurityGroupApi().get().authorizeSecurityGroupIngressInRegion(config.getRegion(), sg, IpProtocol.TCP, config.getInboundPorts()[0], config.getInboundPorts()[config.getInboundPorts().length - 1], "0.0.0.0/0");
                client.getSecurityGroupApi().get().authorizeSecurityGroupIngressInRegion(config.getRegion(), sg, IpProtocol.TCP, 22, 22, "0.0.0.0/0");
            } catch (IllegalStateException e) {
                // Lets ignore it, most likely its due to existing security roles, it might fail
                logger.error("Failed to create and authorize IP ports in security group" + e.getMessage());
            }
        }
    }
    Template template = computeService.templateBuilder().imageId(config.getRegion() + "/" + config.getImageId()).locationId(config.getRegion()).hardwareId(config.getInstanceType()).build();
    String publicKey = keyPair.readPublicKey();
    EC2TemplateOptions options = template.getOptions().as(EC2TemplateOptions.class);
    options.authorizePublicKey(publicKey).securityGroups(config.getSecurityGroups()).inboundPorts(config.getInboundPorts()).overrideLoginUser(config.getUser());
    // FIXME: the key pair name we set here just doesn't get used at all. as of 1.6.0
    // in CreateKeyPairPlacementAndSecurityGroupsAsNeededAndReturnRunOptions
    // #createNewKeyPairUnlessUserSpecifiedOtherwise
    // it goes through "and(hasPublicKeyMaterial, or(doesntNeedSshAfterImportingPublicKey, hasLoginCredential))"
    // check, which evaluates to true (because doesntNeedSshAfterImportingPublicKey is true), and it ends up going
    // through importExistingKeyPair.apply(...) that doesn't look at the key pair name we prefer.
    String kn = config.getKeyPairName();
    if (kn == null) {
        try {
            kn = "jenkins-test-" + keyPair.getFingerprint().substring(0, 11);
        } catch (GeneralSecurityException e) {
            throw new IOException("Failed to compute key fingerprint of", e);
        }
    }
    options.keyPair(kn);
    return template;
}
Also used : EC2Api(org.jclouds.ec2.EC2Api) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) EC2TemplateOptions(org.jclouds.ec2.compute.options.EC2TemplateOptions) Template(org.jclouds.compute.domain.Template)

Aggregations

EC2Api (org.jclouds.ec2.EC2Api)5 IOException (java.io.IOException)4 GeneralSecurityException (java.security.GeneralSecurityException)2 Template (org.jclouds.compute.domain.Template)2 EC2TemplateOptions (org.jclouds.ec2.compute.options.EC2TemplateOptions)2 Ssh (org.jenkinsci.test.acceptance.Ssh)2 Test (org.junit.Test)1