Search in sources :

Example 6 with Builder

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder in project cloudbreak by hortonworks.

the class YarnResourceConnector method launch.

@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier persistenceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception {
    CreateApplicationRequest createApplicationRequest = new CreateApplicationRequest();
    createApplicationRequest.setName(createApplicationName(authenticatedContext));
    createApplicationRequest.setQueue(stack.getParameters().getOrDefault(YarnConstants.YARN_QUEUE_PARAMETER, defaultQueue));
    String lifeTimeStr = stack.getParameters().get(YarnConstants.YARN_LIFETIME_PARAMETER);
    createApplicationRequest.setLifetime(lifeTimeStr != null ? Integer.parseInt(lifeTimeStr) : defaultLifeTime);
    Artifact artifact = new Artifact();
    artifact.setId(stack.getImage().getImageName());
    artifact.setType("DOCKER");
    List<YarnComponent> components = new ArrayList<>();
    for (Group group : stack.getGroups()) {
        YarnComponent component = new YarnComponent();
        component.setName(group.getName());
        component.setNumberOfContainers(group.getInstancesSize());
        String userData = stack.getImage().getUserDataByType(group.getType());
        component.setLaunchCommand(String.format("/bootstrap/start-systemd '%s' '%s' '%s'", Base64.getEncoder().encodeToString(userData.getBytes()), stack.getLoginUserName(), stack.getPublicKey()));
        component.setArtifact(artifact);
        component.setDependencies(new ArrayList<>());
        InstanceTemplate instanceTemplate = group.getReferenceInstanceConfiguration().getTemplate();
        Resource resource = new Resource();
        resource.setCpus(instanceTemplate.getParameter(PlatformParametersConsts.CUSTOM_INSTANCETYPE_CPUS, Integer.class));
        resource.setMemory(instanceTemplate.getParameter(PlatformParametersConsts.CUSTOM_INSTANCETYPE_MEMORY, Integer.class));
        component.setResource(resource);
        component.setRunPrivilegedContainer(true);
        Configuration configuration = new Configuration();
        Map<String, String> propsMap = Maps.newHashMap();
        propsMap.put("conf.cb-conf.per.component", "true");
        propsMap.put("site.cb-conf.userData", '\'' + Base64.getEncoder().encodeToString(userData.getBytes()) + '\'');
        propsMap.put("site.cb-conf.sshUser", '\'' + stack.getLoginUserName() + '\'');
        propsMap.put("site.cb-conf.groupname", '\'' + group.getName() + '\'');
        propsMap.put("site.cb-conf.sshPubKey", '\'' + stack.getPublicKey() + '\'');
        configuration.setProperties(propsMap);
        ConfigFile configFileProps = new ConfigFile();
        configFileProps.setType(ConfigFileType.PROPERTIES.name());
        configFileProps.setSrcFile("cb-conf");
        configFileProps.setDestFile("/etc/cloudbreak-config.props");
        configuration.setFiles(Collections.singletonList(configFileProps));
        component.setConfiguration(configuration);
        components.add(component);
    }
    createApplicationRequest.setComponents(components);
    CloudResource yarnApplication = new Builder().type(YARN_APPLICATION).name(createApplicationRequest.getName()).build();
    persistenceNotifier.notifyAllocation(yarnApplication, authenticatedContext.getCloudContext());
    YarnClient yarnClient = yarnClientUtil.createYarnClient(authenticatedContext);
    ResponseContext responseContext = yarnClient.createApplication(createApplicationRequest);
    if (responseContext.getResponseError() != null) {
        ApplicationErrorResponse applicationErrorResponse = responseContext.getResponseError();
        throw new CloudConnectorException(String.format("Yarn Application creation error: HTTP Return: %d Error: %s", responseContext.getStatusCode(), applicationErrorResponse.getDiagnostics()));
    }
    return check(authenticatedContext, Collections.singletonList(yarnApplication));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) Configuration(com.sequenceiq.cloudbreak.cloud.yarn.client.model.core.Configuration) ConfigFile(com.sequenceiq.cloudbreak.cloud.yarn.client.model.core.ConfigFile) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Resource(com.sequenceiq.cloudbreak.cloud.yarn.client.model.core.Resource) ApplicationErrorResponse(com.sequenceiq.cloudbreak.cloud.yarn.client.model.response.ApplicationErrorResponse) Artifact(com.sequenceiq.cloudbreak.cloud.yarn.client.model.core.Artifact) YarnClient(com.sequenceiq.cloudbreak.cloud.yarn.client.YarnClient) CreateApplicationRequest(com.sequenceiq.cloudbreak.cloud.yarn.client.model.request.CreateApplicationRequest) ResponseContext(com.sequenceiq.cloudbreak.cloud.yarn.client.model.response.ResponseContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) YarnComponent(com.sequenceiq.cloudbreak.cloud.yarn.client.model.core.YarnComponent) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 7 with Builder

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder 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 8 with Builder

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder in project cloudbreak by hortonworks.

