Search in sources :

Example 41 with Handler

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

the class OwnedItem method handler.

@Handler
public void handler() throws OpsException {
    T itemTemplate = buildItemTemplate();
    Tag uniqueTag = getUniqueTag(itemTemplate);
    if (OpsContext.isConfigure()) {
        try {
            item = platformLayer.putItemByTag(itemTemplate, uniqueTag);
        } catch (PlatformLayerClientException e) {
            throw new OpsException("Error creating owned item", e);
        }
    }
    if (OpsContext.isDelete()) {
        List<? extends ItemBase> items = platformLayer.listItems(itemTemplate.getClass(), TagFilter.byTag(uniqueTag));
        if (items.size() != 0) {
            if (items.size() != 1) {
                throw new OpsException("Found multiple items with unique tag: " + uniqueTag);
            }
            item = (T) items.get(0);
            platformLayer.ensureDeleted(item);
        }
    }
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) Handler(org.platformlayer.ops.Handler)

Example 42 with Handler

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

the class PosixUser method doOperation.

@Handler
public void doOperation() throws OpsException {
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    // TODO: Only if user not found
    {
        Command command = Command.build("adduser");
        command.addLiteral("--system");
        command.addLiteral("--no-create-home");
        if (shell != null) {
            command.addLiteral("--shell").addFile(shell);
        }
        if (!Strings.isNullOrEmpty(primaryGroup)) {
            command.addLiteral("--ingroup");
            command.addQuoted(primaryGroup);
        }
        command.addQuoted(userName);
        target.executeCommand(command);
    }
    for (String secondaryGroup : secondaryGroups) {
        Command command = Command.build("adduser");
        command.addQuoted(userName);
        command.addQuoted(secondaryGroup);
        target.executeCommand(command);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Example 43 with Handler

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

the class TagFromChildren method handler.

@Handler
public void handler() throws OpsException {
    if (OpsContext.isConfigure()) {
        for (OwnedItem<?> childServer : parentController.getChildren(OwnedItem.class)) {
            ItemBase server = childServer.getItem();
            if (server == null) {
                // Right now, we have to go through a retry cycle
                throw new OpsException("Child server not ready");
            }
            List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(server.getTags(), port);
            if (endpoints.isEmpty()) {
                // TODO: Cope in future e.g. if we only need one of two in a cluster
                throw new OpsException("Child server not ready");
            }
            for (EndpointInfo endpoint : endpoints) {
                platformLayer.addTag(parentItem.getKey(), endpoint.toTag());
            }
        }
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) Handler(org.platformlayer.ops.Handler)

Example 44 with Handler

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

the class OperationInvoker method findBetter.

private Method findBetter(Method l, Method r, BindingScope scope) {
    Handler lHandler = l.getAnnotation(Handler.class);
    Handler rHandler = r.getAnnotation(Handler.class);
    if (lHandler != null && rHandler == null) {
        return l;
    }
    if (rHandler != null && lHandler == null) {
        return r;
    }
    if (rHandler != null && lHandler != null) {
        Action action = scope.getInstance(Action.class);
        if (action != null) {
            boolean lExplicit = canHandleAction(lHandler, action, false);
            boolean rExplicit = canHandleAction(rHandler, action, false);
            if (lExplicit && !rExplicit) {
                return l;
            }
            if (rExplicit && !lExplicit) {
                return r;
            }
            // TODO: How do we get here??
            boolean lWildcard = canHandleAction(lHandler, action, true);
            boolean rWildcard = canHandleAction(lHandler, action, true);
            if (lWildcard && !rWildcard) {
                return l;
            }
            if (rWildcard && !lWildcard) {
                return r;
            }
        } else {
            log.warn("No OperationType in scope");
        }
    }
    // Favor the more derived class
    Class<?> declaringClassL = l.getDeclaringClass();
    Class<?> declaringClassR = r.getDeclaringClass();
    if (!declaringClassL.equals(declaringClassR)) {
        if (declaringClassL.isAssignableFrom(declaringClassR)) {
            // R is derived from L
            return r;
        }
        if (declaringClassR.isAssignableFrom(declaringClassL)) {
            // L is derived from R
            return l;
        }
    }
    throw new IllegalArgumentException("Cannot compare " + l + " with " + r);
}
Also used : Action(org.platformlayer.core.model.Action) Handler(org.platformlayer.ops.Handler)

Example 45 with Handler

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

the class EndpointDnsRecord method handler.

@Handler
public void handler() throws OpsException {
    PublicEndpointBase endpoint = endpointProvider.get();
    if (OpsContext.isConfigure()) {
        // Create a DNS record
        Tag parentTag = Tag.buildParentTag(endpoint.getKey());
        List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(endpoint.getTags(), destinationPort);
        if (endpoints.isEmpty()) {
            throw new OpsException("Cannot find endpoint for port: " + destinationPort);
        }
        DnsRecord record = new DnsRecord();
        record.setDnsName(dnsName);
        for (EndpointInfo endpointInfo : endpoints) {
            record.getAddress().add(endpointInfo.publicIp);
        }
        record.getTags().add(parentTag);
        record.setKey(PlatformLayerKey.fromId(dnsName));
        try {
            platformLayerClient.putItemByTag((ItemBase) record, parentTag);
        } catch (PlatformLayerClientException e) {
            throw new OpsException("Error registering persistent instance", e);
        }
    }
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase) DnsRecord(org.platformlayer.dns.model.DnsRecord) 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