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;
}
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;
}
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;
}
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);
}
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;
}
Aggregations