Changeset - bac78764d0a8
[Not reviewed]
0 1 0
Lance Edgar - 6 years ago 2019-02-19 22:36:35
ledgar@techsupport.coop
Fix how we run sudo commands as postgres user
1 file changed with 12 insertions and 11 deletions:
0 comments (0 inline, 0 general) First comment
rattail_fabric2/postgresql.py
Show inline comments
 
@@ -42,10 +42,10 @@ def sql(c, 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 c.sudo(cmd, shell=False)
 
    return c.sudo(cmd, user='postgres')
 

	
 

	
 
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.
 
    """
 
    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 '',
 
            createdb='--{}createdb'.format('' if createdb else 'no-'),
 
            name=name))
 
            name=name)
 
        c.sudo(cmd, user='postgres')
 
        if password:
 
            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.
 
    """
 
    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 '',
 
            owner='--owner={}'.format(owner) if owner else '',
 
            name=name)
 
        c.sudo(cmd)
 
        c.sudo(cmd, user='postgres')
 

	
 

	
 
def drop_db(c, name, checkfirst=True):
 
@@ -103,7 +104,7 @@ def drop_db(c, name, checkfirst=True):
 
    Drop a PostgreSQL database.
 
    """
 
    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):
 
@@ -114,11 +115,11 @@ def download_db(c, name, destination=None, port=None, exclude_tables=None):
 
        destination = './{}.sql.gz'.format(name)
 
    c.run('touch {}.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,
 
        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.get('{}.sql.gz'.format(name), destination)
 
    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
 
    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))
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now