Search in sources :

Example 1 with PostgresqlServer

use of org.platformlayer.service.postgresql.model.PostgresqlServer in project platformlayer by platformlayer.

the class PostgresqlServerBootstrap method handler.

@Handler
public void handler() throws OpsException {
    PostgresqlServer model = OpsContext.get().getInstance(PostgresqlServer.class);
    OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
    // su postgres -c "psql --command=\"ALTER USER postgres WITH PASSWORD 'secret';\""
    String password = model.rootPassword.plaintext();
    String sql = String.format("ALTER USER postgres WITH PASSWORD '%s';", password);
    String psql = String.format("psql --command=\"%s\"", sql);
    Command command = Command.build("su postgres -c {0}", psql);
    target.executeCommand(command);
}
Also used : OpsTarget(org.platformlayer.ops.OpsTarget) Command(org.platformlayer.ops.Command) PostgresqlServer(org.platformlayer.service.postgresql.model.PostgresqlServer) Handler(org.platformlayer.ops.Handler)

Example 2 with PostgresqlServer

use of org.platformlayer.service.postgresql.model.PostgresqlServer 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)

Aggregations

Command (org.platformlayer.ops.Command)2 Handler (org.platformlayer.ops.Handler)2 OpsTarget (org.platformlayer.ops.OpsTarget)2 PostgresqlServer (org.platformlayer.service.postgresql.model.PostgresqlServer)2 OpsContext (org.platformlayer.ops.OpsContext)1 Backup (org.platformlayer.ops.backups.Backup)1 BackupContext (org.platformlayer.ops.backups.BackupContext)1 BackupItem (org.platformlayer.ops.backups.BackupItem)1