Commits

Vladimir Mihailenco committed 1734a6b Merge

Merge with mysql_refactoring

Comments (0)

Files changed (2)

fab_deploy/mysql.py

 from __future__ import with_statement
 from datetime import datetime
 from fabric.api import *
+from fabric.operations import _handle_failure
 from fab_deploy import utils
 from fab_deploy import system
 
 
-__all__ = ['mysql_execute', 'mysql_install', 'mysql_create_db', 'mysqldump']
+__all__ = ['mysql_execute', 'mysql_install', 'mysql_create_db', 'mysql_create_user', 'mysqldump']
+
+
+MYSQL_CREATE_USER = """CREATE USER '{db_user}'@'localhost' IDENTIFIED BY '{db_password}';"""
+
+
+MYSQL_CREATE_DB = """
+CREATE DATABASE {db_name} DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
+GRANT ALL ON {db_name}.* TO '{db_user}'@'localhost';
+FLUSH PRIVILEGES;
+"""
+
+
+def _get_root_password():
+    """Ask root password only once if needed"""
+    if env.conf.DB_ROOT_PASSWORD is None:
+        env.conf.DB_ROOT_PASSWORD = prompt('Please enter MySQL root password:')
+    return env.conf.DB_ROOT_PASSWORD
 
 
 @utils.run_as_sudo
         puts('Mysql is already installed.')
         return
 
+    passwd = _get_root_password()
+
     # this way mysql won't ask for a password on installation
     # see http://serverfault.com/questions/19367/scripted-install-of-mysql-on-ubuntu
     os = utils.detect_os()
     system.aptitude_install('debconf-utils')
-    passwd = env.conf['DB_PASSWORD']
 
     mysql_versions = {'lenny': '5.0', 'squeeze': '5.1', 'maverick': '5.1'}
     version = mysql_versions[os]
     return output.succeeded
 
 @utils.inside_project
-def mysqldump(dir=None):
+def mysqldump(dir=None, db_name=None, db_password=None):
     """ Runs mysqldump. Result is stored at <env>/var/backups/ """
     if dir is None:
-        dir = env.conf['ENV_DIR'] + '/var/backups'
+        dir = env.conf.ENV_DIR + '/var/backups'
         run('mkdir -p ' + dir)
+    if db_name is None:
+        db_name = env.conf.DB_NAME
+    if db_password is None:
+        db_password = env.conf.DB_PASSWORD
+
     now = datetime.now().strftime("%Y.%m.%d-%H.%M")
-    db = env.conf['DB_NAME']
-    password = env.conf['DB_PASSWORD']
-    run('mysqldump -uroot -p%s %s > %s/%s%s.sql' % (password, db, dir, db, now))
+    run('mysqldump -uroot -p%s %s > %s/%s%s.sql' % (db_password, db_name, dir, db_name, now))
 
 def mysql_execute(sql, user=None, password=None):
     """ Executes passed sql command using mysql shell. """
-    user = user or env.conf['DB_USER']
-    password = env.conf['DB_PASSWORD'] if password is None else password
+    user = user or env.conf.DB_USER
+    if user == 'root' and password is None:
+        password = _get_root_password()
+    elif password is None:
+        password = env.conf.DB_PASSWORD
+
     return run("echo '%s' | mysql -u%s -p%s" % (sql, user , password))
 
-def mysql_create_db():
+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
+
+    sql = MYSQL_CREATE_USER.format(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. """
-    db_name = env.conf['DB_NAME']
-    if env.conf['DB_USER'] != 'root':
-        password = prompt('Please enter mysql root password:')
-    else:
-        password = env.conf['DB_PASSWORD']
-    params = 'DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci'
-    mysql_execute('CREATE DATABASE %s %s;' % (db_name, params), 'root', password)
+    if db_name is None:
+        db_name = env.conf.DB_NAME
+    if db_user is None:
+        db_user = env.conf.DB_USER
+
+    sql = MYSQL_CREATE_DB.format(db_name=db_name, db_user=db_user)
+    mysql_execute(sql, 'root')

fab_deploy/utils.py

     defaults = state._AttributeDict(
         HG_BRANCH='default',
         GIT_BRANCH='master',
+        DB_ROOT_PASSWORD=env.conf.get('DB_PASSWORD'),
         DB_NAME=env.conf['INSTANCE_NAME'],
         DB_USER='root',
         SUDO_USER='root',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.