Search in sources :

Example 1 with InstanceStatus

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

the class MockInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext authenticatedContext, List<CloudInstance> vms) {
    try {
        MockCredentialView mockCredentialView = mockCredentialViewFactory.createCredetialView(authenticatedContext.getCloudCredential());
        LOGGER.info("collect instance statuses from mock spi, server address: " + mockCredentialView.getMockEndpoint());
        CloudVmInstanceStatus[] cloudVmInstanceStatusArray = Unirest.post(mockCredentialView.getMockEndpoint() + "/spi/cloud_instance_statuses").asObject(CloudVmInstanceStatus[].class).getBody();
        LOGGER.info("collected instance statuses: " + Arrays.toString(cloudVmInstanceStatusArray));
        List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
        for (CloudInstance instance : vms) {
            Optional<CloudVmInstanceStatus> vmInstanceStatusFromApi = Arrays.stream(cloudVmInstanceStatusArray).filter(instanceStatus -> instanceStatus.getCloudInstance().getTemplate().getPrivateId().equals(instance.getTemplate().getPrivateId())).findFirst();
            InstanceStatus instanceStatus = InstanceStatus.TERMINATED;
            if (vmInstanceStatusFromApi.isPresent()) {
                instanceStatus = vmInstanceStatusFromApi.get().getStatus();
            }
            CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(instance, instanceStatus);
            cloudVmInstanceStatuses.add(cloudVmInstanceStatus);
        }
        return cloudVmInstanceStatuses;
    } catch (UnirestException e) {
        throw new RuntimeException("can't convert to object", e);
    }
}
Also used : InstanceConnector(com.sequenceiq.cloudbreak.cloud.InstanceConnector) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) LoggerFactory(org.slf4j.LoggerFactory) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) List(java.util.List) Unirest(com.mashape.unirest.http.Unirest) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Service(org.springframework.stereotype.Service) Optional(java.util.Optional) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) UnirestException(com.mashape.unirest.http.exceptions.UnirestException)

Example 2 with InstanceStatus

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

the class StackToCloudStackConverter method buildInstanceGroups.

public List<Group> buildInstanceGroups(List<InstanceGroup> instanceGroups, StackAuthentication stackAuthentication, Collection<String> deleteRequests) {
    // sort by name to avoid shuffling the different instance groups
    Collections.sort(instanceGroups);
    List<Group> groups = new ArrayList<>();
    long privateId = getFirstValidPrivateId(instanceGroups);
    for (InstanceGroup instanceGroup : instanceGroups) {
        if (instanceGroup.getTemplate() != null) {
            CloudInstance skeleton = null;
            List<CloudInstance> instances = new ArrayList<>();
            Template template = instanceGroup.getTemplate();
            int desiredNodeCount = instanceGroup.getNodeCount();
            // existing instances
            for (InstanceMetaData metaData : instanceGroup.getInstanceMetaData()) {
                InstanceStatus status = getInstanceStatus(metaData, deleteRequests);
                instances.add(buildInstance(metaData, template, stackAuthentication, instanceGroup.getGroupName(), metaData.getPrivateId(), status));
            }
            // new instances
            int existingNodesSize = instances.size();
            if (existingNodesSize < desiredNodeCount) {
                for (long i = 0; i < desiredNodeCount - existingNodesSize; i++) {
                    instances.add(buildInstance(null, template, stackAuthentication, instanceGroup.getGroupName(), privateId++, InstanceStatus.CREATE_REQUESTED));
                }
            }
            if (existingNodesSize == desiredNodeCount && desiredNodeCount == 0) {
                skeleton = buildInstance(null, template, stackAuthentication, instanceGroup.getGroupName(), 0L, InstanceStatus.CREATE_REQUESTED);
            }
            Json attributes = instanceGroup.getAttributes();
            InstanceAuthentication instanceAuthentication = buildInstanceAuthentication(stackAuthentication);
            Map<String, Object> fields = attributes == null ? Collections.emptyMap() : attributes.getMap();
            groups.add(new Group(instanceGroup.getGroupName(), instanceGroup.getInstanceGroupType(), instances, buildSecurity(instanceGroup), skeleton, fields, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey()));
        }
    }
    return groups;
}
Also used : InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Json(com.sequenceiq.cloudbreak.domain.json.Json) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) StackTemplate(com.sequenceiq.cloudbreak.cloud.model.StackTemplate) Template(com.sequenceiq.cloudbreak.domain.Template) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus)

