Пример #1
0
def tunePostgresParams(pgsql, config):
    if pgsql is None:
        pgsql = uPgSQL.PostgreSQLConfig(version = uPgSQL.TargetPostgreSQLVersion, commander = config.__dict__.get("commander"))

    tunePostgresLogs(pgsql.get_commander())

    uLogging.info("Stopping PostgreSQL service before its tuning is initiated...")
    pgsql.stop()
    tunePostgresParamsImpl(pgsql)
    pgsql.start()
Пример #2
0
def configureDatabaseImpl(pgsql, config, access_ips=[]):
    '''access_ips must be of  type [(ip, auth_method)] and provide additional ip-based access to database.'''
    if pgsql is None:
        pgsql = uPgSQL.PostgreSQLConfig(version = uPgSQL.TargetPostgreSQLVersion, commander = config.__dict__.get("commander"))
        pgsql.cleanup()
        pgsql.init_db()

    run = pgsql.get_commander()
    uLogging.info("Configuring installed PostgreSQL %s ..." % pgsql.get_version())
    tunePostgresParamsImpl(pgsql)

    pg_hba = pgsql.get_pghba_conf()
    #Replace ident by md5 for local IPv6
    run("sed -i -E 's|([ \\t]*host[ \\t]+all[ \\t]+all[ \\t]+::1/128[ \\t]+).*|\\1md5|g' "+pg_hba)
    #Replace ident by md5 for local IPv4
    run("sed -i -E 's|([ \\t]*host[ \\t]+all[ \\t]+all[ \\t]+127\.0\.0\.1/32[ \\t]+).*|\\1md5|g' "+pg_hba)
    #Remove peer for local sockets for all users but postgres
    run("sed -i -E 's|([ \\t]*local[ \\t]+all[ \\t]+)(all)([ \\t]+.*)|\\1postgres\\3|g' "+pg_hba)

    #Odin Automation prolog
    run("sed -i -e '$,+0a\\# Odin Automation required parameters (BEGIN)' "+pg_hba)

    #Trusting communication IPs
    params = [{'ip_address': ip, 'method':auth_method} for ip, auth_method in access_ips if ip and auth_method]
    for p in params:
        trusted_cfg_text = (uPgSQL.pg_hba_conf_tail_template % p)
        for s in trusted_cfg_text.splitlines():
            run("sed -i -e '$,+0a\\%s' %s" % (s, pg_hba))

    #Create ssl certificates
    create_pgslq_certificates(run, pgsql.get_data_dir())

    #Odin Automation epilog
    run("sed -i -e '$,+0a\\# Odin Automation required parameters (END)' "+pg_hba)
    uLogging.info("Configuring has finished!")

    uLogging.info("Starting PostgreSQL service ...")
    #Starting newly installed server
    pgsql.start()
    uLogging.info("PostgreSQL service has started!")

    uLogging.info("Post-configuring installed PostgreSQL to be started on OS start up ")
    #server has been started successfully so that
    #we mark server to be started on OS start up
    pgsql.set_autostart()
    uLogging.info("Post-configuration has finished!")

    uLogging.info("Starting PostgreSQL logging configuration")
    tunePostgresLogs(run)
    pgsql.reload()
    uLogging.info("PostgreSQL logging configuration completed")
Пример #3
0
def configureODBC(config):
    # defaults for external DB:
    odbc_driver = '/usr/lib64/psqlodbc.so'

    if uUtil.isLocalAddress(config.database_host):
        pgsql = uPgSQL.PostgreSQLConfig()
        odbc_driver = pgsql.get_odbc_driver()

    path = os.path.join(config.rootpath, 'etc')
    params = {'driver': odbc_driver, 'setup': odbc_driver,
              'dsn': config.dsn, 'database_name': config.database_name,
              'database_host': config.database_host, 'database_port': config.database_port}
    for fn, template in [('odbc.ini', uPgSQL.odbc_ini_template), ('odbcinst.ini', uPgSQL.odbcinst_ini_template)]:
        f = open(os.path.join(path, fn), 'w')
        f.write(template % params)
        f.close()
