Search in sources :

Example 16 with OpsContext

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();
            }
        }
    });
}
Also used : MetricDataSource(org.platformlayer.metrics.model.MetricDataSource) ItemBase(org.platformlayer.core.model.ItemBase) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) OpsContext(org.platformlayer.ops.OpsContext) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) OpsContextBuilder(org.platformlayer.ops.tasks.OpsContextBuilder) BindingScope(org.platformlayer.ops.BindingScope) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 17 with OpsContext

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;
}
Also used : ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) OpsContext(org.platformlayer.ops.OpsContext) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebApplicationException(javax.ws.rs.WebApplicationException) OpsContextBuilder(org.platformlayer.ops.tasks.OpsContextBuilder) Path(javax.ws.rs.Path)

Example 18 with OpsContext

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);
                }
            }
        }
    }
}
Also used : IThrowableProxy(ch.qos.logback.classic.spi.IThrowableProxy) Level(ch.qos.logback.classic.Level) OpsContext(org.platformlayer.ops.OpsContext) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) StackTraceElementProxy(ch.qos.logback.classic.spi.StackTraceElementProxy)

Example 19 with OpsContext

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);
    }
}
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 20 with OpsContext

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();
    }
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) DatabaseServer(org.platformlayer.ops.databases.DatabaseServer) DatabaseTarget(org.platformlayer.ops.databases.DatabaseTarget) OpsContext(org.platformlayer.ops.OpsContext) BindingScope(org.platformlayer.ops.BindingScope)

Aggregations

OpsContext (org.platformlayer.ops.OpsContext)23 OpsException (org.platformlayer.ops.OpsException)12 ItemBase (org.platformlayer.core.model.ItemBase)11 BindingScope (org.platformlayer.ops.BindingScope)10 RepositoryException (org.platformlayer.RepositoryException)9 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)9 OpsTarget (org.platformlayer.ops.OpsTarget)6 Machine (org.platformlayer.ops.Machine)5 OpsContextBuilder (org.platformlayer.ops.tasks.OpsContextBuilder)5 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)4 IOException (java.io.IOException)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 JAXBException (javax.xml.bind.JAXBException)3 ServiceType (org.platformlayer.ids.ServiceType)3 List (java.util.List)2 GET (javax.ws.rs.GET)2 SecretProvider (org.platformlayer.auth.crypto.SecretProvider)2 ProjectId (org.platformlayer.ids.ProjectId)2