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