Search in sources :

Example 46 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class PackageRemoval method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    // Make sure openssh-server is manually installed (i.e. not through task-ssh-server)
    target.executeCommand("apt-get install openssh-server");
    // Only for wheezy? Squeeze has a depencency of install-info -> findutils
    Command command = Command.build("apt-get remove --yes aptitude tasksel tasksel-data man-db manpages libxapian22 libboost-iostreams1.49.0 info ");
    target.executeCommand(command);
    // Maybe:
    command = Command.build("apt-get remove --yes groff-base consolekit sane-utils  exim4-config installation-report");
    target.executeCommand(command);
    // Replace netcat with netcat6
    target.executeCommand("apt-get install netcat6");
    target.executeCommand("apt-get remove netcat-traditional");
    // We just want headless...
    command = Command.build("apt-get remove --yes dbus openjdk-7-jdk openjdk-7-jre  icedtea-7-jre-cacao icedtea-7-jre-jamvm");
    target.executeCommand(command);
    // Do we need python?? Does it still get installed??
    // apt-get remove python
    // Warnings??
    // apt-get remove install-info
    //
    // Squeeze:
    // apt-get remove aptitude tasksel tasksel-data man-db manpages libxapian22 info
    target.executeCommand("apt-get autoremove");
}
Also used : Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Example 47 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class ManagedSymlink method doConfigureValidate.

@Handler
public void doConfigureValidate(OpsTarget target) throws Exception {
    File filePath = getFilePath();
    FilesystemInfo fsInfo = target.getFilesystemInfoFile(filePath);
    boolean exists = (fsInfo != null);
    File symlinkTarget = getSymlinkTarget();
    boolean symlinkTargetMatch = false;
    if (fsInfo != null) {
        symlinkTargetMatch = Objects.equal(fsInfo.symlinkTarget, symlinkTarget.toString());
    }
    if (OpsContext.isConfigure()) {
        if (!exists) {
            target.symlink(symlinkTarget, filePath, false);
            createdNewFile = true;
            doUpdateAction(target);
        } else {
            if (!symlinkTargetMatch) {
                target.symlink(symlinkTarget, filePath, true);
            }
        }
        configureOwnerAndMode(target, fsInfo);
    // FilesystemInfo targetInfo = agent.getFilesystemInfoFile(getSymlinkTarget());
    // configureOwnerAndMode(agent, targetInfo, getSymlinkTarget());
    }
    if (OpsContext.isValidate()) {
        if (!exists) {
            OpsContext.get().addWarning(this, "DoesNotExist", "Symlink not found: " + filePath);
        } else if (!symlinkTargetMatch) {
            OpsContext.get().addWarning(this, "TargetMismatch", "Symlink points at wrong target: " + fsInfo);
            validateOwner(fsInfo);
        // Mode is meaningless on symlinks
        // validateMode(fsInfo);
        }
    }
    if (OpsContext.isDelete()) {
        if (exists) {
            target.rm(filePath);
            doUpdateAction(target);
        }
    }
}
Also used : File(java.io.File) Handler(org.platformlayer.ops.Handler)

Example 48 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class DiskImageController method build.

@Handler
public void build(DiskImage image) throws OpsException, IOException {
    String imageId = Tag.IMAGE_ID.findUnique(image.getTags());
    if (imageId == null) {
        // Check for existing image
        MachineProvider targetCloud = cloudHelpers.getCloud(image.cloud);
        DiskImageRecipe recipe = platformLayer.getItem(image.recipeId, DiskImageRecipe.class);
        ImageStore imageStore = cloud.getImageStore(targetCloud);
        List<CloudImage> existingImages = imageStore.findImages(Collections.<Tag>emptyList());
        for (CloudImage existingImage : existingImages) {
            // TODO: Fetch the parent, fetch the description, see if it's a match??
            log.info("Image found, but not know whether we can re-use: " + existingImage);
        }
    }
    if (imageId == null) {
        buildImage(image);
    }
}
Also used : MachineProvider(org.platformlayer.ops.machines.MachineProvider) DiskImageRecipe(org.platformlayer.images.model.DiskImageRecipe) CloudImage(org.platformlayer.ops.images.CloudImage) ImageStore(org.platformlayer.ops.images.ImageStore) Handler(org.platformlayer.ops.Handler)

Example 49 with Handler

use of org.platformlayer.ops.Handler 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)

Example 50 with Handler

use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.

the class EnsureFirewallIngress method handler.

@Handler
public void handler(OpenstackCloud cloud, OpenstackComputeMachine machine) throws OpsException, OpenstackException {
    CloudBehaviours cloudBehaviours = new CloudBehaviours(cloud);
    OpenstackComputeClient openstackComputeClient = cloudContext.getComputeClient(cloud);
    // Find the public address, although the OpenStack firewall may be blocking it
    publicAddress = machine.getNetworkPoint().getBestAddress(NetworkPoint.forPublicInternet());
    if (cloudBehaviours.supportsSecurityGroups()) {
        Server server = machine.getServer();
        SecurityGroup securityGroup = openstackHelpers.getMachineSecurityGroup(openstackComputeClient, server);
        securityGroup = openstackComputeClient.root().securityGroups().securityGroup(securityGroup.getId()).show();
        SecurityGroupRule matchingRule = findMatchingRule(securityGroup);
        if (OpsContext.isConfigure()) {
            if (matchingRule == null) {
                CreateSecurityGroupRuleRequest rule = new CreateSecurityGroupRuleRequest();
                rule.setCidr("0.0.0.0/0");
                rule.setIpProtocol("tcp");
                rule.setFromPort(model.publicPort);
                rule.setToPort(model.publicPort);
                rule.setParentGroupId(securityGroup.getId());
                openstackComputeClient.root().securityGroupRules().create(rule);
            }
        }
        if (OpsContext.isDelete()) {
            if (matchingRule != null) {
                openstackComputeClient.root().securityGroupRules().securityGroupRule(matchingRule.id).delete();
            }
        }
    }
}
Also used : CreateSecurityGroupRuleRequest(org.openstack.model.compute.CreateSecurityGroupRuleRequest) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) CloudBehaviours(org.platformlayer.service.cloud.openstack.ops.openstack.CloudBehaviours) SecurityGroupRule(org.openstack.model.compute.SecurityGroupRule) SecurityGroup(org.openstack.model.compute.SecurityGroup) Handler(org.platformlayer.ops.Handler)

Aggregations

Handler (org.platformlayer.ops.Handler)58 File (java.io.File)21 Command (org.platformlayer.ops.Command)17 OpsException (org.platformlayer.ops.OpsException)17 OpsTarget (org.platformlayer.ops.OpsTarget)17 Tag (org.platformlayer.core.model.Tag)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)5 SshKey (org.platformlayer.ops.helpers.SshKey)5 Machine (org.platformlayer.ops.Machine)4 PublicKey (java.security.PublicKey)3 Action (org.platformlayer.core.model.Action)3 EndpointInfo (org.platformlayer.core.model.EndpointInfo)3 ItemBase (org.platformlayer.core.model.ItemBase)3 Tags (org.platformlayer.core.model.Tags)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 Map (java.util.Map)2 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)2 SecurityGroup (org.openstack.model.compute.SecurityGroup)2 Server (org.openstack.model.compute.Server)2 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)2