Search in sources :

Example 1 with CloudInstanceMetaData

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

the class AwsMetadataCollector method collectGroupMetaData.

private Collection<CloudVmMetaDataStatus> collectGroupMetaData(AuthenticatedContext ac, AmazonAutoScaling amazonASClient, AmazonEC2 amazonEC2Client, AmazonCloudFormation amazonCFClient, String groupName, Iterable<CloudInstance> cloudInstances) {
    Collection<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
    String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, groupName);
    List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
    DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
    DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
    // contains instances with instanceId
    Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(cloudInstances);
    // contains instances with privateId (without instanceId)
    Queue<CloudInstance> untrackedInstances = untrackedInstances(cloudInstances);
    for (Reservation reservation : instancesResult.getReservations()) {
        LOGGER.info("Number of instances found in reservation: {}", reservation.getInstances().size());
        for (Instance instance : reservation.getInstances()) {
            String instanceId = instance.getInstanceId();
            CloudInstance cloudInstance = ensureInstanceTag(mapByInstanceId, instance, instanceId, untrackedInstances, amazonEC2Client);
            if (cloudInstance != null) {
                CloudInstanceMetaData md = new CloudInstanceMetaData(instance.getPrivateIpAddress(), instance.getPublicIpAddress());
                CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md);
                cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus);
            }
        }
    }
    return cloudVmMetaDataStatuses;
}
Also used : CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)

Example 2 with CloudInstanceMetaData

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

the class AzureMetadataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    CloudResource resource = azureUtils.getTemplateResource(resources);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    List<InstanceTemplate> templates = Lists.transform(vms, CloudInstance::getTemplate);
    String resourceName = resource.getName();
    Map<String, InstanceTemplate> templateMap = Maps.uniqueIndex(templates, from -> azureUtils.getPrivateInstanceId(resourceName, from.getGroupName(), Long.toString(from.getPrivateId())));
    try {
        for (Entry<String, InstanceTemplate> instance : templateMap.entrySet()) {
            AzureClient azureClient = authenticatedContext.getParameter(AzureClient.class);
            VirtualMachine vm = azureClient.getVirtualMachine(resourceName, instance.getKey());
            String subnetId = vm.getPrimaryNetworkInterface().primaryIPConfiguration().subnetName();
            String privateIp = null;
            String publicIp = null;
            Integer faultDomainCount = azureClient.getFaultDomainNumber(resourceName, vm.name());
            String platform = authenticatedContext.getCloudContext().getPlatform().value();
            String location = authenticatedContext.getCloudContext().getLocation().getRegion().value();
            String hostgroupNm = instance.getValue().getGroupName();
            StringBuilder localityIndicatorBuilder = new StringBuilder();
            localityIndicatorBuilder.append(LOCALITY_SEPARATOR).append(platform).append(LOCALITY_SEPARATOR).append(location).append(LOCALITY_SEPARATOR).append(resourceName).append(LOCALITY_SEPARATOR).append(hostgroupNm).append(LOCALITY_SEPARATOR).append(faultDomainCount);
            AzureUtils.removeBlankSpace(localityIndicatorBuilder);
            List<String> networkInterfaceIdList = vm.networkInterfaceIds();
            for (String networkInterfaceId : networkInterfaceIdList) {
                NetworkInterface networkInterface = azureClient.getNetworkInterfaceById(networkInterfaceId);
                privateIp = networkInterface.primaryPrivateIP();
                PublicIPAddress publicIpAddress = networkInterface.primaryIPConfiguration().getPublicIPAddress();
                List<LoadBalancerBackend> backends = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerBackends();
                List<LoadBalancerInboundNatRule> inboundNatRules = networkInterface.primaryIPConfiguration().listAssociatedLoadBalancerInboundNatRules();
                if (!backends.isEmpty() || !inboundNatRules.isEmpty()) {
                    publicIp = azureClient.getLoadBalancerIps(resource.getName(), azureUtils.getLoadBalancerId(resource.getName())).get(0);
                }
                if (publicIpAddress != null && publicIpAddress.ipAddress() != null) {
                    publicIp = publicIpAddress.ipAddress();
                }
            }
            String instanceId = instance.getKey();
            CloudInstanceMetaData md = new CloudInstanceMetaData(privateIp, publicIp, faultDomainCount == null ? null : localityIndicatorBuilder.toString());
            InstanceTemplate template = templateMap.get(instanceId);
            if (template != null) {
                Map<String, Object> params = new HashMap<>(1);
                params.put(CloudInstance.SUBNET_ID, subnetId);
                // TODO use shhkey here
                CloudInstance cloudInstance = new CloudInstance(instanceId, template, null, params);
                CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
                results.add(new CloudVmMetaDataStatus(status, md));
            }
        }
    } catch (RuntimeException e) {
        throw new CloudConnectorException(e.getMessage(), e);
    }
    return results;
}
Also used : HashMap(java.util.HashMap) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) LoadBalancerInboundNatRule(com.microsoft.azure.management.network.LoadBalancerInboundNatRule) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) NetworkInterface(com.microsoft.azure.management.network.NetworkInterface) LoadBalancerBackend(com.microsoft.azure.management.network.LoadBalancerBackend) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 3 with CloudInstanceMetaData

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

