Commits

Mikhail Korobov committed d9b1cd1 Merge

Vladimir's changes are merged

Comments (0)

Files changed (5)

fab_deploy/apache.py

 from fab_deploy import system
 
 
-__all__ = ['touch']
+__all__ = ['touch', 'apache_restart']
 
 
 APACHE_PORTS_FILE = '/etc/apache2/ports.conf'

fab_deploy/django_commands.py

 from fab_deploy import utils
 
 
-__all__ = ['migrate', 'manage', 'syncdb', 'compress', 'test', 'coverage', 'command_is_available']
+__all__ = ['migrate', 'manage', 'syncdb', 'compress', 'collectstatic', 'test', 'coverage',
+           'command_is_available']
 
 
 @utils.inside_project
     if not command_is_available(command):
         warn('Management command "%s" is not available' % command)
     else:
-        run('python manage.py '+ command)
+        run('python manage.py ' + command)
 
 def migrate(params='', do_backup=True):
     """ Runs migrate management command. Database backup is performed
     before migrations if ``do_backup=False`` is not passed. """
     if do_backup:
         backup_dir = env.conf['ENV_DIR'] + '/var/backups/before-migrate'
-        run('mkdir -p '+ backup_dir)
+        run('mkdir -p ' + backup_dir)
         with settings(warn_only=True):
             mysql.mysqldump(backup_dir)
     manage('migrate --noinput %s' % params)
     with settings(warn_only=True):
         manage('synccompress %s' % params)
 
+def collectstatic(params=''):
+    manage('collectstatic --noinput %s' % params)
+
 @utils.inside_project
 def test(what=''):
     """ Runs 'runtests.sh' script from project root.

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};"""
+
+MYSQL_GRANT_PERMISSIONS = """
+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_user=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)
-    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))
+    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
+
+    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))
 
 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
-    return run("echo '%s' | mysql -u%s -p%s" % (sql, user , 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
 
-def mysql_create_db():
+    sql = sql.replace('"', r'\"')
+    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
+
+    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')
+
+    if db_user != '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
+
+    sql = MYSQL_GRANT_PERMISSIONS.format(db_name=db_name, db_user=db_user)
+    mysql_execute(sql, 'root')

fab_deploy/utils.py

 from fabric.contrib import files
 from fabric import state
 from fabric import network
+from fabric import operations
 
 
 __all__ = ['run_as', 'update_env', 'inside_project', 'inside_virtualenv',
 
     if config_template is None and skip_unexistent:
         return
+    elif config_template is None:
+        operations._handle_failure('Config template "%s" not found' % name)
+
     files.upload_template(config_template, to, env.conf, use_jinja=True,
                           **kwargs)
 
     env.conf = getattr(env, 'conf', {})
     env.conf.setdefault('INSTANCE_NAME', user)
     env.conf.setdefault('PROJECT_PATH', '')
+    if env.conf.get('DB_USER') == 'root':
+        env.conf.setdefault('DB_ROOT_PASSWORD', env.conf.get('DB_PASSWORD'))
 
     HOME_DIR = '/home/%s' % user
     SRC_DIR = posixpath.join(HOME_DIR, 'src', env.conf['INSTANCE_NAME'])

fab_deploy/vcs/git.py

 
 def init():
     run('git init')
-    run('git config receive.denyCurrentBranch ignore') # allow update current branch
 
 def up(branch):
     run('git checkout --force %s' % branch) # overwrite local changes
 
 def push():
     user, host = env.hosts[0].split('@')
-    local('git push ssh://%s/~%s/src/%s/ %s' % (env.hosts[0], user, env.conf.INSTANCE_NAME,
-                                             env.conf.GIT_BRANCH))
+    local('git push --force ssh://%s/~%s/src/%s/ %s' % (env.hosts[0], user,
+        env.conf.INSTANCE_NAME, env.conf.GIT_BRANCH))
 
 def configure():
     pass