Commits

Mikhail Korobov committed 64cac54

'fab_deploy.mysql' module. Thanks Andrey Rahmatullin (wrar) for help with non-interactive mysql installation.

  • Participants
  • Parent commits defc849

Comments (0)

Files changed (6)

File fab_deploy/__init__.py

 from fabric.api import *
 
 from fab_deploy.deploy import *
-from fab_deploy.commands import touch, pip, mysqldump, delete_pyc, pip_install, pip_update
+from fab_deploy.commands import touch, pip, delete_pyc, pip_install, pip_update
 from fab_deploy.django_commands import migrate, manage, syncdb, compress, test, coverage
 from fab_deploy.crontab import crontab_set, crontab_add, crontab_show, crontab_remove, crontab_update
 from fab_deploy.utils import run_as, update_env, inside_project, inside_virtualenv

File fab_deploy/commands.py

-from datetime import datetime
 from fabric.api import run, env
 from fab_deploy.apache import touch
 from fab_deploy.utils import run_as, inside_project
         touch()
 
 @inside_project
-def mysqldump(dir=None):
-    if dir is None:
-        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))
-
-@inside_project
 def delete_pyc():
     """ Deletes *.pyc files from project source dir """
     run("find . -name '*.pyc' -delete")

File fab_deploy/django_commands.py

 #coding: utf-8
 from __future__ import with_statement
 from fabric.api import run, settings
-from fab_deploy.commands import mysqldump
+from fab_deploy.mysql import mysqldump
 from fab_deploy.utils import inside_project
 
 @inside_project

File fab_deploy/mysql.py

+from datetime import datetime
+from fabric.api import *
+from fab_deploy.utils import run_as, inside_project
+from fab_deploy.system import aptitude_install
+
+@run_as('root')
+def mysql_install():
+    if mysql_is_installed():
+        puts('Mysql is already installed.')
+        return
+
+    # this way mysql won't ask for password on installation
+    aptitude_install('debconf-utils')
+    passwd=env.conf['DB_PASSWORD']
+    defaults = """mysql-server-5.0\tmysql-server/root_password_again\tpassword\t%s
+mysql-server-5.0\tmysql-server/root_password\tpassword\t%s""" % (passwd, passwd,)
+    run("echo '%s' | debconf-set-selections" % defaults)
+    warn('\n=========\nThe password for mysql "root" user will be set to "%s"\n=========\n' % passwd)
+
+    aptitude_install('mysql-server')
+
+def mysql_is_installed():
+    with settings(warn_only=True):
+        output = run('mysql --version')
+    return output.succeeded
+
+@inside_project
+def mysqldump(dir=None):
+    if dir is None:
+        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))
+
+def mysql_execute(sql):
+    run ("echo '%s' | mysql -u%s -p%s" % (sql, env.conf['DB_USER'], env.conf['DB_PASSWORD']))
+
+def mysql_create_db():
+    db_name = env.conf['DB_NAME']
+    params = 'DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci'
+    mysql_execute('CREATE DATABASE %s %s;' % (db_name, params))

File fab_deploy/system.py

 #coding: utf-8
 from __future__ import with_statement
 import os.path
-from fabric.api import run, settings, env, prefix, cd
+from fabric.api import run, settings, env, cd
 from fabric.contrib.files import append
 from fab_deploy.utils import run_as
 
         'build-essential', 'python-dev', 'python-setuptools',
         'python-profiler', 'screen', 'locales-all', 'curl',
         'libssl-dev', 'libcurl3-dev',
-    ] # + mysql-server
+    ]
 
     aptitude_install(" ".join(to_install))
 
             run('chown -R %s:%s .ssh' % (username, username))
 
 def ssh_add_key(pub_key_file):
+    """ Adds a ssh key from passed file to user's authorized_keys on server """
     with open(os.path.normpath(pub_key_file), 'rt') as f:
         ssh_key = f.read()
     append('.ssh/authorized_keys', ssh_key)

File fab_deploy/utils.py

     defaults = dict(
         HG_BRANCH = 'default',
         DB_NAME = env.conf['INSTANCE_NAME'],
+        DB_USER = 'root',
         PROCESSES = 1,
         THREADS = 5,
         HOME_DIR = HOME_DIR,