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

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

Introduction

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

Prototype


public void setPlacement(SpotPlacement placement) 

Source Link

Document

The placement information for the instance.

Usage

From source file:InlineGettingStartedCodeApp.java

License:Open Source License

/**
 * @param args/*from  www .  j a va2  s .c  om*/
 */
public static void main(String[] args) {
    //============================================================================================//
    //=============================== Submitting a Request =======================================//
    //============================================================================================//

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    ec2.setRegion(usEast1);

    // Initializes a Spot Instance Request
    RequestSpotInstancesRequest requestRequest = new RequestSpotInstancesRequest();

    //*************************** Required Parameters Settings ************************//
    // Request 1 x t1.micro instance with a bid price of $0.03.
    requestRequest.setSpotPrice("0.03");
    requestRequest.setInstanceCount(Integer.valueOf(1));

    // Setup the specifications of the launch. This includes the instance type (e.g. t1.micro)
    // and the latest Amazon Linux AMI id available. Note, you should always use the latest
    // Amazon Linux AMI id or another of your choosing.
    LaunchSpecification launchSpecification = new LaunchSpecification();
    launchSpecification.setImageId("ami-700e4a19");
    launchSpecification.setInstanceType("t1.micro");

    // Add the security group to the request.
    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add("ForAssignment2");
    launchSpecification.setSecurityGroups(securityGroups);

    //*************************** Bid Type Settings ************************//
    // Set the type of the bid to persistent.
    requestRequest.setType("persistent");

    //*************************** Valid From/To Settings ************************//
    // Set the valid start time to be two minutes from now.
    Calendar from = Calendar.getInstance();
    from.add(Calendar.MINUTE, 2);
    requestRequest.setValidFrom(from.getTime());

    // Set the valid end time to be two minutes and two hours from now.
    Calendar until = (Calendar) from.clone();
    until.add(Calendar.HOUR, 2);
    requestRequest.setValidUntil(until.getTime());

    //*************************** Launch Group Settings ************************//
    // Set the launch group.
    requestRequest.setLaunchGroup("ADVANCED-DEMO-LAUNCH-GROUP");

    //*************************** Availability Zone Group Settings ************************//
    // Set the availability zone group.
    requestRequest.setAvailabilityZoneGroup("ADVANCED-DEMO-AZ-GROUP");

    //*************************** Add the block device mapping ************************//

    // Goal: Setup block device mappings to ensure that we will not delete
    // the root partition on termination.

    // Create the block device mapping to describe the root partition.
    BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
    blockDeviceMapping.setDeviceName("/dev/sda1");

    // Set the delete on termination flag to false.
    EbsBlockDevice ebs = new EbsBlockDevice();
    ebs.setDeleteOnTermination(Boolean.FALSE);
    blockDeviceMapping.setEbs(ebs);

    // Add the block device mapping to the block list.
    ArrayList<BlockDeviceMapping> blockList = new ArrayList<BlockDeviceMapping>();
    blockList.add(blockDeviceMapping);

    // Set the block device mapping configuration in the launch specifications.
    launchSpecification.setBlockDeviceMappings(blockList);

    //*************************** Add the availability zone ************************//
    // Setup the availability zone to use. Note we could retrieve the availability
    // zones using the ec2.describeAvailabilityZones() API. For this demo we will just use
    // us-east-1b.
    SpotPlacement placement = new SpotPlacement("us-east-1b");
    launchSpecification.setPlacement(placement);

    //*************************** Add the placement group ************************//
    // Setup the placement group to use with whatever name you desire.
    // For this demo we will just use "ADVANCED-DEMO-PLACEMENT-GROUP".
    // Note: We have commented this out, because we are not leveraging cc1.4xlarge or
    // cg1.4xlarge in this example.
    /*
    SpotPlacement pg = new SpotPlacement();
    pg.setGroupName("ADVANCED-DEMO-PLACEMENT-GROUP");
    launchSpecification.setPlacement(pg);
    */

    //*************************** Add the launch specification ************************//
    // Add the launch specification.
    requestRequest.setLaunchSpecification(launchSpecification);

    //============================================================================================//
    //=========================== Getting the Request ID from the Request ========================//
    //============================================================================================//

    // Call the RequestSpotInstance API.
    RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(requestRequest);
    List<SpotInstanceRequest> requestResponses = requestResult.getSpotInstanceRequests();

    // Setup an arraylist to collect all of the request ids we want to watch hit the running
    // state.
    ArrayList<String> spotInstanceRequestIds = new ArrayList<String>();

    // Add all of the request ids to the hashset, so we can determine when they hit the
    // active state.
    for (SpotInstanceRequest requestResponse : requestResponses) {
        System.out.println("Created Spot Request: " + requestResponse.getSpotInstanceRequestId());
        spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
    }

    //============================================================================================//
    //=========================== Determining the State of the Spot Request ======================//
    //============================================================================================//

    // Create a variable that will track whether there are any requests still in the open state.
    boolean anyOpen;

    // Initialize variables.
    ArrayList<String> instanceIds = new ArrayList<String>();

    do {
        // Create the describeRequest with tall of the request id to monitor (e.g. that we started).
        DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotInstanceRequestsRequest();
        describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds);

        // Initialize the anyOpen variable to false ??? which assumes there are no requests open unless
        // we find one that is still open.
        anyOpen = false;

        try {
            // Retrieve all of the requests we want to monitor.
            DescribeSpotInstanceRequestsResult describeResult = ec2
                    .describeSpotInstanceRequests(describeRequest);
            List<SpotInstanceRequest> describeResponses = describeResult.getSpotInstanceRequests();

            // Look through each request and determine if they are all in the active state.
            for (SpotInstanceRequest describeResponse : describeResponses) {
                // If the state is open, it hasn't changed since we attempted to request it.
                // There is the potential for it to transition almost immediately to closed or
                // cancelled so we compare against open instead of active.
                if (describeResponse.getState().equals("open")) {
                    anyOpen = true;
                    break;
                }

                // Add the instance id to the list we will eventually terminate.
                instanceIds.add(describeResponse.getInstanceId());
            }
        } catch (AmazonServiceException e) {
            // If we have an exception, ensure we don't break out of the loop.
            // This prevents the scenario where there was blip on the wire.
            anyOpen = true;
        }

        try {
            // Sleep for 60 seconds.
            Thread.sleep(60 * 1000);
        } catch (Exception e) {
            // Do nothing because it woke up early.
        }
    } while (anyOpen);

    //============================================================================================//
    //====================================== Canceling the Request ==============================//
    //============================================================================================//

    try {
        // Cancel requests.
        CancelSpotInstanceRequestsRequest cancelRequest = new CancelSpotInstanceRequestsRequest(
                spotInstanceRequestIds);
        ec2.cancelSpotInstanceRequests(cancelRequest);
    } catch (AmazonServiceException e) {
        // Write out any exceptions that may have occurred.
        System.out.println("Error cancelling instances");
        System.out.println("Caught Exception: " + e.getMessage());
        System.out.println("Reponse Status Code: " + e.getStatusCode());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    }

    //============================================================================================//
    //=================================== Terminating any Instances ==============================//
    //============================================================================================//
    try {
        // Terminate instances.
        TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(instanceIds);
        ec2.terminateInstances(terminateRequest);
    } catch (AmazonServiceException e) {
        // Write out any exceptions that may have occurred.
        System.out.println("Error terminating instances");
        System.out.println("Caught Exception: " + e.getMessage());
        System.out.println("Reponse Status Code: " + e.getStatusCode());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    }

}