the class AwsResourceConnector method saveGeneratedSubnet.

private void saveGeneratedSubnet(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormation client, PersistenceNotifier resourceNotifier) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    if (awsNetworkView.isExistingVPC()) {
        String vpcId = awsNetworkView.getExistingVPC();
        CloudResource vpc = new Builder().type(ResourceType.AWS_VPC).name(vpcId).build();
        resourceNotifier.notifyAllocation(vpc, ac.getCloudContext());
    } else {
        String vpcId = getCreatedVpc(cFStackName, client);
        CloudResource vpc = new Builder().type(ResourceType.AWS_VPC).name(vpcId).build();
        resourceNotifier.notifyAllocation(vpc, ac.getCloudContext());
    }
    if (awsNetworkView.isExistingSubnet()) {
        String subnetId = awsNetworkView.getExistingSubnet();
        CloudResource subnet = new Builder().type(ResourceType.AWS_SUBNET).name(subnetId).build();
        resourceNotifier.notifyAllocation(subnet, ac.getCloudContext());
    } else {
        String subnetId = getCreatedSubnet(cFStackName, client);
        CloudResource subnet = new Builder().type(ResourceType.AWS_SUBNET).name(subnetId).build();
        resourceNotifier.notifyAllocation(subnet, ac.getCloudContext());
    }
}
Also used : AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 9 with Builder

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder in project cloudbreak by hortonworks.

the class AwsResourceConnector method saveS3AccessRoleArn.

private void saveS3AccessRoleArn(AuthenticatedContext ac, CloudStack stack, String cFStackName, AmazonCloudFormation client, PersistenceNotifier resourceNotifier) {
    AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
    if (awsInstanceProfileView.isEnableInstanceProfileStrategy() && !awsInstanceProfileView.isInstanceProfileAvailable()) {
        String s3AccessRoleArn = getCreatedS3AccessRoleArn(cFStackName, client);
        CloudResource s3AccessRoleArnCloudResource = new Builder().type(ResourceType.S3_ACCESS_ROLE_ARN).name(s3AccessRoleArn).build();
        resourceNotifier.notifyAllocation(s3AccessRoleArnCloudResource, ac.getCloudContext());
    }
}
Also used : Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)

Example 10 with Builder

use of com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder in project cloudbreak by hortonworks.

the class GcpNetworkResourceBuilder method build.

@Override
public CloudResource build(GcpContext context, AuthenticatedContext auth, Network network, Security security, CloudResource resource) throws Exception {
    if (!isExistingNetwork(network)) {
        Compute compute = context.getCompute();
        String projectId = context.getProjectId();
        com.google.api.services.compute.model.Network gcpNetwork = new com.google.api.services.compute.model.Network();
        gcpNetwork.setName(resource.getName());
        gcpNetwork.setAutoCreateSubnetworks(false);
        Insert networkInsert = compute.networks().insert(projectId, gcpNetwork);
        try {
            Operation operation = networkInsert.execute();
            if (operation.getHttpErrorStatusCode() != null) {
                throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), resource.getName());
            }
            context.putParameter(NETWORK_NAME, resource.getName());
            return createOperationAwareCloudResource(resource, operation);
        } catch (GoogleJsonResponseException e) {
            throw new GcpResourceException(checkException(e), resourceType(), resource.getName());
        }
    }
    context.putParameter(NETWORK_NAME, resource.getName());
    return new Builder().cloudResource(resource).persistent(false).build();
}
Also used : Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Networks.Insert) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) GcpStackUtil.isExistingNetwork(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil.isExistingNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)

Aggregations

Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)14 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)12 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)5 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)4 Operation (com.google.api.services.compute.model.Operation)3 ArrayList (java.util.ArrayList)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 Compute (com.google.api.services.compute.Compute)2 AwsInstanceProfileView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsInstanceProfileView)2 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView)2 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)2 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)2 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)2 Group (com.sequenceiq.cloudbreak.cloud.model.Group)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 Insert (com.google.api.services.compute.Compute.Networks.Insert)1