use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsNetworkConnector method createNetworkWithSubnets.
@Override
public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) {
AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential());
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, networkRequest.getRegion().value());
List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest);
String cfStackName = networkRequest.getStackName();
try {
cloudFormationClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName));
LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName);
return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationClient, subnetRequests);
} catch (AmazonServiceException e) {
if (networkDoesNotExist(e)) {
LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage());
String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests);
return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationClient, cloudFormationTemplate, subnetRequests);
} else {
String region = networkRequest.getRegion().getRegionName();
String errorReason = awsCloudFormationErrorMessageProvider.getErrorReason(credentialView, region, cfStackName, ResourceStatus.CREATE_FAILED);
String message = String.format("Failed to create network: %s", errorReason);
LOGGER.debug(message, e);
throw new CloudConnectorException(message, e);
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsLaunchService method launch.
public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) throws Exception {
LOGGER.info("Launch AWS cluster with adjustment and threshold: {}", adjustmentTypeWithThreshold);
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.createEc2Client(credentialView, regionName);
Network network = stack.getNetwork();
AwsNetworkView awsNetworkView = new AwsNetworkView(network);
boolean mapPublicIpOnLaunch = awsNetworkService.isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
ModelContext modelContext = null;
try {
cfClient.describeStacks(describeStacksRequest);
LOGGER.debug("Stack already exists: {}", cFStackName);
} catch (AmazonServiceException ignored) {
boolean existingVPC = awsNetworkView.isExistingVPC();
boolean existingSubnet = awsNetworkView.isExistingSubnet();
CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).name(cFStackName).build();
resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
String cidr = network.getSubnet().getCidr();
String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? awsNetworkService.findNonOverLappingCIDR(ac, stack) : cidr;
modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
}
LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("CloudFormation stack %s creation failed.", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, ResourceStatus.CREATE_FAILED));
List<CloudResource> networkResources = saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
suspendAutoscalingGoupsWhenNewInstancesAreReady(ac, stack);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfClient, amazonASClient, stack.getGroups());
if (mapPublicIpOnLaunch) {
associatePublicIpsToGatewayInstances(stack, cFStackName, cfClient, amazonEC2Client, instances);
}
awsComputeResourceService.buildComputeResourcesForLaunch(ac, stack, adjustmentTypeWithThreshold, instances, networkResources);
awsTaggingService.tagRootVolumes(ac, amazonEC2Client, instances, stack.getTags());
awsCloudWatchService.addCloudWatchAlarmsForSystemFailures(instances, regionName, credentialView);
return awsResourceConnector.check(ac, instances);
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsLaunchService method suspendAutoscalingGoupsWhenNewInstancesAreReady.
private void suspendAutoscalingGoupsWhenNewInstancesAreReady(AuthenticatedContext ac, CloudStack stack) {
AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
try {
awsAutoScalingService.scheduleStatusChecks(stack.getGroups(), ac, cloudFormationClient);
} catch (AmazonAutoscalingFailed amazonAutoscalingFailed) {
LOGGER.info("Amazon autoscaling failed", amazonAutoscalingFailed);
throw new CloudConnectorException(amazonAutoscalingFailed);
}
awsAutoScalingService.suspendAutoScaling(ac, stack);
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsLoadBalancerLaunchService method updateCloudFormationStack.
private ListStackResourcesResult updateCloudFormationStack(AuthenticatedContext ac, CloudStack stack, ModelContext modelContext) {
String cFStackName = cfStackUtil.getCfStackName(ac);
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
cfClient.updateStack(awsStackRequestHelper.createUpdateStackRequest(ac, stack, cFStackName, cfTemplate));
Waiter<DescribeStacksRequest> updateWaiter = cfClient.waiters().stackUpdateComplete();
StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
run(updateWaiter, describeStacksRequest, stackCancellationCheck, String.format("CloudFormation stack %s update failed.", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, ResourceStatus.UPDATE_FAILED, ResourceStatus.CREATE_FAILED));
return cfClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
}
use of com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient in project cloudbreak by hortonworks.
the class AwsRdsLaunchService method launch.
public List<CloudResourceStatus> launch(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier resourceNotifier) {
String cFStackName = cfStackUtil.getCfStackName(ac);
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
DatabaseServer databaseServer = stack.getDatabaseServer();
boolean useSslEnforcement = databaseServer.isUseSslEnforcement();
try {
cfClient.describeStacks(describeStacksRequest);
LOGGER.debug("Stack already exists: {}", cFStackName);
} catch (AmazonServiceException exception) {
// all subnets desired for DB subnet group are in the stack
boolean existingSubnet = awsNetworkView.isExistingSubnet();
LOGGER.warn("API call failed with this error:", exception);
if (!existingSubnet) {
throw new CloudConnectorException("Can only create RDS instance with existing subnets", exception);
}
CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).name(cFStackName).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).build();
resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
RDSModelContext rdsModelContext = new RDSModelContext().withTemplate(stack.getTemplate()).withNetworkCidrs(awsNetworkView.getExistingVpcCidrs()).withHasPort(databaseServer.getPort() != null).withUseSslEnforcement(useSslEnforcement).withSslCertificateIdentifierDefined(new AwsRdsInstanceView(databaseServer).isSslCertificateIdentifierDefined()).withHasSecurityGroup(!databaseServer.getSecurity().getCloudSecurityIds().isEmpty()).withIsKmsCustom(stack.getDatabaseServer().getParameters().containsKey("key")).withGetKmsKey(getKmsKey(stack));
String cfTemplate = cloudFormationTemplateBuilder.build(rdsModelContext);
LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, cfTemplate));
}
LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("RDS CloudFormation stack %s creation failed", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, CREATE_FAILED));
List<CloudResource> databaseResources = getCreatedOutputs(ac, stack, cFStackName, cfClient, resourceNotifier, useSslEnforcement);
databaseResources.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
// FIXME: For now, just return everything wrapped in a status object
return databaseResources.stream().map(resource -> new CloudResourceStatus(resource, ResourceStatus.CREATED)).collect(Collectors.toList());
// FIXME check does nothing?!
// return awsResourceConnector.check(ac, databaseResources);
}
Aggregations