List of usage examples for com.amazonaws.services.ec2.model RunInstancesRequest setMonitoring
public void setMonitoring(Boolean monitoring)
Specifies whether detailed monitoring is enabled for the instance.
From source file:org.gridgain.grid.spi.cloud.ec2lite.GridEc2LiteCloudSpi.java
License:GNU General Public License
/** * Creates Amazon EC2 run instances request by provided command. * * @param cmd Cloud command.//from w ww . j a va2s. c om * @return EC2 run instances request. * @throws GridSpiException If any exception occurs. */ private RunInstancesRequest createRunInstancesRequest(GridCloudCommand cmd) throws GridSpiException { assert cmd != null; Collection<GridCloudResource> rsrcs = cmd.resources(); int num = cmd.number(); assert rsrcs != null && !rsrcs.isEmpty(); assert num > 0; GridCloudResource img = null; Collection<String> grps = new ArrayList<String>(); // Separate image and security groups for (GridCloudResource rsrc : rsrcs) if (rsrc.type() == CLD_IMAGE) img = rsrc; else if (rsrc.type() == CLD_SECURITY_GROUP) grps.add(rsrc.id()); assert img != null; Map<String, String> imgParams = img.parameters(); Map<String, String> cmdParams = cmd.parameters(); if (imgParams == null) throw new GridSpiException( "Unable to process command (image parameters are null) [cmd=" + cmd + ", image=" + img + ']'); RunInstancesRequest req = new RunInstancesRequest(); req.setImageId(img.id()); req.setMinCount(num); req.setMaxCount(num); if (!grps.isEmpty()) req.setSecurityGroups(grps); String val; if (!F.isEmpty(val = imgParams.get(IMG_KERNEL_ID))) req.setKernelId(val); if (!F.isEmpty(val = imgParams.get(IMG_RAMDISK_ID))) req.setRamdiskId(val); Collection<String> userDataList = new LinkedList<String>(); if (cmdParams != null) { if (!F.isEmpty(val = cmdParams.get(INST_TYPE))) req.setInstanceType(val); if (!F.isEmpty(val = cmdParams.get(INST_PLACEMENT))) req.setPlacement(new Placement(val)); if (!F.isEmpty(val = cmdParams.get(INST_KEY_PAIR_NAME))) req.setKeyName(val); if (!F.isEmpty(val = cmdParams.get(INST_MON))) req.setMonitoring(Boolean.parseBoolean(val)); if (!F.isEmpty(val = cmdParams.get(INST_PASS_EC2_KEYS)) && Boolean.parseBoolean(val)) { userDataList.add(GRIDGAIN_ACCESS_KEY_ID_KEY + '=' + accessKeyId); userDataList.add(GRIDGAIN_SECRET_KEY_KEY + '=' + secretAccessKey); } if (!F.isEmpty(val = cmdParams.get(INST_MAIN_S3_BUCKET))) userDataList.add(GRIDGAIN_MAIN_S3_BUCKET_KEY + '=' + val); if (!F.isEmpty(val = cmdParams.get(INST_USER_S3_BUCKET))) userDataList.add(GRIDGAIN_EXT_S3_BUCKET_KEY + '=' + val); if (!F.isEmpty(val = cmdParams.get(INST_JVM_OPTS))) userDataList.add(val); } if (req.isMonitoring() == null) // Monitoring was not set from params, set default value req.setMonitoring(enableMon); if (!userDataList.isEmpty()) req.setUserData(new String(Base64.encodeBase64(F.concat(userDataList, USER_DATA_DELIM).getBytes()))); return req; }
From source file:org.gridgain.grid.util.ec2.GridEc2Helper.java
License:GNU General Public License
/** * @param imgId EC2 image ID./*from www . j a v a2s . c o m*/ * @param keyPair Key pair name. * @param secGrps Security groups. * @param instType EC2 Instance type. * @param userData Instances user data. * @param count Instance count. * @param enableMon Enable CloudWatch monitoring. * @return List of started instances' IDs. * @throws GridException Thrown in case of any exception. */ private List<String> startInstances(String imgId, String keyPair, Collection<String> secGrps, String instType, String userData, int count, boolean enableMon) throws GridException { RunInstancesRequest req = new RunInstancesRequest(); req.setImageId(imgId); req.setInstanceType(instType); req.setKeyName(keyPair); req.setMaxCount(count); req.setMinCount(count); req.setMonitoring(enableMon); if (!F.isEmpty(secGrps)) req.setSecurityGroups(secGrps); if (userData != null) req.setUserData(new String(Base64.encodeBase64(userData.getBytes()))); RunInstancesResult res; try { res = ec2.runInstances(req); } catch (AmazonClientException ex) { throw new GridException("Failed to perform EC2 run instances request: " + ex.getMessage(), ex); } List<Instance> running = res.getReservation().getInstances(); if (running == null) throw new GridException("Received unexpected EC2 response (instances have not been started)."); List<String> ids = new ArrayList<String>(); for (Instance inst : running) ids.add(inst.getInstanceId()); if (enableMon) { MonitorInstancesRequest mReq = new MonitorInstancesRequest(); mReq.setInstanceIds(ids); try { ec2.monitorInstances(mReq); } catch (AmazonClientException ex) { throw new GridException("Failed to start instances monitoring.", ex); } } return ids; }
From source file:org.occiware.clouddriver.instance.InstanceOperations.java
License:Apache License
/** * Create one ec2 instance with data instance object. * @param instance/* ww w . j av a 2s .c o m*/ * @throws InstanceOperationException */ public InstanceDO createInstance(InstanceDO instance) throws InstanceOperationException { // Check instance data object before creation. checkInstanceObjCreation(instance); boolean hasPlacement = false; String keyPairName = instance.getKeyPairName(); String imageId = instance.getImage().getImageId(); String instanceType = instance.getInstanceType(); Boolean monitoring = instance.isMonitoring(); String region = instance.getRegionId(); String zone = instance.getZoneId(); List<GroupIdentifierDO> securityGroups = instance.getSecurityGroups(); String name = instance.getName(); String userData = instance.getUserData(); RunInstancesRequest rRequest = new RunInstancesRequest(imageId, 1, 1); rRequest.setInstanceType(instanceType); rRequest.setMonitoring(monitoring); if (keyPairName != null) { rRequest.setKeyName(keyPairName); } if (userData != null) { rRequest.setUserData(userData); } Placement placement = new Placement(); PlacementDO placementDO = instance.getPlacement(); if (placementDO != null && placementDO.getAvailabilityZone() != null) { placement.setAvailabilityZone(placementDO.getAvailabilityZone()); hasPlacement = true; } else { if (zone != null) { if (!zone.trim().isEmpty()) { placement.setAvailabilityZone(region + zone); hasPlacement = true; } } } if (placementDO != null) { String groupName = placementDO.getGroupName(); String tenancy = placementDO.getTenancy(); if (groupName != null) { placement.setGroupName(groupName); hasPlacement = true; } if (tenancy != null) { placement.setTenancy(tenancy); hasPlacement = true; } } if (hasPlacement) { rRequest.setPlacement(placement); } if (securityGroups != null && !securityGroups.isEmpty()) { List<String> securityGroupNames = new ArrayList<>(); for (GroupIdentifierDO secGroup : securityGroups) { securityGroupNames.add(secGroup.getGroupName()); } rRequest.setSecurityGroups(securityGroupNames); } RunInstancesResult runInstancesResult; List<Instance> instances; try { runInstancesResult = ec2Client.getClientInstance().runInstances(rRequest); instances = runInstancesResult.getReservation().getInstances(); } catch (AmazonServiceException ase) { logger.error("Exception thrown from aws : " + ase.getErrorCode() + " --> " + ase.getErrorMessage()); throw new InstanceOperationException(ase); } catch (AmazonClientException ace) { logger.error("Exception thrown from aws : " + ace.getMessage()); throw new InstanceOperationException(ace); } InstanceDO instanceDOToReturn = null; if (instances != null && !instances.isEmpty()) { instanceDOToReturn = InstanceDataFactory.buildInstanceDataFromModel(instances.get(0)); if (instanceDOToReturn != null && name != null && instanceDOToReturn.getInstanceId() != null) { TagsOperation tagOperation = new TagsOperation(ec2Client); tagOperation.createTag(instanceDOToReturn.getInstanceId(), "Name", name); } } return instanceDOToReturn; }