Пример #4
0
def removePgSlave(slaveHostID, masterRootPwd):
    if not uLogging.logfile:
        uLogging.init2("/var/log/pa/deregister_slave.log", True, False)

    slave = uPEM.getHost(slaveHostID)
    slaveCommunicationIP = uPEM.getHostCommunicationIP(slaveHostID)
    runOnSlave = lambda cmd: uHCL.runHCLCmd(slaveHostID, cmd)
    uLogging.info("Slave database server at %s (%s) is going to be removed.", slaveCommunicationIP, slave.name)

    pghaSettings = getPghaSettings()

    pgsqlOnSlave = None
    try:
        pgsqlOnSlave = uPgSQL.PostgreSQLConfig(commander = runOnSlave)
    except Exception, e:
        uLogging.info("Could not find slave database server at %s (%s): %s", slaveCommunicationIP, slave.name, str(e))
        return
Пример #5
0
def cleanUpOldPostgresRPMs(run):
    currConfig = uPgSQL.PostgreSQLConfig(commander = run)
    run = currConfig.get_commander()
    vergrp = "|".join(["".join(i.split(".")) for i in uPgSQL.SupportedPostgreSQLVersions])
    postgreSqlRPMs = run("rpm -qa --queryformat '%{name}.%{arch}\\n' | grep -E '^postgresql("+vergrp+")??(\\-(libs|upgrade|server))??\\.(x86_64|i686)'").strip().splitlines()
    if not postgreSqlRPMs:
        return
    postgresqlDirsToErase = []
    postgreSqlRPMsToErase = []
    currVersion = currConfig.get_version_as_int()
    for rpmName in postgreSqlRPMs:
        rpmVer = run("rpm -q --queryformat '%{version}' "+rpmName).strip()
        rpmVer = ".".join(rpmVer.split(".")[:2])
        rpmVerInt = int("".join(rpmVer.split(".")))
        if rpmVerInt < currVersion:
            if "-server" in rpmName:
                postgresqlDirsToErase.append("/usr/pgsql-"+rpmVer)
            postgreSqlRPMsToErase.append(rpmName)
    if postgreSqlRPMsToErase:
        #On (CentOS|RHEL)6 PA is 32 bits application thus it continues depending on postgresql95-libs.i686
        if currConfig.get_os_version().startswith("6."):
            if 'postgresql95-libs.i686' in postgreSqlRPMsToErase:
                postgreSqlRPMsToErase.remove('postgresql95-libs.i686') #That's why we excluding this RPM from the list to erase
        if postgreSqlRPMsToErase:
            run("rpm -e --nodeps "+" ".join(postgreSqlRPMsToErase))
    for d in postgresqlDirsToErase:
        run("rm -fr "+d)
    #checking psql tool is available
    #for more details see following issue https://jira.int.zone/browse/POA-105993
    psqlOk = True
    try:
        run("psql --version 2> /dev/null")
    except:
        psqlOk = False
    if not psqlOk:
        #postCleanUpRepairRPMs callable object is assigned by 173000-APS-34471-PostgreSQL-91-to-96-migration.py upgrade action
        #it happens when we deal with the PostgreSQL server running remotely - in such a case DB node cna not connect to pa-repo yum repository
        #that's why it is required to run very special remote commad to repair postgresql RPM
        #for more details see this bug https://jira.int.zone/browse/POA-109067
        repairRPMs = run.__dict__.get("postCleanUpRepairRPMs")
        if repairRPMs is None:
            run("yum -y reinstall postgresql"+str(currVersion).replace(".", ""))
        else:
            repairRPMs()
