use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsResourceConnector method downscale.
@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms, Object resourcesToRemove) {
Collection<String> instanceIds = new ArrayList<>();
for (CloudInstance vm : vms) {
instanceIds.add(vm.getInstanceId());
}
String asGroupName = cfStackUtil.getAutoscalingGroupName(auth, vms.get(0).getTemplate().getGroupName(), auth.getCloudContext().getLocation().getRegion().value());
DetachInstancesRequest detachInstancesRequest = new DetachInstancesRequest().withAutoScalingGroupName(asGroupName).withInstanceIds(instanceIds).withShouldDecrementDesiredCapacity(true);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
try {
amazonASClient.detachInstances(detachInstancesRequest);
} catch (AmazonServiceException e) {
if (!"ValidationError".equals(e.getErrorCode()) || !e.getErrorMessage().contains("not part of Auto Scaling") || instanceIds.stream().anyMatch(id -> !e.getErrorMessage().contains(id))) {
throw e;
}
LOGGER.info(e.getErrorMessage());
}
AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
try {
amazonEC2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceIds));
} catch (AmazonServiceException e) {
if (!"InvalidInstanceID.NotFound".equals(e.getErrorCode())) {
throw e;
}
LOGGER.info(e.getErrorMessage());
}
LOGGER.info("Terminated instances in stack '{}': '{}'", auth.getCloudContext().getId(), instanceIds);
return check(auth, resources);
}
use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsResourceConnector method getRootDeviceName.
private String getRootDeviceName(AuthenticatedContext ac, CloudStack cloudStack) {
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
DescribeImagesResult images = ec2Client.describeImages(new DescribeImagesRequest().withImageIds(cloudStack.getImage().getImageName()));
if (images.getImages().isEmpty()) {
throw new CloudConnectorException(String.format("AMI is not available: '%s'.", cloudStack.getImage().getImageName()));
}
Image image = images.getImages().get(0);
if (image == null) {
throw new CloudConnectorException(String.format("Couldn't describe AMI '%s'.", cloudStack.getImage().getImageName()));
}
return image.getRootDeviceName();
}
use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsResourceConnector method createKeyPair.
private void createKeyPair(AuthenticatedContext ac, CloudStack stack) {
if (!awsClient.existingKeyPairNameSpecified(stack.getInstanceAuthentication())) {
AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
try {
String region = ac.getCloudContext().getLocation().getRegion().value();
LOGGER.info(String.format("Importing public key to %s region on AWS", region));
AmazonEC2Client client = awsClient.createAccess(awsCredential, region);
String keyPairName = awsClient.getKeyPairName(ac);
ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(keyPairName, stack.getInstanceAuthentication().getPublicKey());
try {
client.describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyPairName));
LOGGER.info("Key-pair already exists: {}", keyPairName);
} catch (AmazonServiceException e) {
client.importKeyPair(importKeyPairRequest);
}
} catch (Exception e) {
String errorMessage = String.format("Failed to import public key [roleArn:'%s'], detailed message: %s", awsCredential.getRoleArn(), e.getMessage());
LOGGER.error(errorMessage, e);
throw new CloudConnectorException(e.getMessage(), e);
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView 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);
}
use of com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsResourceConnector method findNonOverLappingCIDR.
protected String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) {
AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);
DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVPC());
Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
String vpcCidr = vpc.getCidrBlock();
LOGGER.info("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr);
DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVPC())));
List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets();
List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList());
LOGGER.info("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), subnetCidrs.stream().collect(Collectors.joining(",")));
return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs);
}
Aggregations