Beispiel #1
0
    def control_service(self, service, action):
        """ remotely start/stop/restart/status a service, using CLIENT_SCRIPT """
        from options import VERBOSE

        if service not in ('londiste', 'ticker', 'pgbouncer'):
            raise Exception, "Error: unknown service '%s'" % service

        args = [action, service]

        if service in ('londiste', 'ticker') and not self.replication:
            raise Exception, "Error: no replication in your setup"

        if service == 'pgbouncer':
            bargs = args + [self.pgbouncer_port]
            out   = utils.run_client_script(self.host, bargs, self.use_sudo)

            if (action == 'status' or VERBOSE) and out:
                print out

            return

        if service == 'londiste':
            l = londiste.londiste(self.replication, self.section,
                                  self.dbname, self.dated_dbname,
                                  self.tmpdir, clean = True)

            for t, host in l.tickers():
                targs = [ action, 'ticker',
                          os.path.basename( t.get_config_filename() ) ]
                out   = utils.run_client_script(host, targs, self.use_sudo)

                if (action == 'status' or VERBOSE) and out:
                    print out

            for p, host in l.providers():
                pargs = args + [ p, os.path.basename( l.get_config_filename(p) ) ]
                out   = utils.run_client_script(host, pargs, self.use_sudo)

                if (action == 'status' or VERBOSE) and out:
                    print out

            return

        if service == 'ticker':
            l = londiste.londiste(self.replication, self.section,
                                  self.dbname, self.dated_dbname,
                                  self.tmpdir, clean = True)

            for t, host in l.tickers():
                targs = args + [ os.path.basename( t.get_config_filename() ) ]
                out   = utils.run_client_script(self.host, targs, self.use_sudo)

                if (action == 'status' or VERBOSE) and out:
                    print out

        return
Beispiel #2
0
    def pgbouncer_update_conf(self, newconffile):
        """ reconfigure targeted pgbouncer with given file """
        import os.path
        from options import VERBOSE, TERSE, CLIENT_SCRIPT

        baseconfdir = os.path.dirname(self.pgbouncer_conf)

        # skip scp when target is localhost
        if self.host not in ("localhost", "127.0.0.1"):
            utils.scp(self.host, newconffile, '/tmp')

        utils.run_client_script(
            self.host, ["pgbouncer", newconffile, self.pgbouncer_port],
            self.use_sudo)

        # if target isn't localhost, rm the local temp file
        # when target host is localhost, we used mv already
        if self.host not in ("localhost", "127.0.0.1"):
            os.unlink(newconffile)
Beispiel #3
0
    def pgbouncer_update_conf(self, newconffile):
        """ reconfigure targeted pgbouncer with given file """
        import os.path
        from options import VERBOSE, TERSE, CLIENT_SCRIPT

        baseconfdir = os.path.dirname(self.pgbouncer_conf)

        # skip scp when target is localhost
        if self.host not in ("localhost", "127.0.0.1"):
            utils.scp(self.host, newconffile, '/tmp')

        utils.run_client_script(self.host,
                                ["pgbouncer", newconffile, self.pgbouncer_port],
                                self.use_sudo)

        # if target isn't localhost, rm the local temp file
        # when target host is localhost, we used mv already
        if self.host not in ("localhost", "127.0.0.1"):
            os.unlink(newconffile)
Beispiel #4
0
    def start(self, host, filename, use_sudo, init=True):
        """ start the ticker daemon on remote host """
        from options import VERBOSE

        if init:
            filename = self.init_remote(host, filename, use_sudo)

        args = ['ticker', filename]
        out = utils.run_client_script(host, args, use_sudo)

        if VERBOSE:
            print out

        return
Beispiel #5
0
    def init_remote(self, host, filename, use_sudo):
        """ send the pgqadm file to the remote host """
        from options import VERBOSE

        utils.scp(host, filename, '/tmp')
        remote_filename = os.path.basename(filename)

        args = ['init-pgq', remote_filename]
        out = utils.run_client_script(host, args, use_sudo)

        if VERBOSE:
            print out

        return remote_filename
Beispiel #6
0
    def pitr(self, target, value):
        """ launch a Point In Time Recovery """
        import datetime
        from options import VERBOSE

        if self.base_backup_cmd is None or self.wal_archive_cmd is None \
           or self.pitr_basedir is None:
            raise Exception, "Error: please configure PITR"

        cluster = datetime.date.today().isoformat().replace('-', '')
        cluster = os.path.join(self.pitr_basedir, cluster)

        if VERBOSE:
            print "pitr:  target  %s [%s]" % (value, target)
            print "pitr:    base  %s" % self.base_backup_cmd
            print "pitr:     wal  %s" % self.wal_archive_cmd
            print "pitr: basedir  %s" % cluster

        client_args = [cluster, self.base_backup_cmd, self.wal_archive_cmd]
        if target:
            client_args += [target, value]

        utils.run_client_script(self.host, client_args, self.use_sudo)
Beispiel #7
0
    def start(self, host, filename, use_sudo, init = True):
        """ start the ticker daemon on remote host """
        from options import VERBOSE

        if init:
            filename = self.init_remote(host, filename, use_sudo)

        args = ['ticker', filename]
        out  = utils.run_client_script(host, args, use_sudo)

        if VERBOSE:
            print out

        return
Beispiel #8
0
    def init_remote(self, host, filename, use_sudo):
        """ send the pgqadm file to the remote host """
        from options import VERBOSE

        utils.scp(host, filename, '/tmp')
        remote_filename = os.path.basename(filename)

        args = ['init-pgq', remote_filename]
        out  = utils.run_client_script(host, args, use_sudo)

        if VERBOSE:
            print out

        return remote_filename