From source file:advanced.InlineGettingStartedCodeSampleApp.java

License:Open Source License

/**
 * @param args//from   w  w w.jav a2 s.  co  m
 */
public static void main(String[] args) {
    //============================================================================================//
    //=============================== Submitting a Request =======================================// 
    //============================================================================================//

    // Retrieves the credentials from an AWSCredentials.properties file.
    AWSCredentials credentials = null;
    try {
        credentials = new PropertiesCredentials(
                InlineTaggingCodeSampleApp.class.getResourceAsStream("AwsCredentials.properties"));
    } catch (IOException e1) {
        System.out.println("Credentials were not properly entered into AwsCredentials.properties.");
        System.out.println(e1.getMessage());
        System.exit(-1);
    }

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

    // Initializes a Spot Instance Request
    RequestSpotInstancesRequest requestRequest = new RequestSpotInstancesRequest();

    //*************************** Required Parameters Settings ************************// 
    // Request 1 x t1.micro instance with a bid price of $0.03. 
    requestRequest.setSpotPrice("0.03");
    requestRequest.setInstanceCount(Integer.valueOf(1));

    // Setup the specifications of the launch. This includes the instance type (e.g. t1.micro)
    // and the latest Amazon Linux AMI id available. Note, you should always use the latest 
    // Amazon Linux AMI id or another of your choosing.
    LaunchSpecification launchSpecification = new LaunchSpecification();
    launchSpecification.setImageId("ami-8c1fece5");
    launchSpecification.setInstanceType("t1.micro");

    // Add the security group to the request.
    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add("GettingStartedGroup");
    launchSpecification.setSecurityGroups(securityGroups);

    //*************************** Bid Type Settings ************************// 
    // Set the type of the bid to persistent.
    requestRequest.setType("persistent");

    //*************************** Valid From/To Settings ************************// 
    // Set the valid start time to be two minutes from now.
    Calendar from = Calendar.getInstance();
    from.add(Calendar.MINUTE, 2);
    requestRequest.setValidFrom(from.getTime());

    // Set the valid end time to be two minutes and two hours from now.
    Calendar until = (Calendar) from.clone();
    until.add(Calendar.HOUR, 2);
    requestRequest.setValidUntil(until.getTime());

    //*************************** Launch Group Settings ************************// 
    // Set the launch group.
    requestRequest.setLaunchGroup("ADVANCED-DEMO-LAUNCH-GROUP");

    //*************************** Availability Zone Group Settings ************************// 
    // Set the availability zone group.
    requestRequest.setAvailabilityZoneGroup("ADVANCED-DEMO-AZ-GROUP");

    //*************************** Add the block device mapping ************************// 

    // Goal: Setup block device mappings to ensure that we will not delete
    // the root partition on termination.

    // Create the block device mapping to describe the root partition.
    BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
    blockDeviceMapping.setDeviceName("/dev/sda1");

    // Set the delete on termination flag to false.
    EbsBlockDevice ebs = new EbsBlockDevice();
    ebs.setDeleteOnTermination(Boolean.FALSE);
    blockDeviceMapping.setEbs(ebs);

    // Add the block device mapping to the block list.
    ArrayList<BlockDeviceMapping> blockList = new ArrayList<BlockDeviceMapping>();
    blockList.add(blockDeviceMapping);

    // Set the block device mapping configuration in the launch specifications.
    launchSpecification.setBlockDeviceMappings(blockList);

    //*************************** Add the availability zone ************************// 
    // Setup the availability zone to use. Note we could retrieve the availability 
    // zones using the ec2.describeAvailabilityZones() API. For this demo we will just use
    // us-east-1b.
    SpotPlacement placement = new SpotPlacement("us-east-1b");
    launchSpecification.setPlacement(placement);

    //*************************** Add the placement group ************************// 
    // Setup the placement group to use with whatever name you desire.
    // For this demo we will just use "ADVANCED-DEMO-PLACEMENT-GROUP".
    // Note: We have commented this out, because we are not leveraging cc1.4xlarge or
    // cg1.4xlarge in this example.
    /*
    SpotPlacement pg = new SpotPlacement();
    pg.setGroupName("ADVANCED-DEMO-PLACEMENT-GROUP");
    launchSpecification.setPlacement(pg);
    */

    //*************************** Add the launch specification ************************// 
    // Add the launch specification.
    requestRequest.setLaunchSpecification(launchSpecification);

    //============================================================================================//
    //=========================== Getting the Request ID from the Request ========================// 
    //============================================================================================//

    // Call the RequestSpotInstance API. 
    RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(requestRequest);
    List<SpotInstanceRequest> requestResponses = requestResult.getSpotInstanceRequests();

    // Setup an arraylist to collect all of the request ids we want to watch hit the running
    // state.
    ArrayList<String> spotInstanceRequestIds = new ArrayList<String>();

    // Add all of the request ids to the hashset, so we can determine when they hit the 
    // active state.
    for (SpotInstanceRequest requestResponse : requestResponses) {
        System.out.println("Created Spot Request: " + requestResponse.getSpotInstanceRequestId());
        spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
    }

    //============================================================================================//
    //=========================== Determining the State of the Spot Request ======================// 
    //============================================================================================//

    // Create a variable that will track whether there are any requests still in the open state.
    boolean anyOpen;

    // Initialize variables.
    ArrayList<String> instanceIds = new ArrayList<String>();

    do {
        // Create the describeRequest with tall of the request id to monitor (e.g. that we started).
        DescribeSpotInstanceRequestsRequest describeRequest = new DescribeSpotInstanceRequestsRequest();
        describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds);

        // Initialize the anyOpen variable to false  which assumes there are no requests open unless
        // we find one that is still open.
        anyOpen = false;

        try {
            // Retrieve all of the requests we want to monitor. 
            DescribeSpotInstanceRequestsResult describeResult = ec2
                    .describeSpotInstanceRequests(describeRequest);
            List<SpotInstanceRequest> describeResponses = describeResult.getSpotInstanceRequests();

            // Look through each request and determine if they are all in the active state.
            for (SpotInstanceRequest describeResponse : describeResponses) {
                // If the state is open, it hasn't changed since we attempted to request it.
                // There is the potential for it to transition almost immediately to closed or
                // cancelled so we compare against open instead of active.
                if (describeResponse.getState().equals("open")) {
                    anyOpen = true;
                    break;
                }

                // Add the instance id to the list we will eventually terminate.
                instanceIds.add(describeResponse.getInstanceId());
            }
        } catch (AmazonServiceException e) {
            // If we have an exception, ensure we don't break out of the loop.
            // This prevents the scenario where there was blip on the wire.
            anyOpen = true;
        }

        try {
            // Sleep for 60 seconds.
            Thread.sleep(60 * 1000);
        } catch (Exception e) {
            // Do nothing because it woke up early.
        }
    } while (anyOpen);

    //============================================================================================//
    //====================================== Canceling the Request ==============================// 
    //============================================================================================//

    try {
        // Cancel requests.
        CancelSpotInstanceRequestsRequest cancelRequest = new CancelSpotInstanceRequestsRequest(
                spotInstanceRequestIds);
        ec2.cancelSpotInstanceRequests(cancelRequest);
    } catch (AmazonServiceException e) {
        // Write out any exceptions that may have occurred.
        System.out.println("Error cancelling instances");
        System.out.println("Caught Exception: " + e.getMessage());
        System.out.println("Reponse Status Code: " + e.getStatusCode());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    }

    //============================================================================================//
    //=================================== Terminating any Instances ==============================// 
    //============================================================================================//
    try {
        // Terminate instances.
        TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(instanceIds);
        ec2.terminateInstances(terminateRequest);
    } catch (AmazonServiceException e) {
        // Write out any exceptions that may have occurred.
        System.out.println("Error terminating instances");
        System.out.println("Caught Exception: " + e.getMessage());
        System.out.println("Reponse Status Code: " + e.getStatusCode());
        System.out.println("Error Code: " + e.getErrorCode());
        System.out.println("Request ID: " + e.getRequestId());
    }

}

