Search in sources :

Example 16 with CloudResource

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

the class OpenStackInstanceBuilder method build.

@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) {
    CloudResource resource = buildableResource.get(0);
    try {
        OSClient<?> osClient = createOSClient(auth);
        InstanceTemplate template = getInstanceTemplate(group, privateId);
        CloudResource port = getPort(context.getComputeResources(privateId));
        KeystoneCredentialView osCredential = new KeystoneCredentialView(auth);
        NovaInstanceView novaInstanceView = new NovaInstanceView(context.getName(), template, group.getType(), group.getLoginUserName());
        String imageId = osClient.imagesV2().list(Collections.singletonMap("name", image.getImageName())).get(0).getId();
        LOGGER.info("Selected image id: {}", imageId);
        Map<String, String> metadata = mergeMetadata(novaInstanceView.getMetadataMap(), tags);
        ServerCreateBuilder serverCreateBuilder = Builders.server().name(resource.getName()).image(imageId).flavor(getFlavorId(osClient, novaInstanceView.getFlavor())).keypairName(osCredential.getKeyPairName()).addMetadata(metadata).addNetworkPort(port.getStringParameter(OpenStackConstants.PORT_ID)).userData(new String(Base64.encodeBase64(image.getUserDataByType(group.getType()).getBytes())));
        BlockDeviceMappingBuilder blockDeviceMappingBuilder = Builders.blockDeviceMapping().uuid(imageId).sourceType(BDMSourceType.IMAGE).deviceName("/dev/vda").bootIndex(0).deleteOnTermination(true).destinationType(BDMDestType.LOCAL);
        serverCreateBuilder = serverCreateBuilder.blockDevice(blockDeviceMappingBuilder.build());
        for (CloudResource computeResource : context.getComputeResources(privateId)) {
            if (computeResource.getType() == ResourceType.OPENSTACK_ATTACHED_DISK) {
                BlockDeviceMappingCreate blockDeviceMappingCreate = Builders.blockDeviceMapping().uuid(computeResource.getReference()).deviceName(computeResource.getStringParameter(OpenStackConstants.VOLUME_MOUNT_POINT)).sourceType(BDMSourceType.VOLUME).destinationType(BDMDestType.VOLUME).build();
                serverCreateBuilder.blockDevice(blockDeviceMappingCreate);
            }
        }
        ServerCreate serverCreate = serverCreateBuilder.build();
        Server server = osClient.compute().servers().boot(serverCreate);
        return Collections.singletonList(createPersistedResource(resource, group.getName(), server.getId(), Collections.singletonMap(OpenStackConstants.SERVER, server)));
    } catch (OS4JException ex) {
        LOGGER.error("Failed to create OpenStack instance with privateId: {}", privateId, ex);
        throw new OpenStackResourceException("Instance creation failed", resourceType(), resource.getName(), ex);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) ServerCreate(org.openstack4j.model.compute.ServerCreate) BlockDeviceMappingCreate(org.openstack4j.model.compute.BlockDeviceMappingCreate) BlockDeviceMappingBuilder(org.openstack4j.model.compute.builder.BlockDeviceMappingBuilder) OS4JException(org.openstack4j.api.exceptions.OS4JException) NovaInstanceView(com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView) ServerCreateBuilder(org.openstack4j.model.compute.builder.ServerCreateBuilder) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) KeystoneCredentialView(com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 17 with CloudResource

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

the class OpenStackContextBuilder method contextInit.

@Override
public OpenStackContext contextInit(CloudContext cloudContext, AuthenticatedContext auth, Network network, List<CloudResource> resources, boolean build) {
    OpenStackContext openStackContext = new OpenStackContext(utils.getStackName(auth), cloudContext.getLocation(), PARALLEL_RESOURCE_REQUEST, build);
    if (openStackClient.isV2Keystone(auth)) {
        String v2TenantId = openStackClient.getV2TenantId(auth);
        openStackContext.putParameter(OpenStackConstants.TENANT_ID, v2TenantId);
    } else {
        throw new CloudConnectorException("In case on native openstack api only V2 keystone is supported");
    }
    if (resources != null) {
        for (CloudResource resource : resources) {
            switch(resource.getType()) {
                case OPENSTACK_SUBNET:
                    openStackContext.putParameter(OpenStackConstants.SUBNET_ID, resource.getReference());
                    break;
                case OPENSTACK_NETWORK:
                    openStackContext.putParameter(OpenStackConstants.NETWORK_ID, resource.getReference());
                    break;
                case OPENSTACK_SECURITY_GROUP:
                    openStackContext.addGroupResources(resource.getGroup(), Collections.singletonList(resource));
                    break;
                default:
                    LOGGER.debug("Resource is not used during context build: {}", resource);
            }
        }
    }
    openStackContext.putParameter(OpenStackConstants.FLOATING_IP_IDS, Collections.synchronizedList(new ArrayList<String>()));
    if (network != null) {
        openStackContext.putParameter(OpenStackConstants.PUBLIC_NET_ID, network.getStringParameter(OpenStackConstants.PUBLIC_NET_ID));
    }
    return openStackContext;
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 18 with CloudResource

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

the class OpenStackResourceConnector method launch.

@SuppressWarnings("unchecked")
@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier notifier, AdjustmentType adjustmentType, Long threshold) {
    String stackName = utils.getStackName(authenticatedContext);
    NeutronNetworkView neutronNetworkView = new NeutronNetworkView(stack.getNetwork());
    boolean existingNetwork = neutronNetworkView.isExistingNetwork();
    String existingSubnetCidr = getExistingSubnetCidr(authenticatedContext, stack);
    ModelContext modelContext = new ModelContext();
    modelContext.withExistingNetwork(existingNetwork);
    modelContext.withExistingSubnet(existingSubnetCidr != null);
    modelContext.withGroups(stack.getGroups());
    modelContext.withInstanceUserData(stack.getImage());
    modelContext.withLocation(authenticatedContext.getCloudContext().getLocation());
    modelContext.withStackName(stackName);
    modelContext.withNeutronNetworkView(neutronNetworkView);
    modelContext.withTemplateString(stack.getTemplate());
    modelContext.withTags(stack.getTags());
    String heatTemplate = heatTemplateBuilder.build(modelContext);
    Map<String, String> parameters = heatTemplateBuilder.buildParameters(authenticatedContext, stack, existingNetwork, existingSubnetCidr);
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    List<CloudResourceStatus> resources;
    Stack existingStack = client.heat().stacks().getStackByName(stackName);
    if (existingStack == null) {
        if (stack.getInstanceAuthentication().getPublicKeyId() == null) {
            createKeyPair(authenticatedContext, stack, client);
        }
        Stack heatStack = client.heat().stacks().create(Builders.stack().name(stackName).template(heatTemplate).disableRollback(false).parameters(parameters).timeoutMins(OPERATION_TIMEOUT).build());
        List<CloudResource> cloudResources = collectResources(authenticatedContext, notifier, heatStack, stack, neutronNetworkView);
        resources = check(authenticatedContext, cloudResources);
    } else {
        LOGGER.info("Heat stack already exists: {}", existingStack.getName());
        CloudResource cloudResource = new Builder().type(ResourceType.HEAT_STACK).name(existingStack.getId()).build();
        resources = Collections.singletonList(new CloudResourceStatus(cloudResource, ResourceStatus.CREATED));
    }
    LOGGER.debug("Launched resources: {}", resources);
    return resources;
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) NeutronNetworkView(com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView) Stack(org.openstack4j.model.heat.Stack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack)

Example 19 with CloudResource

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

the class OpenStackResourceConnector method updateHeatStack.

private List<CloudResourceStatus> updateHeatStack(AuthenticatedContext authenticatedContext, List<CloudResource> resources, String heatTemplate, Map<String, String> parameters) {
    CloudResource resource = utils.getHeatResource(resources);
    String stackName = utils.getStackName(authenticatedContext);
    String heatStackId = resource.getName();
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    StackUpdate updateRequest = Builders.stackUpdate().template(heatTemplate).parameters(parameters).timeoutMins(OPERATION_TIMEOUT).build();
    client.heat().stacks().update(stackName, heatStackId, updateRequest);
    LOGGER.info("Heat stack update request sent with stack name: '{}' for Heat stack: '{}'", stackName, heatStackId);
    return check(authenticatedContext, resources);
}
Also used : CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) StackUpdate(org.openstack4j.model.heat.StackUpdate)

