Fix how we run sudo commands as postgres user

This commit is contained in:
Lance Edgar 2019-02-19 22:36:24 -06:00
parent 0a33e7569b
commit bac78764d0

View file

@ -42,10 +42,10 @@ def sql(c, sql, database='', port=None):
""" """
Execute some SQL as the 'postgres' user. Execute some SQL as the 'postgres' user.
""" """
cmd = 'sudo -u postgres psql {port} --tuples-only --no-align --command="{sql}" {database}'.format( cmd = 'psql {port} --tuples-only --no-align --command="{sql}" {database}'.format(
port='--port={}'.format(port) if port else '', port='--port={}'.format(port) if port else '',
sql=sql, database=database) sql=sql, database=database)
return c.sudo(cmd, shell=False) return c.sudo(cmd, user='postgres')
def user_exists(c, name, port=None): def user_exists(c, name, port=None):
@ -61,10 +61,11 @@ def create_user(c, name, password=None, port=None, checkfirst=True, createdb=Fal
Create a PostgreSQL user account. Create a PostgreSQL user account.
""" """
if not checkfirst or not user_exists(c, name, port=port): if not checkfirst or not user_exists(c, name, port=port):
c.sudo('sudo -u postgres createuser {port} {createdb} --no-createrole --no-superuser {name}'.format( cmd = 'createuser {port} {createdb} --no-createrole --no-superuser {name}'.format(
port='--port={}'.format(port) if port else '', port='--port={}'.format(port) if port else '',
createdb='--{}createdb'.format('' if createdb else 'no-'), createdb='--{}createdb'.format('' if createdb else 'no-'),
name=name)) name=name)
c.sudo(cmd, user='postgres')
if password: if password:
set_user_password(c, name, password, port=port) set_user_password(c, name, password, port=port)
@ -91,11 +92,11 @@ def create_db(c, name, owner=None, port=None, checkfirst=True):
Create a PostgreSQL database. Create a PostgreSQL database.
""" """
if not checkfirst or not db_exists(c, name, port=port): if not checkfirst or not db_exists(c, name, port=port):
cmd = 'sudo -u postgres createdb {port} {owner} {name}'.format( cmd = 'createdb {port} {owner} {name}'.format(
port='--port={}'.format(port) if port else '', port='--port={}'.format(port) if port else '',
owner='--owner={}'.format(owner) if owner else '', owner='--owner={}'.format(owner) if owner else '',
name=name) name=name)
c.sudo(cmd) c.sudo(cmd, user='postgres')
def drop_db(c, name, checkfirst=True): def drop_db(c, name, checkfirst=True):
@ -103,7 +104,7 @@ def drop_db(c, name, checkfirst=True):
Drop a PostgreSQL database. Drop a PostgreSQL database.
""" """
if not checkfirst or db_exists(c, name): if not checkfirst or db_exists(c, name):
c.sudo('sudo -u postgres dropdb {}'.format(name)) c.sudo('dropdb {}'.format(name), user='postgres')
def download_db(c, name, destination=None, port=None, exclude_tables=None): def download_db(c, name, destination=None, port=None, exclude_tables=None):
@ -114,11 +115,11 @@ def download_db(c, name, destination=None, port=None, exclude_tables=None):
destination = './{}.sql.gz'.format(name) destination = './{}.sql.gz'.format(name)
c.run('touch {}.sql'.format(name)) c.run('touch {}.sql'.format(name))
c.run('chmod 0666 {}.sql'.format(name)) c.run('chmod 0666 {}.sql'.format(name))
c.sudo('sudo -u postgres pg_dump {port} {exclude_tables} --file={name}.sql {name}'.format( cmd = 'pg_dump {port} {exclude_tables} --file={name}.sql {name}'.format(
name=name, name=name,
port='--port={}'.format(port) if port else '', port='--port={}'.format(port) if port else '',
exclude_tables='--exclude-table-data={}'.format(exclude_tables) if exclude_tables else '', exclude_tables='--exclude-table-data={}'.format(exclude_tables) if exclude_tables else '')
)) c.sudo(cmd, user='postgres')
c.run('gzip --force {}.sql'.format(name)) c.run('gzip --force {}.sql'.format(name))
c.get('{}.sql.gz'.format(name), destination) c.get('{}.sql.gz'.format(name), destination)
c.run('rm {}.sql.gz'.format(name)) c.run('rm {}.sql.gz'.format(name))
@ -155,5 +156,5 @@ def clone_db(c, name, owner, download, user='rattail', force=False, workdir=None
# restore database on target server # restore database on target server
c.run('gunzip --force {}.sql.gz'.format(name)) c.run('gunzip --force {}.sql.gz'.format(name))
c.sudo('sudo -u postgres psql --echo-errors --file={0}.sql {0}'.format(name)) c.sudo('psql --echo-errors --file={0}.sql {0}'.format(name), user='postgres')
c.run('rm {}.sql'.format(name)) c.run('rm {}.sql'.format(name))