com.amazonaws.services.autoscaling.model.AutoScalingGroup Java Examples

The following examples show how to use com.amazonaws.services.autoscaling.model.AutoScalingGroup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileManager.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Generate asg files.
 *
 * @param instanceMap the instance map
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void generateAsgFiles(Map<String,List<AutoScalingGroup>> instanceMap) throws IOException {

	String fieldNames;
	String keys;

	fieldNames ="AutoScalingGroupARN`AutoScalingGroupName`AvailabilityZones`CreatedTime`DefaultCooldown`DesiredCapacity`HealthCheckGracePeriod`HealthCheckType`LaunchConfigurationName`MaxSize`MinSize`"
			+ "NewInstancesProtectedFromScaleIn`PlacementGroup`Status`SuspendedProcesses`TargetGroupARNs`TerminationPolicies`VPCZoneIdentifier";
	keys ="discoverydate`accountid`accountname`region`autoscalinggrouparn`autoscalinggroupname`availabilityzones`createdtime`defaultcooldown`desiredcapacity`healthcheckgraceperiod`healthchecktype`"
			+ "launchconfigurationname`maxsize`minsize`newinstancesprotectedfromscalein`placementgroup`status`suspendedprocesses`targetgrouparns`terminationpolicies`vpczoneidentifier";
	FileGenerator.generateJson(instanceMap, fieldNames, "aws-asg.data",keys);

	fieldNames ="AutoScalingGroupARN`instances.instanceid";
	keys ="discoverydate`accountid`accountname`region`autoscalinggrouparn`instancesinstanceid";
	FileGenerator.generateJson(instanceMap, fieldNames, "aws-asg-instances.data",keys);

	fieldNames ="AutoScalingGroupARN`LoadBalancerNames";
	keys ="discoverydate`accountid`accountname`region`autoscalinggrouparn`loadbalancernames";
	FileGenerator.generateJson(instanceMap, fieldNames, "aws-asg-elb.data",keys);

	fieldNames ="AutoScalingGroupARN`tags.key`tags.value";
	keys ="discoverydate`accountid`accountname`region`autoscalinggrouparn`key`value";
	FileGenerator.generateJson(instanceMap, fieldNames, "aws-asg-tags.data",keys);

}
 
Example #2
Source File: AWSSdkClient.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/***
 * Get list of {@link AutoScalingGroup}s for a given tag
 *
 * @param tag Tag to filter the auto scaling groups
 * @return List of {@link AutoScalingGroup}s qualifying the filter tag
 */
public List<AutoScalingGroup> getAutoScalingGroupsWithTag(Tag tag) {

  final AmazonAutoScaling autoScaling = getAmazonAutoScalingClient();

  final DescribeAutoScalingGroupsRequest describeAutoScalingGroupsRequest = new DescribeAutoScalingGroupsRequest();

  final List<AutoScalingGroup> allAutoScalingGroups = autoScaling
      .describeAutoScalingGroups(describeAutoScalingGroupsRequest)
      .getAutoScalingGroups();

  final List<AutoScalingGroup> filteredAutoScalingGroups = Lists.newArrayList();
  for (AutoScalingGroup autoScalingGroup : allAutoScalingGroups) {
    for (TagDescription tagDescription : autoScalingGroup.getTags()) {
      if (tagDescription.getKey().equalsIgnoreCase(tag.getKey()) &&
          tagDescription.getValue().equalsIgnoreCase(tag.getValue())) {
        filteredAutoScalingGroups.add(autoScalingGroup);
      }
    }
  }

  return filteredAutoScalingGroups;
}
 
Example #3
Source File: DeployStateStrategyFactory.java    From vertx-deploy-tools with Apache License 2.0 6 votes vote down vote up
public static boolean isDeployable(DeployConfiguration activeConfiguration, AutoScalingGroup autoScalingGroup, List<Ec2Instance> instances) {
    boolean canDeploy = false;
    switch (activeConfiguration.getDeployStrategy()) {
        case KEEP_CAPACITY:
            canDeploy = new KeepCapacityStrategy().isDeployable(activeConfiguration, autoScalingGroup, instances);
            break;
        case DEFAULT:
            canDeploy = new DefaultDeployStrategy().isDeployable(activeConfiguration, autoScalingGroup, instances);
            break;
        case GUARANTEE_MINIMUM:
            canDeploy = new GuaranteeMinimumStrategy().isDeployable(activeConfiguration, autoScalingGroup, instances);
            break;
        case WHATEVER:
            canDeploy = new WhateverStrategy().isDeployable(activeConfiguration, autoScalingGroup, instances);
            break;
        case SPIN_AND_REMOVE:
            canDeploy = true;
            break;
        default:

    }
    return canDeploy;
}
 