Example 20 with CloudResource

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

the class AbstractOpenStackResourceBuilder method checkResources.

protected List<CloudResourceStatus> checkResources(ResourceType type, OpenStackContext context, AuthenticatedContext auth, Iterable<CloudResource> resources) {
    List<CloudResourceStatus> result = new ArrayList<>();
    for (CloudResource resource : resources) {
        LOGGER.info("Check {} resource: {}", type, resource);
        try {
            boolean finished = checkStatus(context, auth, resource);
            ResourceStatus successStatus = context.isBuild() ? ResourceStatus.CREATED : ResourceStatus.DELETED;
            result.add(new CloudResourceStatus(resource, finished ? successStatus : ResourceStatus.IN_PROGRESS));
            if (finished) {
                if (successStatus == ResourceStatus.CREATED) {
                    LOGGER.info("Creation of {} was successful", resource);
                } else {
                    LOGGER.info("Deletion of {} was successful", resource);
                }
            }
        } catch (OS4JException ex) {
            throw new OpenStackResourceException("Error during status check", type, resource.getName(), ex);
        }
    }
    return result;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ArrayList(java.util.ArrayList) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) OS4JException(org.openstack4j.api.exceptions.OS4JException)

Aggregations

CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)79 ArrayList (java.util.ArrayList)33 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)29 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)17 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)16 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)13 List (java.util.List)11 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)9 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)8 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)8 Location (com.sequenceiq.cloudbreak.cloud.model.Location)8 Group (com.sequenceiq.cloudbreak.cloud.model.Group)7 Operation (com.google.api.services.compute.model.Operation)6 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)6 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)6 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)6 Map (java.util.Map)6 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)5