Search in sources :

Example 21 with OpsTarget

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

the class MachineResolver method doRecurseOperation.

@Override
public void doRecurseOperation() throws OpsException {
    ItemBase dest = platformLayerHelpers.getItem(key);
    boolean required = !OpsContext.isDelete();
    for (Machine machine : instanceHelpers.getMachines(dest, required)) {
        OpsTarget target = instanceHelpers.getTarget(dest, machine);
        BindingScope scope = BindingScope.push(machine, target);
        try {
            OpsContext opsContext = OpsContext.get();
            OperationRecursor.doRecurseChildren(opsContext, this);
        } finally {
            scope.pop();
        }
    }
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) ItemBase(org.platformlayer.core.model.ItemBase) OpsContext(org.platformlayer.ops.OpsContext) Machine(org.platformlayer.ops.Machine) BindingScope(org.platformlayer.ops.BindingScope)

Example 22 with OpsTarget

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

the class GitServerAssignment method handler.

@Handler
public void handler(GitRepository model) throws Exception {
    PlatformLayerKey assignedTo = Tag.ASSIGNED_TO.findUnique(model.getTags());
    if (OpsContext.isConfigure()) {
        if (assignedTo == null) {
            List<GitService> gitServices = platformLayer.listItems(GitService.class);
            if (gitServices.size() == 0) {
                throw new OpsException("No git service found");
            }
            GitService gitService = RandomChooser.chooseRandom(gitServices);
            if (gitService == null) {
                throw new IllegalStateException();
            }
            assignedTo = gitService.getKey();
            platformLayer.addTag(model.getKey(), Tag.ASSIGNED_TO.build(assignedTo));
        }
    }
    GitService gitService = null;
    if (assignedTo != null) {
        gitService = platformLayer.getItem(assignedTo, GitService.class);
    }
    if (OpsContext.isDelete()) {
        if (gitService == null) {
            log.info("Deleting, but not assigned to a server; nothing to do");
            getRecursionState().setPreventRecursion(true);
            return;
        }
    }
    if (gitService == null) {
        throw new OpsException("No git servers found");
    }
    if (gitService.getState() != ManagedItemState.ACTIVE) {
        throw new OpsException("Server not yet active: " + gitService);
    }
    Machine machine = instances.findMachine(gitService);
    if (machine == null) {
        throw new OpsException("Server machine not found:" + gitService);
    }
    SshKey sshKey = service.getSshKey();
    OpsTarget target = machine.getTarget(sshKey);
    getRecursionState().pushChildScope(OpsTarget.class, target);
}
Also used : SshKey(org.platformlayer.ops.helpers.SshKey) OpsException(org.platformlayer.ops.OpsException) OpsTarget(org.platformlayer.ops.OpsTarget) GitService(org.platformlayer.service.git.model.GitService) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) Machine(org.platformlayer.ops.Machine) Handler(org.platformlayer.ops.Handler)

Example 23 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 24 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 25 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)

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