List of usage examples for com.amazonaws.services.ec2.model IamInstanceProfileSpecification IamInstanceProfileSpecification
IamInstanceProfileSpecification
From source file:com.cloudera.director.aws.ec2.EC2Provider.java
License:Apache License
/** * Builds a {@code RunInstancesRequest} starting from a template and a set of * virtual IDs./* w w w. java 2s . co m*/ * * @param template the instance template * @param virtualInstanceIds the virtual instance IDs */ @SuppressWarnings("ConstantConditions") private RunInstancesRequest newRunInstancesRequest(EC2InstanceTemplate template, Collection<String> virtualInstanceIds, int minCount) { LOG.info(">> Building instance requests"); int groupSize = virtualInstanceIds.size(); String image = template.getImage(); String type = template.getType(); InstanceNetworkInterfaceSpecification network = getInstanceNetworkInterfaceSpecification(template); List<BlockDeviceMapping> deviceMappings = getBlockDeviceMappings(template); LOG.info(">> Instance request type: {}, image: {}, group size: {}", type, image, groupSize); RunInstancesRequest request = new RunInstancesRequest().withImageId(image).withInstanceType(type) .withMaxCount(groupSize).withMinCount(minCount) .withClientToken( getHashOfVirtualInstanceIdsForClientToken(virtualInstanceIds, Optional.<Long>absent())) .withNetworkInterfaces(network).withBlockDeviceMappings(deviceMappings); if (template.getIamProfileName().isPresent()) { request.withIamInstanceProfile( new IamInstanceProfileSpecification().withName(template.getIamProfileName().get())); } if (template.getKeyName().isPresent()) { request.withKeyName(template.getKeyName().get()); } Placement placement = null; if (template.getAvailabilityZone().isPresent()) { placement = new Placement().withAvailabilityZone(template.getAvailabilityZone().get()); } if (template.getPlacementGroup().isPresent()) { placement = (placement == null) ? new Placement().withGroupName(template.getPlacementGroup().get()) : placement.withGroupName(template.getPlacementGroup().get()); } placement = (placement == null) ? new Placement().withTenancy(template.getTenancy()) : placement.withTenancy(template.getTenancy()); request.withPlacement(placement); return request; }
From source file:com.pinterest.arcee.aws.EC2HostInfoDAOImpl.java
License:Apache License
@Override public List<HostBean> launchEC2Instances(GroupBean groupBean, int instanceCnt, String subnet) throws Exception { RunInstancesRequest request = new RunInstancesRequest(); request.setImageId(groupBean.getImage_id()); request.setInstanceType(groupBean.getInstance_type()); request.setKeyName("ops"); request.setSecurityGroupIds(Arrays.asList(groupBean.getSecurity_group())); request.setSubnetId(subnet);// ww w.j av a 2 s. co m request.setUserData(groupBean.getUser_data()); IamInstanceProfileSpecification iamRole = new IamInstanceProfileSpecification(); iamRole.setArn(groupBean.getIam_role()); request.setIamInstanceProfile(iamRole); request.setMinCount(instanceCnt); request.setMaxCount(instanceCnt); List<HostBean> newHosts = new ArrayList<>(); try { RunInstancesResult result = ec2Client.runInstances(request); List<Instance> instances = result.getReservation().getInstances(); LOG.info("Launch instances {}", instances.toString()); for (Instance instance : instances) { HostBean host = new HostBean(); host.setHost_name(instance.getInstanceId()); host.setHost_id(instance.getInstanceId()); host.setIp(instance.getPrivateIpAddress()); host.setGroup_name(groupBean.getGroup_name()); host.setState(HostState.PROVISIONED); host.setCreate_date(instance.getLaunchTime().getTime()); host.setLast_update(instance.getLaunchTime().getTime()); newHosts.add(host); } } catch (AmazonClientException ex) { LOG.error(String.format("Failed to call aws runInstances when launching host %s", newHosts.toString()), ex); throw new DeployInternalException( String.format("Failed to call aws runInstances when launching host %s", newHosts.toString()), ex); } return newHosts; }
From source file:edu.iit.ec2.EC2.java
/** * *///from www .ja va 2s . c om public void createInstances() { amazonEC2Client.setEndpoint("ec2.us-west-2.amazonaws.com"); Set<Instance> instances = new HashSet<Instance>(); for (int i = 0; i < NUM_WORKERS; i++) { RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); runInstancesRequest.withImageId("ami-b5a7ea85").withInstanceType("t2.micro").withMinCount(1) .withMaxCount(1).withKeyName(KEY_NAME) .withIamInstanceProfile(new IamInstanceProfileSpecification().withName(IAM_PROFILE_NAME)); //.withSecurityGroups("my-security-group"); RunInstancesResult runInstancesResult = amazonEC2Client.runInstances(runInstancesRequest); try { Thread.sleep(5000); instances.addAll(runInstancesResult.getReservation().getInstances()); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } } //DescribeInstancesResult describeInstancesRequest = amazonEC2Client.describeInstances(); //List<Reservation> reservations = describeInstancesRequest.getReservations(); /* // add all instances to a Set. for (Reservation reservation : reservations) { instances.addAll(reservation.getInstances()); }*/ System.out.println("You have " + instances.size() + " Amazon EC2 instance(s)."); DOA doa = new DOA(); for (Instance ins : instances) { if (ins.getKeyName().equals(KEY_NAME)) { System.out.println("DNS name is " + ins.getPrivateIpAddress()); System.out.println("queue name is " + doa.getEc2SendQueue()); doa.updateEc2Queue(ins.getPrivateIpAddress(), doa.getEc2SendQueue()); } } }
From source file:hudson.plugins.ec2.SlaveTemplate.java
License:Open Source License
/** * Provisions an On-demand EC2 slave by launching a new instance or * starting a previously-stopped instance. *//* w ww . jav a 2s .com*/ private EC2AbstractSlave provisionOndemand(TaskListener listener) throws AmazonClientException, IOException { PrintStream logger = listener.getLogger(); AmazonEC2 ec2 = getParent().connect(); try { String msg = "Launching " + ami + " for template " + description; logger.println(msg); LOGGER.info(msg); KeyPair keyPair = getKeyPair(ec2); RunInstancesRequest riRequest = new RunInstancesRequest(ami, 1, 1); InstanceNetworkInterfaceSpecification net = new InstanceNetworkInterfaceSpecification(); if (useEphemeralDevices) { setupEphemeralDeviceMapping(riRequest); } else { setupCustomDeviceMapping(riRequest); } List<Filter> diFilters = new ArrayList<Filter>(); diFilters.add(new Filter("image-id").withValues(ami)); if (StringUtils.isNotBlank(getZone())) { Placement placement = new Placement(getZone()); if (getUseDedicatedTenancy()) { placement.setTenancy("dedicated"); } riRequest.setPlacement(placement); diFilters.add(new Filter("availability-zone").withValues(getZone())); } if (StringUtils.isNotBlank(getSubnetId())) { if (getAssociatePublicIp()) { net.setSubnetId(getSubnetId()); } else { riRequest.setSubnetId(getSubnetId()); } diFilters.add(new Filter("subnet-id").withValues(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 { riRequest.setSecurityGroupIds(group_ids); } diFilters.add(new Filter("instance.group-id").withValues(group_ids)); } } } else { /* No subnet: we can use standard security groups by name */ riRequest.setSecurityGroups(securityGroupSet); if (securityGroupSet.size() > 0) diFilters.add(new Filter("instance.group-name").withValues(securityGroupSet)); } String userDataString = Base64.encodeBase64String(userData.getBytes()); riRequest.setUserData(userDataString); riRequest.setKeyName(keyPair.getKeyName()); diFilters.add(new Filter("key-name").withValues(keyPair.getKeyName())); riRequest.setInstanceType(type.toString()); diFilters.add(new Filter("instance-type").withValues(type.toString())); if (getAssociatePublicIp()) { net.setAssociatePublicIpAddress(true); net.setDeviceIndex(0); riRequest.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())); diFilters.add(new Filter("tag:" + t.getName()).withValues(t.getValue())); if (StringUtils.equals(t.getName(), EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE)) { hasCustomTypeTag = true; } } } if (!hasCustomTypeTag) { if (inst_tags == null) { inst_tags = new HashSet<Tag>(); } inst_tags.add(new Tag(EC2Tag.TAG_NAME_JENKINS_SLAVE_TYPE, "demand")); } DescribeInstancesRequest diRequest = new DescribeInstancesRequest(); diFilters.add(new Filter("instance-state-name").withValues(InstanceStateName.Stopped.toString(), InstanceStateName.Stopping.toString())); diRequest.setFilters(diFilters); msg = "Looking for existing instances with describe-instance: " + diRequest; logger.println(msg); LOGGER.fine(msg); DescribeInstancesResult diResult = ec2.describeInstances(diRequest); Instance existingInstance = null; if (StringUtils.isNotBlank(getIamInstanceProfile())) { riRequest.setIamInstanceProfile( new IamInstanceProfileSpecification().withArn(getIamInstanceProfile())); // cannot filter on IAM Instance Profile, so search in result reservationLoop: for (Reservation reservation : diResult.getReservations()) { for (Instance instance : reservation.getInstances()) { if (instance.getIamInstanceProfile() != null && instance.getIamInstanceProfile().getArn().equals(getIamInstanceProfile())) { existingInstance = instance; break reservationLoop; } } } } else if (diResult.getReservations().size() > 0) { existingInstance = diResult.getReservations().get(0).getInstances().get(0); } if (existingInstance == null) { // Have to create a new instance Instance inst = ec2.runInstances(riRequest).getReservation().getInstances().get(0); /* Now that we have our instance, we can set tags on it */ if (inst_tags != null) { for (int i = 0; i < 5; i++) { try { updateRemoteTags(ec2, inst_tags, inst.getInstanceId()); break; } catch (AmazonServiceException e) { if (e.getErrorCode().equals("InvalidInstanceRequestID.NotFound")) { Thread.sleep(5000); continue; } throw e; } } // That was a remote request - we should also update our local instance data. inst.setTags(inst_tags); } msg = "No existing instance found - created: " + inst; logger.println(msg); LOGGER.info(msg); return newOndemandSlave(inst); } msg = "Found existing stopped instance: " + existingInstance; logger.println(msg); LOGGER.info(msg); List<String> instances = new ArrayList<String>(); instances.add(existingInstance.getInstanceId()); StartInstancesRequest siRequest = new StartInstancesRequest(instances); StartInstancesResult siResult = ec2.startInstances(siRequest); msg = "Starting existing instance: " + existingInstance + " result:" + siResult; logger.println(msg); LOGGER.fine(msg); for (EC2AbstractSlave ec2Node : NodeIterator.nodes(EC2AbstractSlave.class)) { if (ec2Node.getInstanceId().equals(existingInstance.getInstanceId())) { msg = "Found existing corresponding Jenkins slave: " + ec2Node; logger.println(msg); LOGGER.finer(msg); return ec2Node; } } // Existing slave not found msg = "Creating new Jenkins slave for existing instance: " + existingInstance; logger.println(msg); LOGGER.info(msg); return newOndemandSlave(existingInstance); } catch (FormException e) { throw new AssertionError(); // we should have discovered all configuration issues upfront } catch (InterruptedException e) { throw new RuntimeException(e); } }
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 ww .java 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:io.druid.indexing.overlord.autoscaling.ec2.EC2IamProfileData.java
License:Apache License
public IamInstanceProfileSpecification toIamInstanceProfileSpecification() { final IamInstanceProfileSpecification spec = new IamInstanceProfileSpecification(); spec.setName(name);// w w w . j a v a 2 s . co m spec.setArn(arn); return spec; }