Search in sources :

Example 21 with Instance

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

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tags instanceTags = instance.getTags();
    GoogleCloud cloud = findCloud();
    if (cloud == null) {
        throw new OpsException("Could not find cloud");
    }
    GoogleComputeClient computeClient = googleComputeClientFactory.getComputeClient(cloud);
    getRecursionState().pushChildScope(cloud);
    List<String> assignedInstanceIds = instanceTags.findAll(Tag.ASSIGNED);
    if (assignedInstanceIds.isEmpty()) {
        if (createInstance && !OpsContext.isDelete()) {
            MachineCreationRequest request = buildMachineCreationRequest();
            PlatformLayerKey instanceKey = instance.getKey();
            request.tags.add(Tag.buildParentTag(instanceKey));
            PublicKey servicePublicKey = service.getSshKey().getKeyPair().getPublic();
            Instance created = computeClient.createInstance(cloud, request, servicePublicKey);
            {
                Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getName());
                platformLayer.addTag(instance.getKey(), instanceTag);
            }
            assignedInstanceIds.add(created.getName());
        }
    }
    if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
        throw new OpsException("Instance not yet assigned");
    }
    GoogleComputeMachine machine = null;
    OpsTarget target = null;
    if (!assignedInstanceIds.isEmpty()) {
        if (assignedInstanceIds.size() != 1) {
            log.warn("Multiple instance ids found: " + assignedInstanceIds);
        }
        // We just take the first instance id
        String assignedInstanceId = Iterables.getFirst(assignedInstanceIds, null);
        Instance server = computeClient.findInstanceByName(assignedInstanceId);
        if (server == null) {
            if (OpsContext.isConfigure()) {
                throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
            }
        } else {
            server = computeClient.ensureHasPublicIp(server);
            machine = new GoogleComputeMachine(computeClient, cloud, server);
            SshKey sshKey = service.getSshKey();
            target = machine.getTarget(GoogleComputeClient.USER_NAME, sshKey.getKeyPair());
            // We need to use sudo while we set up root access
            ((SshOpsTarget) target).setEnsureRunningAsRoot(true);
        }
    }
    if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
        for (String instanceId : assignedInstanceIds) {
            Instance server = computeClient.findInstanceByName(instanceId);
            if (server == null) {
                log.warn("Could not find assigned server: " + instanceId + ", ignoring");
                continue;
            }
            // TODO: Remove associated firewall rules
            log.warn("Deleting firewall rules not yet implemented");
            // SecurityGroup securityGroup = null;
            // if (supportsSecurityGroups) {
            // securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
            // }
            Operation terminateOperation = computeClient.terminateInstance(instanceId);
            try {
                computeClient.waitComplete(terminateOperation, 5, TimeUnit.MINUTES);
            } catch (TimeoutException e) {
                throw new OpsException("Timeout while waiting for instance termination", e);
            }
        // if (securityGroup != null) {
        // // We need to terminate the instance before we delete the security group it uses
        // if (terminateOperation != null) {
        // waitOperation(terminateOperation);
        // }
        // 
        // try {
        // log.info("Deleting security group: " + securityGroup.getId());
        // computeClient.root().securityGroups().securityGroup(securityGroup.getId()).delete();
        // } catch (OpenstackNotFoundException e) {
        // log.info("Ignoring not-found error while deleting security group: " + securityGroup.getId());
        // }
        // }
        }
        if (machine != null) {
            machine.refreshState();
        }
    }
    RecursionState recursion = getRecursionState();
    if (OpsContext.isDelete() && machine == null) {
        recursion.setPreventRecursion(true);
    } else {
        recursion.pushChildScope(machine);
        recursion.pushChildScope(target);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Instance(com.google.api.services.compute.model.Instance) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance) PublicKey(java.security.PublicKey) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) Operation(com.google.api.services.compute.model.Operation) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) SshOpsTarget(org.platformlayer.ops.SshOpsTarget) GoogleComputeClient(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeClient) GoogleCloud(org.platformlayer.service.cloud.google.model.GoogleCloud) Tag(org.platformlayer.core.model.Tag) Tags(org.platformlayer.core.model.Tags) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) TimeoutException(java.util.concurrent.TimeoutException) Handler(org.platformlayer.ops.Handler)

Example 22 with Instance

use of com.google.api.services.compute.model.Instance 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

Instance (com.google.api.services.compute.model.Instance)22 InstanceList (com.google.api.services.compute.model.InstanceList)9 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)6 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)6 Compute (com.google.api.services.compute.Compute)5 AccessConfig (com.google.api.services.compute.model.AccessConfig)5 Operation (com.google.api.services.compute.model.Operation)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)3 Metadata (com.google.api.services.compute.model.Metadata)3 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)3 OpsException (org.platformlayer.ops.OpsException)3 Instances (com.google.api.services.compute.Compute.Instances)2 AttachedDiskInitializeParams (com.google.api.services.compute.model.AttachedDiskInitializeParams)2 Items (com.google.api.services.compute.model.Metadata.Items)2 ServiceAccount (com.google.api.services.compute.model.ServiceAccount)2 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2