@Test public void testIncrementMinInstancesForAsgHappy() { String logicalId = "asg id"; String instanceId = "instance id"; int minSize = 2; when(autoScalingClient.describeAutoScalingGroups( new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(logicalId)) ).thenReturn( new DescribeAutoScalingGroupsResult() .withAutoScalingGroups( new AutoScalingGroup().withInstances( new Instance().withInstanceId(instanceId)) .withMinSize(minSize) ) ); autoScalingService.updateMinInstancesForAutoScalingGroup(logicalId, minSize - 1); verify(autoScalingClient).updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(logicalId) .withMinSize(minSize - 1)); }
@Override public void execute(Context context) throws Exception { ASGroupHelper helper = new ASGroupHelper(context.env); String asGroupName = resource.remoteASGroup.getAutoScalingGroupName(); String oldLaunchConfigName = null; if (resource.launchConfig.changed()) { oldLaunchConfigName = resource.remoteASGroup.getLaunchConfigurationName(); helper.createLaunchConfig(resource); AWS.as.updateTag(asGroupName, helper.nameTag(resource)); } AWS.as.updateASGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(asGroupName) .withLaunchConfigurationName(resource.launchConfig.remoteLaunchConfig.getLaunchConfigurationName()) .withTerminationPolicies(ASGroup.TERMINATE_POLICY_OLDEST_INSTANCE) .withDesiredCapacity(resource.desiredSize) .withMinSize(resource.minSize) .withMaxSize(resource.maxSize)); if (oldLaunchConfigName != null) { AWS.as.deleteLaunchConfig(oldLaunchConfigName); } }
/** * Updates the minimum number of instances allowed in the auto scaling group * @param logicalId - Name of the auto scaling group */ public void updateMinInstancesForAutoScalingGroup(final String logicalId, final int minInstances) { final UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(logicalId) .withMinSize(minInstances); autoScalingClient.updateAutoScalingGroup(request); }
@Override public void updateAutoScalingSettings(int minNodeNumber, int maxNodeNumber) { autoScaling.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withMaxSize(maxNodeNumber) .withMinSize(minNodeNumber) .withDesiredCapacity(minNodeNumber)); }
public void updateASGroup(UpdateAutoScalingGroupRequest request) throws Exception { new Runner<>() .retryInterval(Duration.ofSeconds(5)) .maxAttempts(3) .retryOn(e -> e instanceof AmazonServiceException) .run(() -> { logger.info("update auto scaling group, request={}", request); autoScaling.updateAutoScalingGroup(request); return null; }); }
@Override public void execute(Context context) throws Exception { AWS.as.updateASGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(resource.remoteASGroup.getAutoScalingGroupName()) .withDesiredCapacity(0) .withMinSize(0) .withMaxSize(0)); }
@Override public void execute(Context context) throws Exception { AWS.as.updateASGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(resource.remoteASGroup.getAutoScalingGroupName()) .withDesiredCapacity(resource.desiredSize) .withMinSize(resource.minSize) .withMaxSize(resource.maxSize)); }
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 void configureClusterInfrastructure() { LOG.info("Configuration of cluster infrastructure started"); // update AutoScalingGroup with min and max node number autoScaling.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withMaxSize(configurationMediator.getMaxNodeNumberInCluster()) .withMinSize(configurationMediator.getMinNodeNumberInCluster()) .withDesiredCapacity(configurationMediator.getMinNodeNumberInCluster())); LOG.info("AutoScalingGroup {} updated: {}", autoScalingGroup.getAutoScalingGroupName(), autoScalingGroup.toString()); // we create this infrustructure from JAVA since currently we can not get arn when we create policy from CFT //create AutoScaling Policies String scaleUpPolicyARN = autoScaling.putScalingPolicy(new PutScalingPolicyRequest().withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withPolicyName(SCALE_UP_POLICY) .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) //Increase or decrease the current capacity of the group by the specified number of instances. .withAdjustmentType("ChangeInCapacity") .withPolicyType("SimpleScaling") .withScalingAdjustment(1)).getPolicyARN(); LOG.info("Scale up policy created: {}", SCALE_UP_POLICY); String scaleDownPolicyARN = autoScaling.putScalingPolicy(new PutScalingPolicyRequest().withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withPolicyName(SCALE_DOWN_POLICY) .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) //Increase or decrease the current capacity of the group by the specified number of instances. .withAdjustmentType("ChangeInCapacity") .withPolicyType("SimpleScaling") .withScalingAdjustment(-1)).getPolicyARN(); LOG.info("Scale down policy created: {}", SCALE_DOWN_POLICY); // create custom metric MetricDatum metricDatum = new MetricDatum(); metricDatum.setValue(0.0); metricDatum.setUnit(StandardUnit.Count); metricDatum.setTimestamp(new Date()); metricDatum.setMetricName(METRIC_DATA_NAME); cloudWatch.putMetricData(new PutMetricDataRequest() .withNamespace("ESS/Tasks").withMetricData(metricDatum)); LOG.info("Custom metric added: {}", metricDatum.toString()); // create custom alarm cloudWatch.putMetricAlarm(new PutMetricAlarmRequest() .withAlarmName(ESS_OVERLOAD_ALARM) .withMetricName(METRIC_DATA_NAME) .withComparisonOperator(ComparisonOperator.GreaterThanOrEqualToThreshold) .withThreshold(80.0) .withPeriod(300) .withEvaluationPeriods(2) .withStatistic(Statistic.Average) .withNamespace("ESS/Tasks") .withAlarmActions(scaleUpPolicyARN)); LOG.info("Load alarm added: ", cloudWatch.describeAlarms().getMetricAlarms() .stream().filter(alarm -> alarm.getAlarmName().equals(ESS_OVERLOAD_ALARM)).findFirst().get().toString()); // create custom alarm cloudWatch.putMetricAlarm(new PutMetricAlarmRequest() .withAlarmName(ESS_IDLE_ALARM) .withMetricName(METRIC_DATA_NAME) .withComparisonOperator(ComparisonOperator.LessThanThreshold) .withThreshold(40.0) .withPeriod(300) .withEvaluationPeriods(2) .withStatistic(Statistic.Average) .withNamespace("ESS/Tasks") .withAlarmActions(scaleDownPolicyARN)); LOG.info("Alarm for idle resources added: ", cloudWatch.describeAlarms().getMetricAlarms() .stream().filter(alarm -> alarm.getAlarmName().equals(ESS_IDLE_ALARM)).findFirst().get().toString()); // subscribe to topic amazonSQS.createQueue(new CreateQueueRequest().withQueueName(ESS_QUEUE_NAME)); Topics.subscribeQueue(amazonSNS, amazonSQS, getEssTopicArn(), amazonSQS.getQueueUrl(ESS_QUEUE_NAME).getQueueUrl()); LOG.info("Cluster infrastructure successfully configured."); }
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" ); } }
@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)); }