예제 #1
0
    def __init__(self, log, pgbouncer_conn_str, dbname, dbuser):
        self.log = log

        pgbouncer_conn_str = ConnectionString(pgbouncer_conn_str)
        if not pgbouncer_conn_str.dbname:
            pgbouncer_conn_str.dbname = 'pgbouncer'
        if pgbouncer_conn_str.dbname != 'pgbouncer':
            log.warn("pgbouncer administrative database not named 'pgbouncer'")
        self.pgbouncer_con = pg_connect(pgbouncer_conn_str)

        self.master_name = None
        self.master = None
        self.slaves = {}

        for db in self.pgbouncer_cmd('show databases', results=True):
            if db.database != dbname:
                continue

            conn_str = ConnectionString(
                'dbname=%s port=%s user=%s' % (dbname, db.port, dbuser))
            if db.host:
                conn_str.host = db.host
            con = pg_connect(conn_str)
            cur = con.cursor()
            cur.execute('select pg_is_in_recovery()')
            if cur.fetchone()[0] is True:
                self.slaves[db.name] = conn_str
            else:
                self.master_name = db.name
                self.master = conn_str

        if self.master_name is None:
            log.fatal('No master detected.')
            raise SystemExit(98)
예제 #2
0
    def __init__(self, log, pgbouncer_conn_str, dbname, dbuser):
        self.log = log

        pgbouncer_conn_str = ConnectionString(pgbouncer_conn_str)
        if not pgbouncer_conn_str.dbname:
            pgbouncer_conn_str.dbname = 'pgbouncer'
        if pgbouncer_conn_str.dbname != 'pgbouncer':
            log.warn("pgbouncer administrative database not named 'pgbouncer'")
        self.pgbouncer_con = pg_connect(pgbouncer_conn_str)

        self.master_name = None
        self.master = None
        self.slaves = {}

        for db in self.pgbouncer_cmd('show databases', results=True):
            if db.database != dbname:
                continue

            conn_str = ConnectionString(
                'dbname=%s port=%s user=%s' % (dbname, db.port, dbuser))
            if db.host:
                conn_str.host = db.host
            con = pg_connect(conn_str)
            cur = con.cursor()
            cur.execute('select pg_is_in_recovery()')
            if cur.fetchone()[0] is True:
                self.slaves[db.name] = conn_str
            else:
                self.master_name = db.name
                self.master = conn_str

        if self.master_name is None:
            log.fatal('No master detected.')
            raise SystemExit(98)
예제 #3
0
def connect_string(user=None, dbname=None):
    """Return a PostgreSQL connection string.

    Allows you to pass the generated connection details to external
    programs like pg_dump or embed in slonik scripts.
    """
    # We must connect to the read-write DB here, so we use rw_main_master
    # directly.
    from lp.services.database.postgresql import ConnectionString
    con_str = ConnectionString(dbconfig.rw_main_master)
    if user is not None:
        con_str.user = user
    if dbname is not None:
        con_str.dbname = dbname
    return str(con_str)
예제 #4
0
def connect_string(user=None, dbname=None):
    """Return a PostgreSQL connection string.

    Allows you to pass the generated connection details to external
    programs like pg_dump or embed in slonik scripts.
    """
    # We must connect to the read-write DB here, so we use rw_main_master
    # directly.
    from lp.services.database.postgresql import ConnectionString
    con_str = ConnectionString(dbconfig.rw_main_master)
    if user is not None:
        con_str.user = user
    if dbname is not None:
        con_str.dbname = dbname
    return str(con_str)