Search in sources :

Example 1 with FileUpload

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

the class SshAuthorizedKey method ensureSshAuthorization.

public static void ensureSshAuthorization(OpsTarget target, String user, PublicKey sshPublicKey) throws OpsException {
    File homeDir = getDefaultHomedir(user);
    File sshDir = new File(homeDir, ".ssh");
    if (target.getFilesystemInfoFile(sshDir) == null) {
        target.mkdir(sshDir, "500");
        target.chown(sshDir, user, null, false, false);
    }
    File sshAuthorizationsFile = new File(sshDir, "authorized_keys");
    String sshAuthorizations = target.readTextFile(sshAuthorizationsFile);
    String serialized = SshKeys.serialize(sshPublicKey);
    boolean keyPresent = sshAuthorizations != null && sshAuthorizations.contains(serialized);
    if (OpsContext.isValidate()) {
        Deviations.assertTrue(keyPresent, "SSH key not present");
    }
    if (OpsContext.isConfigure()) {
        if (!keyPresent) {
            if (sshAuthorizations == null) {
                sshAuthorizations = "";
            } else {
                sshAuthorizations += "\n";
            }
            sshAuthorizations += serialized + "\n";
            FileUpload upload = FileUpload.build(sshAuthorizations);
            upload.mode = "644";
            upload.path = sshAuthorizationsFile;
            FileUpload.upload(target, sshAuthorizationsFile, sshAuthorizations);
            target.chown(sshAuthorizationsFile, user, null, false, false);
        }
    }
}
Also used : File(java.io.File) FileUpload(org.platformlayer.ops.FileUpload)

Example 2 with FileUpload

use of org.platformlayer.ops.FileUpload 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 3 with FileUpload

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

the class DnsHelpers method upload.

public void upload(TargetServer targetServer, ZoneFile zoneFile) throws OpsException {
    // TODO: Validate / sanitize key
    File path = new File(DnsServerTemplate.getZonesDir(), zoneFile.getKey());
    String data = zoneFile.getData();
    OpsTarget target = targetServer.getTarget();
    String existing = target.readTextFile(path);
    boolean isSame = Objects.equal(data, existing);
    if (!isSame) {
        // TODO: The serial value means that this is always dirty
        log.info("Uploading zone file: " + path);
        // Upload then atomic move
        File tempFile = new File(targetServer.getTempDir(), zoneFile.getKey());
        FileUpload upload = FileUpload.build(data);
        upload.path = tempFile;
        upload.mode = "0644";
        target.doUpload(upload);
        target.mv(tempFile, path);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) File(java.io.File) FileUpload(org.platformlayer.ops.FileUpload)

Aggregations

File (java.io.File)3 FileUpload (org.platformlayer.ops.FileUpload)3 Command (org.platformlayer.ops.Command)1 OpsException (org.platformlayer.ops.OpsException)1 OpsTarget (org.platformlayer.ops.OpsTarget)1 ScriptBuilder (org.platformlayer.ops.networks.ScriptBuilder)1