Changeset c87ef42 in rattail-fabric


Ignore:
Timestamp:
02/22/19 14:57:13 (6 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
c8e24a5
Parents:
2393374
Message:

Fix how we run sudo commands as postgres user

per similar changes made in rattail-fabric2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail_fabric/postgresql.py

    r2393374 rc87ef42  
    5757    Execute some SQL as the 'postgres' user.
    5858    """
    59     cmd = 'sudo -u postgres psql {port} --tuples-only --no-align --command="{sql}" {database}'.format(
     59    cmd = 'psql {port} --tuples-only --no-align --command="{sql}" {database}'.format(
    6060        port='--port={}'.format(port) if port else '',
    6161        sql=sql, database=database)
    62     return sudo(cmd, shell=False)
     62    return sudo(cmd, user='postgres')
    6363
    6464
     
    7676    else: # run as postgres
    7777        kw = dict(port=port, path=path, db=database)
    78         return sudo("sudo -u postgres psql {port} --file='{path}' {db}".format(**kw), shell=False)
     78        return sudo("psql {port} --file='{path}' {db}".format(**kw), user='postgres')
    7979
    8080
     
    9292    """
    9393    if not checkfirst or not user_exists(name, port=port):
    94         sudo('sudo -u postgres createuser {port} {createdb} --no-createrole --no-superuser {name}'.format(
     94        cmd = 'createuser {port} {createdb} --no-createrole --no-superuser {name}'.format(
    9595            port='--port={}'.format(port) if port else '',
    9696            createdb='--{}createdb'.format('' if createdb else 'no-'),
    97             name=name))
     97            name=name)
     98        sudo(cmd, user='postgres')
    9899        if password:
    99100            set_user_password(name, password, port=port)
     
    121122    """
    122123    if not checkfirst or not db_exists(name, port=port):
    123         cmd = 'sudo -u postgres createdb {port} {owner} {name}'.format(
     124        cmd = 'createdb {port} {owner} {name}'.format(
    124125            port='--port={}'.format(port) if port else '',
    125126            owner='--owner={}'.format(owner) if owner else '',
    126127            name=name)
    127         sudo(cmd, shell=False)
     128        sudo(cmd, user='postgres')
    128129
    129130
     
    141142    """
    142143    if not checkfirst or db_exists(name):
    143         sudo('sudo -u postgres dropdb {0}'.format(name), shell=False)
     144        sudo('dropdb {}'.format(name), user='postgres')
    144145
    145146
     
    152153    run('touch {0}.sql'.format(name))
    153154    run('chmod 0666 {0}.sql'.format(name))
    154     sudo('sudo -u postgres pg_dump {port} {exclude_tables} --file={name}.sql {name}'.format(
     155    cmd = 'pg_dump {port} {exclude_tables} --file={name}.sql {name}'.format(
    155156        name=name,
    156157        port='--port={}'.format(port) if port else '',
    157         exclude_tables='--exclude-table-data={}'.format(exclude_tables) if exclude_tables else '',
    158     ), shell=False)
     158        exclude_tables='--exclude-table-data={}'.format(exclude_tables) if exclude_tables else '')
     159    sudo(cmd, user='postgres')
    159160    run('gzip --force {0}.sql'.format(name))
    160161    get('{0}.sql.gz'.format(name), destination)
     
    193194    # restore database on target server
    194195    run('gunzip --force {}.sql.gz'.format(name))
    195     sudo('sudo -u postgres psql --echo-errors --file={0}.sql {0}'.format(name), shell=False)
     196    sudo('psql --echo-errors --file={0}.sql {0}'.format(name), user='postgres')
    196197    run('rm {}.sql'.format(name))
Note: See TracChangeset for help on using the changeset viewer.