Commits

Mikhail Korobov  committed 1c6a104

mysql: boilerplate is reduced a bit, missing docstrings are added, mysql_grant_permissions command is now public

  • Participants
  • Parent commits 455fc26

Comments (0)

Files changed (1)

File fab_deploy/mysql.py

 from fab_deploy import system
 
 
-__all__ = ['mysql_execute', 'mysql_install', 'mysql_create_db', 'mysql_create_user', 'mysqldump']
+__all__ = ['mysql_execute', 'mysql_install', 'mysql_create_db',
+           'mysql_create_user', 'mysql_grant_permissions', 'mysqldump']
 
 
 MYSQL_CREATE_USER = """CREATE USER '%(db_user)s'@'localhost' IDENTIFIED BY '%(db_password)s';"""
         env.conf.DB_ROOT_PASSWORD = prompt('Please enter MySQL root password:')
     return env.conf.DB_ROOT_PASSWORD
 
+def _credentials(db_name=None, db_user=None, db_password=None):
+    db_name = db_name or env.conf.DB_NAME
+    db_user = db_user or env.conf.DB_USER
+    if db_password is None:
+        db_password = env.conf.DB_PASSWORD
+    return db_user, db_password, db_name
 
 @utils.run_as_sudo
 def mysql_install():
     if dir is None:
         dir = env.conf.ENV_DIR + '/var/backups'
         run('mkdir -p ' + dir)
-    if db_name is None:
-        db_name = env.conf.DB_NAME
-    if db_user is None:
-        db_user = env.conf.DB_USER
-    if db_password is None:
-        db_password = env.conf.DB_PASSWORD
+
+    db_name, db_user, db_password = _credentials(db_name, db_user, db_password)
 
     now = datetime.now().strftime('%Y.%m.%d-%H.%M')
-    run('mysqldump --user="%s" --password="%s" %s > %s/%s%s.sql' % (db_user, db_password,
-                                                                    db_name, dir, db_name, now))
+    filename = '%s/%s%s.sql' % (dir, db_name, now)
+    run('mysqldump --user="%s" --password="%s" %s > %s' % (
+                        db_user, db_password, db_name, filename))
 
 def mysql_execute(sql, user=None, password=None):
     """ Executes passed sql command using mysql shell. """
     user = user or env.conf.DB_USER
+
     if user == 'root' and password is None:
         password = _get_root_password()
     elif password is None:
     return run('echo "%s" | mysql --user="%s" --password="%s"' % (sql, user , password))
 
 def mysql_create_user(db_user=None, db_password=None):
-    if db_user is None:
-        db_user = env.conf.DB_USER
-        if db_user == 'root':
-            _handle_failure('MySQL root user can not be created')
-            return
-    if db_password is None:
-        db_password = env.conf.DB_PASSWORD
+    """ Creates mysql user. """
+    _, db_user, db_password = _credentials(None, db_user, db_password)
+
+    if db_user == 'root': # do we really need this?
+        _handle_failure('MySQL root user can not be created')
+        return
 
     sql = MYSQL_CREATE_USER % dict(db_user=db_user, db_password=db_password)
     mysql_execute(sql, 'root')
 
 def mysql_create_db(db_name=None, db_user=None):
     """ Creates an empty mysql database. """
-    if db_name is None:
-        db_name = env.conf.DB_NAME
-    if db_user is None:
-        db_user = env.conf.DB_USER
+    db_name, db_user, _ = _credentials(db_name, db_user, None)
 
     sql = MYSQL_CREATE_DB % dict(db_name=db_name, db_user=db_user)
     mysql_execute(sql, 'root')
         mysql_grant_permissions(db_name=db_name, db_user=db_user)
 
 def mysql_grant_permissions(db_name=None, db_user=None):
-    if db_name is None:
-        db_name = env.conf.DB_NAME
-    if db_user is None:
-        db_user = env.conf.DB_USER
+    """ Grants all permissions on ``db_name`` for ``db_user``. """
+    db_name, db_user, _ = _credentials(db_name, db_user, None)
 
     sql = MYSQL_GRANT_PERMISSIONS % dict(db_name=db_name, db_user=db_user)
     mysql_execute(sql, 'root')