Search in sources :

Example 1 with GoogleComputeMachine

use of org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine in project platformlayer by platformlayer.

the class GoogleCloudInstanceController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final GoogleCloudInstance model = OpsContext.get().getInstance(GoogleCloudInstance.class);
    PublicKey rootPublicKey;
    try {
        rootPublicKey = OpenSshUtils.readSshPublicKey(model.sshPublicKey);
    } catch (IOException e) {
        throw new OpsException("Cannot read SSH key");
    }
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = model;
        addChild(instance);
    }
    {
        SshAuthorizedKey authorizeRoot = instance.addChild(SshAuthorizedKey.class);
        authorizeRoot.user = "root";
        authorizeRoot.publicKey = rootPublicKey;
    }
    {
        instance.addChild(ConfigureSshd.class);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                GoogleComputeMachine machine = OpsContext.get().getInstance(GoogleComputeMachine.class);
                TagChanges tagChanges = new TagChanges();
                tagChanges.addTags.add(Tag.INSTANCE_KEY.build(model.getKey()));
                tagChanges.addTags.addAll(machine.buildAddressTags());
                return tagChanges;
            }
        };
        instance.addChild(Tagger.build(model, tagChanges));
    }
// Note: We can't bootstrap an instance, because we can't log in to it,
// because the public key is not our service's public key
// if (model.publicPorts != null) {
// for (int publicPort : model.publicPorts) {
// PublicPorts publicPortForward = injected(PublicPorts.class);
// publicPortForward.port = publicPort;
// publicPortForward.backendItem = model;
// kvm.addChild(publicPortForward);
// }
// }
}
Also used : OpsException(org.platformlayer.ops.OpsException) SshAuthorizedKey(org.platformlayer.ops.ssh.SshAuthorizedKey) OpsProvider(org.platformlayer.ops.OpsProvider) PublicKey(java.security.PublicKey) IOException(java.io.IOException) TagChanges(org.platformlayer.core.model.TagChanges) ConfigureSshd(org.platformlayer.ops.bootstrap.ConfigureSshd) GoogleComputeMachine(org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance)

Example 2 with GoogleComputeMachine

use of org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine 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)

Aggregations

PublicKey (java.security.PublicKey)2 OpsException (org.platformlayer.ops.OpsException)2 GoogleCloudInstance (org.platformlayer.service.cloud.google.model.GoogleCloudInstance)2 GoogleComputeMachine (org.platformlayer.service.cloud.google.ops.compute.GoogleComputeMachine)2 Instance (com.google.api.services.compute.model.Instance)1 Operation (com.google.api.services.compute.model.Operation)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 Tag (org.platformlayer.core.model.Tag)1 TagChanges (org.platformlayer.core.model.TagChanges)1 Tags (org.platformlayer.core.model.Tags)1 Handler (org.platformlayer.ops.Handler)1 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)1 OpsProvider (org.platformlayer.ops.OpsProvider)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 SshOpsTarget (org.platformlayer.ops.SshOpsTarget)1 ConfigureSshd (org.platformlayer.ops.bootstrap.ConfigureSshd)1 SshKey (org.platformlayer.ops.helpers.SshKey)1 SshAuthorizedKey (org.platformlayer.ops.ssh.SshAuthorizedKey)1