Search in sources :

Example 36 with OpsTarget

use of org.platformlayer.ops.OpsTarget 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)

Example 37 with OpsTarget

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

the class PostgresqlServerBackup method doBackup.

@Handler(BackupAction.class)
public void doBackup() throws OpsException, IOException {
    OpsContext opsContext = OpsContext.get();
    // Machine machine = opsContext.getInstance(Machine.class);
    OpsTarget target = opsContext.getInstance(OpsTarget.class);
    // We use pg_dump, not pg_dumpall:
    // 1) pg_dumpall doesn't support binary dumping (?)
    // 2) pg_dumpall wouldn't let us split the dump into different files (?)
    List<String> databases = listDatabases(target);
    BackupContext backupContext = backups.getContext();
    String baseName = UUID.randomUUID().toString();
    PostgresqlServer server = OpsContext.get().getInstance(PostgresqlServer.class);
    backupContext.add(new BackupItem(server.getKey(), FORMAT, baseName));
    {
        Command dumpAll = Command.build("su postgres -c \"pg_dumpall --globals-only\"");
        Backup request = new Backup();
        request.target = target;
        request.objectName = baseName + "/pgdump_meta";
        backupContext.uploadStream(request, dumpAll);
    }
    for (String database : databases) {
        // template0 cannot be backed up
        if (database.equals("template0")) {
            continue;
        }
        // template1 can be backed up, even though it isn't typically very useful
        String fileName = "pgdump_db_" + database;
        Backup request = new Backup();
        request.target = target;
        request.objectName = baseName + "/" + fileName;
        Command dumpDatabase = Command.build("su postgres -c \"pg_dump --oids -Fc --verbose {0}\"", database);
        backupContext.uploadStream(request, dumpDatabase);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) BackupItem(org.platformlayer.ops.backups.BackupItem) Command(org.platformlayer.ops.Command) Backup(org.platformlayer.ops.backups.Backup) BackupContext(org.platformlayer.ops.backups.BackupContext) PostgresqlServer(org.platformlayer.service.postgresql.model.PostgresqlServer) OpsContext(org.platformlayer.ops.OpsContext) Handler(org.platformlayer.ops.Handler)

Example 38 with OpsTarget

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

the class RecurseOverAll method doRecursion.

public void doRecursion(Object controller, SshKey sshKey, Class<? extends ItemBase> machineItemClass) throws OpsException {
    boolean failed = false;
    for (ItemBase machineItem : platformLayer.listItems(machineItemClass)) {
        switch(machineItem.getState()) {
            case ACTIVE:
                break;
            case DELETED:
                log.warn("Ignoring deleted server: " + machineItem);
                continue;
            default:
                log.warn("Item not yet active: " + machineItem);
                failed = true;
                continue;
        }
        Machine machine = instances.findMachine(machineItem);
        if (machine == null) {
            log.warn("Server instance not found: " + machineItem);
            failed = true;
            continue;
        }
        OpsTarget target = machine.getTarget(sshKey);
        try {
            // Execute the children in a scope with the paired item and machine
            BindingScope scope = BindingScope.push(machine, target, machineItem);
            try {
                OpsContext opsContext = OpsContext.get();
                OperationRecursor.doRecurseChildren(opsContext, controller);
            } finally {
                scope.pop();
            }
        } catch (OpsException e) {
            failed = true;
            log.warn("Error updating machine: " + machine, e);
        }
    }
    if (failed) {
        throw new OpsException("Could not update all servers").setRetry(TimeSpan.ONE_MINUTE);
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine) BindingScope(org.platformlayer.ops.BindingScope)

Example 39 with OpsTarget

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

the class HdbDatabaseEntry method alreadyExists.

@Override
protected boolean alreadyExists() throws OpsException {
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    FilesystemInfo dirInfo = target.getFilesystemInfoFile(new File(getDataDirectory(), "objectClass.bdb"));
    return dirInfo != null;
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) FilesystemInfo(org.platformlayer.ops.filesystem.FilesystemInfo) File(java.io.File)

Example 40 with OpsTarget

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

the class NexusApp method handler.

@Handler
public void handler() throws IOException, OpsException {
    // TODO: This needs to be idempotent
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    String url = "http://nexus.sonatype.org/downloads/all/nexus-webapp-1.9.2.4.war";
    File warFile = new File("/var/lib/jetty/wars/nexus-webapp-1.9.2.4.war");
    target.executeCommand("wget {0} -O {1}", url, warFile);
    // Whatever version of nexus we have, we want it to be the root
    target.symlink(warFile, new File("/var/lib/jetty/webapps/root.war"), false);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) File(java.io.File) Handler(org.platformlayer.ops.Handler)

Aggregations

OpsTarget (org.platformlayer.ops.OpsTarget)41 Handler (org.platformlayer.ops.Handler)17 File (java.io.File)14 Machine (org.platformlayer.ops.Machine)12 OpsException (org.platformlayer.ops.OpsException)11 SshKey (org.platformlayer.ops.helpers.SshKey)10 Command (org.platformlayer.ops.Command)9 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)6 OpaqueMachine (org.platformlayer.ops.OpaqueMachine)6 OpsContext (org.platformlayer.ops.OpsContext)6 ItemBase (org.platformlayer.core.model.ItemBase)4 PublicKey (java.security.PublicKey)3 Tag (org.platformlayer.core.model.Tag)3 Tags (org.platformlayer.core.model.Tags)3 BindingScope (org.platformlayer.ops.BindingScope)3 CommandEnvironment (org.platformlayer.ops.CommandEnvironment)3 MachineCreationRequest (org.platformlayer.ops.MachineCreationRequest)3 FilesystemInfo (org.platformlayer.ops.filesystem.FilesystemInfo)3 ProcessExecution (org.platformlayer.ops.process.ProcessExecution)3 ServiceType (org.platformlayer.ids.ServiceType)2