Changeset - eae08ad5d426
[Not reviewed]
0 1 0
Lance Edgar - 5 years ago 2019-08-10 02:44:44
ledgar@techsupport.coop
Force using sudo "as root" for various mysql commands

this lets us leverage `/root/.my.cnf` b/c it uses `sudo -H`
1 file changed with 12 insertions and 4 deletions:
0 comments (0 inline, 0 general) First comment
rattail_fabric2/mysql.py
Show inline comments
 
@@ -72,7 +72,9 @@ def create_db(c, name, checkfirst=True, user=None):
 
    Create a MySQL database.
 
    """
 
    if not checkfirst or not db_exists(c, name):
 
        c.sudo('mysqladmin create {}'.format(name))
 
        # note, we force sudo "as root" to ensure -H flag is used
 
        # (which allows us to leverage /root/.my.cnf config file)
 
        c.sudo('mysqladmin create {}'.format(name), user='root')
 
        if user:
 
            grant_access(c, name, user)
 

	
 
@@ -82,7 +84,9 @@ def drop_db(c, name, checkfirst=True):
 
    Drop a MySQL database.
 
    """
 
    if not checkfirst or db_exists(c, name):
 
        c.sudo('mysqladmin drop --force {}'.format(name))
 
        # note, we force sudo "as root" to ensure -H flag is used
 
        # (which allows us to leverage /root/.my.cnf config file)
 
        c.sudo('mysqladmin drop --force {}'.format(name), user='root')
 

	
 

	
 
def grant_access(c, dbname, username):
 
@@ -110,7 +114,9 @@ def sql(c, sql, database=''):
 
    # some crazy quoting required here, see also
 
    # http://stackoverflow.com/a/1250279
 
    sql = sql.replace("'", "'\"'\"'")
 
    return c.sudo("mysql --execute='{}' --batch --skip-column-names {}".format(sql, database))
 
    # note, we force sudo "as root" to ensure -H flag is used
 
    # (which allows us to leverage /root/.my.cnf config file)
 
    return c.sudo("mysql --execute='{}' --batch --skip-column-names {}".format(sql, database), user='root')
 

	
 

	
 
def download_db(c, name, destination=None):
 
@@ -119,7 +125,9 @@ def download_db(c, name, destination=None):
 
    """
 
    if destination is None:
 
        destination = './{}.sql.gz'.format(name)
 
    c.sudo('mysqldump --result-file={0}.sql {0}'.format(name))
 
    # note, we force sudo "as root" to ensure -H flag is used
 
    # (which allows us to leverage /root/.my.cnf config file)
 
    c.sudo('mysqldump --result-file={0}.sql {0}'.format(name), user='root')
 
    c.sudo('gzip --force {}.sql'.format(name))
 
    c.get('{}.sql.gz'.format(name), destination)
 
    c.sudo('rm {}.sql.gz'.format(name))
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now