Example #4
Source File: AwsInstanceCloudConnector.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
private InstanceGroup toInstanceGroup(AutoScalingGroup awsScalingGroup) {
    boolean isLaunchSuspended = false;
    boolean isTerminateSuspended = false;
    for (SuspendedProcess suspendedProcess : awsScalingGroup.getSuspendedProcesses()) {
        if (suspendedProcess.getProcessName().equals("Launch")) {
            isLaunchSuspended = true;
        } else if (suspendedProcess.getProcessName().equals("Terminate")) {
            isTerminateSuspended = true;
        }
    }
    Map<String, String> attributes = CollectionsExt.isNullOrEmpty(awsScalingGroup.getTags())
            ? Collections.emptyMap()
            : awsScalingGroup.getTags().stream().collect(Collectors.toMap(TagDescription::getKey, TagDescription::getValue));
    return new InstanceGroup(
            awsScalingGroup.getAutoScalingGroupName(),
            awsScalingGroup.getLaunchConfigurationName(),
            DEFAULT_INSTANCE_TYPE,
            awsScalingGroup.getMinSize(),
            awsScalingGroup.getDesiredCapacity(),
            awsScalingGroup.getMaxSize(),
            isLaunchSuspended,
            isTerminateSuspended,
            attributes,
            awsScalingGroup.getInstances().stream().map(com.amazonaws.services.autoscaling.model.Instance::getInstanceId).collect(Collectors.toList())
    );
}
 
Example #5
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 6 votes vote down vote up
/**
 * Fetch launch configurations test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchLaunchConfigurationsTest() throws Exception {
    
    mockStatic(AmazonAutoScalingClientBuilder.class);
    AmazonAutoScaling asgClient = PowerMockito.mock(AmazonAutoScaling.class);
    AmazonAutoScalingClientBuilder amazonAutoScalingClientBuilder = PowerMockito.mock(AmazonAutoScalingClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonAutoScalingClientBuilder.standard()).thenReturn(amazonAutoScalingClientBuilder);
    when(amazonAutoScalingClientBuilder.withCredentials(anyObject())).thenReturn(amazonAutoScalingClientBuilder);
    when(amazonAutoScalingClientBuilder.withRegion(anyString())).thenReturn(amazonAutoScalingClientBuilder);
    when(amazonAutoScalingClientBuilder.build()).thenReturn(asgClient);
    
    DescribeAutoScalingGroupsResult autoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    List<AutoScalingGroup> asgList = new ArrayList<>();
    asgList.add(new AutoScalingGroup());
    autoScalingGroupsResult.setAutoScalingGroups(asgList);
    when(asgClient.describeAutoScalingGroups(anyObject())).thenReturn(autoScalingGroupsResult);
    assertThat(inventoryUtil.fetchAsg(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
    
}
 
Example #6
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch asg.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @param accountName the account name
 * @return the map
 */
public static Map<String,List<AutoScalingGroup>> fetchAsg(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){

	AmazonAutoScaling asgClient;
	Map<String,List<AutoScalingGroup>> asgList = new LinkedHashMap<>();

	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"ASG\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				List<AutoScalingGroup> asgListTemp = new ArrayList<>();
				asgClient = AmazonAutoScalingClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				String nextToken = null;
				DescribeAutoScalingGroupsResult  describeResult ;
				do{
					describeResult =  asgClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withNextToken(nextToken).withMaxRecords(asgMaxRecord));
					asgListTemp.addAll(describeResult.getAutoScalingGroups());
					nextToken = describeResult.getNextToken();
				}while(nextToken!=null);

				if(!asgListTemp.isEmpty() ){
					log.debug(InventoryConstants.ACCOUNT + accountId + " Type : ASG "+region.getName()+" >> " + asgListTemp.size());
					asgList.put(accountId+delimiter+accountName+delimiter+region.getName(), asgListTemp);
				}
		   	}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"asg",e.getMessage());
		}
	}
	return asgList;
}
 
Example #7
Source File: AwsUpscaleService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Map<String, Integer> getAutoScalingGroupsBySize(Set<String> autoScalingGroupNames, AmazonAutoScalingRetryClient amazonASClient) {
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(new ArrayList<>(autoScalingGroupNames));
    return amazonASClient.describeAutoScalingGroups(request).getAutoScalingGroups().stream()
            .collect(Collectors.toMap(AutoScalingGroup::getAutoScalingGroupName,
                    autoScalingGroup -> autoScalingGroup.getInstances().size()));
}
 
Example #8
Source File: AwsTerminateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
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) {
                AmazonAutoScalingRetryClient amazonASClient = awsClient.createAutoScalingRetryClient(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.debug("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.debug(e.getMessage());
            } else {
                throw e;
            }
        }
    }
}
 
