Force using sudo "as root" for various mysql commands
this lets us leverage `/root/.my.cnf` b/c it uses `sudo -H`
This commit is contained in:
		
							parent
							
								
									3771688660
								
							
						
					
					
						commit
						eae08ad5d4
					
				
					 1 changed files with 12 additions and 4 deletions
				
			
		|  | @ -72,7 +72,9 @@ def create_db(c, name, checkfirst=True, user=None): | ||||||
|     Create a MySQL database. |     Create a MySQL database. | ||||||
|     """ |     """ | ||||||
|     if not checkfirst or not db_exists(c, name): |     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: |         if user: | ||||||
|             grant_access(c, name, user) |             grant_access(c, name, user) | ||||||
| 
 | 
 | ||||||
|  | @ -82,7 +84,9 @@ def drop_db(c, name, checkfirst=True): | ||||||
|     Drop a MySQL database. |     Drop a MySQL database. | ||||||
|     """ |     """ | ||||||
|     if not checkfirst or db_exists(c, name): |     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): | def grant_access(c, dbname, username): | ||||||
|  | @ -110,7 +114,9 @@ def sql(c, sql, database=''): | ||||||
|     # some crazy quoting required here, see also |     # some crazy quoting required here, see also | ||||||
|     # http://stackoverflow.com/a/1250279 |     # http://stackoverflow.com/a/1250279 | ||||||
|     sql = sql.replace("'", "'\"'\"'") |     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): | def download_db(c, name, destination=None): | ||||||
|  | @ -119,7 +125,9 @@ def download_db(c, name, destination=None): | ||||||
|     """ |     """ | ||||||
|     if destination is None: |     if destination is None: | ||||||
|         destination = './{}.sql.gz'.format(name) |         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.sudo('gzip --force {}.sql'.format(name)) | ||||||
|     c.get('{}.sql.gz'.format(name), destination) |     c.get('{}.sql.gz'.format(name), destination) | ||||||
|     c.sudo('rm {}.sql.gz'.format(name)) |     c.sudo('rm {}.sql.gz'.format(name)) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lance Edgar
						Lance Edgar