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