Пример #6
0
def upgradeDatabase(config, progress, access_ips):
    #with the potential customized commander that could operate on remote PostgreSQL server

    #postgresql version to upgrade onto
    trgVer = config.__dict__.get("target_postgresql_version")
    if trgVer is None:
        trgVer = uPgSQL.TargetPostgreSQLVersion

    #current one
    pgsqlOrgn = uPgSQL.PostgreSQLConfig(commander = config.__dict__.get("commander"))
    #next version
    run = pgsqlOrgn.get_commander()
    pgsqlNext = uPgSQL.PostgreSQLConfig(version = trgVer, commander = run)

    #check version is the same or even newer
    if pgsqlOrgn.get_version_as_int() >= pgsqlNext.get_version_as_int():
        uLogging.info("PostgreSQL runs required or newer version %s, nothing to uprade." % (pgsqlOrgn.get_version(),))
        return False#so nothing to upgrade

    uLogging.info("Stopping original server...")
    pgsqlOrgn.stop(True)
    uLogging.info("Original server has stopped!")

    uLogging.info("Initilizing database...")
    pgsqlNext.cleanup()
    pgsqlNext.init_db()
    uLogging.info("Database has been initialized!")

    uLogging.info("Upgrading databases...")
    oldBinDir = pgsqlOrgn.get_bin_dir()
    oldDataDir = pgsqlOrgn.get_data_dir()
    newBinDir = pgsqlNext.get_bin_dir()
    newDataDir = pgsqlNext.get_data_dir()

    orgnlConf = None
    orgnlConfBu = None
    if pgsqlOrgn.get_version() == "9.0":
        if pgsqlNext.get_version_as_int() >= 95:
            orgnlConf = pgsqlOrgn.get_postgresql_conf()
            orgnlConfBu = orgnlConf + "-orig"
            run("cp -f "+orgnlConf+" "+orgnlConfBu)
            #this is required unless the unix domain socket would not be found by new pg_upgrade
            run("sed -i -E 's|[ \\t\\#]*(unix_socket_directory[ \\t]*=[ \\t]*)|\\1\\x27/var/run/postgresql/\\x27 # PA Installer |g' "+orgnlConf)
    elif pgsqlOrgn.get_version() == "9.2":
        #check unix_socket_directories is presented in the config file - this is used as a marker of patched RHEL postgresql-9.2
        #for more details see https://jira.int.zone/browse/POA-105930
        socket_dirs_cfg_is_presented = run("grep -E '\\bunix_socket_directories\\b' %s 2> /dev/null || echo -n" % pgsqlOrgn.get_postgresql_conf()).strip()
        if socket_dirs_cfg_is_presented:
            orgnlConf = oldBinDir+"/pg_ctl"
            orgnlConfBu = orgnlConf+"-orig"
            run("mv -f "+orgnlConf+" "+orgnlConfBu)
            run("echo '#!/bin/bash' > " + orgnlConf)
            run("echo '\"$0\"-orig \"${@/unix_socket_directory/unix_socket_directories}\"' >> " + orgnlConf)
            run("chmod +x " + orgnlConf)
    try:
        cmdUpgrade = "'%s/pg_upgrade --new-port=8352 --old-port=8352 --old-bindir=%s --new-bindir=%s --old-datadir=%s --new-datadir=%s'" % (newBinDir, oldBinDir, newBinDir, oldDataDir, newDataDir)
        run("su - postgres -c "+ cmdUpgrade)
    finally:
        if orgnlConfBu:
            #restoring original configuration file
            run("mv -f "+orgnlConfBu+" "+orgnlConf)
    uLogging.info("Upgrading databases has fnished!")

    #performing post upgrade configuration
    configureDatabaseImpl(pgsqlNext, config, access_ips)
    pgsqlOrgn.set_autostart(False)
    return True
Пример #7
0
def allowTcpConnectionsFromHost(hostIp, pgDbServerRootPwd):
    pgDbServerIp = _getPgDbInfo()[0]
    pgDbServerCmd = _getCommandRunner(pgDbServerIp, pgDbServerRootPwd)
    pgDb = uPgSQL.PostgreSQLConfig(commander=pgDbServerCmd)
    _tunePgHba(pgDbServerCmd, "host", "all", "all", hostIp, pgDb.get_pghba_conf())
    pgDb.reload()
