Search in sources :

Example 16 with AmazonAutoScalingClient

use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsResourceConnector method launch.

@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);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 17 with AmazonAutoScalingClient

use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project cloudbreak by hortonworks.

the class AwsMetadataCollector method collect.

@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);
    }
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 18 with AmazonAutoScalingClient

use of com.amazonaws.services.autoscaling.AmazonAutoScalingClient in project eureka by Netflix.

the class AwsAsgUtil method retrieveAutoScalingGroupCrossAccount.

private AutoScalingGroup retrieveAutoScalingGroupCrossAccount(String asgAccount, String asgName) {
    logger.debug("Getting cross account ASG for asgName: {}, asgAccount: {}", asgName, asgAccount);
    Credentials credentials = stsCredentials.get(asgAccount);
    if (credentials == null || credentials.getExpiration().getTime() < System.currentTimeMillis() + 1000) {
        stsCredentials.put(asgAccount, initializeStsSession(asgAccount));
        credentials = stsCredentials.get(asgAccount);
    }
    ClientConfiguration clientConfiguration = new ClientConfiguration().withConnectionTimeout(serverConfig.getASGQueryTimeoutMs());
    AmazonAutoScaling autoScalingClient = new AmazonAutoScalingClient(new BasicSessionCredentials(credentials.getAccessKeyId(), credentials.getSecretAccessKey(), credentials.getSessionToken()), clientConfiguration);
    String region = clientConfig.getRegion();
    if (!region.equals("us-east-1")) {
        autoScalingClient.setEndpoint("autoscaling." + region + ".amazonaws.com");
    }
    DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest().withAutoScalingGroupNames(asgName);
    DescribeAutoScalingGroupsResult result = autoScalingClient.describeAutoScalingGroups(request);
    List<AutoScalingGroup> asgs = result.getAutoScalingGroups();
    if (asgs.isEmpty()) {
        return null;
    } else {
        return asgs.get(0);
    }
}
Also used : DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) AmazonAutoScaling(com.amazonaws.services.autoscaling.AmazonAutoScaling) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) Credentials(com.amazonaws.services.securitytoken.model.Credentials) BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Aggregations

AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)18 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)9 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)6 AmazonServiceException (com.amazonaws.AmazonServiceException)5 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)5 ArrayList (java.util.ArrayList)5 Group (com.sequenceiq.cloudbreak.cloud.model.Group)4 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)3 ResumeProcessesRequest (com.amazonaws.services.autoscaling.model.ResumeProcessesRequest)3 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)3 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)3 ClientConfiguration (com.amazonaws.ClientConfiguration)2 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)2 AmazonAutoScaling (com.amazonaws.services.autoscaling.AmazonAutoScaling)2 DescribeScalingActivitiesRequest (com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest)2 DescribeScalingActivitiesResult (com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult)2 DetachInstancesRequest (com.amazonaws.services.autoscaling.model.DetachInstancesRequest)2 SuspendProcessesRequest (com.amazonaws.services.autoscaling.model.SuspendProcessesRequest)2 UpdateAutoScalingGroupRequest (com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest)2 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)2