From source file:advanced.Requests.java

License:Open Source License

/**
 * The submit method will create 1 x one-time t1.micro request with a maximum bid
 * price of $0.03 using the Amazon Linux AMI. 
 * //from w ww  .  ja va  2  s  .  c  o m
 * Note the AMI id may change after the release of this code sample, and it is important 
 * to use the latest. You can find the latest version by logging into the AWS Management 
 * console, and attempting to perform a launch. You will be presented with AMI options, 
 * one of which will be Amazon Linux. Simply use that AMI id.
 */
public void submitRequests() {
    //==========================================================================//
    //================= Submit a Spot Instance Request =====================//
    //==========================================================================//

    // Initializes a Spot Instance Request
    RequestSpotInstancesRequest requestRequest = new RequestSpotInstancesRequest();

    // Request 1 x t1.micro instance with a bid price of $0.03. 
    requestRequest.setSpotPrice(bidPrice);
    requestRequest.setInstanceCount(Integer.valueOf(1));

    // Setup the specifications of the launch. This includes the instance type (e.g. t1.micro)
    // and the latest Amazon Linux AMI id available. Note, you should always use the latest 
    // Amazon Linux AMI id or another of your choosing.
    LaunchSpecification launchSpecification = new LaunchSpecification();
    launchSpecification.setImageId(amiID);
    launchSpecification.setInstanceType(instanceType);

    // Add the security group to the request.
    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add(securityGroup);
    launchSpecification.setSecurityGroups(securityGroups);

    // If a placement group has been set, then we will use it in the request.
    if (placementGroupName != null && !placementGroupName.equals("")) {
        // Setup the placement group to use with whatever name you desire.
        SpotPlacement placement = new SpotPlacement();
        placement.setGroupName(placementGroupName);
        launchSpecification.setPlacement(placement);
    }

    // Check to see if we need to set the availability zone name.
    if (availabilityZoneName != null && !availabilityZoneName.equals("")) {
        // Setup the availability zone to use. Note we could retrieve the availability 
        // zones using the ec2.describeAvailabilityZones() API. 
        SpotPlacement placement = new SpotPlacement(availabilityZoneName);
        launchSpecification.setPlacement(placement);
    }

    if (availabilityZoneGroupName != null && !availabilityZoneGroupName.equals("")) {
        // Set the availability zone group.
        requestRequest.setAvailabilityZoneGroup(availabilityZoneGroupName);
    }

    // Check to see if we need to set the launch group.
    if (launchGroupName != null && !launchGroupName.equals("")) {
        // Set the availability launch group.
        requestRequest.setLaunchGroup(launchGroupName);
    }

    // Check to see if we need to set the valid from option.
    if (validFrom != null) {
        requestRequest.setValidFrom(validFrom);
    }

    // Check to see if we need to set the valid until option.
    if (validTo != null) {
        requestRequest.setValidUntil(validFrom);
    }

    // Check to see if we need to set the request type.
    if (requestType != null && !requestType.equals("")) {
        // Set the type of the bid.
        requestRequest.setType(requestType);
    }

    // If we should delete the EBS boot partition on termination.
    if (!deleteOnTermination) {
        // Create the block device mapping to describe the root partition.
        BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
        blockDeviceMapping.setDeviceName("/dev/sda1");

        // Set the delete on termination flag to false.
        EbsBlockDevice ebs = new EbsBlockDevice();
        ebs.setDeleteOnTermination(Boolean.FALSE);
        blockDeviceMapping.setEbs(ebs);

        // Add the block device mapping to the block list.
        ArrayList<BlockDeviceMapping> blockList = new ArrayList<BlockDeviceMapping>();
        blockList.add(blockDeviceMapping);

        // Set the block device mapping configuration in the launch specifications.
        launchSpecification.setBlockDeviceMappings(blockList);
    }

    // Add the launch specifications to the request.
    requestRequest.setLaunchSpecification(launchSpecification);

    // Call the RequestSpotInstance API. 
    RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(requestRequest);
    List<SpotInstanceRequest> requestResponses = requestResult.getSpotInstanceRequests();

    // Setup an arraylist to collect all of the request ids we want to watch hit the running
    // state.
    spotInstanceRequestIds = new ArrayList<String>();

    // Add all of the request ids to the hashset, so we can determine when they hit the 
    // active state.
    for (SpotInstanceRequest requestResponse : requestResponses) {
        System.out.println("Created Spot Request: " + requestResponse.getSpotInstanceRequestId());
        spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
    }

}

