Search in sources :

Example 6 with CloudInstanceMetaData

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

the class TestApplicationContext method cloudConnectors.

@Bean
public CloudConnector cloudConnectors() throws Exception {
    CloudResource resource = new Builder().type(ResourceType.HEAT_STACK).name("ref").build();
    when(cloudConnector.authentication()).thenReturn(authenticator);
    when(cloudConnector.credentials()).thenReturn(credentialConnector);
    when(credentialConnector.create(any(AuthenticatedContext.class))).thenReturn(new CloudCredentialStatus(null, CredentialStatus.CREATED));
    when(credentialConnector.delete(any(AuthenticatedContext.class))).thenReturn(new CloudCredentialStatus(null, CredentialStatus.DELETED));
    when(authenticator.authenticate(any(), any())).thenReturn(g.createAuthenticatedContext());
    when(cloudConnector.platform()).thenReturn(Platform.platform("TESTCONNECTOR"));
    when(cloudConnector.variant()).thenReturn(Variant.variant("TESTVARIANT"));
    when(cloudConnector.resources()).thenReturn(resourceConnector);
    when(cloudConnector.instances()).thenReturn(instanceConnector);
    when(cloudConnector.metadata()).thenReturn(collector);
    when(resourceConnector.launch(any(), any(), any(), any(), anyLong())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.CREATED)));
    when(resourceConnector.terminate(any(), any(), any())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.DELETED)));
    when(resourceConnector.update(any(), any(), any())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.UPDATED)));
    when(resourceConnector.upscale(any(), any(), any())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.UPDATED)));
    when(resourceConnector.downscale(any(), any(), any(), anyList(), anyMap())).thenReturn(Collections.singletonList(new CloudResourceStatus(resource, ResourceStatus.UPDATED)));
    when(instanceConnector.check(any(), any())).thenReturn(Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.STARTED)));
    CloudVmInstanceStatus collectInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.IN_PROGRESS);
    when(collector.collect(any(), any(), any())).thenReturn(Collections.singletonList(new CloudVmMetaDataStatus(collectInstanceStatus, new CloudInstanceMetaData("privateIp", "publicIp", "hypervisor"))));
    when(instanceConnector.start(any(), any(), any())).thenReturn(Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.STARTED)));
    when(instanceConnector.stop(any(), any(), any())).thenReturn(Collections.singletonList(new CloudVmInstanceStatus(cloudInstance, InstanceStatus.STOPPED)));
    when(instanceConnector.getConsoleOutput(any(), eq(cloudInstance))).thenReturn(g.getSshFingerprint() + "    RSA/n-----END SSH HOST KEY FINGERPRINTS-----");
    when(instanceConnector.getConsoleOutput(any(), eq(cloudInstanceBad))).thenReturn("XYZ    RSA/n-----END SSH HOST KEY FINGERPRINTS-----");
    return cloudConnector;
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Bean(org.springframework.context.annotation.Bean)

Example 7 with CloudInstanceMetaData

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

