Search in sources :

Example 6 with Command

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

the class SshPostgresTarget method execute.

public ProcessExecution execute(String sql, String maskedSql) throws OpsException {
    OpsTarget target = getOpsTarget();
    // We probably want psql -A -t -c "command"
    Command command = Command.build("psql");
    command.addQuoted("--username=", username);
    command.addQuoted("--host=", hostname);
    command.addArgument(Argument.buildQuoted("--command=", sql).setMasked("--command=" + Command.MASKED));
    CommandEnvironment env = new CommandEnvironment();
    env.put("PGPASSWORD", password.plaintext());
    command.setEnvironment(env);
    return target.executeCommand(command);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) CommandEnvironment(org.platformlayer.ops.CommandEnvironment)

Example 7 with Command

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

the class ExpandArchive method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    if (OpsContext.isConfigure()) {
        target.mkdir(extractPath);
        String archiveFileName = archiveFile.getName();
        if (archiveFileName.endsWith(".zip")) {
            // -u = update, for (something close to) idempotency
            // -o = overwrite (no prompt)
            Command unzipCommand = Command.build("unzip -u -o {0} -d {1}", archiveFile, extractPath);
            target.executeCommand(unzipCommand.setTimeout(TimeSpan.FIVE_MINUTES));
        } else if (archiveFileName.endsWith(".tgz") || archiveFileName.endsWith(".tar.gz")) {
            Command unzipCommand = Command.build("cd {0}; tar zxf {1}", extractPath, archiveFile);
            target.executeCommand(unzipCommand.setTimeout(TimeSpan.FIVE_MINUTES));
        } else {
            throw new UnsupportedOperationException();
        }
    }
}
Also used : Command(org.platformlayer.ops.Command) Handler(org.platformlayer.ops.Handler)

Example 8 with Command

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

the class RegenerateLocales method getGeneratedLocales.

private List<String> getGeneratedLocales(OpsTarget target) throws OpsException {
    Command listLocales = Command.build("localedef --list-archive");
    ProcessExecution execution = target.executeCommand(listLocales);
    List<String> locales = Lists.newArrayList();
    for (String line : Splitter.on("\n").split(execution.getStdOut())) {
        line = line.trim();
        if (line.isEmpty()) {
            continue;
        }
        locales.add(normalize(line));
    }
    return locales;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 9 with Command

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

the class IpTablesManager method getCurrentFirewallState.

public static List<FirewallRecord> getCurrentFirewallState(OpsTarget target, Transport transport) throws OpsException {
    Command command = getIptablesCommand(transport);
    command.addLiteral("--list-rules");
    List<FirewallRecord> records = Lists.newArrayList();
    ProcessExecution execution = target.executeCommand(command);
    String stdout = execution.getStdOut();
    for (String line : stdout.split("\n")) {
        parseAndAdd(records, line);
    }
    return records;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 10 with Command

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

the class IpTablesManager method buildCommandAddFirewallRule.

public static Command buildCommandAddFirewallRule(OpsTarget server, FirewallRecord add) throws OpsException {
    if (isPolicyDefault(add)) {
        Command command = getIptablesCommand(add.getTransport());
        command.addLiteral("-P");
        command.addLiteral(toChain(add));
        command.addLiteral(toIpTableDecision(add.decision));
        return command;
    }
    // iptables --append INPUT -s 74.125.67.103/32 -p tcp -m tcp --dport 22 -j ACCEPT
    String action;
    // Passes take precedence
    switch(add.decision) {
        case Pass:
            action = " --insert " + toChain(add) + " 1 ";
            break;
        case Block:
            action = " --append " + toChain(add);
            break;
        default:
            throw new IllegalStateException();
    }
    String ipTableRule = buildIpTableRule(add);
    String commandString = getIptablesCommand(add.getTransport()).buildCommandString() + action + ipTableRule;
    Command command = Command.build(commandString);
    return command;
}
Also used : Command(org.platformlayer.ops.Command)

Aggregations

Command (org.platformlayer.ops.Command)72 File (java.io.File)21 Handler (org.platformlayer.ops.Handler)17 OpsException (org.platformlayer.ops.OpsException)16 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)16 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)11 OpsTarget (org.platformlayer.ops.OpsTarget)9 InetAddress (java.net.InetAddress)4 CurlRequest (org.platformlayer.ops.helpers.CurlRequest)4 Md5Hash (com.fathomdb.hash.Md5Hash)3 IOException (java.io.IOException)3 UnknownHostException (java.net.UnknownHostException)3 Map (java.util.Map)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ImageFormat (org.platformlayer.ops.images.ImageFormat)3 List (java.util.List)2 Properties (java.util.Properties)2 RequestBuilder (org.openstack.client.common.RequestBuilder)2 AddressModel (org.platformlayer.core.model.AddressModel)2 Tag (org.platformlayer.core.model.Tag)2