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;
}
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);
}
}
}
}
}
}
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;
}
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;
}
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;
}
Aggregations