Search in sources :

Example 1 with Stack

use of org.openstack4j.model.heat.Stack 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 2 with Stack

use of org.openstack4j.model.heat.Stack in project cloudbreak by hortonworks.

the class OpenStackResourceConnector method checkByResourceType.

private CloudResourceStatus checkByResourceType(AuthenticatedContext authenticatedContext, OSClient<?> client, String stackName, Collection<CloudResource> list, CloudResource resource) {
    CloudResourceStatus result = null;
    switch(resource.getType()) {
        case HEAT_STACK:
            String heatStackId = resource.getName();
            LOGGER.info("Checking OpenStack Heat stack status of: {}", stackName);
            Stack heatStack = client.heat().stacks().getDetails(stackName, heatStackId);
            result = utils.heatStatus(resource, heatStack);
            break;
        case OPENSTACK_NETWORK:
            result = checkResourceStatus(authenticatedContext, stackName, list, resource, ResourceType.OPENSTACK_NETWORK);
            break;
        case OPENSTACK_SUBNET:
            result = checkResourceStatus(authenticatedContext, stackName, list, resource, ResourceType.OPENSTACK_SUBNET);
            break;
        case OPENSTACK_ROUTER:
        case OPENSTACK_INSTANCE:
        case OPENSTACK_PORT:
        case OPENSTACK_ATTACHED_DISK:
        case OPENSTACK_SECURITY_GROUP:
        case OPENSTACK_FLOATING_IP:
            break;
        default:
            throw new CloudConnectorException(String.format("Invalid resource type: %s", resource.getType()));
    }
    return result;
}
Also used : CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Stack(org.openstack4j.model.heat.Stack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack)

Example 3 with Stack

use of org.openstack4j.model.heat.Stack in project cloudbreak by hortonworks.

the class OpenStackMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    CloudResource resource = utils.getHeatResource(resources);
    String stackName = utils.getStackName(authenticatedContext);
    String heatStackId = resource.getName();
    List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
    Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> utils.getPrivateInstanceId(from.getGroupName(), Long.toString(from.getPrivateId())));
    OSClient<?> client = openStackClient.createOSClient(authenticatedContext);
    Stack heatStack = client.heat().stacks().getDetails(stackName, heatStackId);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    List<Map<String, Object>> outputs = heatStack.getOutputs();
    for (Map<String, Object> map : outputs) {
        String instanceUUID = (String) map.get("output_value");
        if (!StringUtils.isEmpty(instanceUUID)) {
            Server server = client.compute().servers().get(instanceUUID);
            Map<String, String> metadata = server.getMetadata();
            String privateInstanceId = utils.getPrivateInstanceId(metadata);
            InstanceTemplate template = templateMap.get(privateInstanceId);
            if (template != null) {
                CloudInstanceMetaData md = cloudInstanceMetaDataExtractor.extractMetadata(client, server, instanceUUID);
                // TODO use here sshkey
                CloudInstance cloudInstance = new CloudInstance(instanceUUID, template, null);
                CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                results.add(new CloudVmMetaDataStatus(status, md));
            }
        }
    }
    return results;
}
Also used : Server(org.openstack4j.model.compute.Server) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Stack(org.openstack4j.model.heat.Stack) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Map(java.util.Map) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 4 with Stack

use of org.openstack4j.model.heat.Stack in project openstack4j by ContainX.

the class StackServiceTests method testAdoptStack.

public void testAdoptStack() throws Exception {
    respondWith(JSON_ADOPT);
    AdoptStackData adoptStackData = new ObjectMapper().readValue(getResource(JSON_ABANDON), HeatAdoptStackData.class);
    Stack adoptedStack = osv3().heat().stacks().adopt(adoptStackData, new HashMap<String, String>(), false, 30L, null);
    takeRequest();
    assertEquals(adoptedStack.getId(), "79370050-6038-4ea2-baaa-3e4706d59e0e");
}
Also used : HeatAdoptStackData(org.openstack4j.openstack.heat.domain.HeatAdoptStackData) AdoptStackData(org.openstack4j.model.heat.AdoptStackData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Stack(org.openstack4j.model.heat.Stack)

Aggregations

Stack (org.openstack4j.model.heat.Stack)4 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)2 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)1 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)1 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)1 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)1 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)1 ModelContext (com.sequenceiq.cloudbreak.cloud.openstack.heat.HeatTemplateBuilder.ModelContext)1 NeutronNetworkView (com.sequenceiq.cloudbreak.cloud.openstack.view.NeutronNetworkView)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Server (org.openstack4j.model.compute.Server)1 AdoptStackData (org.openstack4j.model.heat.AdoptStackData)1 HeatAdoptStackData (org.openstack4j.openstack.heat.domain.HeatAdoptStackData)1