use of com.google.api.services.compute.model.NetworkInterface in project elasticsearch by elastic.
the class GceUnicastHostsProvider method buildDynamicNodes.
/**
* We build the list of Nodes from GCE Management API
* Information can be cached using `cloud.gce.refresh_interval` property if needed.
*/
@Override
public List<DiscoveryNode> buildDynamicNodes() {
// We check that needed properties have been set
if (this.project == null || this.project.isEmpty() || this.zones == null || this.zones.isEmpty()) {
throw new IllegalArgumentException("one or more gce discovery settings are missing. " + "Check elasticsearch.yml file. Should have [" + GceInstancesService.PROJECT_SETTING.getKey() + "] and [" + GceInstancesService.ZONE_SETTING.getKey() + "].");
}
if (refreshInterval.millis() != 0) {
if (cachedDiscoNodes != null && (refreshInterval.millis() < 0 || (System.currentTimeMillis() - lastRefresh) < refreshInterval.millis())) {
if (logger.isTraceEnabled())
logger.trace("using cache to retrieve node list");
return cachedDiscoNodes;
}
lastRefresh = System.currentTimeMillis();
}
logger.debug("start building nodes list using GCE API");
cachedDiscoNodes = new ArrayList<>();
String ipAddress = null;
try {
InetAddress inetAddress = networkService.resolvePublishHostAddresses(null);
if (inetAddress != null) {
ipAddress = NetworkAddress.format(inetAddress);
}
} catch (IOException e) {
// We can't find the publish host address... Hmmm. Too bad :-(
// We won't simply filter it
}
try {
Collection<Instance> instances = gceInstancesService.instances();
if (instances == null) {
logger.trace("no instance found for project [{}], zones [{}].", this.project, this.zones);
return cachedDiscoNodes;
}
for (Instance instance : instances) {
String name = instance.getName();
String type = instance.getMachineType();
String status = instance.getStatus();
logger.trace("gce instance {} with status {} found.", name, status);
// See https://github.com/elastic/elasticsearch-cloud-gce/issues/3
if (Status.TERMINATED.equals(status)) {
logger.debug("node {} is TERMINATED. Ignoring", name);
continue;
}
// see if we need to filter by tag
boolean filterByTag = false;
if (tags.isEmpty() == false) {
logger.trace("start filtering instance {} with tags {}.", name, tags);
if (instance.getTags() == null || instance.getTags().isEmpty() || instance.getTags().getItems() == null || instance.getTags().getItems().isEmpty()) {
// If this instance have no tag, we filter it
logger.trace("no tags for this instance but we asked for tags. {} won't be part of the cluster.", name);
filterByTag = true;
} else {
// check that all tags listed are there on the instance
logger.trace("comparing instance tags {} with tags filter {}.", instance.getTags().getItems(), tags);
for (String tag : tags) {
boolean found = false;
for (String instancetag : instance.getTags().getItems()) {
if (instancetag.equals(tag)) {
found = true;
break;
}
}
if (!found) {
filterByTag = true;
break;
}
}
}
}
if (filterByTag) {
logger.trace("filtering out instance {} based tags {}, not part of {}", name, tags, instance.getTags() == null || instance.getTags().getItems() == null ? "" : instance.getTags());
continue;
} else {
logger.trace("instance {} with tags {} is added to discovery", name, tags);
}
String ip_public = null;
String ip_private = null;
List<NetworkInterface> interfaces = instance.getNetworkInterfaces();
for (NetworkInterface networkInterface : interfaces) {
if (ip_public == null) {
// Trying to get Public IP Address (For future use)
if (networkInterface.getAccessConfigs() != null) {
for (AccessConfig accessConfig : networkInterface.getAccessConfigs()) {
if (Strings.hasText(accessConfig.getNatIP())) {
ip_public = accessConfig.getNatIP();
break;
}
}
}
}
if (ip_private == null) {
ip_private = networkInterface.getNetworkIP();
}
// If we have both public and private, we can stop here
if (ip_private != null && ip_public != null)
break;
}
try {
if (ip_private.equals(ipAddress)) {
// We found the current node.
// We can ignore it in the list of DiscoveryNode
logger.trace("current node found. Ignoring {} - {}", name, ip_private);
} else {
String address = ip_private;
// Test if we have es_port metadata defined here
if (instance.getMetadata() != null && instance.getMetadata().containsKey("es_port")) {
Object es_port = instance.getMetadata().get("es_port");
logger.trace("es_port is defined with {}", es_port);
if (es_port instanceof String) {
address = address.concat(":").concat((String) es_port);
} else {
// Ignoring other values
logger.trace("es_port is instance of {}. Ignoring...", es_port.getClass().getName());
}
}
// ip_private is a single IP Address. We need to build a TransportAddress from it
// If user has set `es_port` metadata, we don't need to ping all ports
// we only limit to 1 addresses, makes no sense to ping 100 ports
TransportAddress[] addresses = transportService.addressesFromString(address, 1);
for (TransportAddress transportAddress : addresses) {
logger.trace("adding {}, type {}, address {}, transport_address {}, status {}", name, type, ip_private, transportAddress, status);
cachedDiscoNodes.add(new DiscoveryNode("#cloud-" + name + "-" + 0, transportAddress, emptyMap(), emptySet(), Version.CURRENT.minimumCompatibilityVersion()));
}
}
} catch (Exception e) {
final String finalIpPrivate = ip_private;
logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to add {}, address {}", name, finalIpPrivate), e);
}
}
} catch (Exception e) {
logger.warn("exception caught during discovery", e);
}
logger.debug("{} node(s) added", cachedDiscoNodes.size());
logger.debug("using dynamic discovery nodes {}", cachedDiscoNodes);
return cachedDiscoNodes;
}
use of com.google.api.services.compute.model.NetworkInterface in project druid by druid-io.
the class GceAutoScaler method ipToIdLookup.
/**
* Converts the IPs to IDs
*/
@Override
public List<String> ipToIdLookup(List<String> ips) {
log.info("Asked IPs -> IDs for: [%s]", String.join(",", ips));
if (ips.isEmpty()) {
return new ArrayList<>();
}
// actually IPs and can send IDs to this function instead
if (!InetAddresses.isInetAddress(ips.get(0))) {
log.debug("Not IPs, doing nothing");
return ips;
}
final String project = envConfig.getProjectId();
final String zone = envConfig.getZoneName();
try {
Compute computeService = createComputeService();
Compute.Instances.List request = computeService.instances().list(project, zone);
// Cannot filter by IP atm, see below
// request.setFilter(GceUtils.buildFilter(ips, "networkInterfaces[0].networkIP"));
List<String> instanceIds = new ArrayList<>();
InstanceList response;
do {
response = request.execute();
if (response.getItems() == null) {
continue;
}
for (Instance instance : response.getItems()) {
// by IP, see https://issuetracker.google.com/issues/73455339
for (NetworkInterface ni : instance.getNetworkInterfaces()) {
if (ips.contains(ni.getNetworkIP())) {
instanceIds.add(instance.getName());
}
}
}
request.setPageToken(response.getNextPageToken());
} while (response.getNextPageToken() != null);
log.debug("Converted to [%s]", String.join(",", instanceIds));
return instanceIds;
} catch (Exception e) {
log.error(e, "Unable to convert IPs to IDs.");
}
return new ArrayList<>();
}
use of com.google.api.services.compute.model.NetworkInterface in project druid by druid-io.
the class GceAutoScalerTest method makeInstance.
private Instance makeInstance(String name, String ip) {
Instance instance = new Instance();
instance.setName(name);
NetworkInterface net = new NetworkInterface();
net.setNetworkIP(ip);
instance.setNetworkInterfaces(Collections.singletonList(net));
return instance;
}
use of com.google.api.services.compute.model.NetworkInterface in project platformlayer by platformlayer.
the class GoogleComputeClient method createInstance.
public Instance createInstance(GoogleCloud cloud, MachineCreationRequest request, PublicKey sshPublicKey) throws OpsException {
try {
Image foundImage = null;
{
DiskImageRecipe recipe = null;
if (request.recipeId != null) {
recipe = platformLayerClient.getItem(request.recipeId, DiskImageRecipe.class);
}
OperatingSystemRecipe operatingSystem = null;
if (recipe != null) {
operatingSystem = recipe.getOperatingSystem();
}
log.info("Listing images to pick best image");
Iterable<Image> images = listImages(PROJECTID_GOOGLE);
// TODO: We need a better solution here!!
log.warn("Hard coding image names");
Set<String> imageNames = Sets.newHashSet("ubuntu-12-04-v20120621");
for (Image image : images) {
if (imageNames.contains(image.getName())) {
foundImage = image;
break;
}
}
if (foundImage == null) {
throw new IllegalArgumentException("Could not find image");
}
}
// GCE requires that the name comply with RFC1035, which I think means a valid DNS
// For now, just use a UUID, with a pl- prefix so it doesn't start with a number
// TODO: Fix this!
String instanceName = "pl-" + UUID.randomUUID().toString();
Operation createServerOperation;
{
Instance create = new Instance();
create.setName(instanceName);
create.setZone(buildZoneUrl(projectId, ZONE_US_CENTRAL1_A));
{
NetworkInterface networkInterface = new NetworkInterface();
networkInterface.setNetwork(buildNetworkUrl(projectId, "default"));
AccessConfig networkAccessConfig = new AccessConfig();
networkAccessConfig.setType("ONE_TO_ONE_NAT");
networkInterface.setAccessConfigs(Lists.newArrayList(networkAccessConfig));
create.setNetworkInterfaces(Lists.newArrayList(networkInterface));
}
Metadata metadata = new Metadata();
metadata.setItems(Lists.<Items>newArrayList());
create.setMetadata(metadata);
if (request.tags != null) {
for (Tag tag : request.tags) {
Metadata.Items meta = new Metadata.Items();
meta.setKey(tag.getKey());
meta.setValue(tag.getValue());
metadata.getItems().add(meta);
}
}
if (request.sshPublicKey != null) {
Metadata.Items meta = new Metadata.Items();
meta.setKey("sshKeys");
meta.setValue(USER_NAME + ":" + OpenSshUtils.serialize(sshPublicKey));
metadata.getItems().add(meta);
}
create.setImage(foundImage.getSelfLink());
MachineType flavor = getClosestInstanceType(request);
if (flavor == null) {
throw new OpsException("Cannot determine machine type for request");
}
create.setMachineType(flavor.getSelfLink());
if (request.securityGroups != null) {
// TODO: Reimplement if needed
throw new UnsupportedOperationException();
}
// if (createdSecurityGroup != null) {
// ServerForCreate.SecurityGroup serverSecurityGroup = new ServerForCreate.SecurityGroup();
// serverSecurityGroup.setName(createdSecurityGroup.getName());
// create.getSecurityGroups().add(serverSecurityGroup);
// }
// create.setConfigDrive(cloudBehaviours.useConfigDrive());
log.info("Launching new server: " + instanceName);
try {
createServerOperation = compute.instances().insert(projectId, create).execute();
} catch (IOException e) {
throw new OpsException("Error launching new instance", e);
}
}
log.info("Waiting for server to be ready");
createServerOperation = waitComplete(createServerOperation, 10, TimeUnit.MINUTES);
Instance created;
InstanceState state = null;
while (true) {
created = findInstanceByName(instanceName);
state = InstanceState.get(created);
log.info("Instance state: " + state);
if (state.isRunning()) {
break;
}
Thread.sleep(1000);
}
return created;
} catch (InterruptedException e) {
ExceptionUtils.handleInterrupted(e);
throw new OpsException("Error building server", e);
} catch (TimeoutException e) {
throw new OpsException("Timeout waiting for server build", e);
}
}
Aggregations