the class GcpMetadataCollector method getCloudVmMetaDataStatus.

private CloudVmMetaDataStatus getCloudVmMetaDataStatus(AuthenticatedContext authenticatedContext, CloudResource cloudResource, CloudInstance matchedInstance) {
    CloudVmMetaDataStatus cloudVmMetaDataStatus;
    if (cloudResource != null) {
        CloudInstance cloudInstance = new CloudInstance(cloudResource.getName(), matchedInstance.getTemplate(), matchedInstance.getAuthentication());
        try {
            CloudCredential credential = authenticatedContext.getCloudCredential();
            CloudContext cloudContext = authenticatedContext.getCloudContext();
            Compute compute = GcpStackUtil.buildCompute(credential);
            Instance executeInstance = getInstance(cloudContext, credential, compute, cloudResource.getName());
            String privateIp = executeInstance.getNetworkInterfaces().get(0).getNetworkIP();
            String publicIp = null;
            List<AccessConfig> acl = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs();
            if (acl != null && acl.get(0) != null) {
                publicIp = executeInstance.getNetworkInterfaces().get(0).getAccessConfigs().get(0).getNatIP();
            }
            CloudInstanceMetaData metaData = new CloudInstanceMetaData(privateIp, publicIp);
            CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
            cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, metaData);
        } catch (IOException e) {
            LOGGER.warn(String.format("Instance %s is not reachable", cloudResource.getName()), e);
            CloudVmInstanceStatus status = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.UNKNOWN);
            cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
        }
    } else {
        CloudVmInstanceStatus status = new CloudVmInstanceStatus(matchedInstance, InstanceStatus.TERMINATED);
        cloudVmMetaDataStatus = new CloudVmMetaDataStatus(status, CloudInstanceMetaData.EMPTY_METADATA);
    }
    return cloudVmMetaDataStatus;
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Instance(com.google.api.services.compute.model.Instance) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Compute(com.google.api.services.compute.Compute) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) IOException(java.io.IOException) AccessConfig(com.google.api.services.compute.model.AccessConfig)

Example 4 with CloudInstanceMetaData

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

the class PortApiExtractor method extractMetadata.

@Override
public CloudInstanceMetaData extractMetadata(OSClient<?> client, Server server, String instanceId) {
    String hypervisor = hypervisorExtractor.getHypervisor(server);
    LOGGER.debug("Address map was empty, trying to extract ips");
    List<? extends Port> ports = client.networking().port().list(getPortListOptions(instanceId));
    String portId = ports.get(0).getId();
    List<? extends NetFloatingIP> floatingIps = client.networking().floatingip().list(getFloatingIpListOptions(portId));
    NetFloatingIP ips = floatingIps.get(0);
    LOGGER.info("PrivateIp of instance: {} is {}", server.getName(), ips.getFixedIpAddress());
    LOGGER.info("FloatingIp of instance: {} is {}", server.getName(), ips.getFloatingIpAddress());
    return new CloudInstanceMetaData(ips.getFixedIpAddress(), ips.getFloatingIpAddress(), hypervisor);
}
Also used : CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) NetFloatingIP(org.openstack4j.model.network.NetFloatingIP)

Example 5 with CloudInstanceMetaData

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

the class OpenStackNativeMetaDataCollector method collect.

@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
    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);
    List<CloudVmMetaDataStatus> results = new ArrayList<>();
    for (CloudResource resource : resources) {
        if (resource.getType() == ResourceType.OPENSTACK_INSTANCE) {
            String instanceUUID = resource.getReference();
            Server server = client.compute().servers().get(instanceUUID);
            if (server != null) {
                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) CloudInstanceMetaData(com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) 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