Пример #8
0
def deployPgSlave(slaveHostID, isBillingMaster, masterRootPwd, readOnlyUserType, additionalIPs, slaveScript, slaveScriptArgs):
    if not uLogging.logfile:
        uLogging.init2("/var/log/pa/register_slave.log", True, False)
    uLogging.info("Deploying PostgreSQL slave server on PA service node #%d...", slaveHostID)

    masterHostID = 0
    pghaSettings = getPghaSettings()

    if not isBillingMaster:
        if slaveHostID == 1:
            raise Exception("The target slave host is MN: no possibility to use MN node as a database replica.")

        row = _getPgDbInfo()
        databaseName = row[2]
        if pghaSettings.isHa:
            masterAddr = getHaMasterAddr(pghaSettings)
            masterPort = pghaSettings.haBackendPort
            targetReplicationSourceMasterAddr = pghaSettings.vip_2
        else:
            masterAddr = row[0]
            masterPort = int(row[1])
            targetReplicationSourceMasterAddr = masterAddr
        uLogging.info("Master DB location: '%s at %d'" % (masterAddr, masterPort))

        runOnMaster = _getCommandRunner(masterAddr, masterRootPwd)
        if not runOnMaster.isLocal:
            uLogging.info("Master is automation database server running remotely at %s:%d.", masterAddr, masterPort)
        else:
            uLogging.info("Master is automation database server running locally at %s:%d.", masterAddr, masterPort)
            masterHostID = 1
    else:
        if slaveHostID in (b.get_host_id() for b in uBilling.get_billing_hosts()):
            raise Exception("The target slave host is billing node: no possibility to use billing node as a database slave.")
        dbParams = uBilling.PBAConf.getBillingDBPrams()
        masterAddr = uBilling.PBAConf.getBillingDBHost()
        masterPort = int(uBilling.PBAConf.getBillingDBPort())
        databaseName = uBilling.PBAConf.getBillingDBName()
        runOnMaster = _getCommandRunner(masterAddr, masterRootPwd)
        targetReplicationSourceMasterAddr = masterAddr
        uLogging.info("Master is billing database server running at %s:%d.", masterAddr, masterPort)
        masterHostID = None

    isPermitted = False
    slave = uPEM.getHost(slaveHostID)
    if not runOnMaster.isLocal:
        try:
            runCheck = lambda cmd: uUtil.runLocalCmd(cmd)
            checkHostPermittedToBeReplicaOfDB(runCheck, slave.name)
            isPermitted = True
        except:
            pass
    if not isPermitted:
        checkHostPermittedToBeReplicaOfDB(runOnMaster, slave.name)

    slaveCommunicationIP = uPEM.getHostCommunicationIP(slaveHostID)
    ipAddrJoined = ipAddrToUserUniqPostfix(slaveCommunicationIP)
    replUserName = "******"+ipAddrJoined
    replUserPwd = uUtil.generate_random_password(16)

    runOnSlave = lambda cmd: uHCL.runHCLCmd(slaveHostID, cmd)
    uLogging.info("Slave database server is going to be deployed at %s (%s)", slaveCommunicationIP, slave.name)
    pgsqlOnMaster = uPgSQL.PostgreSQLConfig(commander = runOnMaster)
    pgsqlVer = str(pgsqlOnMaster.get_version_as_int())

    uLogging.info("Current running PostgreSQL version is '%s'" % pgsqlVer)
    verifyPostgresCertificate(pgsqlOnMaster)

    uLogging.info("Instaling PostgreSQL Server on the slave...")
    runOnSlave("yum install -y odin-perftools postgresql%s postgresql%s-server postgresql%s-contrib" % (pgsqlVer, pgsqlVer, pgsqlVer))
    runOnSlave("yum reinstall -y odin-perftools postgresql%s postgresql%s-server postgresql%s-contrib" % (pgsqlVer, pgsqlVer, pgsqlVer))
    uLogging.info("Installation has finished!")

    uLogging.info("Initializing database on slave...")
    pgsqlOnSlave = uPgSQL.PostgreSQLConfig(commander = runOnSlave)
    pgsqlOnSlave.cleanup()
    pgsqlOnSlave.init_db()
    uLinux.configureDatabaseImpl(pgsqlOnSlave, None, [])
    uLogging.info("Saving some slave personal configuration files...")

    slavePersonalFilesBu = []
    slavePersonalFiles = (
        pgsqlOnSlave.get_data_dir()+"/server.key",
        pgsqlOnSlave.get_data_dir()+"/server.crt",
#        pgsqlOnSlave.get_postgresql_conf(),
        pgsqlOnSlave.get_pghba_conf()
    )
    slavePersonalDir = os.path.dirname(pgsqlOnSlave.get_data_dir().rstrip("/"))
    for pf in slavePersonalFiles:
        runOnSlave(""" su - postgres -c 'cp -f "%s" "%s/"' """ % (pf, slavePersonalDir))
        slavePersonalFilesBu.append(os.path.join(slavePersonalDir, os.path.basename(pf)))
    pgsqlOnSlave.stop()
    uLogging.info("Database has been initialized!")

    uLogging.info("Enabling replication connection from slave to master...")
    runOnMaster(""" su - postgres -c "psql --port=%d -c \\"DROP ROLE IF EXISTS %s\\"" """ % (masterPort, replUserName,))
    runOnMaster(""" su - postgres -c "psql --port=%d -c \\"CREATE ROLE %s WITH REPLICATION ENCRYPTED PASSWORD '%s' LOGIN CONNECTION LIMIT 8\\"" """ % (masterPort, replUserName, replUserPwd))

    uLogging.info("Creating read-only user and users to be replicated from master to slave for farther readonly use on the slave node.")
    roUserName = "******"+ipAddrJoined
    roUserPwd = uUtil.generate_random_password(32)
    # Provide the reentrancy, make sure the database doesn't contain objects created by possible previous launches
    runOnMaster(""" su - postgres -c "psql --port=%d --dbname=%s -c \\"REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM %s\\"" 2> /dev/null || echo -n """ % (masterPort, databaseName, roUserName))
    runOnMaster(""" su - postgres -c "psql --port=%d -c \\"REVOKE EXECUTE ON FUNCTION func_stat_wal_receiver() from %s\\"" 2> /dev/null || echo -n """ % (masterPort, roUserName,))
    runOnMaster(""" su - postgres -c "psql --port=%d -c \\"DROP ROLE IF EXISTS %s\\"" """ % (masterPort, roUserName,))
    runOnMaster(""" su - postgres -c "psql --port=%d -c \\"CREATE ROLE %s WITH ENCRYPTED PASSWORD '%s' LOGIN\\"" """ % (masterPort, roUserName, roUserPwd))

    #add ability to monitor replication status for RO user
    def psql_as_postgres(input):
        return r'su - postgres -c "psql --port=%d -c \"%s\""' % (masterPort, input)
    runOnMaster(psql_as_postgres("DROP FUNCTION IF EXISTS func_stat_wal_receiver();"))
    psql11_fields = []
    if LooseVersion(pgsqlVer) >= LooseVersion("11"):
        psql11_fields = ["cast('' as text) as sender_host", "-1 as sender_port"]
    pg_stat_wal_receiver_sql = 'CREATE FUNCTION func_stat_wal_receiver() RETURNS SETOF pg_stat_wal_receiver as '
    columns_to_select = ", ".join(["pid", "status", "receive_start_lsn", "receive_start_tli", "received_lsn", "received_tli",
                                   "last_msg_send_time", "last_msg_receipt_time", "latest_end_lsn", "latest_end_time",
                                   "cast('' as text) as slot_name"] +
                                   psql11_fields +
                                   ["cast('' as text) as conninfo"])
    pg_stat_wal_receiver_sql += r'\\$\\$ select %s from pg_stat_wal_receiver; \\$\\$ LANGUAGE sql SECURITY DEFINER;' % columns_to_select
    runOnMaster(psql_as_postgres(pg_stat_wal_receiver_sql))
    runOnMaster(psql_as_postgres("REVOKE EXECUTE ON FUNCTION func_stat_wal_receiver() FROM public;"))
    runOnMaster(psql_as_postgres("GRANT EXECUTE ON FUNCTION func_stat_wal_receiver() to %s;" % roUserName))

    if readOnlyUserType == "uinode":
        uiBoosterTables = ("aps_resource", "aps_property_value", "aps_resource_link", "aps_application", "aps_property_info",
                           "aps_package", "aps_relation_info", "aps_relation_types", "aps_type_info", "aps_type_inheritance", "aps_package_series", "aps_package_service",
                           "aps_property_enum_info", "aps_type_info_to_package",
                           "aps_operation_param", "aps_operation_info")
        runOnMaster(""" su - postgres -c "psql --port=%d --dbname=%s -c \\"GRANT SELECT ON TABLE %s TO %s\\"" """ % (masterPort, databaseName, ",".join(uiBoosterTables), roUserName))
    else:
        runOnMaster(""" su - postgres -c "psql --port=%d --dbname=%s -c \\"GRANT SELECT ON ALL TABLES IN SCHEMA public TO %s\\"" """ % (masterPort, databaseName, roUserName))
    uLogging.info("Read-only user has been created.")

    _tunePgHba(runOnMaster, "hostssl", "replication", replUserName, slaveCommunicationIP, pgsqlOnMaster.get_pghba_conf())

    if int(getWalKeepSegments(runOnMaster, masterPort)) != 16384:
        runOnMaster(""" sed -i '/^[ \t]*wal_keep_segments[ \t]*=.*/d' "%s" """ % (pgsqlOnMaster.get_postgresql_conf(),))
        runOnMaster(""" sed -i -e '$,+0a\wal_keep_segments = 16384' "%s" """ % (pgsqlOnMaster.get_postgresql_conf(),))

    #For more details see the following KB: https://kb.cloudblue.com/en/115916
    #Chain called Postgres could be absent if KB is not applied, so that we have to add that rules only in case if KB applied
    if runOnMaster(""" iptables -nL Postgres 2> /dev/null || echo -n """):
        uLogging.info("Configuring iptables for replication access")
        iptablesConfigAllowDb(run = runOnMaster, slaveCommunicationIP= slaveCommunicationIP, masterPort=masterPort)
        uLogging.info("Configuring iptables on master done!")

        if pghaSettings.isHa:
            pghaSlaveAddr = pghaSettings.bDbNode if masterAddr == pghaSettings.aDbNode else pghaSettings.aDbNode
            uLogging.info("Configuring iptables for replication access on PGHA slave '%s'" % pghaSlaveAddr)
            runOnPghaSlave = uUtil.getSSHRemoteRunner(pghaSlaveAddr, masterRootPwd) # providing of password is an extra measure since SSH certificates are distributed
            iptablesConfigAllowDb(run = runOnPghaSlave, slaveCommunicationIP= slaveCommunicationIP, masterPort=masterPort)
            uLogging.info("Configuring iptables o PGHA slave done!")

    pgsqlOnMaster.reload()
    uLogging.info("Replication connection has been enabled!")

    if pghaSettings.isHa:
        forceHaMasterSyncConf(runOnMaster)

    uLogging.info("Setting up initial database replication...")
    cleanPgCertificate(pgsqlOnSlave.get_data_dir(), runOnSlave) # clean certificate if exists
    baseBackupCmd = """ su - postgres -c 'PGPASSWORD=%s "%s/pg_basebackup" -X stream --host=%s --port=%s
"--pgdata=%s" "--username=%s" --write-recovery-conf --checkpoint=fast' """ % (replUserPwd, pgsqlOnSlave.get_bin_dir(), targetReplicationSourceMasterAddr, str(masterPort), pgsqlOnSlave.get_data_dir(), replUserName)
    pgsqlOnSlave.cleanup()
    #targeting errors like f.e. this-> ERROR:  could not open file "./pg_hba.conf.bak": Permission denied
    runOnMaster(""" chown -R postgres:postgres "%s" """ % (pgsqlOnMaster.get_data_dir(),))
    runOnSlave(baseBackupCmd)
    uLogging.info("Initial database replication has been done!")

    uLogging.info("Doing post-configuration...")

    dotPostgresDir = os.path.dirname(os.path.dirname(pgsqlOnSlave.get_data_dir().rstrip("/"))) + "/.postgresql"
    runOnSlave(""" su - postgres -c 'mkdir -p "%s"' """ % (dotPostgresDir,))
    runOnSlave(""" su - postgres -c 'cp -f "%s/%s" "%s/%s"' """ % (pgsqlOnSlave.get_data_dir(), "server.crt", dotPostgresDir, "root.crt"))

    for i, pf in enumerate(slavePersonalFilesBu):
        runOnSlave(""" su - postgres -c 'mv -f "%s" "%s/"' """ % (pf, os.path.dirname(slavePersonalFiles[i])))

    uLinux.tunePostgresParamsImpl(pgsqlOnSlave)

    runOnSlave("sed -i -E 's|(.*[ \\t]+sslmode[ \\t]*=[ \\t]*)prefer([ \\t]+.*)|\\1verify-ca\\2|g' \"%s/recovery.conf\" " % (pgsqlOnSlave.get_data_dir().rstrip("/"),))
    #marking server as a hot standby
    runOnSlave(""" sed -i '/^[ \t]*hot_standby[ \t]*=.*/d' "%s" """ % (pgsqlOnSlave.get_postgresql_conf(),))
    runOnSlave(""" sed -i -e '$,+0a\\hot_standby = on' "%s" """ % (pgsqlOnSlave.get_postgresql_conf(),))
    if additionalIPs is not None:
        for ip in additionalIPs:
            ipEsc = ip.replace(".", "\\.")
            runOnSlave(""" sed -i -e '$,+0a\hostssl     all    all     %s\/32     md5' "%s" """ % (ipEsc, pgsqlOnSlave.get_pghba_conf()))
    runOnSlave(""" sed -i '/^listen_addresses/s/\*/127.0.0.1/g' "%s" """ % (pgsqlOnSlave.get_postgresql_conf(),))
    uLogging.info("Post-configuration has been done!")

    uLogging.info("Starting new slave database server!")
    pgsqlOnSlave.restart()
    pgsqlOnSlave.set_autostart()
    waitSlaveRecoveryComplete(runOnSlave) # make sure recovery stage is complete

    uLinux.tunePostgresLogs(runOnSlave)
    pgsqlOnSlave.reload()
    uLogging.info("New slave database server has started!")

    if slaveScript:
        uLogging.info("Running post configuration script on slave: %s", slaveScript)
        cbCmd = """python "%s" connect_slave "%s" "%s" "%s" "%s" """ % (slaveScript, slaveCommunicationIP, databaseName, roUserName, roUserPwd)
        for a in slaveScriptArgs:
            cbCmd = cbCmd + ' "%s" ' % a
        runOnSlave(cbCmd)
        uLogging.info("Post configuration has been done!")

    rv = DeployPgSlaveResult()
    rv.replUserName = replUserName
    rv.roUserName = roUserName
    rv.masterHostID = masterHostID
    rv.masterAddr = masterAddr
    return rv
