/** * Tests to make sure the slave created has been configured properly. * Also tests to make sure the spot max bid price has been set properly. * @throws Exception - Exception that can be thrown by the Jenkins test harness */ public void testConfigWithSpotBidPrice() throws Exception { String ami = "ami1"; String description = "foo ami"; EC2Tag tag1 = new EC2Tag( "name1", "value1" ); EC2Tag tag2 = new EC2Tag( "name2", "value2" ); List<EC2Tag> tags = new ArrayList<EC2Tag>(); tags.add( tag1 ); tags.add( tag2 ); SpotConfiguration spotConfig = new SpotConfiguration(".05", SpotInstanceType.OneTime.toString()); SlaveTemplate orig = new SlaveTemplate(ami, EC2AbstractSlave.TEST_ZONE, spotConfig, "default", "foo", InstanceType.M1Large, "ttt", Node.Mode.NORMAL, "foo ami", "bar", "bbb", "aaa", "10", "fff", null, "-Xmx1g", false, "subnet 456", tags, null, true, null, "", false, false, "", false, ""); List<SlaveTemplate> templates = new ArrayList<SlaveTemplate>(); templates.add(orig); AmazonEC2Cloud ac = new AmazonEC2Cloud("us-east-1", false, "abc", "def", "us-east-1", "ghi", "3", templates); hudson.clouds.add(ac); submit(createWebClient().goTo("configure").getFormByName("config")); SlaveTemplate received = ((EC2Cloud)hudson.clouds.iterator().next()).getTemplate(description); assertEqualBeans(orig, received, "ami,zone,spotConfig,description,remoteFS,type,jvmopts,stopOnTerminate,securityGroups,subnetId,tags,usePrivateDnsName"); }
public List<SpotInstanceOfferResult> createSpotInstanceOffer(SpotInstanceOffer request) { RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest(request.getOfferValue().toPlainString()) .withValidFrom(request.getValidFrom()) .withValidUntil(request.getValidUntil()) .withType(SpotInstanceType.fromValue(request.getType().getName())) .withInstanceCount(request.getNumberOfInstances()) .withLaunchSpecification(new LaunchSpecification() .withInstanceType(request.getInstanceType().getName()) .withImageId(request.getImageId()) .withKeyName(request.getKeyName())); RequestSpotInstancesResult requestedSpotInstances = ec2_.requestSpotInstances(spotRequest); request.setStatus(new InstanceTemplateStatus().setStatus(Status.SUCCESS)); return toSpotInstanceResults(request, requestedSpotInstances.getSpotInstanceRequests()); }
public static void main(String[] args) { String region = "us-east-1"; AWSCredentials credentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey); AmazonEC2 api = AmazonEC2ClientBuilder.standard().withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(credentials)).build(); // no particular availability zone String availabilityZone = null; String instanceType = "t1.micro"; String imageId = "ami-3cf8b154"; List<String> securityGroups = Arrays.asList("webserver"); String keyPair = "instancekey"; String bootScript = Joiner.on("\n") .join(Arrays.asList("#!/bin/bash", "sudo apt-get update -qy", "sudo apt-get install -qy apache2")); int instanceCount = 50; String bidPrice = "0.001"; SpotPlacement placement = new SpotPlacement().withAvailabilityZone(availabilityZone); LaunchSpecification launchSpec = new LaunchSpecification().withInstanceType(instanceType).withImageId(imageId) .withPlacement(placement).withSecurityGroups(securityGroups).withKeyName(keyPair) .withUserData(AmazonApiUtils.base64Encode(bootScript)); RequestSpotInstancesRequest request = new RequestSpotInstancesRequest().withInstanceCount(instanceCount) .withType(SpotInstanceType.Persistent).withSpotPrice(bidPrice).withLaunchSpecification(launchSpec); RequestSpotInstancesResult result = api.requestSpotInstances(request); for (SpotInstanceRequest spotRequest : result.getSpotInstanceRequests()) { LOG.info("placed request: {}", spotRequest.getSpotInstanceRequestId()); } }
@Override public List<SpotInstanceRequest> call() { LaunchSpecification spec = new LaunchSpecification(); spec.withInstanceType(this.instanceTemplate.getInstanceType()); spec.withImageId(this.instanceTemplate.getAmiId()); InstanceNetworkInterfaceSpecification nic = new InstanceNetworkInterfaceSpecification(); nic.withDeviceIndex(0); // select a subnet at random nic.withSubnetId(randomSubnet()); nic.withAssociatePublicIpAddress(this.instanceTemplate.isAssignPublicIp()); nic.withGroups(this.instanceTemplate.getSecurityGroupIds()); spec.withNetworkInterfaces(nic); spec.withKeyName(this.instanceTemplate.getKeyPair()); spec.withIamInstanceProfile( new IamInstanceProfileSpecification().withArn(this.instanceTemplate.getIamInstanceProfileARN())); spec.withUserData(this.instanceTemplate.getEncodedUserData()); spec.withEbsOptimized(this.instanceTemplate.isEbsOptimized()); RequestSpotInstancesRequest spotRequest = new RequestSpotInstancesRequest().withInstanceCount(this.count) .withType(SpotInstanceType.Persistent).withSpotPrice(this.bidPrice).withLaunchSpecification(spec); RequestSpotInstancesResult result = getClient().getApi().requestSpotInstances(spotRequest); List<String> spotRequestIds = result.getSpotInstanceRequests().stream() .map(SpotInstanceRequest::getSpotInstanceRequestId).collect(Collectors.toList()); if (!this.instanceTemplate.getTags().isEmpty()) { tagRequests(spotRequestIds); } return awaitSpotRequests(spotRequestIds); }
private AmazonWebServiceRequest createRequest(Pool pool, DelegateExecution execution, boolean spot) throws IOException { final String businessKey = execution.getProcessBusinessKey(); final String securityGroupName = SecurityGroups.formatNameFromBusinessKey(businessKey); final String keyPairName = KeyPairs.formatNameFromBusinessKey(businessKey); final String instanceType = pool.getHardware().getType(); final String imageId = getImageIdFromPoolConfigurationOrQueryImageTable( pool, pool.getProvider(), instanceType); final String userData = Resources.toString(Resources.getResource(RunInstances.class, "/org/apache/provisionr/amazon/userdata.sh"), Charsets.UTF_8); List<BlockDevice> blockDevices = pool.getHardware().getBlockDevices(); List<BlockDeviceMapping> blockDeviceMappings = Lists.newArrayList(); if (blockDevices != null && blockDevices.size() > 0) { for (BlockDevice device : blockDevices) { blockDeviceMappings.add(new BlockDeviceMapping() .withDeviceName(device.getName()) .withEbs(new EbsBlockDevice() .withVolumeSize(device.getSize()) .withDeleteOnTermination(true) )); } } if (spot) { Calendar validUntil = Calendar.getInstance(); validUntil.add(Calendar.MINUTE, 10); final String spotPrice = checkNotNull(pool.getProvider().getOption(ProviderOptions.SPOT_BID), "The bid for spot instances was not specified"); LaunchSpecification ls = new LaunchSpecification() .withInstanceType(instanceType) .withKeyName(keyPairName) .withImageId(imageId) .withBlockDeviceMappings(blockDeviceMappings) .withSecurityGroups(Lists.newArrayList(securityGroupName)) .withUserData(Base64.encodeBytes(userData.getBytes(Charsets.UTF_8))); return new RequestSpotInstancesRequest() .withSpotPrice(spotPrice) .withLaunchSpecification(ls) .withLaunchGroup(businessKey) .withInstanceCount(pool.getExpectedSize()) .withType(SpotInstanceType.OneTime) .withValidUntil(validUntil.getTime()); } else { return new RunInstancesRequest() .withClientToken(businessKey) .withSecurityGroups(securityGroupName) .withKeyName(keyPairName) .withInstanceType(instanceType) .withImageId(imageId) .withBlockDeviceMappings(blockDeviceMappings) .withMinCount(pool.getMinSize()) .withMaxCount(pool.getExpectedSize()) .withUserData(Base64.encodeBytes(userData.getBytes(Charsets.UTF_8))); } }