Commits

Vladimir Mihailenco  committed 2601e42

Various fixes and improvements

  • Participants
  • Parent commits 1734a6b

Comments (0)

Files changed (5)

File fab_deploy/apache.py

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

File 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.

File fab_deploy/mysql.py

 
 MYSQL_CREATE_USER = """CREATE USER '{db_user}'@'localhost' IDENTIFIED BY '{db_password}';"""
 
+MYSQL_CREATE_DB = """CREATE DATABASE {db_name};"""
 
-MYSQL_CREATE_DB = """
-CREATE DATABASE {db_name} DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;
+MYSQL_GRANT_PERMISSIONS = """
 GRANT ALL ON {db_name}.* TO '{db_user}'@'localhost';
 FLUSH PRIVILEGES;
 """
     return output.succeeded
 
 @utils.inside_project
-def mysqldump(dir=None, db_name=None, db_password=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'
         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
 
-    now = datetime.now().strftime("%Y.%m.%d-%H.%M")
-    run('mysqldump -uroot -p%s %s > %s/%s%s.sql' % (db_password, db_name, dir, db_name, now))
+    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. """
     elif password is None:
         password = env.conf.DB_PASSWORD
 
-    return run("echo '%s' | mysql -u%s -p%s" % (sql, user , password))
+    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:
 
     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')

File 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'])
     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',

File 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 -f 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