Example #9
Source File: LaunchConfigurationHandlerTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void getLaunchConfigurations() {
    when(autoScalingClient.describeLaunchConfigurations(any(DescribeLaunchConfigurationsRequest.class)))
            .thenReturn(new DescribeLaunchConfigurationsResult());

    Collection<AutoScalingGroup> autoScalingGroups = Lists.newArrayList(new AutoScalingGroup().withLaunchConfigurationName("a"),
            new AutoScalingGroup().withLaunchConfigurationName("b"));
    underTest.getLaunchConfigurations(autoScalingClient, autoScalingGroups);
    ArgumentCaptor<DescribeLaunchConfigurationsRequest> captor = ArgumentCaptor.forClass(DescribeLaunchConfigurationsRequest.class);
    verify(autoScalingClient, times(1)).describeLaunchConfigurations(captor.capture());
    assertEquals(autoScalingGroups.size(), captor.getValue().getLaunchConfigurationNames().size());
}
 
Example #10
Source File: AwsLaunchConfigurationImageUpdateServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUpdateImage() {
    String lcName = "lcName";
    CloudResource cfResource = CloudResource.builder()
            .type(ResourceType.CLOUDFORMATION_STACK)
            .name("cf")
            .build();
    String autoScalingGroupName = "autoScalingGroupName";
    Map<AutoScalingGroup, String> scalingGroupStringMap =
            Collections.singletonMap(new AutoScalingGroup().withLaunchConfigurationName(lcName)
                    .withAutoScalingGroupName(autoScalingGroupName), autoScalingGroupName);
    when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody("AWS::AutoScaling::LaunchConfiguration"));
    when(autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource))
            .thenReturn(scalingGroupStringMap);
    List<LaunchConfiguration> oldLaunchConfigs = Collections.singletonList(new LaunchConfiguration().withLaunchConfigurationName(lcName));
    when(launchConfigurationHandler.getLaunchConfigurations(autoScalingClient, scalingGroupStringMap.keySet()))
            .thenReturn(oldLaunchConfigs);
    String newLCName = "newLCName";
    when(launchConfigurationHandler.createNewLaunchConfiguration(eq("imageName"), eq(autoScalingClient), eq(oldLaunchConfigs.get(0)),
            eq(ac.getCloudContext()), eq(null))).thenReturn(newLCName);

    underTest.updateImage(ac, stack, cfResource);

    verify(autoScalingGroupHandler, times(1)).getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
    verify(launchConfigurationHandler, times(1)).getLaunchConfigurations(autoScalingClient, scalingGroupStringMap.keySet());
    verify(launchConfigurationHandler, times(1)).createNewLaunchConfiguration(anyString(), eq(autoScalingClient),
            eq(oldLaunchConfigs.get(0)), eq(ac.getCloudContext()), eq(null));
    verify(autoScalingGroupHandler, times(1)).updateAutoScalingGroupWithLaunchConfiguration(autoScalingClient,
            autoScalingGroupName, oldLaunchConfigs.get(0), newLCName);
    verify(launchConfigurationHandler, times(1)).removeOldLaunchConfiguration(oldLaunchConfigs.get(0), autoScalingClient,
            ac.getCloudContext());
}
 
Example #11
Source File: AutoScalingGroupHandlerTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAutoScalingGroups() {
    CloudResource cfResource = CloudResource.builder()
            .type(ResourceType.CLOUDFORMATION_STACK)
            .name("cf")
            .build();
    DescribeStackResourcesResult resourcesResult = new DescribeStackResourcesResult();
    StackResource stackResource = new StackResource()
            .withLogicalResourceId("logicalResourceId")
            .withPhysicalResourceId("physicalResourceId")
            .withResourceType("AWS::AutoScaling::AutoScalingGroup");
    resourcesResult.getStackResources().add(stackResource);
    resourcesResult.getStackResources().add(new StackResource().withResourceType("other"));
    when(cloudFormationClient.describeStackResources(any(DescribeStackResourcesRequest.class))).thenReturn(resourcesResult);

    DescribeAutoScalingGroupsResult scalingGroupsResult = new DescribeAutoScalingGroupsResult();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup().withAutoScalingGroupName(stackResource.getPhysicalResourceId());
    scalingGroupsResult.getAutoScalingGroups().add(autoScalingGroup);
    when(autoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(scalingGroupsResult);

    Map<AutoScalingGroup, String> autoScalingGroups = underTest.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
    assertEquals(1, autoScalingGroups.size());
    assertEquals(autoScalingGroup, autoScalingGroups.entrySet().stream().findFirst().get().getKey());
    assertEquals(stackResource.getLogicalResourceId(), autoScalingGroups.entrySet().stream().findFirst().get().getValue());

    ArgumentCaptor<DescribeStackResourcesRequest> stackResourcesRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeStackResourcesRequest.class);
    verify(cloudFormationClient).describeStackResources(stackResourcesRequestArgumentCaptor.capture());
    assertEquals(cfResource.getName(), stackResourcesRequestArgumentCaptor.getValue().getStackName());

    ArgumentCaptor<DescribeAutoScalingGroupsRequest> scalingGroupsRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
    verify(autoScalingClient).describeAutoScalingGroups(scalingGroupsRequestArgumentCaptor.capture());
    assertEquals(1, scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().size());
    assertEquals(stackResource.getPhysicalResourceId(), scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().get(0));
}
 