From source file:com.norbl.cbp.ppe.Ec2Wrangler.java

License:Open Source License

private void setAvailabilityZone(LaunchSpecification spec, String zone) {
    try {/*from   w  ww  .j  a va 2s.  c o  m*/
        //            Placement p = new Placement(zone);
        SpotPlacement p = new SpotPlacement(zone);
        // Placement changed to SpotPlacement migrating from
        // AWS java sdk 1.1.6 -> 1.3.3
        spec.setPlacement(p);
    } catch (Exception xxx) {
        ExceptionHandler.display(xxx);
    }
}

From source file:com.norbl.cbp.ppe.Ec2Wrangler.java

License:Open Source License

private void setupClusterPlacementGroup(LaunchSpecification spec) {

    String clusterName;//  w w w  . j a v  a 2 s. c  om

    if (params.clusterGroupName != null)
        clusterName = params.clusterGroupName;
    else
        clusterName = ConstantsEc2.CLUSTER_GROUP_DEFAULT;

    createClusterGroupIfNecessary(clusterName);
    //        Placement placement = new Placement();
    SpotPlacement placement = new SpotPlacement();
    // Placement changed to SpotPlacement migrating from
    // AWS java sdk 1.1.6 -> 1.3.3
    placement.setGroupName(clusterName);

    spec.setPlacement(placement);

    //        /* D */ System.out.println("Ec2W: setup placement group name=" +
    //                                clusterName + " for spot");
}

