Search in sources :

Example 16 with InstanceTemplate

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

the class StackToCloudStackConverter method buildInstance.

public CloudInstance buildInstance(InstanceMetaData instanceMetaData, Template template, StackAuthentication stackAuthentication, String name, Long privateId, InstanceStatus status) {
    String id = instanceMetaData == null ? null : instanceMetaData.getInstanceId();
    String hostName = instanceMetaData == null ? null : instanceMetaData.getShortHostname();
    String subnetId = instanceMetaData == null ? null : instanceMetaData.getSubnetId();
    InstanceTemplate instanceTemplate = buildInstanceTemplate(template, name, privateId, status);
    InstanceAuthentication instanceAuthentication = buildInstanceAuthentication(stackAuthentication);
    Map<String, Object> params = new HashMap<>();
    if (hostName != null) {
        params.put(CloudInstance.DISCOVERY_NAME, hostName);
    }
    if (subnetId != null) {
        params.put(CloudInstance.SUBNET_ID, subnetId);
    }
    return new CloudInstance(id, instanceTemplate, instanceAuthentication, params);
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 17 with InstanceTemplate

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

the class StackToCloudStackConverter method buildInstanceTemplate.

public InstanceTemplate buildInstanceTemplate(Template template, String name, Long privateId, InstanceStatus status) {
    Json attributes = template.getAttributes();
    Map<String, Object> fields = attributes == null ? Collections.emptyMap() : attributes.getMap();
    List<Volume> volumes = new ArrayList<>();
    for (int i = 0; i < template.getVolumeCount(); i++) {
        Volume volume = new Volume(VolumeUtils.VOLUME_PREFIX + (i + 1), template.getVolumeType(), template.getVolumeSize());
        volumes.add(volume);
    }
    return new InstanceTemplate(template.getInstanceType(), name, privateId, volumes, status, fields, template.getId());
}
Also used : Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) ArrayList(java.util.ArrayList) Json(com.sequenceiq.cloudbreak.domain.json.Json) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 18 with InstanceTemplate

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

the class InstanceMetadataService method saveInstanceRequests.

public void saveInstanceRequests(Stack stack, Iterable<Group> groups) {
    Set<InstanceGroup> instanceGroups = stack.getInstanceGroups();
    for (Group group : groups) {
        InstanceGroup instanceGroup = getInstanceGroup(instanceGroups, group.getName());
        List<InstanceMetaData> existingInGroup = instanceMetaDataRepository.findAllByInstanceGroupAndInstanceStatus(instanceGroup, com.sequenceiq.cloudbreak.api.model.InstanceStatus.REQUESTED);
        for (CloudInstance cloudInstance : group.getInstances()) {
            InstanceTemplate instanceTemplate = cloudInstance.getTemplate();
            boolean exists = existingInGroup.stream().anyMatch(i -> i.getPrivateId().equals(instanceTemplate.getPrivateId()));
            if (InstanceStatus.CREATE_REQUESTED == instanceTemplate.getStatus() && !exists) {
                InstanceMetaData instanceMetaData = new InstanceMetaData();
                instanceMetaData.setPrivateId(instanceTemplate.getPrivateId());
                instanceMetaData.setInstanceStatus(com.sequenceiq.cloudbreak.api.model.InstanceStatus.REQUESTED);
                instanceMetaData.setInstanceGroup(instanceGroup);
                instanceMetaDataRepository.save(instanceMetaData);
            }
        }
    }
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 19 with InstanceTemplate

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

the class GcpAttachedDiskResourceBuilder method create.

@Override
public List<CloudResource> create(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image) {
    List<CloudResource> cloudResources = new ArrayList<>();
    CloudInstance instance = group.getReferenceInstanceConfiguration();
    InstanceTemplate template = instance.getTemplate();
    GcpResourceNameService resourceNameService = getResourceNameService();
    String groupName = group.getName();
    CloudContext cloudContext = auth.getCloudContext();
    String stackName = cloudContext.getName();
    for (int i = 0; i < template.getVolumes().size(); i++) {
        String resourceName = resourceNameService.resourceName(resourceType(), stackName, groupName, privateId, i);
        cloudResources.add(createNamedResource(resourceType(), resourceName));
    }
    return cloudResources;
}
Also used : CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) GcpResourceNameService(com.sequenceiq.cloudbreak.cloud.gcp.service.GcpResourceNameService) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 20 with InstanceTemplate

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