Пример #9
0
        masterPort = pghaSettings.haBackendPort
    else:
        masterAddr = recoveryHost
        masterPort = int(conInfoDict["port"])

    if recoveryHost != uPEM.getHostCommunicationIP(1): #MN node?
        billingHostID = None
        runOnMaster = None
    else:
        uLogging.info("Master is automation database server running locally at %s:%d.", masterAddr, masterPort)
        runOnMaster = lambda cmd: uUtil.runLocalCmd(cmd)
    if runOnMaster is None: #Master is running as an external database server
        uLogging.info("Master is automation database server running remotely at %s:%d.", masterAddr, masterPort)
        runOnMaster = uUtil.getSSHRemoteRunner(masterAddr, masterRootPwd)

    pgsqlOnMaster = uPgSQL.PostgreSQLConfig(commander = runOnMaster)
    replUserName = conInfoDict["user"]
    uLogging.info("Disabling replication connection from slave to master...")
    ipEscpd = slaveCommunicationIP.replace(".", "\\.")
    runOnMaster(""" sed -i '/^[ \t]*hostssl[ \t]\+replication[ \t]\\+%s[ \t]\+%s\/32[ \t]\+md5[ \t]*/d' "%s" """ % (replUserName, ipEscpd, pgsqlOnMaster.get_pghba_conf()))
    runOnMaster(""" su - postgres -c "psql --port=%d -c \\"DROP ROLE IF EXISTS %s\\"" """ % (masterPort, replUserName,))

    if pghaSettings.isHa:
        forceHaMasterSyncConf(runOnMaster)

    uLogging.info("Dropping slave read only user...")
    runOnMaster(r'su - postgres -c "psql --port=%d -c \"DROP FUNCTION IF EXISTS func_stat_wal_receiver();\""' % masterPort)
    roUserName = "******"+ipAddrToUserUniqPostfix(slaveCommunicationIP)
    for db in listPgDatabases(runOnMaster, masterPort):
        db = db.strip()
        if not (db in ("postgres", "template0", "template1")):