Beispiel #9
0
    def pitr(self, target, value):
        """ launch a Point In Time Recovery """
        import datetime
        from options import VERBOSE

        if self.base_backup_cmd is None or self.wal_archive_cmd is None \
           or self.pitr_basedir is None:
            raise Exception, "Error: please configure PITR"

        cluster = datetime.date.today().isoformat().replace('-', '')
        cluster = os.path.join(self.pitr_basedir, cluster)

        if VERBOSE:
            print "pitr:  target  %s [%s]" % (value, target)
            print "pitr:    base  %s" % self.base_backup_cmd
            print "pitr:     wal  %s" % self.wal_archive_cmd
            print "pitr: basedir  %s" % cluster

        client_args = [cluster, self.base_backup_cmd, self.wal_archive_cmd]
        if target:
            client_args += [target, value]

        utils.run_client_script(self.host, client_args, self.use_sudo)
Beispiel #10
0
    def init_remote(self, provider, host, filename, use_sudo):
        """ send the londiste file for provider to the remote host """
        from options import VERBOSE

        utils.scp(host, filename, '/tmp')

        remote_filename = os.path.basename(filename)
        tables = self.clean_provides(self.config.get(provider, 'provides'))
        args = ['init-londiste', provider, remote_filename] + tables
        out = utils.run_client_script(host, args, use_sudo)

        if VERBOSE:
            print out

        return remote_filename
Beispiel #11
0
    def init_remote(self, provider, host, filename, use_sudo):
        """ send the londiste file for provider to the remote host """
        from options import VERBOSE

        utils.scp(host, filename, '/tmp')

        remote_filename = os.path.basename(filename)
        tables = self.clean_provides( self.config.get(provider, 'provides') )
        args   = ['init-londiste', provider, remote_filename] + tables
        out    = utils.run_client_script(host, args, use_sudo)

        if VERBOSE:
            print out

        return remote_filename
Beispiel #12
0
    def drop(self, dbname = None):
        """ drop the given database: dbname_%(backup_date) """
        from options import VERBOSE, TERSE, CLIENT_SCRIPT
        from utils import RET_CODE

        if dbname is None:
            dbname = self.dated_dbname

        # Drop DB via staging-client.sh 
        dropped = utils.run_client_script(self.host, ["dropdb", dbname, self.pgbouncer_port, self.postgres_port, self.postgres_major], self.use_sudo, RET_CODE)

        if VERBOSE:
            print "Database %s dropped (%s), then clean pgbouncer" % (dbname, dropped)

        if dropped == 0:
            # first remove the database from pgbouncer configuration
            self.pgbouncer_del_database(dbname)

            # and restart pgbouncer, so that there's no connection left
            self.control_service('pgbouncer', 'restart')
Beispiel #13
0
    def drop(self, dbname=None):
        """ drop the given database: dbname_%(backup_date) """
        from options import VERBOSE, TERSE, CLIENT_SCRIPT
        from utils import RET_CODE

        if dbname is None:
            dbname = self.dated_dbname

        # Drop DB via staging-client.sh
        dropped = utils.run_client_script(
            self.host, ["dropdb", dbname, self.pgbouncer_port], self.use_sudo,
            RET_CODE)

        if VERBOSE:
            print "Database %s dropped (%s), then clean pgbouncer" % (dbname,
                                                                      dropped)

        if dropped == 0:
            # first remove the database from pgbouncer configuration
            self.pgbouncer_del_database(dbname)

            # and restart pgbouncer, so that there's no connection left
            self.control_service('pgbouncer', 'restart')
Beispiel #14
0
    def control_service(self, service, action):
        """ remotely start/stop/restart/status a service, using CLIENT_SCRIPT """
        from options import VERBOSE

        if service not in ('londiste', 'ticker', 'pgbouncer'):
            raise Exception, "Error: unknown service '%s'" % service

        args = [action, service]

        if service in ('londiste', 'ticker') and not self.replication:
            raise Exception, "Error: no replication in your setup"

        if service == 'pgbouncer':
            bargs = args + [self.pgbouncer_port]
            out = utils.run_client_script(self.host, bargs, self.use_sudo)

            if (action == 'status' or VERBOSE) and out:
                print out

            return

        if service == 'londiste':
            l = londiste.londiste(self.replication,
                                  self.section,
                                  self.dbname,
                                  self.dated_dbname,
                                  self.tmpdir,
                                  clean=True)

            for t, host in l.tickers():
                targs = [
                    action, 'ticker',
                    os.path.basename(t.get_config_filename())
                ]
                out = utils.run_client_script(host, targs, self.use_sudo)

                if (action == 'status' or VERBOSE) and out:
                    print out

            for p, host in l.providers():
                pargs = args + [p, os.path.basename(l.get_config_filename(p))]
                out = utils.run_client_script(host, pargs, self.use_sudo)

                if (action == 'status' or VERBOSE) and out:
                    print out

            return

        if service == 'ticker':
            l = londiste.londiste(self.replication,
                                  self.section,
                                  self.dbname,
                                  self.dated_dbname,
                                  self.tmpdir,
                                  clean=True)

            for t, host in l.tickers():
                targs = args + [os.path.basename(t.get_config_filename())]
                out = utils.run_client_script(self.host, targs, self.use_sudo)

                if (action == 'status' or VERBOSE) and out:
                    print out

        return