Search in sources :

Example 16 with Command

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

the class IptablesRuleRaw method buildIptablesAddCommand.

@Override
public Command buildIptablesAddCommand() {
    Command command = SimpleIptablesRules.buildCommand(transport, chain);
    command.addLiteral(ruleSpec);
    return command;
}
Also used : Command(org.platformlayer.ops.Command)

Example 17 with Command

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

the class IptablesSimpleRuleBase method handler.

@Handler
public void handler(OpsTarget target) throws OpsException {
    // We're trying not parsing everything (as IpTablesManager does!)
    List<Protocol> protocols = Lists.newArrayList();
    if (protocol == null) {
        protocols = Arrays.asList(Protocol.Tcp, Protocol.Udp);
    } else {
        protocols = Collections.singletonList(protocol);
    }
    List<Transport> transports = Lists.newArrayList();
    if (transport == null) {
        transports = Transport.all();
    } else {
        transports = Collections.singletonList(transport);
    }
    IptablesChain chain = getChain();
    for (Transport transport : transports) {
        SimpleIptablesRules rules = SimpleIptablesRules.listRules(target, transport, chain);
        for (Protocol protocol : protocols) {
            String comment = "pl-" + uuid + "-" + protocol.toString().toLowerCase();
            SimpleIptablesRules matches = rules.filterByComment(comment);
            if (matches.size() > 1) {
                log.warn("Found multiple matching rules: " + Joiner.on("\n").join(matches));
            }
            if (OpsContext.isConfigure()) {
                List<SimpleIptablesRule> correct = checkMatchingRules(matches, protocol);
                if (correct.isEmpty()) {
                    String ruleSpec = buildRuleSpec(protocol);
                    Command command = SimpleIptablesRules.buildCommand(transport, chain);
                    command.addLiteral("-A").addLiteral(ruleSpec);
                    command.addLiteral("-m").addLiteral("comment");
                    command.addLiteral("--comment").addQuoted(comment);
                    target.executeCommand(command);
                } else {
                    log.info("Found existing rule: " + Joiner.on("\n").join(matches));
                }
            }
            if (OpsContext.isDelete()) {
                if (!matches.isEmpty()) {
                    for (SimpleIptablesRule rule : matches) {
                        log.info("Deleting rule: " + rule);
                        String deleteRuleSpec = rule.convertToDeleteSpec();
                        Command command = SimpleIptablesRules.buildCommand(transport, chain);
                        command.addLiteral(deleteRuleSpec);
                        target.executeCommand(command);
                    }
                }
            }
        }
    }
}
Also used : Command(org.platformlayer.ops.Command) IptablesChain(org.platformlayer.ops.firewall.IptablesChain) Protocol(org.platformlayer.ops.firewall.Protocol) Transport(org.platformlayer.ops.firewall.Transport) Handler(org.platformlayer.ops.Handler)

Example 18 with Command

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

the class SimpleIptablesRules method buildCommand.

public static Command buildCommand(Transport transport, IptablesChain chain) {
    Command command;
    switch(transport) {
        case Ipv4:
            command = Command.build("iptables");
            break;
        case Ipv6:
            command = Command.build("ip6tables");
            break;
        default:
            throw new IllegalStateException();
    }
    command.addLiteral("-t").addQuoted(chain.toString().toLowerCase());
    return command;
}
Also used : Command(org.platformlayer.ops.Command)

Example 19 with Command

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

the class SimpleIptablesRules method listRules.

public static SimpleIptablesRules listRules(OpsTarget target, Transport transport, IptablesChain chain) throws OpsException {
    Command command = buildCommand(transport, chain);
    command.addLiteral("--list-rules");
    ProcessExecution iptablesExecution = target.executeCommand(command);
    SimpleIptablesRules ret = new SimpleIptablesRules();
    for (String line : Splitter.on("\n").split(iptablesExecution.getStdOut())) {
        SimpleIptablesRule rule = new SimpleIptablesRule(line);
        ret.rules.add(rule);
    }
    return ret;
}
Also used : Command(org.platformlayer.ops.Command) ProcessExecution(org.platformlayer.ops.process.ProcessExecution)

Example 20 with Command

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

the class JavaCommandBuilder method get.

public Command get() {
    Command command = Command.build("java");
    command.addLiteral("-server");
    for (Map.Entry<String, String> define : defines.entrySet()) {
        command.addLiteral("-D" + define.getKey() + "=" + define.getValue());
    }
    StringBuilder cp = new StringBuilder();
    for (ClasspathEntry entry : classpath) {
        if (cp.length() != 0) {
            cp.append(":");
        }
        String s = entry.path.getAbsolutePath();
        if (entry.wildcard) {
            // TODO: Adding * is gross
            s += "/*";
        }
        cp.append(s);
    }
    // TODO: This is not nice either
    if (cp.length() == 0) {
        cp.append(".");
    } else {
        cp.append(":.");
    }
    command.addLiteral("-cp").addQuoted(cp.toString());
    if (jar != null) {
        command.addLiteral("-jar").addFile(jar);
    } else {
        command.addQuoted(mainClass);
    }
    for (Argument argument : arguments) {
        command.addArgument(argument);
    }
    return command;
}
Also used : Argument(org.platformlayer.ops.Command.Argument) Command(org.platformlayer.ops.Command) Map(java.util.Map)

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