Example usage for com.amazonaws.services.ec2.model LaunchSpecification withInstanceType

List of usage examples for com.amazonaws.services.ec2.model LaunchSpecification withInstanceType

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model LaunchSpecification withInstanceType.

Prototype


public LaunchSpecification withInstanceType(InstanceType instanceType) 

Source Link

Document

The instance type.

Usage

From source file:de.unibi.cebitec.bibigrid.meta.aws.CreateClusterAWS.java

@Override
public boolean launchClusterInstances() {
    log.info("Requesting master instance ...");

    if (config.isUseSpotInstances()) {
        RequestSpotInstancesRequest masterReq = new RequestSpotInstancesRequest();
        masterReq.withType(SpotInstanceType.OneTime).withInstanceCount(1).withLaunchGroup("lg_" + clusterId)
                .withSpotPrice(Double.toString(config.getBidPriceMaster()));

        LaunchSpecification masterLaunchSpecification = new LaunchSpecification();
        masterLaunchSpecification
                .withInstanceType(InstanceType.fromValue(this.config.getMasterInstanceType().getValue()))
                .withPlacement(spotInstancePlacement).withKeyName(this.config.getKeypair())
                .withImageId(this.config.getMasterImage()).withUserData(base64MasterUserData)
                .withBlockDeviceMappings(masterDeviceMappings).withNetworkInterfaces(masterNetworkInterfaces);

        masterReq.setLaunchSpecification(masterLaunchSpecification);

        RequestSpotInstancesResult masterReqResult = ec2.requestSpotInstances(masterReq);

        List<SpotInstanceRequest> masterReqResponses = masterReqResult.getSpotInstanceRequests();
        // collect all spotInstanceRequestIds ...
        List<String> spotInstanceRequestIds = new ArrayList<>();

        for (SpotInstanceRequest requestResponse : masterReqResponses) {

            spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());

        }/*  w w  w  .j  a va2s .c o m*/
        // Tag spotrequest
        CreateTagsRequest ctr = new CreateTagsRequest();
        ctr.withResources(spotInstanceRequestIds);
        ctr.withTags(bibigridid, username, new Tag().withKey("Name").withValue(PREFIX + "master-" + clusterId));
        ec2.createTags(ctr);
        // Wait for spot request finished  
        log.info("Waiting for master instance (spot request) to finish booting ...");
        masterInstance = waitForInstances(waitForSpotInstances(spotInstanceRequestIds)).get(0);
    } else {

        RunInstancesRequest masterReq = new RunInstancesRequest();
        masterReq.withInstanceType(InstanceType.fromValue(this.config.getMasterInstanceType().getValue()))
                .withMinCount(1).withMaxCount(1).withPlacement(instancePlacement)
                .withKeyName(this.config.getKeypair()).withImageId(this.config.getMasterImage())
                .withUserData(base64MasterUserData).withBlockDeviceMappings(masterDeviceMappings)
                .withNetworkInterfaces(masterNetworkInterfaces);

        // mounting ephemerals
        RunInstancesResult masterReqResult = ec2.runInstances(masterReq);
        String masterReservationId = masterReqResult.getReservation().getReservationId();
        masterInstance = masterReqResult.getReservation().getInstances().get(0);
        log.info("Waiting for master instance to finish booting ...");

        /////////////////////////////////////////////
        //// Waiting for master instance to run ////
        masterInstance = waitForInstances(Arrays.asList(new String[] { masterInstance.getInstanceId() }))
                .get(0);

    }
    log.info(I, "Master instance is now running!");

    ModifyInstanceAttributeRequest ia_req = new ModifyInstanceAttributeRequest();
    ia_req.setInstanceId(masterInstance.getInstanceId());
    ia_req.setSourceDestCheck(Boolean.FALSE);
    ec2.modifyInstanceAttribute(ia_req);

    ////////////////////////////////////
    //// Tagging Master with a name ////
    CreateTagsRequest masterNameTagRequest = new CreateTagsRequest();
    masterNameTagRequest.withResources(masterInstance.getInstanceId()).withTags(bibigridid, username,
            new Tag().withKey("Name").withValue(PREFIX + "master-" + clusterId));

    ec2.createTags(masterNameTagRequest);

    /*
     * Waiting for Status Checks to finish
     *
     */
    log.info("Waiting for Status Checks on master ...");
    do {
        DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest();
        request.setInstanceIds((Arrays.asList(new String[] { masterInstance.getInstanceId() })));

        DescribeInstanceStatusResult response = ec2.describeInstanceStatus(request);

        InstanceStatus status = response.getInstanceStatuses().get(0);
        String instanceStatus = status.getInstanceStatus().getStatus();
        String systemStatus = status.getSystemStatus().getStatus();
        log.debug("Status of master instance: " + instanceStatus + "," + systemStatus);
        if (instanceStatus.equalsIgnoreCase("ok") && systemStatus.equalsIgnoreCase("ok")) {
            break;
        } else {
            log.info(V, "...");
            sleep(10);
        }
    } while (true);
    log.info(I, "Status checks successful.");
    ////////////////////////////////////////////////////////////////////////
    ///// run slave instances and supply userdata //////////////////////////

    if (config.getSlaveInstanceCount() > 0) {

        String base64SlaveUserData = UserDataCreator.forSlave(masterInstance.getPrivateIpAddress(),
                masterInstance.getPrivateDnsName(), slaveDeviceMapper, this.config, environment.getKeypair());

        log.info(V, "Slave Userdata:\n{}", new String(Base64.decodeBase64(base64SlaveUserData)));

        if (config.isUseSpotInstances()) {
            RequestSpotInstancesRequest slaveReq = new RequestSpotInstancesRequest();
            slaveReq.withType(SpotInstanceType.OneTime).withInstanceCount(config.getSlaveInstanceCount())
                    .withLaunchGroup("lg_" + clusterId).withSpotPrice(Double.toString(config.getBidPrice()));

            LaunchSpecification slaveLaunchSpecification = new LaunchSpecification();
            slaveLaunchSpecification
                    .withInstanceType(InstanceType.fromValue(this.config.getSlaveInstanceType().getValue()))
                    .withPlacement(spotInstancePlacement).withKeyName(this.config.getKeypair())
                    .withImageId(this.config.getSlaveImage()).withUserData(base64SlaveUserData)
                    .withBlockDeviceMappings(slaveBlockDeviceMappings)
                    .withNetworkInterfaces(slaveNetworkInterfaces);

            slaveReq.setLaunchSpecification(slaveLaunchSpecification);

            RequestSpotInstancesResult slaveReqResult = ec2.requestSpotInstances(slaveReq);

            List<SpotInstanceRequest> slaveReqResponses = slaveReqResult.getSpotInstanceRequests();
            // collect all spotInstanceRequestIds ...
            List<String> spotInstanceRequestIds = new ArrayList<>();

            for (SpotInstanceRequest requestResponse : slaveReqResponses) {

                spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());

            }
            // wait for a second
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {

            }

            log.info(V, "tag spot request instances");

            // tag spot requests (slave)
            CreateTagsRequest ctr = new CreateTagsRequest();
            ctr.withResources(spotInstanceRequestIds);
            ctr.withTags(bibigridid, username,
                    new Tag().withKey("Name").withValue(PREFIX + "slave-" + clusterId));
            /* Setting tags for spot requests can cause an amazon service 
             exception, if the spot request returns an id, but the id 
             isn't registered in spot request registy yet. */
            int counter = 0;
            boolean finished = false;
            while (!finished) {
                try {
                    ec2.createTags(ctr);
                    finished = true;
                } catch (AmazonServiceException ase) {
                    if (counter < 5) {
                        log.warn("{} ... try again in 10 seconds.", ase.getMessage());
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                        }
                        counter++;
                    } else {
                        throw ase;
                    }
                }
            }
            log.info("Waiting for slave instance(s) (spot request) to finish booting ...");
            // wait for spot request (slave) finished
            slaveInstances = waitForInstances(waitForSpotInstances(spotInstanceRequestIds));

        } else {

            RunInstancesRequest slaveReq = new RunInstancesRequest();
            slaveReq.withInstanceType(InstanceType.fromValue(this.config.getSlaveInstanceType().getValue()))
                    .withMinCount(this.config.getSlaveInstanceCount())
                    .withMaxCount(this.config.getSlaveInstanceCount()).withPlacement(instancePlacement)
                    .withKeyName(this.config.getKeypair()).withImageId(this.config.getSlaveImage())
                    .withUserData(base64SlaveUserData).withBlockDeviceMappings(slaveBlockDeviceMappings)
                    .withNetworkInterfaces(slaveNetworkInterfaces);

            RunInstancesResult slaveReqResult = ec2.runInstances(slaveReq);
            String slaveReservationId = slaveReqResult.getReservation().getReservationId();
            // create a list of all slave instances
            List<String> slaveInstanceListIds = new ArrayList<>();
            for (Instance i : slaveReqResult.getReservation().getInstances()) {
                slaveInstanceListIds.add(i.getInstanceId());
            }

            log.info("Waiting for slave instance(s) to finish booting ...");
            slaveInstances = waitForInstances(slaveInstanceListIds);

        }

        /////////////////////////////////////////////
        //// Waiting for master instance to run ////
        log.info(I, "Slave instance(s) is now running!");

        ////////////////////////////////////
        //// Tagging all slaves  with a name
        for (Instance si : slaveInstances) {
            CreateTagsRequest slaveNameTagRequest = new CreateTagsRequest();
            slaveNameTagRequest.withResources(si.getInstanceId()).withTags(bibigridid, username,
                    new Tag().withKey("Name").withValue(PREFIX + "slave-" + clusterId));
            ec2.createTags(slaveNameTagRequest);
        }
    } else {
        log.info("No Slave instance(s) requested !");

    }

    //////////////////////////////////
    ////// post configure master
    configureMaster();

    ////////////////////////////////////
    //// Human friendly output
    StringBuilder sb = new StringBuilder();
    sb.append("\n You might want to set the following environment variable:\n\n");
    sb.append("export BIBIGRID_MASTER=").append(masterInstance.getPublicIpAddress()).append("\n\n");
    sb.append("You can then log on the master node with:\n\n").append("ssh -i ")
            .append(config.getIdentityFile()).append(" ubuntu@$BIBIGRID_MASTER\n\n");
    sb.append("The cluster id of your started cluster is : ").append(clusterId).append("\n\n");
    sb.append("The can easily terminate the cluster at any time with :\n").append("./bibigrid -t ")
            .append(clusterId).append(" ");
    if (getConfig().isAlternativeConfigFile()) {
        sb.append("-o ").append(config.getAlternativeConfigPath()).append(" ");
    }

    sb.append("\n");

    log.info(sb.toString());

    ////////////////////////////////////
    //// Grid Properties file
    if (getConfig().getGridPropertiesFile() != null) {
        Properties gp = new Properties();
        gp.setProperty("BIBIGRID_MASTER", masterInstance.getPublicIpAddress());
        gp.setProperty("IdentityFile", getConfig().getIdentityFile().toString());
        gp.setProperty("clusterId", clusterId);
        if (getConfig().isAlternativeConfigFile()) {
            gp.setProperty("AlternativeConfigFile", config.getAlternativeConfigPath());
        }
        try {
            gp.store(new FileOutputStream(getConfig().getGridPropertiesFile()), "Autogenerated by BiBiGrid");
        } catch (IOException e) {
            log.error(I, "Exception while creating grid properties file : " + e.getMessage());
        }
    }

    return true;
}