Example #12
Source File: AwsLaunchTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void setupAutoscalingResponses() {
    DescribeScalingActivitiesResult describeScalingActivitiesResult = new DescribeScalingActivitiesResult();
    when(amazonAutoScalingRetryClient.describeScalingActivities(any())).thenReturn(describeScalingActivitiesResult);

    DescribeAutoScalingGroupsResult describeAutoScalingGroupsResult = new DescribeAutoScalingGroupsResult()
            .withAutoScalingGroups(
                    new AutoScalingGroup()
                            .withInstances(new Instance().withLifecycleState(LifecycleState.InService).withInstanceId(INSTANCE_ID))
                            .withAutoScalingGroupName(AUTOSCALING_GROUP_NAME)
            );
    when(amazonAutoScalingRetryClient.describeAutoScalingGroups(any())).thenReturn(describeAutoScalingGroupsResult);
}
 
Example #13
Source File: AwsRepairTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void upscaleStack() throws Exception {
    AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
    CloudStack stack = componentTestUtil.getStack(InstanceStatus.CREATE_REQUESTED, InstanceStatus.STARTED);
    List<CloudResource> cloudResources = List.of(
            CloudResource.builder()
                    .name(AWS_SUBNET_ID)
                    .type(ResourceType.AWS_SUBNET)
                    .build(),
            createVolumeResource(VOLUME_ID_1, INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1, CommonStatus.DETACHED),
            createVolumeResource(VOLUME_ID_2, INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2, CommonStatus.DETACHED),
            createVolumeResource(VOLUME_ID_3, INSTANCE_ID_3, SIZE_DISK_2, FSTAB_2, CommonStatus.CREATED));

    InMemoryStateStore.putStack(1L, PollGroup.POLLABLE);

    when(amazonCloudFormationRetryClient.describeStackResource(any()))
            .thenReturn(new DescribeStackResourceResult()
                    .withStackResourceDetail(new StackResourceDetail().withPhysicalResourceId(AUTOSCALING_GROUP_NAME)));

    when(amazonAutoScalingRetryClient.describeAutoScalingGroups(any()))
            .thenReturn(new DescribeAutoScalingGroupsResult()
                    .withAutoScalingGroups(new AutoScalingGroup()
                            .withAutoScalingGroupName(AUTOSCALING_GROUP_NAME)
                            .withInstances(List.of(
                                    new Instance().withInstanceId(INSTANCE_ID_1).withLifecycleState(LifecycleState.InService),
                                    new Instance().withInstanceId(INSTANCE_ID_2).withLifecycleState(LifecycleState.InService)))
                    ));

    when(amazonEC2Client.describeVolumes(any()))
            .thenReturn(new DescribeVolumesResult().withVolumes(
                    new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_1).withState(VolumeState.Available),
                    new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_2).withState(VolumeState.Available),
                    new com.amazonaws.services.ec2.model.Volume().withVolumeId(VOLUME_ID_3).withState(VolumeState.InUse)
            ));

    when(amazonEC2Client.describeInstances(any())).thenReturn(
            new DescribeInstancesResult().withReservations(
                    new Reservation().withInstances(new com.amazonaws.services.ec2.model.Instance().withInstanceId("i-instance")))
    );


    AmazonEC2Waiters waiters = mock(AmazonEC2Waiters.class);
    when(amazonEC2Client.waiters()).thenReturn(waiters);
    Waiter<DescribeInstancesRequest> instanceWaiter = mock(Waiter.class);
    when(waiters.instanceRunning()).thenReturn(instanceWaiter);

    when(amazonAutoScalingClient.waiters()).thenReturn(asWaiters);
    when(asWaiters.groupInService()).thenReturn(describeAutoScalingGroupsRequestWaiter);

    underTest.upscale(authenticatedContext, stack, cloudResources);

    verify(amazonAutoScalingRetryClient).resumeProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName())
            && argument.getScalingProcesses().contains("Launch")));
    verify(amazonAutoScalingRetryClient).updateAutoScalingGroup(argThat(argument -> {
        Group workerGroup = stack.getGroups().get(1);
        return AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName())
                && workerGroup.getInstancesSize().equals(argument.getMaxSize())
                && workerGroup.getInstancesSize().equals(argument.getDesiredCapacity());
    }));

    verify(amazonAutoScalingRetryClient, times(stack.getGroups().size()))
            .suspendProcesses(argThat(argument -> AUTOSCALING_GROUP_NAME.equals(argument.getAutoScalingGroupName())
                    && SUSPENDED_PROCESSES.equals(argument.getScalingProcesses())));

    ArgumentCaptor<CloudResource> updatedCloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(resourceNotifier, times(4)).notifyUpdate(updatedCloudResourceArgumentCaptor.capture(), any());

    assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_1, SIZE_DISK_1, FSTAB_1);
    assertVolumeResource(updatedCloudResourceArgumentCaptor.getAllValues(), INSTANCE_ID_2, SIZE_DISK_2, FSTAB_2);
}
 
