@Test public void testGetComputeDescriptionKeyFromAWSInstance() throws Throwable { Map<String, ZoneData> zones = new HashMap<>(); zones.put(AWS_ZONE_ID, ZoneData.build(AWS_REGION_ID, AWS_ZONE_ID, "")); Instance awsInstance = new Instance(); awsInstance.setInstanceId(AWS_INSTANCE_ID); Placement placement = new Placement(); placement.setAvailabilityZone(AWS_ZONE_ID); awsInstance.setPlacement(placement); String regionId = getRegionId(awsInstance); awsInstance.setInstanceType(AWS_INSTANCE_TYPE); awsInstance.setVpcId(AWS_VPC_ID); assertEquals(AWS_REGION_ID, regionId); InstanceDescKey computeDescriptionKey = getKeyForComputeDescriptionFromInstance(awsInstance, zones); assertEquals(AWS_COMPUTE_DESCRIPTION_KEY, computeDescriptionKey); }
@Test public void convertCompleteInstance() { DateTime launchTime = UtcTime.now(); Instance instance = new Instance().withInstanceId("i-1") .withState(new InstanceState().withName(InstanceStateName.Running)).withPublicIpAddress("1.2.3.4") .withPrivateIpAddress("1.2.3.5").withInstanceType(InstanceType.M1Small) .withLaunchTime(launchTime.toDate()) .withMonitoring(new Monitoring().withState(MonitoringState.Disabled)).withHypervisor(HypervisorType.Xen) .withPlacement(new Placement("us-east-1c")); Machine machine = convert(instance); assertThat(machine.getId(), is(instance.getInstanceId())); assertThat(machine.getLaunchTime(), is(launchTime)); assertThat(machine.getMachineState(), is(MachineState.RUNNING)); assertThat(machine.getCloudProvider(), is(CloudProviders.AWS_EC2)); assertThat(machine.getRegion(), is("us-east-1")); assertThat(machine.getMachineSize(), is("m1.small")); assertThat(machine.getMembershipStatus(), is(MembershipStatus.defaultStatus())); assertThat(machine.getServiceState(), is(ServiceState.UNKNOWN)); assertThat(machine.getPublicIps().size(), is(1)); assertThat(machine.getPublicIps().get(0), is(instance.getPublicIpAddress())); assertThat(machine.getPrivateIps().size(), is(1)); assertThat(machine.getPrivateIps().get(0), is(instance.getPrivateIpAddress())); assertThat(machine.getMetadata(), is(JsonUtils.toJson(instance))); }
@Test public void convertInstanceMissingPublicIp() { DateTime launchTime = UtcTime.now(); Instance instance = new Instance().withInstanceId("i-1") .withState(new InstanceState().withName(InstanceStateName.Running)).withPrivateIpAddress("1.2.3.5") .withInstanceType(InstanceType.M1Small).withLaunchTime(launchTime.toDate()) .withMonitoring(new Monitoring().withState(MonitoringState.Disabled)).withHypervisor(HypervisorType.Xen) .withPlacement(new Placement("us-east-1a")); Machine machine = convert(instance); assertThat(machine.getId(), is(instance.getInstanceId())); assertThat(machine.getCloudProvider(), is(CloudProviders.AWS_EC2)); assertThat(machine.getRegion(), is("us-east-1")); assertThat(machine.getMachineSize(), is("m1.small")); assertThat(machine.getLaunchTime(), is(launchTime)); assertThat(machine.getMachineState(), is(MachineState.RUNNING)); assertThat(machine.getMembershipStatus(), is(MembershipStatus.defaultStatus())); assertThat(machine.getServiceState(), is(ServiceState.UNKNOWN)); assertThat(machine.getPublicIps().size(), is(0)); assertThat(machine.getPrivateIps().size(), is(1)); assertThat(machine.getPrivateIps().get(0), is(instance.getPrivateIpAddress())); assertThat(machine.getMetadata(), is(JsonUtils.toJson(instance))); }
@Test public void convertInstanceMissingPrivateIp() { DateTime launchTime = UtcTime.now(); Instance instance = new Instance().withInstanceId("i-1") .withState(new InstanceState().withName(InstanceStateName.Running)).withPublicIpAddress("1.2.3.4") .withInstanceType(InstanceType.M1Medium).withLaunchTime(launchTime.toDate()) .withMonitoring(new Monitoring().withState(MonitoringState.Disabled)).withHypervisor(HypervisorType.Xen) .withPlacement(new Placement("us-east-1b")); Machine machine = convert(instance); assertThat(machine.getId(), is(instance.getInstanceId())); assertThat(machine.getCloudProvider(), is(CloudProviders.AWS_EC2)); assertThat(machine.getRegion(), is("us-east-1")); assertThat(machine.getMachineSize(), is("m1.medium")); assertThat(machine.getLaunchTime(), is(launchTime)); assertThat(machine.getMachineState(), is(MachineState.RUNNING)); assertThat(machine.getMembershipStatus(), is(MembershipStatus.defaultStatus())); assertThat(machine.getServiceState(), is(ServiceState.UNKNOWN)); assertThat(machine.getPublicIps().size(), is(1)); assertThat(machine.getPublicIps().get(0), is(instance.getPublicIpAddress())); assertThat(machine.getPrivateIps().size(), is(0)); assertThat(machine.getMetadata(), is(JsonUtils.toJson(instance))); }
/** * A converted spot instance {@link Machine} should have a cloud provider * value of {@link CloudProviders#AWS_SPOT} to distinguish it from a regular * EC2 on-demand instance. */ @Test public void convertSpotInstance() { // convert on-demand instance: cloud provider should be AWS_EC2 Instance onDemandInstance = new Instance().withInstanceId("i-1").withInstanceType(InstanceType.M1Medium) .withState(new InstanceState().withName(InstanceStateName.Running)) .withPlacement(new Placement("us-east-1b")); Machine onDemandMachine = convert(onDemandInstance); assertThat(onDemandMachine.getCloudProvider(), is(CloudProviders.AWS_EC2)); // convert spot instance: cloud provider should be AWS_EC2 Instance spotInstance = new Instance().withInstanceId("i-1").withInstanceType(InstanceType.M1Medium) .withState(new InstanceState().withName(InstanceStateName.Running)).withSpotInstanceRequestId("sir-123") .withPlacement(new Placement("us-east-1b")); Machine spotMachine = convert(spotInstance); assertThat(spotMachine.getCloudProvider(), is(CloudProviders.AWS_SPOT)); }
public void createPlacementGroupsIfDoNotExist(org.excalibur.core.cloud.api.Placement... groups) { if (groups != null) { ListeningExecutorService executor = newListeningDynamicScalingThreadPool(String.format("create-groups-%s", credentials_ .getRegion().getName())); List<Callable<Void>> tasks = newArrayList(); for (final org.excalibur.core.cloud.api.Placement placement : groups) { tasks.add(new Callable<Void>() { @Override public Void call() throws Exception { if (placement != null && !isNullOrEmpty(placement.getGroupName())) { try { new AmazonEC2Client(awsCredentials_).describePlacementGroups(new DescribePlacementGroupsRequest().withGroupNames(placement.getGroupName())); } catch (AmazonClientException exception) { LOG.debug("The group {} is unknown! Provider message: {}", placement.getGroupName(), exception.getMessage()); ec2_.createPlacementGroup(new CreatePlacementGroupRequest() .withGroupName(placement.getGroupName()).withStrategy(PlacementStrategy.Cluster)); } } return null; } }); } invokeAllAndShutdownWhenFinish(tasks, executor); } }
@Test public void convertWithServiceStateTag() { Tag serviceStateTag = new Tag().withKey(ScalingTags.SERVICE_STATE_TAG) .withValue(ServiceState.OUT_OF_SERVICE.name()); Instance instance = new Instance().withInstanceId("i-1").withInstanceType(InstanceType.M1Medium) .withState(new InstanceState().withName(InstanceStateName.Running)) .withPlacement(new Placement("us-east-1b")).withTags(serviceStateTag); Machine machine = convert(instance); assertThat(machine.getServiceState(), is(ServiceState.OUT_OF_SERVICE)); }
@Test public void convertWithMembershipStatusTag() { MembershipStatus status = MembershipStatus.awaitingService(); String statusJsonString = JsonUtils.toString(JsonUtils.toJson(status)); Tag membershipStatusTag = new Tag().withKey(ScalingTags.MEMBERSHIP_STATUS_TAG).withValue(statusJsonString); Instance instance = new Instance().withInstanceId("i-1").withInstanceType(InstanceType.M1Medium) .withState(new InstanceState().withName(InstanceStateName.Running)) .withPlacement(new Placement("us-east-1b")).withTags(membershipStatusTag); Machine machine = convert(instance); assertThat(machine.getMembershipStatus(), is(status)); }
/** * Prepares the request. * @param targetProperties the target properties * @param userData the user data to pass * @return a request * @throws UnsupportedEncodingException */ private RunInstancesRequest prepareEC2RequestNode( Map<String,String> targetProperties, String userData ) throws UnsupportedEncodingException { RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); String flavor = targetProperties.get(Ec2Constants.VM_INSTANCE_TYPE); if( Utils.isEmptyOrWhitespaces( flavor )) flavor = "t1.micro"; runInstancesRequest.setInstanceType( flavor ); runInstancesRequest.setImageId( targetProperties.get( Ec2Constants.AMI_VM_NODE )); runInstancesRequest.setMinCount( 1 ); runInstancesRequest.setMaxCount( 1 ); runInstancesRequest.setKeyName( targetProperties.get(Ec2Constants.SSH_KEY_NAME)); String secGroup = targetProperties.get(Ec2Constants.SECURITY_GROUP_NAME); if( Utils.isEmptyOrWhitespaces(secGroup)) secGroup = "default"; runInstancesRequest.setSecurityGroups(Collections.singletonList(secGroup)); String availabilityZone = targetProperties.get(Ec2Constants.AVAILABILITY_ZONE); if(! Utils.isEmptyOrWhitespaces(availabilityZone)) runInstancesRequest.setPlacement(new Placement(availabilityZone)); // The following part enables to transmit data to the VM. // When the VM is up, it will be able to read this data. String encodedUserData = new String( Base64.encodeBase64( userData.getBytes( StandardCharsets.UTF_8 )), "UTF-8" ); runInstancesRequest.setUserData( encodedUserData ); return runInstancesRequest; }
private void createAndRunInstance(AmazonEC2Client ec2Client, Exchange exchange) { String ami; InstanceType instanceType; int minCount; int maxCount; boolean monitoring; String kernelId; boolean ebsOptimized; Collection securityGroups; String keyName; String clientToken; Placement placement; RunInstancesRequest request = new RunInstancesRequest(); if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.IMAGE_ID))) { ami = exchange.getIn().getHeader(EC2Constants.IMAGE_ID, String.class); request.withImageId(ami); } else { throw new IllegalArgumentException("AMI must be specified"); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE))) { instanceType = exchange.getIn().getHeader(EC2Constants.INSTANCE_TYPE, InstanceType.class); request.withInstanceType(instanceType.toString()); } else { throw new IllegalArgumentException("Instance Type must be specified"); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT))) { minCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MIN_COUNT, Integer.class); request.withMinCount(minCount); } else { throw new IllegalArgumentException("Min instances count must be specified"); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT))) { maxCount = exchange.getIn().getHeader(EC2Constants.INSTANCE_MAX_COUNT, Integer.class); request.withMaxCount(maxCount); } else { throw new IllegalArgumentException("Max instances count must be specified"); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING))) { monitoring = exchange.getIn().getHeader(EC2Constants.INSTANCE_MONITORING, Boolean.class); request.withMonitoring(monitoring); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID))) { kernelId = exchange.getIn().getHeader(EC2Constants.INSTANCE_KERNEL_ID, String.class); request.withKernelId(kernelId); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED))) { ebsOptimized = exchange.getIn().getHeader(EC2Constants.INSTANCE_EBS_OPTIMIZED, Boolean.class); request.withEbsOptimized(ebsOptimized); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS))) { securityGroups = exchange.getIn().getHeader(EC2Constants.INSTANCE_SECURITY_GROUPS, Collection.class); request.withSecurityGroups(securityGroups); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR))) { keyName = exchange.getIn().getHeader(EC2Constants.INSTANCES_KEY_PAIR, String.class); request.withKeyName(keyName); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN))) { clientToken = exchange.getIn().getHeader(EC2Constants.INSTANCES_CLIENT_TOKEN, String.class); request.withClientToken(clientToken); } if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT))) { placement = exchange.getIn().getHeader(EC2Constants.INSTANCES_PLACEMENT, Placement.class); request.withPlacement(placement); } RunInstancesResult result; try { result = ec2Client.runInstances(request); } catch (AmazonServiceException ase) { LOG.trace("Run Instances command returned the error code {}", ase.getErrorCode()); throw ase; } LOG.trace("Creating and running instances with ami [{}] and instance type {}", ami, instanceType.toString()); Message message = getMessageForResponse(exchange); message.setBody(result); }
/** * Builds a {@code RunInstancesRequest} starting from a template and a virtual instance ID. * Instances will be tagged as they're created. * * @param template the instance template * @param virtualInstanceId the virtual instance IDs * @param userDefinedTags user defined tags to attach to the instance */ @SuppressWarnings("ConstantConditions") private RunInstancesRequest newRunInstancesRequest(EC2InstanceTemplate template, String virtualInstanceId, List<Tag> userDefinedTags) { String image = template.getImage(); String type = template.getType(); InstanceNetworkInterfaceSpecification network = getInstanceNetworkInterfaceSpecification(template); List<BlockDeviceMapping> deviceMappings = getBlockDeviceMappings(template); LOG.info(">> Instance request type: {}, image: {}", type, image); List<Tag> tags = ec2TagHelper.getInstanceTags(template, virtualInstanceId, userDefinedTags); List<TagSpecification> tagSpecifications = Lists.newArrayList( new TagSpecification().withTags(tags).withResourceType(ResourceType.Instance), new TagSpecification().withTags(tags).withResourceType(ResourceType.Volume)); RunInstancesRequest request = new RunInstancesRequest() .withImageId(image) .withInstanceType(type) .withMaxCount(1) .withMinCount(1) .withClientToken(UUID.randomUUID().toString()) .withNetworkInterfaces(network) .withTagSpecifications(tagSpecifications) .withBlockDeviceMappings(deviceMappings) .withEbsOptimized(template.isEbsOptimized()); 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); Optional<String> userData = template.getUserData(); if (userData.isPresent()) { request.withUserData(userData.get()); } return request; }
public void createPlacementGroups(org.excalibur.core.cloud.api.Placement... groups) { createPlacementGroupsIfDoNotExist(groups); }
private VirtualMachine toExcaliburInstance(Instance instance, KeyPair keyPair) { Map<String, Tag> tags = TAGS.apply(instance.getTags()); VirtualMachine vm = new VirtualMachine() .setName(instance.getInstanceId()) .setImageId(instance.getImageId()) .setType(InstanceType.valueOf(instance.getInstanceType()).setProvider(this.credentials_.getProvider())) .setState(new InstanceStateDetails(InstanceStateType.valueOfFrom(instance.getState().getName()), new Date())) //TODO we need to improve this .setConfiguration( new VmConfiguration() .setKeyName(instance.getKeyName()) .setKeyPairs(new KeyPairs().setPrivateKey(keyPair)) .setPlatform(isNullOrEmpty(instance.getPlatform()) ? DEFAULT_PLATFORM : instance.getPlatform()) .setPlatformUserName(tags.get(DEFAULT_PLATFORM_INSTANCE_USERNAME_TAG) != null ? tags.get(DEFAULT_PLATFORM_INSTANCE_USERNAME_TAG).getValue() : System.getProperty("org.excalibur.default.platform.username")) .setPrivateIpAddress(instance.getPrivateIpAddress()).setPublicIpAddress(instance.getPublicIpAddress()) .setPublicDnsName(instance.getPublicDnsName())).setLaunchTime(instance.getLaunchTime()) // .setLocation(new Region().setName(instance.getPlacement().getAvailabilityZone())) // .setLocation(credentials_.getRegion()) .setLocation(new Zone().setName(instance.getPlacement().getAvailabilityZone()).setRegion(credentials_.getRegion())) .setPlacement(new org.excalibur.core.cloud.api.Placement() .setGroupName(instance.getPlacement().getGroupName()) .setZone(instance.getPlacement().getAvailabilityZone())) .setOwner(new User(this.credentials_.getUserId()).setUsername(tags.get("username") != null ? tags.get("username").getValue(): null)); if (tags.containsKey("keyname")) { if (vm.getConfiguration().getKeyPairs().getPrivateKey() == null) { vm.getConfiguration().getKeyPairs().setPrivateKey(new KeyPair()); } vm.getConfiguration().getKeyPairs().getPrivateKey().setKeyName(tags.get("keyname").getValue()); } else { return null; } InstanceAttribute attribute = new AmazonEC2Client(awsCredentials_).describeInstanceAttribute( new DescribeInstanceAttributeRequest().withInstanceId(instance.getInstanceId()).withAttribute("userData")).getInstanceAttribute(); // List<InstanceStatus> instanceStatuses = new AmazonEC2Client(awsCredentials_).describeInstanceStatus( // new DescribeInstanceStatusRequest().withInstanceIds(instance.getInstanceId())).getInstanceStatuses(); if (!isNullOrEmpty(attribute.getUserData())) { String userData = new String(Base64.decodeBase64(attribute.getUserData().getBytes())); int i = userData.indexOf("#start-data"), f = userData.indexOf("#end-data"); if (i > -1 && f > -1) { String[] keys = userData.substring(i, f).split("#"); checkState(keys.length == 4); vm.getConfiguration().getKeyPairs().getPrivateKey().setKeyMaterial(keys[2]); vm.getConfiguration().getKeyPairs() .setPublicKey(new KeyPair().setKeyName(tags.get("keyname").getValue()).setKeyMaterial(keys[3].trim())); } vm.setUserData(attribute.getUserData()); } for (Tag tag: tags.values()) { vm.getTags().add(org.excalibur.core.cloud.api.domain.Tag.valueOf(tag.getKey(), tag.getValue())); } return vm; }
@Override protected Instance convertObject(com.xerox.amazonws.ec2.ReservationDescription.Instance from) { Instance to = new Instance(); to.setInstanceId(from.getInstanceId()); to.setImageId(from.getImageId()); InstanceState state = new InstanceState(); state.setCode(from.getStateCode()); state.setName(from.getState()); to.setState(state); to.setPrivateDnsName(from.getPrivateDnsName()); to.setPublicDnsName(from.getDnsName()); to.setStateTransitionReason(from.getReason()); to.setKeyName(from.getKeyName()); to.setAmiLaunchIndex(null); to.setProductCodes(null); to.setInstanceType(from.getInstanceType().name()); to.setLaunchTime(from.getLaunchTime().getTime()); Placement placement = new Placement(); placement.setAvailabilityZone(from.getAvailabilityZone()); placement.setGroupName(null); // 未実装 to.setPlacement(placement); to.setKernelId(from.getKernelId()); to.setRamdiskId(from.getRamdiskId()); to.setPlatform(from.getPlatform()); Monitoring monitoring = new Monitoring(); monitoring.setState(Boolean.toString(from.isMonitoring())); to.setMonitoring(monitoring); // 未実装 to.setSubnetId(null); to.setVpcId(null); to.setPrivateIpAddress(null); to.setPublicIpAddress(null); to.setStateReason(null); //to.setArchitecture(null); to.setRootDeviceName(null); to.setRootDeviceName(null); to.setBlockDeviceMappings(null); //to.setVirtualizationType(null); //to.setInstanceLifecycle(null); to.setSpotInstanceRequestId(null); //to.setLicense(null); to.setClientToken(null); to.setTags(null); return to; }
@Override public Placement getPlacement() { return (Placement) resource.getAttribute("Placement"); }
/** * Gets the value of the Placement attribute. If this resource is not yet * loaded, a call to {@code load()} is made to retrieve the value of the * attribute. */ Placement getPlacement();