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