Example #14
Source File: KeepCapacityStrategy.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
@Override
public boolean calculate(DeployConfiguration activeConfiguration, AutoScalingGroup autoScalingGroup, long inService, long healthy, long inStandby) {
    if (activeConfiguration.useElbStatusCheck()) {
        return inService > autoScalingGroup.getDesiredCapacity()-1;
    } else {
        return healthy > autoScalingGroup.getDesiredCapacity()-1;
    }

}
 
Example #15
Source File: DeployStrategy.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
default boolean isDeployable(DeployConfiguration activeConfiguration, AutoScalingGroup autoScalingGroup, List<Ec2Instance> instances) {
    long healthyInstances = autoScalingGroup.getInstances().stream()
            .filter(i -> i.getLifecycleState().equals(LifecycleState.InService.toString()))
            .count();

    long inStandbyInstances = autoScalingGroup.getInstances().stream()
            .filter(i -> i.getLifecycleState().equals(LifecycleState.Standby.toString()))
            .count();

    long inServiceInstances = instances.stream().filter(i -> AwsState.INSERVICE.equals(i.getElbState())).count();
    return this.calculate(activeConfiguration, autoScalingGroup, inServiceInstances, healthyInstances, inStandbyInstances);
}
 
Example #16
Source File: AwsDeployUtilsTest.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testInStandByInstancesFirst() throws Exception {
    AutoScalingGroup asgroup = deployUtils.getAutoScalingGroup();
    Assert.assertEquals(2, asgroup.getInstances().size());
    List<Ec2Instance> instances = deployUtils.getInstancesForAutoScalingGroup(log, asgroup);
    Assert.assertEquals(2, instances.size());
}
 
Example #17
Source File: DeployStateStrategyFactory.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
public static boolean isDeployableOnError(DeployConfiguration activeConfiguration, AutoScalingGroup autoScalingGroup, List<Ec2Instance> instances) {
    boolean canDeploy = false;
    switch (activeConfiguration.getDeployStrategy()) {
        case GUARANTEE_MINIMUM:
            canDeploy = new GuaranteeMinimumStrategy().isDeployable(activeConfiguration, autoScalingGroup, instances);
            break;
        case WHATEVER:
            canDeploy = new WhateverStrategy().isDeployable(activeConfiguration, autoScalingGroup, instances);
            break;
        default:
    }
    return canDeploy;

}
 
Example #18
Source File: DefaultAutoScalingPrePostHandler.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
public void postDeploy(AutoScalingGroup asGroup, Integer originalDesiredCapacity) {
    if (DeployStrategyType.KEEP_CAPACITY.equals(activeConfiguration.getDeployStrategy())) {
        awsDeployUtils.setDesiredCapacity(asGroup, originalDesiredCapacity);
    }

    if (activeConfiguration.isSticky()) {
        asGroup.getLoadBalancerNames().forEach(elbName -> awsDeployUtils.disableStickiness(elbName, activeConfiguration.getStickyPorts()));
    }
}
 
Example #19
Source File: GuaranteeMinimumStrategy.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
@Override
public boolean calculate(DeployConfiguration activeConfiguration, AutoScalingGroup autoScalingGroup, long inService, long healthy, long inStandby) {
    if (activeConfiguration.useElbStatusCheck()) {
        return inService > activeConfiguration.getMinCapacity();
    } else {
        return healthy > activeConfiguration.getMinCapacity();
    }

}
 
Example #20
Source File: AutoScalingDeployService.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
private List<Ec2Instance> checkInstances(AwsAutoScalingDeployUtils awsDeployUtils, AutoScalingGroup asGroup, List<Ec2Instance> instances) {
    List<String> removedInstances = asGroup.getInstances().stream()
            .filter(i -> i.getLifecycleState().equalsIgnoreCase(AwsState.STANDBY.name()))
            .map(Instance::getInstanceId)
            .filter(awsDeployUtils::checkEc2Instance)
            .collect(Collectors.toList());

    if (removedInstances != null && removedInstances.isEmpty()) {
        return instances.stream()
                .filter(i -> !removedInstances.contains(i.getInstanceId()))
                .collect(Collectors.toList());
    }
    return instances;
}
 
