Search in sources :

Example 1 with AccessConfig

use of com.google.api.services.compute.model.AccessConfig in project platformlayer by platformlayer.

the class GoogleComputeClient method findPublicIps.

public static List<String> findPublicIps(Instance instance) {
    List<String> ips = Lists.newArrayList();
    List<NetworkInterface> networkInterfaces = instance.getNetworkInterfaces();
    if (networkInterfaces == null) {
        networkInterfaces = Collections.emptyList();
    }
    for (NetworkInterface networkInterface : networkInterfaces) {
        List<AccessConfig> accessConfigList = networkInterface.getAccessConfigs();
        if (accessConfigList == null) {
            continue;
        }
        for (AccessConfig accessConfig : accessConfigList) {
            if (!Objects.equal(accessConfig.getType(), "ONE_TO_ONE_NAT")) {
                throw new IllegalStateException();
            }
            String natIp = accessConfig.getNatIP();
            if (!Strings.isNullOrEmpty(natIp)) {
                ips.add(natIp);
            }
        }
    }
    return ips;
}
Also used : NetworkInterface(com.google.api.services.compute.model.NetworkInterface) AccessConfig(com.google.api.services.compute.model.AccessConfig)

Example 2 with AccessConfig

use of com.google.api.services.compute.model.AccessConfig 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;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Instance(com.google.api.services.compute.model.Instance) TransportAddress(org.elasticsearch.common.transport.TransportAddress) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) IOException(java.io.IOException) AccessConfig(com.google.api.services.compute.model.AccessConfig) IOException(java.io.IOException) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) InetAddress(java.net.InetAddress)

Example 3 with AccessConfig

use of com.google.api.services.compute.model.AccessConfig 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);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Set(java.util.Set) Instance(com.google.api.services.compute.model.Instance) Metadata(com.google.api.services.compute.model.Metadata) MachineType(com.google.api.services.compute.model.MachineType) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Operation(com.google.api.services.compute.model.Operation) Items(com.google.api.services.compute.model.Metadata.Items) IOException(java.io.IOException) Image(com.google.api.services.compute.model.Image) AccessConfig(com.google.api.services.compute.model.AccessConfig) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) Items(com.google.api.services.compute.model.Metadata.Items) Tag(org.platformlayer.core.model.Tag) OperatingSystemRecipe(org.platformlayer.images.model.OperatingSystemRecipe) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

AccessConfig (com.google.api.services.compute.model.AccessConfig)3 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)3 Instance (com.google.api.services.compute.model.Instance)2 IOException (java.io.IOException)2 Image (com.google.api.services.compute.model.Image)1 MachineType (com.google.api.services.compute.model.MachineType)1 Metadata (com.google.api.services.compute.model.Metadata)1 Items (com.google.api.services.compute.model.Metadata.Items)1 Operation (com.google.api.services.compute.model.Operation)1 InetAddress (java.net.InetAddress)1 Set (java.util.Set)1 TimeoutException (java.util.concurrent.TimeoutException)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 TransportAddress (org.elasticsearch.common.transport.TransportAddress)1 Tag (org.platformlayer.core.model.Tag)1 DiskImageRecipe (org.platformlayer.images.model.DiskImageRecipe)1 OperatingSystemRecipe (org.platformlayer.images.model.OperatingSystemRecipe)1 OpsException (org.platformlayer.ops.OpsException)1