Search in sources :

Example 21 with Handler

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

the class NetworkTunDevice method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        Command findCommand = Command.build("/sbin/ifconfig {0}", interfaceName);
        boolean found = false;
        try {
            target.executeCommand(findCommand);
            found = true;
        } catch (ProcessExecutionException e) {
            ProcessExecution execution = e.getExecution();
            if (execution.getExitCode() == 1 && execution.getStdErr().contains("Device not found")) {
                found = false;
            } else {
                throw new OpsException("Error checking for interface", e);
            }
        }
        if (!found) {
            // This is actually idempotent, but I think it's scary to rely on it being so
            Command command = Command.build("/usr/sbin/tunctl -t {0}", interfaceName);
            target.executeCommand(command);
        }
        {
            // TODO: Safe to re-run?
            Command command = Command.build("ifconfig {0} up", interfaceName);
            target.executeCommand(command);
        }
        if (bridgeName != null) {
            // TODO: Safe to re-run?
            Command command = Command.build("brctl addif {0} {1}", bridgeName.get(), interfaceName);
            try {
                target.executeCommand(command);
            } catch (ProcessExecutionException e) {
                ProcessExecution execution = e.getExecution();
                if (execution.getExitCode() == 1 && execution.getStdErr().contains("already a member of a bridge")) {
                // OK
                // TODO: Check that the bridge is bridgeName
                } else {
                    throw new OpsException("Error attaching interface to bridge", e);
                }
            }
        }
    }
    if (OpsContext.isDelete()) {
        // TODO: Implement
        log.warn("NetworkTunDevice delete not implemented");
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ProcessExecutionException(org.platformlayer.ops.process.ProcessExecutionException) ProcessExecution(org.platformlayer.ops.process.ProcessExecution) Handler(org.platformlayer.ops.Handler)

Example 22 with Handler

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

the class Tagger method handler.

@Handler
public void handler() throws OpsException {
    if (OpsContext.isDelete() || OpsContext.isConfigure()) {
        TagChanges tagChanges = tagChangesProvider.get();
        if (tagChanges != null) {
            log.info("Setting tags on " + platformLayerKey);
            if (OpsContext.isDelete()) {
                // Swap the tags for a removal
                Tags x = tagChanges.addTags;
                tagChanges.addTags = tagChanges.removeTags;
                tagChanges.removeTags = x;
            }
            platformLayer.changeTags(platformLayerKey, tagChanges, null);
        }
    }
}
Also used : TagChanges(org.platformlayer.core.model.TagChanges) Tags(org.platformlayer.core.model.Tags) Handler(org.platformlayer.ops.Handler)

Example 23 with Handler

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

the class OperationInvoker method isCandidate.

private boolean isCandidate(Method method, BindingScope scope) {
    Action action = scope.getInstance(Action.class);
    Handler handler = method.getAnnotation(Handler.class);
    if (handler != null) {
        if (!canHandleAction(handler, action, true)) {
            return false;
        }
        return true;
    }
    int managedCount = 0;
    for (Class<?> parameterType : method.getParameterTypes()) {
        if (parameterType.equals(ItemBase.class)) {
            managedCount++;
        }
    }
    // We require that we take at least one 'Managed<?>' parameter
    if (managedCount == 0) {
        return false;
    }
    return true;
}
Also used : Action(org.platformlayer.core.model.Action) Handler(org.platformlayer.ops.Handler)

Example 24 with Handler

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

the class CreateUser method handler.

@Handler
public void handler(DatabaseTarget db) throws OpsException {
    if (OpsContext.isConfigure()) {
        try {
            String createUser = String.format("CREATE USER %s WITH PASSWORD '%s'", databaseUser, databasePassword.plaintext());
            db.execute(createUser);
        } catch (SQLException e) {
            String sqlState = e.getSQLState();
            if (Objects.equal(sqlState, "42710")) {
                // ProcessExecution execution = e.getExecution();
                // if (execution.getExitCode() == 1 && execution.getStdErr().contains("already exists")) {
                log.info("User already exists");
            } else {
                log.info("Unknown code: " + sqlState);
                throw new OpsException("Error creating user", e);
            }
        }
        String grant = String.format("GRANT ALL PRIVILEGES ON DATABASE %s to %s;", grantDatabaseName, databaseUser);
        try {
            db.execute(grant);
        } catch (SQLException e) {
            String sqlState = e.getSQLState();
            // if (Objects.equal(sqlState, "12345")) {
            // log.info("User already exists");
            // } else {
            log.info("Unknown code: " + sqlState);
            throw new OpsException("Error granting privileges", e);
        // }
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) SQLException(java.sql.SQLException) Handler(org.platformlayer.ops.Handler)

Example 25 with Handler

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

the class IpsecPresharedKey method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        File pskFile = new File("/etc/racoon/psk.txt");
        String psk = target.readTextFile(pskFile);
        if (psk == null) {
            psk = "# Managed by PlatformLayer\n";
        }
        boolean found = false;
        // TODO: Extend MapSplitter / add some helper functions??
        Splitter keyValueSpliter = Splitter.on(CharMatcher.WHITESPACE).limit(2).omitEmptyStrings().trimResults();
        Map<String, String> psks = Maps.newHashMap();
        for (String line : Splitter.on("\n").trimResults().omitEmptyStrings().split(psk)) {
            if (line.startsWith("#")) {
                continue;
            }
            List<String> tokens = Lists.newArrayList(keyValueSpliter.split(line));
            if (tokens.size() != 2) {
                throw new OpsException("Cannot parse PSK line: " + line);
            }
            String key = tokens.get(0);
            String value = tokens.get(1);
            if (psks.containsKey(key)) {
                // (We could check to see if they're the same, but this is generally not good)
                throw new OpsException("Found duplicate PSK");
            }
            psks.put(key, value);
            if (!key.equals(id)) {
                continue;
            }
            if (value.equals(secret.plaintext())) {
                found = true;
            }
        }
        if (!found) {
            psk = psk + "\n";
            psk += id + " " + secret.plaintext() + "\n";
            FileUpload.upload(target, pskFile, psk);
            target.executeCommand(Command.build("racoonctl reload-config"));
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Splitter(com.google.common.base.Splitter) File(java.io.File) 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