From source file:hudson.plugins.ec2.SlaveTemplate.java

License:Open Source License

/**
 * Provision a new slave for an EC2 spot instance to call back to Jenkins
 */// w w w  .j ava  2 s .  co m
private EC2AbstractSlave provisionSpot(TaskListener listener) throws AmazonClientException, IOException {
    PrintStream logger = listener.getLogger();
    AmazonEC2 ec2 = getParent().connect();

    try {
        logger.println("Launching " + ami + " for template " + description);
        KeyPair keyPair = getKeyPair(ec2);

        RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest();

        // Validate spot bid before making the request
        if (getSpotMaxBidPrice() == null) {
            // throw new FormException("Invalid Spot price specified: " + getSpotMaxBidPrice(), "spotMaxBidPrice");
            throw new AmazonClientException("Invalid Spot price specified: " + getSpotMaxBidPrice());
        }

        spotRequest.setSpotPrice(getSpotMaxBidPrice());
        spotRequest.setInstanceCount(Integer.valueOf(1));
        spotRequest.setType(getBidType());

        LaunchSpecification launchSpecification = new LaunchSpecification();
        InstanceNetworkInterfaceSpecification net = new InstanceNetworkInterfaceSpecification();

        launchSpecification.setImageId(ami);
        launchSpecification.setInstanceType(type);

        if (StringUtils.isNotBlank(getZone())) {
            SpotPlacement placement = new SpotPlacement(getZone());
            launchSpecification.setPlacement(placement);
        }

        if (StringUtils.isNotBlank(getSubnetId())) {
            if (getAssociatePublicIp()) {
                net.setSubnetId(getSubnetId());
            } else {
                launchSpecification.setSubnetId(getSubnetId());
            }

            /* If we have a subnet ID then we can only use VPC security groups */
            if (!securityGroupSet.isEmpty()) {
                List<String> group_ids = getEc2SecurityGroups(ec2);
                if (!group_ids.isEmpty()) {
                    if (getAssociatePublicIp()) {
                        net.setGroups(group_ids);
                    } else {
                        ArrayList<GroupIdentifier> groups = new ArrayList<GroupIdentifier>();

                        for (String group_id : group_ids) {
                            GroupIdentifier group = new GroupIdentifier();
                            group.setGroupId(group_id);
                            groups.add(group);
                        }
                        if (!groups.isEmpty())
                            launchSpecification.setAllSecurityGroups(groups);
                    }
                }
            }
        } else {
            /* No subnet: we can use standard security groups by name */
            if (securityGroupSet.size() > 0)
                launchSpecification.setSecurityGroups(securityGroupSet);
        }

        // The slave must know the Jenkins server to register with as well
        // as the name of the node in Jenkins it should register as. The only
        // way to give information to the Spot slaves is through the ec2 user data
        String jenkinsUrl = Hudson.getInstance().getRootUrl();
        // We must provide a unique node name for the slave to connect to Jenkins.
        // We don't have the EC2 generated instance ID, or the Spot request ID
        // until after the instance is requested, which is then too late to set the
        // user-data for the request. Instead we generate a unique name from UUID
        // so that the slave has a unique name within Jenkins to register to.
        String slaveName = UUID.randomUUID().toString();
        String newUserData = "";

        // We want to allow node configuration with cloud-init and user-data,
        // while maintaining backward compatibility with old ami's
        // The 'new' way is triggered by the presence of '${SLAVE_NAME}'' in the user data 
        // (which is not too much to ask)
        if (userData.contains("${SLAVE_NAME}")) {
            // The cloud-init compatible way
            newUserData = new String(userData);
            newUserData = newUserData.replace("${SLAVE_NAME}", slaveName);
            newUserData = newUserData.replace("${JENKINS_URL}", jenkinsUrl);
        } else {
            // The 'old' way - maitain full backward compatibility
            newUserData = "JENKINS_URL=" + jenkinsUrl + "&SLAVE_NAME=" + slaveName + "&USER_DATA="
                    + Base64.encodeBase64String(userData.getBytes());
        }

        String userDataString = Base64.encodeBase64String(newUserData.getBytes());

        launchSpecification.setUserData(userDataString);
        launchSpecification.setKeyName(keyPair.getKeyName());
        launchSpecification.setInstanceType(type.toString());

        if (getAssociatePublicIp()) {
            net.setAssociatePublicIpAddress(true);
            net.setDeviceIndex(0);
            launchSpecification.withNetworkInterfaces(net);
        }

        boolean hasCustomTypeTag = false;
        HashSet<Tag> inst_tags = null;
        if (tags != null && !tags.isEmpty()) {
            inst_tags = new HashSet<Tag>();
            for (EC2Tag t : tags) {
                inst_tags.add(new Tag(t.getName(), t.getValue()));
                if (StringUtils.equals(t.getName(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) {
                    hasCustomTypeTag = true;
                }
            }
        }
        if (!hasCustomTypeTag) {
            inst_tags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "spot"));
        }

        if (StringUtils.isNotBlank(getIamInstanceProfile())) {
            launchSpecification.setIamInstanceProfile(
                    new IamInstanceProfileSpecification().withArn(getIamInstanceProfile()));
        }

        spotRequest.setLaunchSpecification(launchSpecification);

        // Make the request for a new Spot instance
        RequestSpotInstancesResult reqResult = ec2.requestSpotInstances(spotRequest);

        List<SpotInstanceRequest> reqInstances = reqResult.getSpotInstanceRequests();
        if (reqInstances.size() <= 0) {
            throw new AmazonClientException("No spot instances found");
        }

        SpotInstanceRequest spotInstReq = reqInstances.get(0);
        if (spotInstReq == null) {
            throw new AmazonClientException("Spot instance request is null");
        }

        /* Now that we have our Spot request, we can set tags on it */
        if (inst_tags != null) {
            for (int i = 0; i < 5; i++) {
                try {
                    updateRemoteTags(ec2, inst_tags, spotInstReq.getSpotInstanceRequestId());
                    break;
                } catch (AmazonServiceException e) {
                    if (e.getErrorCode().equals("InvalidSpotInstanceRequestID.NotFound")) {
                        Thread.sleep(5000);
                        continue;
                    }
                    throw e;
                }
            }

            // That was a remote request - we should also update our local instance data.
            spotInstReq.setTags(inst_tags);
        }

        logger.println("Spot instance id in provision: " + spotInstReq.getSpotInstanceRequestId());

        return newSpotSlave(spotInstReq, slaveName);

    } catch (FormException e) {
        throw new AssertionError(); // we should have discovered all configuration issues upfront
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:n3phele.factory.ec2.VirtualServerResource.java

License:Open Source License

private int addSpotInstance(List<VirtualServer> items) {

    RequestSpotInstancesRequest sir = new RequestSpotInstancesRequest();

    sir.setInstanceCount(items.size());//from  w  w  w  . ja v a2 s.  c o  m
    HashMap<String, String> map = items.get(0).getParametersMap();
    Injector.inject(sir, map);
    LaunchSpecification launchSpec = new LaunchSpecification();
    Injector.inject(launchSpec, map);
    if (map.containsKey("availabilityZone") || map.containsKey("groupName")) {
        Placement p = new Placement();
        if (map.containsKey("availabilityZone")) {
            String availabilityZone = map.get("availabilityZone");
            if (availabilityZone != null && !availabilityZone.equals("")) {
                p.setAvailabilityZone(map.get("availabilityZone"));
                launchSpec.setPlacement(p);
            }
        }
        if (map.containsKey("groupName")) {
            String groupName = map.get("groupName");
            if (groupName != null && !groupName.equals("")) {
                p.setGroupName(map.get("groupName"));
                launchSpec.setPlacement(p);
            }
        }

    }
    sir.setLaunchSpecification(launchSpec);

    AmazonEC2Client client = getEC2Client(items.get(0).getAccessKey(), items.get(0).getEncryptedKey(),
            items.get(0).getLocation());
    RequestSpotInstancesResult result = null;
    try {
        result = client.requestSpotInstances(sir);
    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "EC2 error " + e.getErrorCode(), e);
        throw new WebApplicationException(e, Status.BAD_REQUEST);
    }
    int i = 0;
    for (SpotInstanceRequest spot : result.getSpotInstanceRequests()) {
        log.warning("Spot Instance[" + i + "] id " + spot.getSpotInstanceRequestId());
        items.get(i).setSpotId(spot.getSpotInstanceRequestId());
        i++;
    }

    return result.getSpotInstanceRequests().size();
}