Example #21
Source File: SpinAndRemovePrePostHandler.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
public void preDeploy(List<Ec2Instance> instances, AutoScalingGroup asGroup) throws MojoFailureException, MojoExecutionException {
    if (asGroup.getInstances().isEmpty()) {
        log.info("No instances found in autoscaling group, spinning new instance");
        WaitForInstanceRequestExecutor.InstanceStatus instanceStatus = newInstance -> awsDeployUtils.checkInstanceInService(newInstance.getInstanceId());
        awsDeployUtils.setDesiredCapacity(asGroup, asGroup.getDesiredCapacity() + 1);
        WaitForInstanceRequestExecutor waitForDeployedInstanceRequestExecutor = new WaitForInstanceRequestExecutor(log, 10);
        waitForDeployedInstanceRequestExecutor.executeRequest(asGroup, awsDeployUtils, instanceStatus);
        instances.addAll(awsDeployUtils.getInstancesForAutoScalingGroup(log, awsDeployUtils.getAutoScalingGroup()));
    }

}
 
Example #22
Source File: DefaultAutoScalingPrePostHandler.java    From vertx-deploy-tools with Apache License 2.0 5 votes vote down vote up
public void preDeploy(List<Ec2Instance> instances, AutoScalingGroup asGroup) throws MojoFailureException, MojoExecutionException {
    if (instances.isEmpty()) {
        throw new MojoFailureException("No instances in AS group." + activeConfiguration.getDeployStrategy());
    }

    if (instances.stream().anyMatch(i -> !i.isReachable(activeConfiguration.getAwsPrivateIp(), activeConfiguration.getPort(), log))) {
        log.error("Error connecting to deploy module on some instances");
        throw new MojoExecutionException("Error connecting to deploy module on some instances");
    }

    if ((activeConfiguration.useElbStatusCheck() && instances.stream().noneMatch(i -> i.getElbState() == AwsState.INSERVICE))
            || !activeConfiguration.useElbStatusCheck() && asGroup.getInstances().stream().noneMatch(i -> "InService".equals(i.getLifecycleState()))) {
        activeConfiguration.setDeployStrategy(DeployStrategyType.WHATEVER);
        log.info("No instances inService, using deploy strategy " + DeployStrategyType.WHATEVER);
    }

    if (shouldSpinNewInstance(awsDeployUtils, asGroup)) {
        WaitForInstanceRequestExecutor.InstanceStatus instanceStatus = newInstance -> !asGroup.getLoadBalancerNames().isEmpty() && awsDeployUtils.checkInstanceInServiceOnAllElb(newInstance, asGroup.getLoadBalancerNames());
        awsDeployUtils.setDesiredCapacity(asGroup, asGroup.getDesiredCapacity() + 1);
        WaitForInstanceRequestExecutor waitForDeployedInstanceRequestExecutor = new WaitForInstanceRequestExecutor(log, 10);
        waitForDeployedInstanceRequestExecutor.executeRequest(asGroup, awsDeployUtils, instanceStatus);
        instances = awsDeployUtils.getInstancesForAutoScalingGroup(log, awsDeployUtils.getAutoScalingGroup());
    }

    instances.sort(Comparator.comparingInt(o -> o.getElbState().ordinal()));

    if (instances.isEmpty()) {
        throw new MojoFailureException("No inService instances found in group " + activeConfiguration.getAutoScalingGroupId() + ". Nothing to do here, move along");
    }

    if (!DeployStateStrategyFactory.isDeployable(activeConfiguration, asGroup, instances)) {
        throw new MojoExecutionException("Auto scaling group is not in a deployable state.");
    }

    if (activeConfiguration.isSticky()) {
        asGroup.getLoadBalancerNames().forEach(elbName -> awsDeployUtils.enableStickiness(elbName, activeConfiguration.getStickyPorts()));
    }
}
 
Example #23
Source File: AwsLaunchConfigurationImageUpdateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Map.Entry<AutoScalingGroup, String> getAutoScalingGroupForLaunchConfiguration(Map<AutoScalingGroup, String> scalingGroups,
        LaunchConfiguration oldLaunchConfiguration) {
    return scalingGroups.entrySet().stream()
            .filter(entry -> entry.getKey().getLaunchConfigurationName()
                    .equalsIgnoreCase(oldLaunchConfiguration.getLaunchConfigurationName()))
            .findFirst().orElseThrow(() -> new NoSuchElementException("Launch configuration not found for: "
                    + oldLaunchConfiguration.getLaunchConfigurationName()));
}
 
