Search in sources :

Example 1 with OpenstackComputeMachine

use of org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine in project platformlayer by platformlayer.

the class OpenstackInstanceController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final OpenstackInstance model = OpsContext.get().getInstance(OpenstackInstance.class);
    CloudInstanceMapper instance;
    {
        instance = injected(CloudInstanceMapper.class);
        instance.instance = model;
        addChild(instance);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                OpenstackComputeMachine machine = OpsContext.get().getInstance(OpenstackComputeMachine.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 : OpsProvider(org.platformlayer.ops.OpsProvider) OpenstackInstance(org.platformlayer.service.cloud.openstack.model.OpenstackInstance) TagChanges(org.platformlayer.core.model.TagChanges) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine)

Example 2 with OpenstackComputeMachine

use of org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine in project platformlayer by platformlayer.

the class CloudInstanceMapper method doOperation.

@Handler
public void doOperation() throws OpsException, IOException {
    Tags instanceTags = instance.getTags();
    OpenstackCloud cloud = findCloud();
    if (cloud == null) {
        throw new OpsException("Could not find cloud");
    }
    OpenstackComputeClient computeClient = openstack.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));
            String serverName = buildServerName();
            Server created = openstack.createInstance(cloud, serverName, request);
            {
                Tag instanceTag = Tag.build(Tag.ASSIGNED, created.getId());
                platformLayer.addTag(instance.getKey(), instanceTag);
            }
            assignedInstanceIds.add(created.getId());
        }
    }
    if (assignedInstanceIds.isEmpty() && !OpsContext.isDelete()) {
        throw new OpsException("Instance not yet assigned");
    }
    Machine 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);
        Server server = openstack.findServerById(cloud, assignedInstanceId);
        if (server == null) {
            if (OpsContext.isConfigure()) {
                throw new OpsException("Unable to find assigned server: " + assignedInstanceId);
            }
        } else {
            server = openstack.ensureHasPublicIp(cloud, server);
            AsyncServerOperation powerOnOperation = openstack.ensurePoweredOn(cloud, server);
            if (powerOnOperation != null) {
                waitOperation(powerOnOperation);
            }
            machine = new OpenstackComputeMachine(openstack, cloud, server);
            SshKey sshKey = service.getSshKey();
            target = machine.getTarget(sshKey);
        }
    }
    if (!assignedInstanceIds.isEmpty() && OpsContext.isDelete()) {
        CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
        boolean supportsSecurityGroups = cloudBehaviours.supportsSecurityGroups();
        for (String instanceId : assignedInstanceIds) {
            Server server = openstack.findServerById(cloud, instanceId);
            if (server == null) {
                log.warn("Could not find assigned server: " + instanceId + ", ignoring");
                continue;
            }
            SecurityGroup securityGroup = null;
            if (supportsSecurityGroups) {
                securityGroup = openstackHelpers.getMachineSecurityGroup(computeClient, server);
            }
            AsyncServerOperation terminateOperation = openstack.terminateInstance(cloud, instanceId);
            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());
                }
            }
        }
    }
    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) OpenstackCloud(org.platformlayer.service.cloud.openstack.model.OpenstackCloud) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) MachineCreationRequest(org.platformlayer.ops.MachineCreationRequest) SecurityGroup(org.openstack.model.compute.SecurityGroup) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine) Machine(org.platformlayer.ops.Machine) SshKey(org.platformlayer.ops.helpers.SshKey) OpsTarget(org.platformlayer.ops.OpsTarget) OpenstackNotFoundException(org.openstack.client.OpenstackNotFoundException) CloudBehaviours(org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours) Tag(org.platformlayer.core.model.Tag) OpenstackComputeMachine(org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine) Tags(org.platformlayer.core.model.Tags) AsyncServerOperation(org.openstack.client.compute.AsyncServerOperation) Handler(org.platformlayer.ops.Handler)

Aggregations

OpenstackComputeMachine (org.platformlayer.service.cloud.openstack.ops.openstack.OpenstackComputeMachine)2 OpenstackNotFoundException (org.openstack.client.OpenstackNotFoundException)1 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)1 AsyncServerOperation (org.openstack.client.compute.AsyncServerOperation)1 SecurityGroup (org.openstack.model.compute.SecurityGroup)1 Server (org.openstack.model.compute.Server)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 Machine (org.platformlayer.ops.Machine)1 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)1 OpsException (org.platformlayer.ops.OpsException)1 OpsProvider (org.platformlayer.ops.OpsProvider)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 SshKey (org.platformlayer.ops.helpers.SshKey)1 OpenstackCloud (org.platformlayer.service.cloud.openstack.model.OpenstackCloud)1 OpenstackInstance (org.platformlayer.service.cloud.openstack.model.OpenstackInstance)1 CloudBehaviours (org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours)1