List of usage examples for com.amazonaws.services.ec2.model RunInstancesRequest setRamdiskId
public void setRamdiskId(String ramdiskId)
The ID of the RAM disk to select.
From source file:com.appdynamics.connectors.AWSConnector.java
License:Apache License
public IMachine createMachine(IComputeCenter computeCenter, IImage image, IMachineDescriptor machineDescriptor) throws InvalidObjectException, ConnectorException { boolean succeeded = false; Exception createFailureRootCause = null; Instance instance = null;/* w w w.jav a 2 s .co m*/ try { IProperty[] macProps = machineDescriptor.getProperties(); AmazonEC2 connector = getConnector(image, computeCenter, controllerServices); String amiName = Utils.getAMIName(image.getProperties(), controllerServices); List<String> securityGroups = getSecurityGroup(macProps); validateAndConfigureSecurityGroups(securityGroups, connector); controllerServices.getStringPropertyByName(macProps, Utils.SECURITY_GROUP) .setValue(getSecurityGroupsAsString(securityGroups)); String keyPair = Utils.getKeyPair(macProps, controllerServices); InstanceType instanceType = getInstanceType(macProps); String zone = Utils.getZone(macProps, controllerServices); String kernel = Utils.getKernel(macProps, controllerServices); String ramdisk = Utils.getRamDisk(macProps, controllerServices); String controllerHost = System.getProperty(CONTROLLER_SERVICES_HOST_NAME_PROPERTY_KEY, InetAddress.getLocalHost().getHostName()); int controllerPort = Integer.getInteger(CONTROLLER_SERVICES_PORT_PROPERTY_KEY, DEFAULT_CONTROLLER_PORT_VALUE); IAccount account = computeCenter.getAccount(); String accountName = account.getName(); String accountAccessKey = account.getAccessKey(); AgentResolutionEncoder agentResolutionEncoder = new AgentResolutionEncoder(controllerHost, controllerPort, accountName, accountAccessKey); String userData = agentResolutionEncoder.encodeAgentResolutionInfo(); String instanceName = Utils.getInstanceName(macProps, controllerServices); logger.info("Starting EC2 machine of Image :" + amiName + " Name :" + instanceName + " security :" + securityGroups + " keypair :" + keyPair + " instance :" + instanceType + " zone :" + zone + " kernel :" + kernel + " ramdisk :" + ramdisk + " userData :" + userData); RunInstancesRequest runInstancesRequest = new RunInstancesRequest(amiName, 1, 1); runInstancesRequest.setSecurityGroups(securityGroups); runInstancesRequest.setUserData(Base64.encodeAsString(userData.getBytes())); runInstancesRequest.setKeyName(keyPair); runInstancesRequest.setInstanceType(instanceType); runInstancesRequest.setKernelId(kernel); runInstancesRequest.setRamdiskId(ramdisk); Reservation reservation = connector.runInstances(runInstancesRequest).getReservation(); List<Instance> instances = reservation.getInstances(); if (instances.size() == 0) throw new ConnectorException("Cannot create instance for image :" + image.getName()); instance = instances.get(0); //Set name for the instance if (!Strings.isNullOrEmpty(instanceName)) { CreateTagsRequest createTagsRequest = new CreateTagsRequest(); createTagsRequest.withResources(instance.getInstanceId()).withTags(new Tag("Name", instanceName)); connector.createTags(createTagsRequest); } logger.info("EC2 machine started; id:" + instance.getInstanceId()); IMachine machine; if (Strings.isNullOrEmpty(instance.getPublicDnsName())) { machine = controllerServices.createMachineInstance(instance.getInstanceId(), agentResolutionEncoder.getUniqueHostIdentifier(), computeCenter, machineDescriptor, image, getAgentPort()); } else { machine = controllerServices.createMachineInstance(instance.getInstanceId(), agentResolutionEncoder.getUniqueHostIdentifier(), instance.getPublicDnsName(), computeCenter, machineDescriptor, image, getAgentPort()); } if (kernel == null) { controllerServices.getStringPropertyByName(macProps, Utils.KERNEL).setValue(instance.getKernelId()); } if (zone == null) { DescribeAvailabilityZonesResult describeAvailabilityZonesResult = connector .describeAvailabilityZones(); List<AvailabilityZone> availabilityZones = describeAvailabilityZonesResult.getAvailabilityZones(); controllerServices.getStringPropertyByName(macProps, Utils.ZONE) .setValue(availabilityZones.get(0).getZoneName()); } controllerServices.getStringPropertyByName(macProps, Utils.INSTANCE_TYPE) .setValue(instance.getInstanceType()); succeeded = true; return machine; } catch (InvalidObjectException e) { createFailureRootCause = e; throw e; } catch (ConnectorException e) { createFailureRootCause = e; throw e; } catch (Exception e) { createFailureRootCause = e; throw new ConnectorException(e.getMessage(), e); } finally { // We have to make sure to terminate any orphan EC2 instances if // the machine create fails. if (!succeeded && instance != null) { try { ConnectorLocator.getInstance().getConnector(computeCenter, controllerServices) .terminateInstances( new TerminateInstancesRequest(Lists.newArrayList(instance.getInstanceId()))); } catch (Exception e) { throw new ConnectorException("Machine create failed, but terminate failed as well! " + "We have an orphan EC2 instance with id: " + instance.getInstanceId() + " that must be shut down manually. Root cause for machine " + "create failure is following: ", createFailureRootCause); } } } }
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.// w ww .j a v a 2s .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:pl.edu.agh.samm.tadapter.eucalyptus.EucalyptusTransportAdapter.java
License:Open Source License
/** * starts new VM instance, <b>does not add new resource to core</b> * * @param resource/*from w w w . j av a 2 s. co m*/ * @param imageId * @param instanceType * @return * @throws Exception */ private Instance startOneInstanceAction(Resource resource, final String imageId, String instanceType, String userData) throws Exception { AmazonEC2Client client = ec2Clients.get(resource); Image image = getImageByImageID(client, imageId); if (!IMAGE_TYPE_MACHINE.equals(image.getImageType())) { throw new RuntimeException("Provided image type is not machine!"); } if (!IMAGE_STATE_AVAILABLE.equals(image.getState())) { throw new RuntimeException("Provided image state is not " + IMAGE_STATE_AVAILABLE); } RunInstancesRequest command = new RunInstancesRequest(); if (userData != null) { command.setUserData(userData); } command.setImageId(image.getImageId()); command.setInstanceType(instanceType); command.setKernelId(image.getKernelId()); command.setMaxCount(1); command.setMinCount(1); command.setRamdiskId(image.getRamdiskId()); command.setKeyName(resource.getProperty(EUCALYPTUS_KEY_NAME).toString()); RunInstancesResult result = client.runInstances(command); List<Instance> instances = result.getReservation().getInstances(); if (instances.size() < 1) { logger.error("Something bad happend while running VM instance"); } Instance instance = instances.get(0); instance = EC2Util.waitForRunningState(client, instance); logger.info("Started new instance of image " + imageId + "! InstanceId = " + instance.getInstanceId()); instance = EC2Util.waitForPublicDNS(client, instance); logger.info("Instance IP address is: " + instance.getPublicDnsName()); return instance; }