the class YarnMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    try {
        YarnClient yarnClient = yarnClientUtil.createYarnClient(authenticatedContext);
        CloudResource yarnApplication = getYarnApplcationResource(resources);
        ApplicationDetailRequest applicationDetailRequest = new ApplicationDetailRequest();
        applicationDetailRequest.setName(yarnApplication.getName());
        ResponseContext responseContext = yarnClient.getApplicationDetail(applicationDetailRequest);
        if (responseContext.getStatusCode() == YarnResourceConstants.HTTP_SUCCESS) {
            ApplicationDetailResponse applicationDetailResponse = (ApplicationDetailResponse) responseContext.getResponseObject();
            ListMultimap<String, CloudInstance> groupInstancesByInstanceGroup = groupInstancesByInstanceGroup(vms);
            ListMultimap<String, Container> groupContainersByInstanceGroup = groupContainersByInstanceGroup(applicationDetailResponse.getContainers());
            List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = Lists.newArrayList();
            for (String groupName : groupContainersByInstanceGroup.keySet()) {
                List<CloudInstance> groupInstances = groupInstancesByInstanceGroup.get(groupName);
                List<Container> groupContainers = groupContainersByInstanceGroup.get(groupName);
                Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(groupInstances);
                Queue<CloudInstance> untrackedInstances = untrackedInstances(groupInstances);
                for (Container container : groupContainers) {
                    String containerId = container.getId();
                    CloudInstance cloudInstance = mapByInstanceId.get(containerId);
                    if (cloudInstance == null) {
                        if (!untrackedInstances.isEmpty()) {
                            cloudInstance = untrackedInstances.remove();
                            cloudInstance = new CloudInstance(containerId, cloudInstance.getTemplate(), cloudInstance.getAuthentication());
                        }
                    }
                    if (cloudInstance != null) {
                        String ipAddress = container.getIp();
                        CloudInstanceMetaData md = new CloudInstanceMetaData(ipAddress, ipAddress);
                        CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                        CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md);
                        cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus);
                    }
                }
            }
            return cloudVmMetaDataStatuses;
        } else {
            ApplicationErrorResponse errorResponse = responseContext.getResponseError();
            throw new CloudConnectorException(String.format("Failed to get yarn application details: HTTP Return: %d Error: %s", responseContext.getStatusCode(), errorResponse == null ? "unknown" : errorResponse.getDiagnostics()));
        }
    } catch (MalformedURLException ex) {
        throw new CloudConnectorException("Failed to get yarn application details", ex);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ApplicationErrorResponse(com.sequenceiq.cloudbreak.cloud.yarn.client.model.response.ApplicationErrorResponse) YarnClient(com.sequenceiq.cloudbreak.cloud.yarn.client.YarnClient) ApplicationDetailResponse(com.sequenceiq.cloudbreak.cloud.yarn.client.model.response.ApplicationDetailResponse) Container(com.sequenceiq.cloudbreak.cloud.yarn.client.model.core.Container) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) ApplicationDetailRequest(com.sequenceiq.cloudbreak.cloud.yarn.client.model.request.ApplicationDetailRequest) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) ResponseContext(com.sequenceiq.cloudbreak.cloud.yarn.client.model.response.ResponseContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 8 with CloudInstanceMetaData

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

the class MetadataSetupService method saveInstanceMetaData.

public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) {
    int newInstances = 0;
    boolean ambariServerFound = false;
    Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataRepository.findNotTerminatedForStack(stack.getId());
    boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY);
    for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) {
        CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance();
        CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData();
        long timeInMillis = Calendar.getInstance().getTimeInMillis();
        Long privateId = cloudInstance.getTemplate().getPrivateId();
        String instanceId = cloudInstance.getInstanceId();
        InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId);
        if (instanceMetaDataEntry.getInstanceId() == null) {
            newInstances++;
        }
        // CB 1.0.x clusters do not have private id thus we cannot correlate them with instance groups thus keep the original one
        InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup();
        String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName();
        InstanceGroup instanceGroup = instanceGroupRepository.findOneByGroupNameInStack(stack.getId(), group);
        instanceMetaDataEntry.setPrivateIp(md.getPrivateIp());
        instanceMetaDataEntry.setPublicIp(md.getPublicIp());
        instanceMetaDataEntry.setSshPort(md.getSshPort());
        instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator());
        instanceMetaDataEntry.setInstanceGroup(instanceGroup);
        instanceMetaDataEntry.setInstanceId(instanceId);
        instanceMetaDataEntry.setPrivateId(privateId);
        instanceMetaDataEntry.setStartDate(timeInMillis);
        instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID));
        if (instanceMetaDataEntry.getInstanceMetadataType() == null) {
            if (ig != null) {
                if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) {
                    if (!primaryIgSelected) {
                        primaryIgSelected = true;
                        instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY);
                    } else {
                        instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY);
                    }
                } else {
                    instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE);
                }
            } else {
                instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE);
            }
        }
        if (!ambariServerFound && InstanceGroupType.GATEWAY.equals(instanceGroup.getInstanceGroupType())) {
            instanceMetaDataEntry.setAmbariServer(Boolean.TRUE);
            ambariServerFound = true;
        } else {
            instanceMetaDataEntry.setAmbariServer(Boolean.FALSE);
        }
        if (status != null) {
            instanceMetaDataEntry.setInstanceStatus(status);
        }
        instanceMetaDataRepository.save(instanceMetaDataEntry);
    }
    return newInstances;
}
Also used : InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) InstanceGroup(com.sequenceiq.cloudbreak.domain.InstanceGroup)

Example 9 with CloudInstanceMetaData

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

the class MockInstanceUtil method addInstance.

public void addInstance(Map<String, CloudVmMetaDataStatus> instanceMap, int numberOfAddedInstances) {
    ServerAddressGenerator serverAddressGenerator = new ServerAddressGenerator(numberOfAddedInstances);
    serverAddressGenerator.setFrom(instanceMap.size());
    serverAddressGenerator.iterateOver((address, number) -> {
        String instanceId = "instance-" + address;
        InstanceTemplate instanceTemplate = new InstanceTemplate("medium", "group", Integer.toUnsignedLong(number), new ArrayList<>(), InstanceStatus.CREATED, null, 0L);
        InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
        CloudInstance cloudInstanceWithId = new CloudInstance(instanceId, instanceTemplate, instanceAuthentication);
        CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstanceWithId, InstanceStatus.STARTED);
        CloudInstanceMetaData cloudInstanceMetaData = new CloudInstanceMetaData(address, mockServerAddress, sshPort, "MOCK");
        CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, cloudInstanceMetaData);
        instanceMap.put(instanceId, cloudVmMetaDataStatus);
    });
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) ServerAddressGenerator(com.sequenceiq.it.util.ServerAddressGenerator) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 10 with CloudInstanceMetaData

use of com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData 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)

Aggregations

CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)12 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)10 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)9 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)9 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)5 ArrayList (java.util.ArrayList)4 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)2 ServerAddressGenerator (com.sequenceiq.it.util.ServerAddressGenerator)2 Server (org.openstack4j.model.compute.Server)2 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 Instance (com.amazonaws.services.ec2.model.Instance)1 Reservation (com.amazonaws.services.ec2.model.Reservation)1 Compute (com.google.api.services.compute.Compute)1 AccessConfig (com.google.api.services.compute.model.AccessConfig)1 Instance (com.google.api.services.compute.model.Instance)1 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)1 LoadBalancerBackend (com.microsoft.azure.management.network.LoadBalancerBackend)1