Example 3 with InstanceStatus

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

the class GcpInstanceResourceBuilder method checkInstances.

@Override
public List<CloudVmInstanceStatus> checkInstances(GcpContext context, AuthenticatedContext auth, List<CloudInstance> instances) {
    CloudInstance cloudInstance = instances.get(0);
    try {
        LOGGER.info("Checking instance: {}", cloudInstance);
        Operation operation = check(context, cloudInstance);
        boolean finished = operation == null || GcpStackUtil.isOperationFinished(operation);
        InstanceStatus status = finished ? context.isBuild() ? InstanceStatus.STARTED : InstanceStatus.STOPPED : InstanceStatus.IN_PROGRESS;
        LOGGER.info("Instance: {} status: {}", instances, status);
        return Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, status));
    } catch (Exception ignored) {
        LOGGER.info("Failed to check instance state of {}", cloudInstance);
        return Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS));
    }
}
Also used : CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Operation(com.google.api.services.compute.model.Operation) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) IOException(java.io.IOException) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)

Example 4 with InstanceStatus

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

the class GcpInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> statuses = new ArrayList<>();
    CloudCredential credential = ac.getCloudCredential();
    CloudContext cloudContext = ac.getCloudContext();
    Compute compute = GcpStackUtil.buildCompute(credential);
    for (CloudInstance instance : vms) {
        InstanceStatus status = InstanceStatus.UNKNOWN;
        try {
            Instance executeInstance = getInstance(cloudContext, credential, compute, instance.getInstanceId());
            if ("RUNNING".equals(executeInstance.getStatus())) {
                status = InstanceStatus.STARTED;
            } else if ("TERMINATED".equals(executeInstance.getStatus())) {
                status = InstanceStatus.STOPPED;
            }
        } catch (GoogleJsonResponseException e) {
            if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                status = InstanceStatus.TERMINATED;
            } else {
                LOGGER.warn(String.format("Instance %s is not reachable", instance), e);
            }
        } catch (IOException e) {
            LOGGER.warn(String.format("Instance %s is not reachable", instance), e);
        }
        statuses.add(new CloudVmInstanceStatus(instance, status));
    }
    return statuses;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) IOException(java.io.IOException)

Example 5 with InstanceStatus

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

the class InstanceMetaDataToCloudInstanceConverter method convert.

@Override
public CloudInstance convert(InstanceMetaData metaDataEnity) {
    InstanceGroup group = metaDataEnity.getInstanceGroup();
    Template template = metaDataEnity.getInstanceGroup().getTemplate();
    StackAuthentication stackAuthentication = group.getStack().getStackAuthentication();
    InstanceStatus status = getInstanceStatus(metaDataEnity);
    InstanceTemplate instanceTemplate = stackToCloudStackConverter.buildInstanceTemplate(template, group.getGroupName(), metaDataEnity.getPrivateId(), status);
    InstanceAuthentication instanceAuthentication = new InstanceAuthentication(stackAuthentication.getPublicKey(), stackAuthentication.getPublicKeyId(), stackAuthentication.getLoginUserName());
    Map<String, Object> params = new HashMap<>();
    params.put(CloudInstance.SUBNET_ID, metaDataEnity.getSubnetId());
    return new CloudInstance(metaDataEnity.getInstanceId(), instanceTemplate, instanceAuthentication, params);
}
Also used : StackAuthentication(com.sequenceiq.cloudbreak.domain.StackAuthentication) InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) HashMap(java.util.HashMap) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup) Template(com.sequenceiq.cloudbreak.domain.Template) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Aggregations

CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)5 InstanceStatus (com.sequenceiq.cloudbreak.cloud.model.InstanceStatus)5 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)3 ArrayList (java.util.ArrayList)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)2 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)2 InstanceGroup (com.sequenceiq.cloudbreak.domain.InstanceGroup)2 Template (com.sequenceiq.cloudbreak.domain.Template)2 IOException (java.io.IOException)2 Compute (com.google.api.services.compute.Compute)1 Instance (com.google.api.services.compute.model.Instance)1 Operation (com.google.api.services.compute.model.Operation)1 Unirest (com.mashape.unirest.http.Unirest)1 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)1 InstanceConnector (com.sequenceiq.cloudbreak.cloud.InstanceConnector)1 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)1