Search in sources :

Example 56 with Command

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

the class CurlRequest method toCommand.

public Command toCommand() {
    List<String> tags = buildTags();
    List<String> escaped = Lists.newArrayList();
    for (String tag : tags) {
        escaped.add("%{" + tag + "}");
    }
    String format = metadataDelimiter.replace("\n", "\\n") + Joiner.on("\\n").join(escaped);
    Command command = Command.build("curl");
    if (!bareRequest) {
        command.addLiteral("--include");
        command.addLiteral("--write-out");
        command.addQuoted(format);
    }
    if (proxy != null) {
        command.addLiteral("--proxy");
        command.addQuoted(proxy);
    }
    if (timeout != null) {
        command.addLiteral("--max-time");
        command.addQuoted(Long.toString(timeout.getTotalSeconds() + 1));
    }
    for (Entry<String, String> entry : headers.entries()) {
        command.addLiteral("-H");
        command.addQuoted(entry.getKey() + ": " + entry.getValue());
    }
    if (body != null) {
        command.addLiteral("--data");
        command.addQuoted(body);
    }
    if (bodyFromStdin) {
        command.addLiteral("--data-binary");
        command.addLiteral("@-");
    }
    if (method != null) {
        command.addLiteral("--request");
        command.addQuoted(method);
    }
    command.addQuoted(getUrl().toString());
    return command;
}
Also used : Command(org.platformlayer.ops.Command)

Example 57 with Command

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

the class CurlRequest method executeRequest.

public CurlResult executeRequest(OpsTarget target) throws OpsException {
    Command command = toCommand();
    ProcessExecution execution = target.executeCommand(command);
    return parseResponse(execution);
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 58 with Command

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

the class IpTablesFirewallManager method configureAddRule.

@Override
public void configureAddRule(OpsTarget target, FirewallRecord add) throws OpsException {
    // OpsServer server = smartGetServer(true);
    Command command = IpTablesManager.buildCommandAddFirewallRule(target, add);
    String fileName = Sanitizer.forFileName().clean(add.buildKey());
    File scriptDirectory = new File("/etc/iptables/eth0");
    File transportDirectory;
    switch(add.getTransport()) {
        case Ipv4:
            transportDirectory = new File(scriptDirectory, "inet");
            break;
        case Ipv6:
            transportDirectory = new File(scriptDirectory, "inet6");
            break;
        default:
            throw new IllegalStateException();
    }
    File scriptFile = new File(transportDirectory, fileName);
    ScriptBuilder sb = new ScriptBuilder();
    sb.add(command);
    String script = sb.toString();
    String existing = target.readTextFile(scriptFile);
    boolean shouldUpload = true;
    if (existing != null) {
        if (Objects.equal(existing, script)) {
            shouldUpload = false;
        } else {
            // TODO: Put a UUID in there, check the UUID is the same??
            throw new OpsException("Script has changed: " + scriptFile);
        }
    }
    if (shouldUpload) {
        target.mkdir(transportDirectory);
        FileUpload upload = FileUpload.build(script);
        upload.path = scriptFile;
        upload.mode = "0755";
        target.doUpload(upload);
    }
    Command executeScript = Command.build("{0}", scriptFile);
    target.executeCommand(executeScript);
// getCurrentFirewallState(operation).state.add(add);
}
Also used : OpsException(org.platformlayer.ops.OpsException) Command(org.platformlayer.ops.Command) ScriptBuilder(org.platformlayer.ops.networks.ScriptBuilder) File(java.io.File) FileUpload(org.platformlayer.ops.FileUpload)

Example 59 with Command

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

the class IpTablesManager method removeFirewallRule.

// public static void reconfigureFirewall(OpsServer server, List<FirewallRecord> allRules) throws OpsException {
// throw new UnsupportedOperationException();
// // StringBuilder ipfConfig = new StringBuilder();
// // for (FirewallRecord record : allRules) {
// // List<String> ipfRules = buildIpfRules(server, record);
// // for (String ipfRule : ipfRules) {
// // ipfConfig.append(ipfRule);
// // ipfConfig.append('\n');
// // }
// // }
// //
// // Agent agent = server.getAgent();
// // String configFile = agent.uploadTempTextFile(ipfConfig.toString(), "0400", "root");
// // try {
// // String commandClearAllInactive = CMD_IPF + " -I -Fa";
// // server.simpleRun(commandClearAllInactive);
// //
// // SimpleBashCommand loadRules = SimpleBashCommand.build(CMD_IPF);
// // loadRules.addLiteralArg("-I");
// // loadRules.addLiteralArg("-f");
// // loadRules.addFileArg(configFile);
// // server.simpleRun(loadRules);
// //
// // String commandSwitchRules = CMD_IPF + " -s";
// // server.simpleRun(commandSwitchRules);
// // } finally {
// // agent.rm(configFile);
// // }
// }
public static void removeFirewallRule(OpsTarget server, FirewallRecord remove) throws OpsException {
    if (isPolicyDefault(remove)) {
        log.info("Ignoring removing of policy default rule: " + remove);
        return;
    }
    // iptables --delete INPUT -s 74.125.67.103/32 -p tcp -m tcp --dport 22 -j ACCEP
    String ipTableRule = buildIpTableRule(remove);
    String commandString = getIptablesCommand(remove.getTransport()).buildCommandString() + " --delete " + toChain(remove) + " " + ipTableRule;
    Command command = Command.build(commandString);
    server.executeCommand(command);
}
Also used : Command(org.platformlayer.ops.Command)

Example 60 with Command

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

the class IpTablesRuleScript method getContentsBytes.

@Override
protected byte[] getContentsBytes() throws OpsException {
    Command command = buildIptablesAddCommand();
    ScriptBuilder sb = new ScriptBuilder();
    sb.addMetadata("key", ruleKey);
    sb.add(command);
    return sb.getBytes();
}
Also used : Command(org.platformlayer.ops.Command) ScriptBuilder(org.platformlayer.ops.networks.ScriptBuilder)

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