Search in sources :

Example 1 with NovaInstanceView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView in project cloudbreak by hortonworks.

the class OpenStackAttachedDiskResourceBuilder method create.

@Override
public List<CloudResource> create(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image) {
    List<CloudResource> cloudResources = new ArrayList<>();
    InstanceTemplate template = getInstanceTemplate(group, privateId);
    NovaInstanceView instanceView = new NovaInstanceView(context.getName(), template, group.getType(), group.getLoginUserName());
    String groupName = group.getName();
    String stackName = getUtils().getStackName(auth);
    for (int i = 0; i < instanceView.getVolumes().size(); i++) {
        String resourceName = resourceNameService.resourceName(resourceType(), stackName, groupName, privateId, i);
        CloudResource resource = createNamedResource(resourceType(), groupName, resourceName);
        resource.putParameter(VOLUME_VIEW, instanceView.getVolumes().get(i));
        cloudResources.add(resource);
    }
    return cloudResources;
}
Also used : ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) NovaInstanceView(com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView)

Example 2 with NovaInstanceView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView 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 3 with NovaInstanceView

use of com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView in project cloudbreak by hortonworks.

the class HeatTemplateBuilder method build.

public String build(ModelContext modelContext) {
    try {
        List<NovaInstanceView> novaInstances = new OpenStackGroupView(modelContext.stackName, modelContext.groups, modelContext.tags).getFlatNovaView();
        Map<String, Object> model = new HashMap<>();
        model.put("cb_stack_name", openStackUtil.adjustStackNameLength(modelContext.stackName));
        model.put("agents", novaInstances);
        model.put("core_user_data", formatUserData(modelContext.instanceUserData.getUserDataByType(InstanceGroupType.CORE)));
        model.put("gateway_user_data", formatUserData(modelContext.instanceUserData.getUserDataByType(InstanceGroupType.GATEWAY)));
        model.put("groups", modelContext.groups);
        model.put("existingNetwork", modelContext.existingNetwork);
        model.put("existingSubnet", modelContext.existingSubnet);
        model.put("network", modelContext.neutronNetworkView);
        model.putAll(defaultCostTaggingService.prepareAllTagsForTemplate());
        AvailabilityZone az = modelContext.location.getAvailabilityZone();
        if (az != null && az.value() != null) {
            model.put("availability_zone", az.value());
        }
        Template template = new Template(openStackHeatTemplatePath, modelContext.templateString, freemarkerConfiguration);
        String generatedTemplate = processTemplateIntoString(template, model);
        LOGGER.debug("Generated Heat template: {}", generatedTemplate);
        return generatedTemplate;
    } catch (IOException | TemplateException e) {
        throw new CloudConnectorException("Failed to process the OpenStack HeatTemplateBuilder", e);
    }
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) FreeMarkerTemplateUtils.processTemplateIntoString(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils.processTemplateIntoString) IOException(java.io.IOException) NovaInstanceView(com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView) Template(freemarker.template.Template) OpenStackGroupView(com.sequenceiq.cloudbreak.cloud.openstack.view.OpenStackGroupView)

Aggregations

NovaInstanceView (com.sequenceiq.cloudbreak.cloud.openstack.view.NovaInstanceView)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)1 OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)1 KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)1 OpenStackGroupView (com.sequenceiq.cloudbreak.cloud.openstack.view.OpenStackGroupView)1 FreeMarkerTemplateUtils.processTemplateIntoString (com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils.processTemplateIntoString)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 OS4JException (org.openstack4j.api.exceptions.OS4JException)1 BlockDeviceMappingCreate (org.openstack4j.model.compute.BlockDeviceMappingCreate)1 Server (org.openstack4j.model.compute.Server)1 ServerCreate (org.openstack4j.model.compute.ServerCreate)1 BlockDeviceMappingBuilder (org.openstack4j.model.compute.builder.BlockDeviceMappingBuilder)1 ServerCreateBuilder (org.openstack4j.model.compute.builder.ServerCreateBuilder)1