use of org.platformlayer.ops.OpsContext in project platformlayer by platformlayer.
the class MetricsResource method getMetrics.
@POST
@Produces({ XML, JSON })
public MetricDataSource getMetrics(final MetricQuery query) throws RepositoryException, OpsException {
final ItemBase managedItem = getManagedItem();
final ServiceProvider serviceProvider = getServiceProvider();
OpsContextBuilder opsContextBuilder = objectInjector.getInstance(OpsContextBuilder.class);
final OpsContext opsContext = opsContextBuilder.buildTemporaryOpsContext(getServiceType(), getProjectAuthorization());
return OpsContext.runInContext(opsContext, new CheckedCallable<MetricDataSource, Exception>() {
@Override
public MetricDataSource call() throws Exception {
BindingScope bindingScope = BindingScope.push(managedItem, managedItem);
try {
MetricDataSource metrics = serviceProvider.getMetricValues(managedItem, query);
return metrics;
} finally {
bindingScope.pop();
}
}
});
}
use of org.platformlayer.ops.OpsContext in project platformlayer by platformlayer.
the class ServiceResource method getExtensionsResource.
@Path("extensions")
public Object getExtensionsResource() throws Exception {
final ServiceProvider serviceProvider = getServiceProvider();
OpsContextBuilder opsContextBuilder = objectInjector.getInstance(OpsContextBuilder.class);
final OpsContext opsContext = opsContextBuilder.buildTemporaryOpsContext(getServiceType(), getProjectAuthorization());
Object extensionResource = OpsContext.runInContext(opsContext, new CheckedCallable<Object, Exception>() {
@Override
public Object call() throws Exception {
return serviceProvider.getExtensionResource();
}
});
if (extensionResource == null) {
raiseNotFound();
}
return extensionResource;
}
use of org.platformlayer.ops.OpsContext in project platformlayer by platformlayer.
the class LogbackHook method append.
@Override
protected void append(E e) {
OpsContext opsContext = OpsContext.get();
if (opsContext != null) {
ILoggingEvent event = (ILoggingEvent) e;
// Note that we can get the unformatted message in getMessage(), presumably along with the parameters...
String message = event.getFormattedMessage();
Level level = event.getLevel();
int levelInt = level.toInt();
List<String[]> exceptionStacks = null;
IThrowableProxy throwableInformation = event.getThrowableProxy();
while (throwableInformation != null) {
String[] exceptionStackTrace = null;
StackTraceElementProxy[] trace = throwableInformation.getStackTraceElementProxyArray();
String exceptionMessage = throwableInformation.getMessage();
String exceptionClass = throwableInformation.getClassName();
if (trace != null) {
exceptionStackTrace = new String[1 + trace.length];
exceptionStackTrace[0] = exceptionClass + ": " + exceptionMessage;
for (int i = 0; i < trace.length; i++) {
exceptionStackTrace[1 + i] = trace[i].getSTEAsString();
}
} else {
exceptionStackTrace = new String[1];
exceptionStackTrace[0] = exceptionClass + ": " + exceptionMessage;
}
if (exceptionStacks == null) {
exceptionStacks = Lists.newArrayList();
}
exceptionStacks.add(exceptionStackTrace);
throwableInformation = throwableInformation.getCause();
}
if (message != null || exceptionStacks != null) {
opsContext.getJobLogger().logMessage(message, exceptionStacks, levelInt);
if (levelInt >= Level.ERROR_INT) {
// String key = "warn-" + OpsSystem.buildSimpleTimeString() + "-" + (System.nanoTime() % 1000);
if (opsContext != null) {
// && opsContext.getOperation() != null) {
if (exceptionStacks != null && !exceptionStacks.isEmpty()) {
String[] exceptionStack = exceptionStacks.get(0);
if (exceptionStack != null && exceptionStack.length > 0) {
message += "; " + exceptionStack[0];
}
}
opsContext.addWarning(null, message);
}
}
}
}
}
use of org.platformlayer.ops.OpsContext 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.OpsContext in project platformlayer by platformlayer.
the class DatabaseConnection method doRecurseOperation.
@Override
public void doRecurseOperation() throws OpsException {
ItemBase server = platformLayer.getItem(serverKey);
DatabaseServer database = providers.toInterface(server, DatabaseServer.class);
String username = this.username;
if (username == null) {
username = database.getRootUsername();
}
if (username.equals("postgres") && password == null) {
password = database.getRootPassword();
}
DatabaseTarget dbTarget = database.buildDatabaseTarget(username, password, databaseName);
BindingScope scope = BindingScope.push(dbTarget);
try {
OpsContext opsContext = OpsContext.get();
OperationRecursor.doRecurseChildren(opsContext, this);
} finally {
scope.pop();
}
}
Aggregations