Example #24
Source File: AwsInstanceCloudConnector.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public Observable<List<InstanceGroup>> getInstanceGroups() {
    PageCollector<DescribeAutoScalingGroupsRequest, AutoScalingGroup> pageCollector = new PageCollector<>(
            token -> new DescribeAutoScalingGroupsRequest().withMaxRecords(AWS_PAGE_MAX).withNextToken(token),
            request -> {
                Observable<DescribeAutoScalingGroupsResult> observable = toObservable(request, autoScalingClient::describeAutoScalingGroupsAsync);
                return observable.map(result -> Pair.of(result.getAutoScalingGroups(), result.getNextToken()));
            }
    );
    return pageCollector.getAll()
            .map(this::toInstanceGroups)
            .timeout(configuration.getInstanceGroupsFetchTimeoutMs(), TimeUnit.MILLISECONDS);
}
 
Example #25
Source File: AwsLaunchConfigurationImageUpdateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void changeImageInAutoscalingGroup(AuthenticatedContext authenticatedContext, CloudStack stack, AmazonAutoScalingClient autoScalingClient,
        Map<AutoScalingGroup, String> scalingGroups, Map<String, String> encryptedImages, LaunchConfiguration oldLaunchConfiguration) {

    Map.Entry<AutoScalingGroup, String> autoScalingGroup = getAutoScalingGroupForLaunchConfiguration(scalingGroups, oldLaunchConfiguration);

    String encryptedImageName = encryptedImages.get(autoScalingGroup.getValue());
    String launchConfigurationName = launchConfigurationHandler.createNewLaunchConfiguration(
            stack.getImage().getImageName(), autoScalingClient, oldLaunchConfiguration, authenticatedContext.getCloudContext(), encryptedImageName);

    autoScalingGroupHandler.updateAutoScalingGroupWithLaunchConfiguration(autoScalingClient, autoScalingGroup.getKey().getAutoScalingGroupName(),
            oldLaunchConfiguration, launchConfigurationName);

    launchConfigurationHandler.removeOldLaunchConfiguration(oldLaunchConfiguration, autoScalingClient, authenticatedContext.getCloudContext());
}
 
Example #26
Source File: AsgLocalMasterReadinessResolver.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private ReadinessStatus resolveStatus(DescribeAutoScalingGroupsResult response) {
    ReadinessState effectiveState = null;
    String message = null;

    if (response.getAutoScalingGroups().size() < 1) {
        setNewTagValue("");
        effectiveState = ReadinessState.Disabled;
        message = "ASG not found: " + configuration.getTitusMasterAsgName();
    } else {
        AutoScalingGroup autoScalingGroup = response.getAutoScalingGroups().get(0);

        if (autoScalingGroup.getTags() != null) {
            for (TagDescription tagDescription : autoScalingGroup.getTags()) {
                if (TAG_MASTER_ENABLED.equals(tagDescription.getKey())) {
                    String value = tagDescription.getValue();
                    setNewTagValue(value);

                    boolean enabled = Boolean.valueOf(value);
                    effectiveState = enabled ? ReadinessState.Enabled : ReadinessState.Disabled;
                    message = "Set as tag on ASG: " + configuration.getTitusMasterAsgName();
                }
            }
        }

        if (effectiveState == null) {
            setNewTagValue("");
            effectiveState = ReadinessState.Disabled;
            message = String.format("ASG tag %s not found: %s", TAG_MASTER_ENABLED, configuration.getTitusMasterAsgName());
        }
    }

    return ReadinessStatus.newBuilder()
            .withState(effectiveState)
            .withMessage(message)
            .withTimestamp(clock.wallTime())
            .build();
}
 
