protected AsgTagInstanceDiscovery(AmazonAutoScalingClient asgClient, AmazonEC2Client ec2Client) { Preconditions.checkNotNull(asgClient); Preconditions.checkNotNull(ec2Client); Preconditions.checkState(!Strings.isNullOrEmpty(CLUSTER_TAG_KEY.get()), TAG_PROPERTY_NAME + " must be supplied!"); this.asgClient = asgClient; this.ec2Client = ec2Client; String regionName = DynamicPropertyFactory.getInstance().getStringProperty("turbine.region", "").get(); if(Strings.isNullOrEmpty(regionName)) { Region currentRegion = Regions.getCurrentRegion(); if(currentRegion != null) { regionName = currentRegion.getName(); } else { regionName = "us-east-1"; } } Region region = Region.getRegion(Regions.fromName(regionName)); ec2Client.setRegion(region); asgClient.setRegion(region); log.debug("Set the region to [{}]", region); }
private List<CloudResource> getCloudResources(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormationClient client, AmazonEC2Client amazonEC2Client, AmazonAutoScalingClient amazonASClient, boolean mapPublicIpOnLaunch) { List<CloudResource> cloudResources = new ArrayList<>(); AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); scheduleStatusChecks(stack, ac, cloudFormationClient); suspendAutoScaling(ac, stack); if (mapPublicIpOnLaunch) { Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, client); List<Group> gateways = getGatewayGroups(stack.getGroups()); for (Group gateway : gateways) { List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway); List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, client, gateway); associateElasticIpsToInstances(amazonEC2Client, eips, instanceIds); } } return cloudResources; }
@Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms) { List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>(); try { String region = ac.getCloudContext().getLocation().getRegion().value(); AmazonCloudFormationClient amazonCFClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), region); AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), region); AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region); //contains all instances ListMultimap<String, CloudInstance> groupByInstanceGroup = groupByInstanceGroup(vms); for (String key : groupByInstanceGroup.keySet()) { List<CloudInstance> cloudInstances = groupByInstanceGroup.get(key); cloudVmMetaDataStatuses.addAll(collectGroupMetaData(ac, amazonASClient, amazonEC2Client, amazonCFClient, key, cloudInstances)); } return cloudVmMetaDataStatuses; } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } }
public AwsAutoScalingDeployUtils(String region, DeployConfiguration activeConfiguration, Log log) { this.activeConfiguration = activeConfiguration; this.log = log; Region awsRegion = Region.getRegion(Regions.fromName(region)); awsAsClient = new AmazonAutoScalingClient(); awsAsClient.setRegion(awsRegion); awsElbClient = new AmazonElasticLoadBalancingClient(); awsElbClient.setRegion(awsRegion); awsEc2Client = new AmazonEC2Client(); awsEc2Client.setRegion(awsRegion); activeConfiguration.withAutoScalingGroup(matchAutoScalingGroupName(activeConfiguration.getAutoScalingGroupId())); }
/** * Binds all the Amazon services used. */ @Override protected void configure() { final Region region = Region.getRegion(Regions.fromName(regionName)); bind(AmazonEC2.class).toInstance(createAmazonClientInstance(AmazonEC2Client.class, region)); bind(AmazonCloudFormation.class).toInstance(createAmazonClientInstance(AmazonCloudFormationClient.class, region)); bind(AmazonIdentityManagement.class).toInstance(createAmazonClientInstance(AmazonIdentityManagementClient.class, region)); bind(AWSKMS.class).toInstance(createAmazonClientInstance(AWSKMSClient.class, region)); bind(AmazonS3.class).toInstance(createAmazonClientInstance(AmazonS3Client.class, region)); bind(AmazonAutoScaling.class).toInstance(createAmazonClientInstance(AmazonAutoScalingClient.class, region)); bind(AWSSecurityTokenService.class).toInstance(createAmazonClientInstance(AWSSecurityTokenServiceClient.class, region)); bind(AWSLambda.class).toInstance(createAmazonClientInstance(AWSLambdaClient.class, region)); bind(AmazonSNS.class).toInstance(createAmazonClientInstance(AmazonSNSClient.class, region)); }
/*** * Initialize the AWS SDK Client * * @param awsClusterSecurityManager The {@link AWSClusterSecurityManager} to fetch AWS credentials * @param region The Amazon AWS {@link Region} */ public AWSSdkClient(final AWSClusterSecurityManager awsClusterSecurityManager, final Region region) { this.amazonEC2Supplier = Suppliers.memoize(new Supplier<AmazonEC2>() { @Override public AmazonEC2 get() { AmazonEC2Client amazonEC2 = new AmazonEC2Client(awsClusterSecurityManager.getCredentialsProvider()); amazonEC2.setRegion(region); return amazonEC2; } }); this.amazonS3Supplier = Suppliers.memoize(new Supplier<AmazonS3>() { @Override public AmazonS3 get() { AmazonS3Client amazonS3 = new AmazonS3Client(awsClusterSecurityManager.getCredentialsProvider()); amazonS3.setRegion(region); return amazonS3; } }); this.amazonAutoScalingSupplier = Suppliers.memoize(new Supplier<AmazonAutoScaling>() { @Override public AmazonAutoScaling get() { AmazonAutoScalingClient amazonAutoScaling = new AmazonAutoScalingClient(awsClusterSecurityManager.getCredentialsProvider()); amazonAutoScaling.setRegion(region); return amazonAutoScaling; } }); }
@Override public AmazonAutoScalingClient getClient() { AmazonAutoScalingClient client = new AmazonAutoScalingClient(new Aws().getAwsCredentials()); String region = getUserProperty(AWS_REGION); String endpoint = "https://autoscaling." + region + ".amazonaws.com"; //$NON-NLS-1$ //$NON-NLS-2$ client.setEndpoint(endpoint); return client; }
@Override public void createAmazonAutoScalingGroup(AWSCredentials credentials, String name, int maxsize, int minsize, int coolDown, String configName, Collection<String> loadBalancerNames, Collection<com.amazonaws.services.autoscaling.model.Tag> tags) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); CreateAutoScalingGroupRequest createAutoScalingGroupRequest = new CreateAutoScalingGroupRequest().withAutoScalingGroupName(name) .withDefaultCooldown(coolDown).withLaunchConfigurationName(configName).withMaxSize(maxsize).withMinSize(minsize).withTags(tags) .withLoadBalancerNames(loadBalancerNames).withAvailabilityZones(Arrays.asList("us-west-2b")); amazonAutoScalingClient.createAutoScalingGroup(createAutoScalingGroupRequest); }
@Override public void updateAmazonAutoScalingGroupCoolDown(AWSCredentials credentials, String name, int coolDown) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); UpdateAutoScalingGroupRequest updateAutoScalingGroupRequest = new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(name) .withDefaultCooldown(coolDown); amazonAutoScalingClient.updateAutoScalingGroup(updateAutoScalingGroupRequest); }
@Override public void updateAmazonAutoScalingGroupMaxSize(AWSCredentials credentials, String name, int maxSize) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); UpdateAutoScalingGroupRequest updateAutoScalingGroupRequest = new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(name) .withMaxSize(maxSize); amazonAutoScalingClient.updateAutoScalingGroup(updateAutoScalingGroupRequest); }
@Override public void updateAmazonAutoScalingGroupMinSize(AWSCredentials credentials, String name, int minSize) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); UpdateAutoScalingGroupRequest updateAutoScalingGroupRequest = new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(name) .withMinSize(minSize); amazonAutoScalingClient.updateAutoScalingGroup(updateAutoScalingGroupRequest); }
private void suspendAutoScaling(AuthenticatedContext ac, CloudStack stack) { AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); for (Group group : stack.getGroups()) { String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, group.getName(), ac.getCloudContext().getLocation().getRegion().value()); amazonASClient.suspendProcesses(new SuspendProcessesRequest().withAutoScalingGroupName(asGroupName).withScalingProcesses(SUSPENDED_PROCESSES)); } }
private void resumeAutoScaling(AuthenticatedContext ac, CloudStack stack) { AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); for (Group group : stack.getGroups()) { String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, group.getName(), ac.getCloudContext().getLocation().getRegion().value()); amazonASClient.resumeProcesses(new ResumeProcessesRequest().withAutoScalingGroupName(asGroupName).withScalingProcesses(SUSPENDED_PROCESSES)); } }
private void resumeAutoScalingPolicies(AuthenticatedContext ac, CloudStack stack) { for (Group instanceGroup : stack.getGroups()) { try { String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, instanceGroup.getName(), ac.getCloudContext().getLocation().getRegion().value()); if (asGroupName != null) { AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); List<AutoScalingGroup> asGroups = amazonASClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest() .withAutoScalingGroupNames(asGroupName)).getAutoScalingGroups(); if (!asGroups.isEmpty()) { if (!asGroups.get(0).getSuspendedProcesses().isEmpty()) { amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(asGroupName) .withMinSize(0) .withDesiredCapacity(0)); amazonASClient.resumeProcesses(new ResumeProcessesRequest().withAutoScalingGroupName(asGroupName)); } } } else { LOGGER.info("Autoscaling Group's physical id is null (the resource doesn't exist), it is not needed to resume scaling policies."); } } catch (AmazonServiceException e) { if (e.getErrorMessage().matches("Resource.*does not exist for stack.*") || e.getErrorMessage().matches("Stack '.*' does not exist.*")) { LOGGER.info(e.getMessage()); } else { throw e; } } } }
public List<String> getInstanceIds(AmazonAutoScalingClient amazonASClient, String asGroupName) { DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = amazonASClient .describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asGroupName)); List<String> instanceIds = new ArrayList<>(); if (describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances() != null) { for (Instance instance : describeAutoScalingGroupsResult.getAutoScalingGroups().get(0).getInstances()) { if ("InService".equals(instance.getLifecycleState())) { instanceIds.add(instance.getInstanceId()); } } } return instanceIds; }
public AmazonAutoScalingClient createAutoScalingClient(AwsCredentialView awsCredential, String regionName) { AmazonAutoScalingClient client = isRoleAssumeRequired(awsCredential) ? new AmazonAutoScalingClient(credentialClient.retrieveCachedSessionCredentials(awsCredential)) : new AmazonAutoScalingClient(createAwsCredentials(awsCredential)); client.setRegion(RegionUtils.getRegion(regionName)); return client; }
private List<CloudVmMetaDataStatus> collectGroupMetaData(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonEC2Client amazonEC2Client, AmazonCloudFormationClient amazonCFClient, String groupName, List<CloudInstance> cloudInstances) { List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>(); String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, groupName); List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName); DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds); DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest); //contains instances with instanceId Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(cloudInstances); //contains instances with privateId (without instanceId) Queue<CloudInstance> untrackedInstances = untrackedInstances(cloudInstances); for (Reservation reservation : instancesResult.getReservations()) { LOGGER.info("Number of instances found in reservation: {}", reservation.getInstances().size()); for (Instance instance : reservation.getInstances()) { String instanceId = instance.getInstanceId(); CloudInstance cloudInstance = ensureInstanceTag(mapByInstanceId, instance, instanceId, untrackedInstances, amazonEC2Client); if (cloudInstance != null) { CloudInstanceMetaData md = new CloudInstanceMetaData(instance.getPrivateIpAddress(), instance.getPublicIpAddress()); CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED); CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md); cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus); } } } return cloudVmMetaDataStatuses; }
private AmazonAutoScaling findClient(Account account, Region region) { // TODO: need to config client config parameter. ignore it for now. // TODO: need a cached version based on account and region as key AWSCredentials credential = new BasicAWSCredentials(account.getAccessKeyId(), account.getSecretAccessKey()); AmazonAutoScaling as = new AmazonAutoScalingClient(credential); // as.setEndpoint(region.toAsEndpoint()); return as; }
@Bean public AmazonAutoScaling autoScaling() { AmazonAutoScaling client = new AmazonAutoScalingClient(); client.setRegion(region); return client; }
public AsgTagInstanceDiscovery() { this(new AmazonAutoScalingClient(), new AmazonEC2Client()); }
public static void main( String[] args ) throws IOException { final String env = "prod"; AWSCredentials credentials = new PropertiesCredentials( ShutdownWorkers.class.getResourceAsStream( "/AwsCredentials.properties" ) ); try { // Scalo a zero int min = 0; int desired = 0; int max = 0; AmazonAutoScaling as = new AmazonAutoScalingClient(credentials); as.setEndpoint("autoscaling.eu-west-1.amazonaws.com"); DescribeAutoScalingGroupsResult dasgres = as.describeAutoScalingGroups(); List<AutoScalingGroup> lasg = dasgres.getAutoScalingGroups(); for (AutoScalingGroup asg : lasg) { if(asg.getAutoScalingGroupName().equals("asg-kopjra-prod-workers")){ desired = asg.getDesiredCapacity(); max = asg.getMaxSize(); min = asg.getMinSize(); break; } } UpdateAutoScalingGroupRequest uasgr = new UpdateAutoScalingGroupRequest(); uasgr.setAutoScalingGroupName("asg-kopjra-prod-workers"); uasgr.setDesiredCapacity(0); uasgr.setMaxSize(0); uasgr.setMinSize(0); as.updateAutoScalingGroup(uasgr); System.out.println("Done: workers shutting down!"); System.out.println("When you run the StartupWorkers, remember to use the following parameters: "+min+" "+max+" "+desired); } catch (AmazonServiceException ase) { System.err.println( "AmazonServiceException" ); } catch (AmazonClientException ace) { System.err.println( "AmazonClientException" ); } catch (Exception e){ System.err.println( "OtherException" ); } }
protected AmazonAutoScaling getAutoScalingClient() { AmazonAutoScaling client = new AmazonAutoScalingClient(provider.getAwsCredentialProvider()); client.setEndpoint("autoscaling." + config.getDC() + ".amazonaws.com"); return client; }
public AmazonAutoScalingClient createAmazonAutoScalingClient(AWSCredentials crendentials) { AmazonAutoScalingClient amazonEC2Client = new AmazonAutoScalingClient(new SimpleAWSCredentialsProvider(crendentials)); amazonEC2Client.setRegion(region); return amazonEC2Client; }
@Override public List<LaunchConfiguration> describeAmazonLaunchConfigurations(AWSCredentials credentials) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); DescribeLaunchConfigurationsResult describeLaunchConfigurationsResult = amazonAutoScalingClient.describeLaunchConfigurations(); return describeLaunchConfigurationsResult.getLaunchConfigurations(); }
@Override public List<AutoScalingGroup> describeAmazonAutoScalingGroups(AWSCredentials credentials) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = amazonAutoScalingClient.describeAutoScalingGroups(); return describeAutoScalingGroupsResult.getAutoScalingGroups(); }
@Override public List<AutoScalingInstanceDetails> describeAmazonAutoScalingInstances(AWSCredentials credentials) { AmazonAutoScalingClient amazonAutoScalingClient = amazonAutoScalingClientFactory.createAmazonAutoScalingClient(credentials); DescribeAutoScalingInstancesResult describeAutoScalingInstancesResult = amazonAutoScalingClient.describeAutoScalingInstances(); return describeAutoScalingInstancesResult.getAutoScalingInstances(); }
@Override public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception { createKeyPair(ac, stack); String cFStackName = cfStackUtil.getCfStackName(ac); AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential()); String regionName = ac.getCloudContext().getLocation().getRegion().value(); AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName); AmazonEC2Client amazonEC2Client = awsClient.createAccess(credentialView, regionName); AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork()); boolean existingVPC = awsNetworkView.isExistingVPC(); boolean existingSubnet = awsNetworkView.isExistingSubnet(); boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(awsNetworkView, amazonEC2Client); try { cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName)); LOGGER.info("Stack already exists: {}", cFStackName); } catch (AmazonServiceException ignored) { CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).build(); resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext()); String cidr = stack.getNetwork().getSubnet().getCidr(); String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? findNonOverLappingCIDR(ac, stack) : cidr; AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack); ModelContext modelContext = new ModelContext() .withAuthenticatedContext(ac) .withStack(stack) .withExistingVpc(existingVPC) .withSnapshotId(getEbsSnapshotIdIfNeeded(ac, stack)) .withExistingIGW(awsNetworkView.isExistingIGW()) .withExistingSubnetCidr(existingSubnet ? getExistingSubnetCidr(ac, stack) : null) .withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null) .mapPublicIpOnLaunch(mapPublicIpOnLaunch) .withEnableInstanceProfile(awsInstanceProfileView.isEnableInstanceProfileStrategy()) .withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()) .withTemplate(stack.getTemplate()) .withDefaultSubnet(subnet); String cfTemplate = cloudFormationTemplateBuilder.build(modelContext); LOGGER.debug("CloudFormationTemplate: {}", cfTemplate); cfClient.createStack(createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate)); } LOGGER.info("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId()); AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(credentialView, regionName); PollTask<Boolean> task = awsPollTaskFactory.newAwsCreateStackStatusCheckerTask(ac, cfClient, asClient, CREATE_COMPLETE, CREATE_FAILED, ERROR_STATUSES, cFStackName); try { Boolean statePollerResult = task.call(); if (!task.completed(statePollerResult)) { syncPollingScheduler.schedule(task); } } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName); saveS3AccessRoleArn(ac, stack, cFStackName, cfClient, resourceNotifier); saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier); List<CloudResource> cloudResources = getCloudResources(ac, stack, cFStackName, cfClient, amazonEC2Client, amazonASClient, mapPublicIpOnLaunch); return check(ac, cloudResources); }
@Override public List<CloudResourceStatus> upscale(AuthenticatedContext ac, CloudStack stack, List<CloudResource> resources) { resumeAutoScaling(ac, stack); AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()); List<Group> scaledGroups = getScaledGroups(stack); for (Group group : scaledGroups) { String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName()); amazonASClient.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(asGroupName) .withMaxSize(group.getInstancesSize()) .withDesiredCapacity(group.getInstancesSize())); LOGGER.info("Updated Auto Scaling group's desiredCapacity: [stack: '{}', to: '{}']", ac.getCloudContext().getId(), resources.size()); } scheduleStatusChecks(stack, ac, cloudFormationClient); suspendAutoScaling(ac, stack); boolean mapPublicIpOnLaunch = isMapPublicOnLaunch(new AwsNetworkView(stack.getNetwork()), amazonEC2Client); List<Group> gateways = getGatewayGroups(scaledGroups); if (mapPublicIpOnLaunch && !gateways.isEmpty()) { String cFStackName = getCloudFormationStackResource(resources).getName(); Map<String, String> eipAllocationIds = getElasticIpAllocationIds(cFStackName, cloudFormationClient); for (Group gateway : gateways) { List<String> eips = getEipsForGatewayGroup(eipAllocationIds, gateway); List<String> freeEips = getFreeIps(eips, amazonEC2Client); List<String> instanceIds = getInstancesForGroup(ac, amazonASClient, cloudFormationClient, gateway); List<String> newInstances = instanceIds.stream().filter( iid -> gateway.getInstances().stream().noneMatch(inst -> iid.equals(inst.getInstanceId()))).collect(Collectors.toList()); associateElasticIpsToInstances(amazonEC2Client, freeEips, newInstances); } } return singletonList(new CloudResourceStatus(getCloudFormationStackResource(resources), ResourceStatus.UPDATED)); }
private List<String> getInstancesForGroup(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonCloudFormationClient client, Group group) { return cfStackUtil.getInstanceIds(amazonASClient, cfStackUtil.getAutoscalingGroupName(ac, client, group.getName())); }
public PollTask<Boolean> newAwsCreateStackStatusCheckerTask(AuthenticatedContext authenticatedContext, AmazonCloudFormationClient cfClient, AmazonAutoScalingClient asClient, StackStatus successStatus, StackStatus errorStatus, List<StackStatus> stackErrorStatuses, String cloudFormationStackName) { return createPollTask(AwsCreateStackStatusCheckerTask.NAME, authenticatedContext, cfClient, asClient, successStatus, errorStatus, stackErrorStatuses, cloudFormationStackName); }
public AwsCreateStackStatusCheckerTask(AuthenticatedContext authenticatedContext, AmazonCloudFormationClient cfClient, AmazonAutoScalingClient asClient, StackStatus successStatus, StackStatus errorStatus, List<StackStatus> stackErrorStatuses, String cloudFormationStackName) { super(authenticatedContext, cfClient, successStatus, errorStatus, stackErrorStatuses, cloudFormationStackName, true); }
@Test public void collectMigratedExistingOneGroup() { List<CloudInstance> vms = new ArrayList<>(); List<Volume> volumes = new ArrayList<>(); InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak"); vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication)); when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient); when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client); when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))) .thenReturn("cbgateway-AAA"); List<String> gatewayIds = Collections.singletonList("i-1"); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))) .thenReturn(gatewayIds); when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw); when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw); Instance instance = Mockito.mock(Instance.class); when(instance.getInstanceId()).thenReturn("i-1"); when(instance.getPrivateIpAddress()).thenReturn("privateIp"); when(instance.getPublicIpAddress()).thenReturn("publicIp"); List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance)); when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations); AuthenticatedContext ac = authenticatedContext(); List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms); verify(amazonEC2Client).createTags(any(CreateTagsRequest.class)); Assert.assertEquals(1, statuses.size()); Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId()); Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp()); Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp()); }
@Test public void collectAlreadyTaggedOneGroup() { List<CloudInstance> vms = new ArrayList<>(); List<Volume> volumes = new ArrayList<>(); InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak"); vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication)); when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient); when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client); when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))) .thenReturn("cbgateway-AAA"); List<String> gatewayIds = Collections.singletonList("i-1"); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))) .thenReturn(gatewayIds); when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw); when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw); Instance instance = Mockito.mock(Instance.class); when(instance.getInstanceId()).thenReturn("i-1"); when(instance.getPrivateIpAddress()).thenReturn("privateIp"); when(instance.getPublicIpAddress()).thenReturn("publicIp"); Tag tag = new Tag(); tag.setKey("cbname"); tag.setValue("somevalue"); when(instance.getTags()).thenReturn(Collections.singletonList(tag)); List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance)); when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations); AuthenticatedContext ac = authenticatedContext(); List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms); verify(amazonEC2Client, never()).createTags(any(CreateTagsRequest.class)); Assert.assertEquals(1, statuses.size()); Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId()); Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp()); Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp()); }
@Test public void collectNewOneGroup() { List<CloudInstance> vms = new ArrayList<>(); List<Volume> volumes = new ArrayList<>(); InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak"); vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication)); when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient); when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client); when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))) .thenReturn("cbgateway-AAA"); List<String> gatewayIds = Collections.singletonList("i-1"); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))) .thenReturn(gatewayIds); when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw); when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw); Instance instance = Mockito.mock(Instance.class); when(instance.getInstanceId()).thenReturn("i-1"); when(instance.getPrivateIpAddress()).thenReturn("privateIp"); when(instance.getPublicIpAddress()).thenReturn("publicIp"); List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance)); when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations); AuthenticatedContext ac = authenticatedContext(); List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms); verify(amazonEC2Client).createTags(any(CreateTagsRequest.class)); Assert.assertEquals(1, statuses.size()); Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId()); Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp()); Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp()); }
@Test public void collectNewAndExistingOneGroup() { List<CloudInstance> vms = new ArrayList<>(); List<Volume> volumes = new ArrayList<>(); InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak"); vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication)); vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication)); when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient); when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client); when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))) .thenReturn("cbgateway-AAA"); List<String> gatewayIds = Arrays.asList("i-1", "i-2"); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))) .thenReturn(gatewayIds); when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw); when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw); Instance instance1 = Mockito.mock(Instance.class); when(instance1.getInstanceId()).thenReturn("i-1"); when(instance1.getPrivateIpAddress()).thenReturn("privateIp1"); when(instance1.getPublicIpAddress()).thenReturn("publicIp1"); Instance instance2 = Mockito.mock(Instance.class); when(instance2.getInstanceId()).thenReturn("i-2"); when(instance2.getPrivateIpAddress()).thenReturn("privateIp2"); when(instance2.getPublicIpAddress()).thenReturn("publicIp2"); List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2)); when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations); AuthenticatedContext ac = authenticatedContext(); List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms); verify(amazonEC2Client, times(2)).createTags(any(CreateTagsRequest.class)); Assert.assertEquals(2, statuses.size()); Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId()); Assert.assertEquals("privateIp1", statuses.get(0).getMetaData().getPrivateIp()); Assert.assertEquals("publicIp1", statuses.get(0).getMetaData().getPublicIp()); Assert.assertEquals("i-2", statuses.get(1).getCloudVmInstanceStatus().getCloudInstance().getInstanceId()); Assert.assertEquals("privateIp2", statuses.get(1).getMetaData().getPrivateIp()); Assert.assertEquals("publicIp2", statuses.get(1).getMetaData().getPublicIp()); }
@Test public void collectNewOldIsTagged() { List<CloudInstance> vms = new ArrayList<>(); List<Volume> volumes = new ArrayList<>(); InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak"); vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication)); when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient); when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client); when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))) .thenReturn("cbgateway-AAA"); List<String> gatewayIds = Arrays.asList("i-1", "i-new"); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))) .thenReturn(gatewayIds); when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw); when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw); Instance instance1 = Mockito.mock(Instance.class); when(instance1.getInstanceId()).thenReturn("i-1"); when(instance1.getPrivateIpAddress()).thenReturn("privateIp1"); when(instance1.getPublicIpAddress()).thenReturn("publicIp1"); Tag tag = new Tag(); tag.setKey("cbname"); tag.setValue("somevalue"); when(instance1.getTags()).thenReturn(Collections.singletonList(tag)); Instance instance2 = Mockito.mock(Instance.class); when(instance2.getInstanceId()).thenReturn("i-new"); when(instance2.getPrivateIpAddress()).thenReturn("privateIp2"); when(instance2.getPublicIpAddress()).thenReturn("publicIp2"); List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2)); when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations); AuthenticatedContext ac = authenticatedContext(); List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms); verify(amazonEC2Client, times(1)).createTags(any(CreateTagsRequest.class)); Assert.assertEquals(1, statuses.size()); Assert.assertEquals("i-new", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId()); Assert.assertEquals("privateIp2", statuses.get(0).getMetaData().getPrivateIp()); Assert.assertEquals("publicIp2", statuses.get(0).getMetaData().getPublicIp()); }
@Test public void successTest() throws Exception { int requiredInstances = 160; AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class); CloudContext cloudContext = mock(CloudContext.class); String regionName = "eu-west-1"; when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName))); when(authenticatedContext.getCloudContext()).thenReturn(cloudContext); CloudCredential cloudCredential = mock(CloudCredential.class); when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential); String asGroupName = "as-group"; AwsClient awsClient = mock(AwsClient.class); AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class); when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client); DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult(); List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 100); firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses); DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult(); List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160); secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses); when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))) .thenReturn(firstDescribeInstanceStatusResult) .thenReturn(secondDescribeInstanceStatusResult); CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class); List<String> instancIds = new ArrayList<>(); for (int i = 0; i < requiredInstances; i++) { instancIds.add(Integer.toString(i)); } AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient); when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult()); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds); ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil); Boolean taskResult = asGroupStatusCheckerTask.call(); ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class); verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture()); List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues(); assertEquals(100, allValues.get(0).getInstanceIds().size()); assertEquals(60, allValues.get(1).getInstanceIds().size()); assertTrue(taskResult); }
@Test public void failTest() throws Exception { int requiredInstances = 160; AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class); CloudContext cloudContext = mock(CloudContext.class); String regionName = "eu-west-1"; when(cloudContext.getLocation()).thenReturn(Location.location(Region.region(regionName))); when(authenticatedContext.getCloudContext()).thenReturn(cloudContext); CloudCredential cloudCredential = mock(CloudCredential.class); when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential); String asGroupName = "as-group"; AwsClient awsClient = mock(AwsClient.class); AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class); when(awsClient.createAccess(any(AwsCredentialView.class), eq(regionName))).thenReturn(amazonEC2Client); DescribeInstanceStatusResult firstDescribeInstanceStatusResult = new DescribeInstanceStatusResult(); List<InstanceStatus> firstInstanceStatuses = returnInstanceStatus(0, 99); firstDescribeInstanceStatusResult.setInstanceStatuses(firstInstanceStatuses); DescribeInstanceStatusResult secondDescribeInstanceStatusResult = new DescribeInstanceStatusResult(); List<InstanceStatus> secondInstanceStatuses = returnInstanceStatus(100, 160); secondDescribeInstanceStatusResult.setInstanceStatuses(secondInstanceStatuses); when(amazonEC2Client.describeInstanceStatus(any(DescribeInstanceStatusRequest.class))) .thenReturn(firstDescribeInstanceStatusResult) .thenReturn(secondDescribeInstanceStatusResult); CloudFormationStackUtil cloudFormationStackUtil = mock(CloudFormationStackUtil.class); List<String> instancIds = new ArrayList<>(); for (int i = 0; i < requiredInstances; i++) { instancIds.add(Integer.toString(i)); } AmazonAutoScalingClient autoScalingClient = mock(AmazonAutoScalingClient.class); when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(autoScalingClient); when(autoScalingClient.describeScalingActivities(any(DescribeScalingActivitiesRequest.class))).thenReturn(new DescribeScalingActivitiesResult()); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq(asGroupName))).thenReturn(instancIds); ASGroupStatusCheckerTask asGroupStatusCheckerTask = new ASGroupStatusCheckerTask(authenticatedContext, asGroupName, requiredInstances, awsClient, cloudFormationStackUtil); Boolean taskResult = asGroupStatusCheckerTask.call(); ArgumentCaptor<DescribeInstanceStatusRequest> instanceStatusRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeInstanceStatusRequest.class); verify(amazonEC2Client, times(2)).describeInstanceStatus(instanceStatusRequestArgumentCaptor.capture()); List<DescribeInstanceStatusRequest> allValues = instanceStatusRequestArgumentCaptor.getAllValues(); assertEquals(100, allValues.get(0).getInstanceIds().size()); assertEquals(60, allValues.get(1).getInstanceIds().size()); assertFalse(taskResult); }