the class GcpInstanceResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> customTags) throws Exception {
    InstanceTemplate template = group.getReferenceInstanceConfiguration().getTemplate();
    String projectId = context.getProjectId();
    Location location = context.getLocation();
    boolean noPublicIp = context.getNoPublicIp();
    Compute compute = context.getCompute();
    List<CloudResource> computeResources = context.getComputeResources(privateId);
    List<AttachedDisk> listOfDisks = new ArrayList<>();
    listOfDisks.addAll(getBootDiskList(computeResources, projectId, location.getAvailabilityZone()));
    listOfDisks.addAll(getAttachedDisks(computeResources, projectId, location.getAvailabilityZone()));
    Instance instance = new Instance();
    instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s", projectId, location.getAvailabilityZone().value(), template.getFlavor()));
    instance.setName(buildableResource.get(0).getName());
    instance.setCanIpForward(Boolean.TRUE);
    instance.setNetworkInterfaces(getNetworkInterface(context.getNetworkResources(), computeResources, location.getRegion(), group, compute, projectId, noPublicIp));
    instance.setDisks(listOfDisks);
    Scheduling scheduling = new Scheduling();
    boolean preemptible = false;
    if (template.getParameter(PREEMPTIBLE, Boolean.class) != null) {
        preemptible = template.getParameter(PREEMPTIBLE, Boolean.class);
    }
    scheduling.setPreemptible(preemptible);
    instance.setScheduling(scheduling);
    Tags tags = new Tags();
    List<String> tagList = new ArrayList<>();
    Map<String, String> labels = new HashMap<>();
    String groupname = group.getName().toLowerCase().replaceAll("[^A-Za-z0-9 ]", "");
    tagList.add(groupname);
    Map<String, String> instanceTag = defaultCostTaggingService.prepareInstanceTagging();
    for (Entry<String, String> entry : instanceTag.entrySet()) {
        tagList.add(String.format("%s-%s", entry.getKey(), entry.getValue()));
        labels.put(entry.getKey(), entry.getValue());
    }
    tagList.add(GcpStackUtil.getClusterTag(auth.getCloudContext()));
    tagList.add(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group));
    customTags.forEach((key, value) -> tagList.add(key + '-' + value));
    labels.putAll(customTags);
    tags.setItems(tagList);
    instance.setTags(tags);
    instance.setLabels(labels);
    Metadata metadata = new Metadata();
    metadata.setItems(new ArrayList<>());
    Items sshMetaData = new Items();
    sshMetaData.setKey("ssh-keys");
    sshMetaData.setValue(group.getInstanceAuthentication().getLoginUserName() + ':' + group.getInstanceAuthentication().getPublicKey());
    Items blockProjectWideSsh = new Items();
    blockProjectWideSsh.setKey("block-project-ssh-keys");
    blockProjectWideSsh.setValue("TRUE");
    Items startupScript = new Items();
    startupScript.setKey("startup-script");
    startupScript.setValue(image.getUserDataByType(group.getType()));
    metadata.getItems().add(sshMetaData);
    metadata.getItems().add(startupScript);
    metadata.getItems().add(blockProjectWideSsh);
    instance.setMetadata(metadata);
    Insert insert = compute.instances().insert(projectId, location.getAvailabilityZone().value(), instance);
    insert.setPrettyPrint(Boolean.TRUE);
    try {
        Operation operation = insert.execute();
        if (operation.getHttpErrorStatusCode() != null) {
            throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), buildableResource.get(0).getName());
        }
        return Collections.singletonList(createOperationAwareCloudResource(buildableResource.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
    }
}
Also used : Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Metadata(com.google.api.services.compute.model.Metadata) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) Operation(com.google.api.services.compute.model.Operation) Insert(com.google.api.services.compute.Compute.Instances.Insert) Items(com.google.api.services.compute.model.Metadata.Items) Tags(com.google.api.services.compute.model.Tags) Scheduling(com.google.api.services.compute.model.Scheduling) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Aggregations

InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)31 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)25 ArrayList (java.util.ArrayList)17 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)16 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)13 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)9 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)8 Group (com.sequenceiq.cloudbreak.cloud.model.Group)8 HashMap (java.util.HashMap)7 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)6 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)5 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)5 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)5 Instance (com.amazonaws.services.ec2.model.Instance)5 Reservation (com.amazonaws.services.ec2.model.Reservation)5 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)5 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)5 Test (org.junit.Test)5 InstanceGroupType (com.sequenceiq.cloudbreak.api.model.InstanceGroupType)4