Fix how we run sudo commands as postgres user
per similar changes made in rattail-fabric2
This commit is contained in:
parent
2393374923
commit
c87ef42bad
|
@ -56,10 +56,10 @@ def sql(sql, database='', port=None):
|
|||
"""
|
||||
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 '',
|
||||
sql=sql, database=database)
|
||||
return sudo(cmd, shell=False)
|
||||
return sudo(cmd, user='postgres')
|
||||
|
||||
|
||||
def script(path, database='', port=None, user=None, password=None):
|
||||
|
@ -75,7 +75,7 @@ def script(path, database='', port=None, user=None, password=None):
|
|||
|
||||
else: # run as postgres
|
||||
kw = dict(port=port, path=path, db=database)
|
||||
return sudo("sudo -u postgres psql {port} --file='{path}' {db}".format(**kw), shell=False)
|
||||
return sudo("psql {port} --file='{path}' {db}".format(**kw), user='postgres')
|
||||
|
||||
|
||||
def user_exists(name, port=None):
|
||||
|
@ -91,10 +91,11 @@ def create_user(name, password=None, port=None, checkfirst=True, createdb=False)
|
|||
Create a PostgreSQL user account.
|
||||
"""
|
||||
if not checkfirst or not user_exists(name, port=port):
|
||||
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 '',
|
||||
createdb='--{}createdb'.format('' if createdb else 'no-'),
|
||||
name=name))
|
||||
name=name)
|
||||
sudo(cmd, user='postgres')
|
||||
if password:
|
||||
set_user_password(name, password, port=port)
|
||||
|
||||
|
@ -120,11 +121,11 @@ def create_db(name, owner=None, port=None, checkfirst=True):
|
|||
Create a PostgreSQL database.
|
||||
"""
|
||||
if not checkfirst or not db_exists(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 '',
|
||||
owner='--owner={}'.format(owner) if owner else '',
|
||||
name=name)
|
||||
sudo(cmd, shell=False)
|
||||
sudo(cmd, user='postgres')
|
||||
|
||||
|
||||
def create_schema(name, dbname, owner='rattail', port=None):
|
||||
|
@ -140,7 +141,7 @@ def drop_db(name, checkfirst=True):
|
|||
Drop a PostgreSQL database.
|
||||
"""
|
||||
if not checkfirst or db_exists(name):
|
||||
sudo('sudo -u postgres dropdb {0}'.format(name), shell=False)
|
||||
sudo('dropdb {}'.format(name), user='postgres')
|
||||
|
||||
|
||||
def download_db(name, destination=None, port=None, exclude_tables=None):
|
||||
|
@ -151,11 +152,11 @@ def download_db(name, destination=None, port=None, exclude_tables=None):
|
|||
destination = './{0}.sql.gz'.format(name)
|
||||
run('touch {0}.sql'.format(name))
|
||||
run('chmod 0666 {0}.sql'.format(name))
|
||||
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,
|
||||
port='--port={}'.format(port) if port else '',
|
||||
exclude_tables='--exclude-table-data={}'.format(exclude_tables) if exclude_tables else '',
|
||||
), shell=False)
|
||||
exclude_tables='--exclude-table-data={}'.format(exclude_tables) if exclude_tables else '')
|
||||
sudo(cmd, user='postgres')
|
||||
run('gzip --force {0}.sql'.format(name))
|
||||
get('{0}.sql.gz'.format(name), destination)
|
||||
run('rm {0}.sql.gz'.format(name))
|
||||
|
@ -192,5 +193,5 @@ def clone_db(name, owner, download, user='rattail', force=False, workdir=None):
|
|||
|
||||
# restore database on target server
|
||||
run('gunzip --force {}.sql.gz'.format(name))
|
||||
sudo('sudo -u postgres psql --echo-errors --file={0}.sql {0}'.format(name), shell=False)
|
||||
sudo('psql --echo-errors --file={0}.sql {0}'.format(name), user='postgres')
|
||||
run('rm {}.sql'.format(name))
|
||||
|
|
Loading…
Reference in a new issue