Example #27
Source File: ASGInventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch scaling policies test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchScalingPoliciesTest() throws Exception {
    
    mockStatic(AmazonAutoScalingClientBuilder.class);
    AmazonAutoScaling asgClient = PowerMockito.mock(AmazonAutoScaling.class);
    AmazonAutoScalingClientBuilder amazonAutoScalingClientBuilder = PowerMockito.mock(AmazonAutoScalingClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonAutoScalingClientBuilder.standard()).thenReturn(amazonAutoScalingClientBuilder);
    when(amazonAutoScalingClientBuilder.withCredentials(anyObject())).thenReturn(amazonAutoScalingClientBuilder);
    when(amazonAutoScalingClientBuilder.withRegion(anyString())).thenReturn(amazonAutoScalingClientBuilder);
    when(amazonAutoScalingClientBuilder.build()).thenReturn(asgClient);
    
    DescribeAutoScalingGroupsResult autoScalingGroupsResult = new DescribeAutoScalingGroupsResult();
    List<AutoScalingGroup> asgList = new ArrayList<>();
    AutoScalingGroup autoScalingGroup = new AutoScalingGroup();
    autoScalingGroup.setAutoScalingGroupName("autoScalingGrpName");
    asgList.add(autoScalingGroup);
    autoScalingGroupsResult.setAutoScalingGroups(asgList);
    
    when(asgClient.describeAutoScalingGroups(anyObject())).thenReturn(autoScalingGroupsResult);
    
    DescribePoliciesResult policiesResult = new DescribePoliciesResult();
    List<ScalingPolicy> scalingPolicies = new ArrayList<>();
    scalingPolicies.add(new ScalingPolicy());
    policiesResult.setScalingPolicies(scalingPolicies);
    when(asgClient.describePolicies(anyObject())).thenReturn(policiesResult);
    assertThat(asgInventoryUtil.fetchScalingPolicies(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
Example #28
Source File: AwsLaunchConfigurationImageUpdateService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void updateImage(AuthenticatedContext authenticatedContext, CloudStack stack, CloudResource cfResource) {
    AwsCredentialView credentialView = new AwsCredentialView(authenticatedContext.getCloudCredential());
    String regionName = authenticatedContext.getCloudContext().getLocation().getRegion().getRegionName();
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AmazonAutoScalingClient autoScalingClient = awsClient.createAutoScalingClient(credentialView, regionName);

    Map<String, String> encryptedImages = getEncryptedImagesMappedByAutoscalingGroupName(authenticatedContext, stack);
    Map<AutoScalingGroup, String> scalingGroups = autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
    List<LaunchConfiguration> oldLaunchConfigurations = launchConfigurationHandler.getLaunchConfigurations(autoScalingClient, scalingGroups.keySet());
    for (LaunchConfiguration oldLaunchConfiguration : oldLaunchConfigurations) {
        changeImageInAutoscalingGroup(authenticatedContext, stack, autoScalingClient, scalingGroups, encryptedImages, oldLaunchConfiguration);
    }
}
 
Example #29
Source File: AutoScalingGroupHandler.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public Map<AutoScalingGroup, String> getAutoScalingGroups(AmazonCloudFormationClient cloudFormationClient, AmazonAutoScalingClient autoScalingClient,
        CloudResource cfResource) {
    DescribeStackResourcesRequest resourcesRequest = new DescribeStackResourcesRequest();
    resourcesRequest.setStackName(cfResource.getName());
    DescribeStackResourcesResult resourcesResult = cloudFormationClient.describeStackResources(resourcesRequest);
    Map<String, String> autoScalingGroups = resourcesResult.getStackResources().stream()
            .filter(stackResource -> "AWS::AutoScaling::AutoScalingGroup".equalsIgnoreCase(stackResource.getResourceType()))
            .collect(Collectors.toMap(StackResource::getPhysicalResourceId, StackResource::getLogicalResourceId));
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
    request.setAutoScalingGroupNames(autoScalingGroups.keySet());
    List<AutoScalingGroup> scalingGroups = autoScalingClient.describeAutoScalingGroups(request).getAutoScalingGroups();
    return scalingGroups.stream()
            .collect(Collectors.toMap(scalingGroup -> scalingGroup, scalingGroup -> autoScalingGroups.get(scalingGroup.getAutoScalingGroupName())));
}
 
Example #30
Source File: ASGInventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch scaling policies.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @return the map
 */
public static Map<String,List<ScalingPolicy>> fetchScalingPolicies(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
		
		AmazonAutoScaling asgClient;
		Map<String,List<ScalingPolicy>> scalingPolicyList = new LinkedHashMap<>();
		
		String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"ASG\" , \"region\":\"" ;
		for(Region region : RegionUtils.getRegions()){ 
			try{
				if(!skipRegions.contains(region.getName())){ //!skipRegions
					List<ScalingPolicy> _scalingPolicyList = new ArrayList<>();
					asgClient = AmazonAutoScalingClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
					String nextToken = null;
					DescribeAutoScalingGroupsResult  describeResult ;
					do{
						describeResult =  asgClient.describeAutoScalingGroups(new DescribeAutoScalingGroupsRequest().withNextToken(nextToken).withMaxRecords(asgMaxRecord));
						for(AutoScalingGroup _asg : describeResult.getAutoScalingGroups()) {
							_scalingPolicyList.addAll(asgClient.describePolicies(new DescribePoliciesRequest().withAutoScalingGroupName(_asg.getAutoScalingGroupName())).getScalingPolicies());
						}
						nextToken = describeResult.getNextToken();
					}while(nextToken!=null);
					
					if(!_scalingPolicyList.isEmpty() ){
						log.debug("Account : " + accountId + " Type : ASG Scaling Policy "+region.getName()+" >> " + _scalingPolicyList.size());
						scalingPolicyList.put(accountId+delimiter+accountName+delimiter+region.getName(), _scalingPolicyList);
					}
			   	}
			}catch(Exception e){
				log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}");
				ErrorManageUtil.uploadError(accountId,region.getName(),"asgpolicy",e.getMessage());
			}
		}
		return scalingPolicyList;
	}