Commits

Vladimir Mihailenco committed 7009a85

Refactor imports

Comments (0)

Files changed (10)

fab_deploy/__init__.py

+from fabric.api import *
 from fab_deploy.deploy import *
-from fab_deploy.virtualenv import pip, pip_install, pip_update
-from fab_deploy.django_commands import migrate, manage, syncdb, compress, test, coverage, command_is_available
-from fab_deploy.utils import run_as, update_env, inside_project, inside_virtualenv, delete_pyc, print_env, detect_os
-from fab_deploy.system import create_linux_account, ssh_add_key
-from fab_deploy.crontab import crontab_set, crontab_add, crontab_show, crontab_remove, crontab_update
-from fab_deploy.mysql import mysql_execute, mysql_install, mysql_create_db, mysqldump
-from fab_deploy.apache import touch
+from fab_deploy.virtualenv import *
+from fab_deploy.django_commands import *
+from fab_deploy.utils import *
+from fab_deploy.system import *
+from fab_deploy.crontab import *
+from fab_deploy.mysql import *
+from fab_deploy.apache import *

fab_deploy/apache.py

 from __future__ import with_statement
 import re
 from fabric.api import *
-from fabric.contrib.files import append
+from fabric.contrib import files
 
-from fab_deploy.utils import run_as, upload_config_template
-from fab_deploy.system import aptitude_install
+from fab_deploy import utils
+from fab_deploy import system
+
+
+__all__ = ['touch']
+
 
 APACHE_PORTS_FILE = '/etc/apache2/ports.conf'
 APACHE_FIRST_PORT = 50000 # see http://www.iana.org/assignments/port-numbers
     wsgi_dir = env.conf['ENV_DIR']+'/var/wsgi/'
     run('mkdir -p ' + wsgi_dir)
     file_name = env.conf['INSTANCE_NAME']+'.py'
-    upload_config_template('django_wsgi.py', wsgi_dir+file_name)
+    utils.upload_config_template('django_wsgi.py', wsgi_dir+file_name)
 
-@run_as('root')
+@utils.run_as('root')
 def apache_restart():
     """ Restarts apache using init.d script. """
     # restart is not used because it can leak memory in some cases
 
 # ==== installation ===
 
-@run_as('root')
+@utils.run_as('root')
 def apache_install():
     """ Installs apache. """
-    aptitude_install('apache2 libapache2-mod-wsgi libapache2-mod-rpaf')
+    system.aptitude_install('apache2 libapache2-mod-wsgi libapache2-mod-rpaf')
     run('rm -f /etc/apache2/sites-enabled/default')
     run('rm -f /etc/apache2/sites-enabled/000-default')
     apache_setup_locale()
 
-@run_as('root')
+@utils.run_as('root')
 def apache_make_config():
     """ Updates apache config. """
     _apache_setup_port()
     name = env.conf['INSTANCE_NAME']
-    upload_config_template('apache.config', '/etc/apache2/sites-available/%s' % name)
+    utils.upload_config_template('apache.config', '/etc/apache2/sites-available/%s' % name)
     run('a2ensite %s' % name)
 
 def apache_setup():
     apache_make_wsgi()
     apache_restart()
 
-@run_as('root')
+@utils.run_as('root')
 def apache_setup_locale():
     """ Setups apache locale. Apache is unable to handle file uploads with
     unicode file names without this. """
-    append('/etc/apache2/envvars', [
+    files.append('/etc/apache2/envvars', [
             'export LANG="en_US.UTF-8"', 'export LC_ALL="en_US.UTF-8"'])
 
 # === automatic apache ports management ===
 
-@run_as('root')
+@utils.run_as('root')
 def _ports_lines():
     with (settings(hide('stdout'))):
         ports_data = run('cat ' + APACHE_PORTS_FILE)
     return ports_data.splitlines()
 
-@run_as('root')
+@utils.run_as('root')
 def _used_ports(lines):
     ports_mapping = dict()
 
             ports_mapping[match.group('port')] = instance
     return ports_mapping
 
-@run_as('root')
+@utils.run_as('root')
 def _apache_setup_port():
     """
     Makes sure some port is correctly listened in

fab_deploy/crontab.py

 from __future__ import with_statement
 from fabric.api import *
 
+
+__all__ = ['crontab_set', 'crontab_add', 'crontab_show', 'crontab_remove', 'crontab_update']
+
+
 def _marker(marker):
     return ' # MARKER:%s' % marker if marker else ''
 

fab_deploy/deploy.py

 #coding: utf-8
 from __future__ import with_statement
 from fabric.api import *
-from fabric.contrib.console import confirm
-from fabric.contrib.files import upload_template
+from fabric.contrib import console
+from fabric.contrib import files
 
 from fab_deploy import utils
-from fab_deploy.utils import delete_pyc, run_as, detect_os, upload_config_template
-from fab_deploy.virtualenv import pip_install, pip_update
-from fab_deploy.django_commands import compress, migrate, syncdb, test
-from fab_deploy.system import prepare_server
-from fab_deploy.apache import apache_setup, apache_install, touch
-from fab_deploy.nginx import nginx_setup, nginx_install
-from fab_deploy.virtualenv import virtualenv_create
+from fab_deploy import virtualenv
+from fab_deploy import django_commands as dj_cmd
+from fab_deploy import system
+from fab_deploy import apache
+from fab_deploy import nginx
 from fab_deploy import vcs
 
+
+__all__ = ['full_deploy', 'deploy_project', 'make_clone', 'update_django_config', 'up',
+           'setup_web_server', 'push', 'undeploy']
+
+
 def full_deploy():
     """ Prepares server and deploys the project. """
-    os = detect_os()
-    if not confirm("Is the OS detected correctly (%s)?" % os, default=False):
+    os = utils.detect_os()
+    if not console.confirm("Is the OS detected correctly (%s)?" % os, default=False):
         abort("Detection fails. Please set env.conf.OS to correct value.")
-    prepare_server()
+    system.prepare_server()
     deploy_project()
 
 def deploy_project():
     """ Deploys project on prepared server. """
-    virtualenv_create()
+    virtualenv.virtualenv_create()
     make_clone()
 
-    upload_config_template('pip.conf', env.conf.HOME_DIR + '/.pip/pip.conf', check_existence=True)
-    pip_install(env.conf.PIP_REQUIREMENTS, restart=False)
+    utils.upload_config_template('pip.conf', env.conf.HOME_DIR + '/.pip/pip.conf', check_existence=True)
+    virtualenv.pip_install(env.conf.PIP_REQUIREMENTS, restart=False)
 
     setup_web_server()
     update_django_config()
 
-    syncdb()
-    migrate()
+    dj_cmd.syncdb()
+    dj_cmd.migrate()
 
 def make_clone():
     """ Creates repository clone on remote server. """
 
 def update_django_config(restart=True):
     """ Updates :file:`config.py` on server (using :file:`config.server.py`) """
-    upload_template(utils._project_path(env.conf.REMOTE_CONFIG_TEMPLATE),
+    files.upload_template(utils._project_path(env.conf.REMOTE_CONFIG_TEMPLATE),
                     utils._remote_project_path(env.conf.LOCAL_CONFIG), env.conf, True)
     if restart:
-        touch()
+        apache.touch()
 
 def up(branch=None):
     """ Runs vcs ``up`` or ``checkout`` command on server and reloads
     mod_wsgi process. """
-    delete_pyc()
+    utils.delete_pyc()
     with cd('src/' + env.conf['INSTANCE_NAME']):
         vcs.up(branch)
-    compress()
-    touch()
+    dj_cmd.compress()
+    apache.touch()
 
 def setup_web_server():
     """ Sets up a web server (apache + nginx). """
-    apache_install()
-    nginx_install()
+    apache.apache_install()
+    nginx.nginx_install()
 
-    apache_setup()
-    nginx_setup()
+    apache.apache_setup()
+    nginx.nginx_setup()
 
 def push(*args):
     ''' Run it instead of your VCS push command.
 
     Arguments:
 
-    * notest - don't run tests
-    * syncdb - run syncdb before code reloading
-    * migrate - run migrate before code reloading
-    * pip_update - run pip_update before code reloading
+    * nodj_cmd.test - don't run dj_cmd.tests
+    * dj_cmd.syncdb - run dj_cmd.syncdb before code reloading
+    * dj_cmd.migrate - run dj_cmd.migrate before code reloading
+    * virtualenv.pip_update - run virtualenv.pip_update before code reloading
     * norestart - do not reload source code
     '''
-    allowed_args = set(['force', 'notest', 'syncdb', 'migrate', 'pip_update', 'norestart'])
+    allowed_args = set(['force', 'nodj_cmd.test', 'dj_cmd.syncdb', 'dj_cmd.migrate', 'virtualenv.pip_update', 'norestart'])
     for arg in args:
         if arg not in allowed_args:
             puts('Invalid argument: %s' % arg)
             return
 
     vcs.push()
-    delete_pyc()
+    utils.delete_pyc()
     with cd('src/'+env.conf['INSTANCE_NAME']):
         vcs.up()
 
-    if 'pip_update' in args:
-        pip_update(restart=False)
-    if 'syncdb' in args:
-        syncdb()
-    if 'migrate' in args:
-        migrate()
-    compress()
+    if 'virtualenv.pip_update' in args:
+        virtualenv.pip_update(restart=False)
+    if 'dj_cmd.syncdb' in args:
+        dj_cmd.syncdb()
+    if 'dj_cmd.migrate' in args:
+        dj_cmd.migrate()
+    dj_cmd.compress()
     if 'norestart' not in args:
-        touch()
-    if 'notest' not in args:
-        test()
+        apache.touch()
+    if 'nodj_cmd.test' not in args:
+        dj_cmd.test()
 
 def undeploy():
     """ Shuts site down. This command doesn't clean everything, e.g.
     user data (database, backups) is preserved. """
 
-    if not confirm("Do you wish to undeploy host %s?" % env.hosts[0], default=False):
+    if not console.confirm("Do you wish to undeploy host %s?" % env.hosts[0], default=False):
         abort("Aborting.")
 
-    @run_as('root')
+    @utils.run_as('root')
     def wipe_web():
         run('rm -f /etc/nginx/sites-enabled/'+env.conf['INSTANCE_NAME'])
         run('a2dissite ' + env.conf['INSTANCE_NAME'])

fab_deploy/django_commands.py

 #coding: utf-8
 from __future__ import with_statement
 from fabric.api import *
-from fab_deploy.mysql import mysqldump
-from fab_deploy.utils import inside_project
 
-@inside_project
+from fab_deploy import mysql
+from fab_deploy import utils
+
+
+__all__ = ['migrate', 'manage', 'syncdb', 'compress', 'test', 'coverage', 'command_is_available']
+
+
+@utils.inside_project
 def command_is_available(command):
     with settings(hide('warnings', 'running', 'stdout', 'stderr'), warn_only=True):
         output = run('python manage.py help ' + command)
     return output.succeeded
 
-@inside_project
+@utils.inside_project
 def manage(command):
     """ Runs django management command.
     Example::
     if do_backup:
         backup_dir = env.conf['ENV_DIR'] + '/var/backups/before-migrate'
         run('mkdir -p '+ backup_dir)
-        mysqldump(backup_dir)
+        mysql.mysqldump(backup_dir)
     manage('migrate --noinput %s' % params)
 
 def syncdb(params=''):
     with settings(warn_only=True):
         manage('synccompress %s' % params)
 
-@inside_project
+@utils.inside_project
 def test(what=''):
     """ Runs 'runtests.sh' script from project root.
     Example runtests.sh content::

fab_deploy/mysql.py

 from __future__ import with_statement
 from datetime import datetime
 from fabric.api import *
-from fab_deploy.utils import run_as, inside_project, detect_os
-from fab_deploy.system import aptitude_install
+from fab_deploy import utils
+from fab_deploy import system
 
-@run_as('root')
+
+__all__ = ['mysql_execute', 'mysql_install', 'mysql_create_db', 'mysqldump']
+
+
+@utils.run_as('root')
 def mysql_install():
     """ Installs mysql. """
     if _mysql_is_installed():
 
     # this way mysql won't ask for a password on installation
     # see http://serverfault.com/questions/19367/scripted-install-of-mysql-on-ubuntu
-    os = detect_os()
-    aptitude_install('debconf-utils')
+    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'}
     run("echo '%s' | debconf-set-selections" % "\n".join(debconf_defaults))
 
     warn('\n=========\nThe password for mysql "root" user will be set to "%s"\n=========\n' % passwd)
-    aptitude_install('mysql-server')
+    system.aptitude_install('mysql-server')
 
 def _mysql_is_installed():
     with settings(warn_only=True):
         output = run('mysql --version')
     return output.succeeded
 
-@inside_project
+@utils.inside_project
 def mysqldump(dir=None):
     """ Runs mysqldump. Result is stored at <env>/var/backups/ """
     if dir is None:

fab_deploy/nginx.py

 from __future__ import with_statement
 from fabric.api import run, env, settings
-from fab_deploy.utils import run_as, upload_config_template, detect_os
-from fab_deploy.system import aptitude_install
-from fab_deploy.apache import _apache_setup_port
+from fab_deploy import utils
+from fab_deploy import system
+from fab_deploy import apache
 
-@run_as('root')
+
+__all__ = ['nginx_install', 'nginx_setup']
+
+
+@utils.run_as('root')
 def nginx_install():
     """ Installs nginx. """
-    os = detect_os()
+    os = utils.detect_os()
     options = {'lenny': '-t lenny-backports'}
-    aptitude_install('nginx', options.get(os, ''))
+    system.aptitude_install('nginx', options.get(os, ''))
     run('rm -f /etc/nginx/sites-enabled/default')
 
-@run_as('root')
+@utils.run_as('root')
 def nginx_setup():
     """ Updates nginx config and restarts nginx. """
-    _apache_setup_port()
+    apache._apache_setup_port()
     name = env.conf['INSTANCE_NAME']
-    upload_config_template('nginx.config', '/etc/nginx/sites-available/%s' % name)
+    utils.upload_config_template('nginx.config', '/etc/nginx/sites-available/%s' % name)
     with settings(warn_only=True):
         run('ln -s /etc/nginx/sites-available/%s /etc/nginx/sites-enabled/%s' % (name, name))
     run('invoke-rc.d nginx restart')

fab_deploy/system.py

 from __future__ import with_statement
 import os.path
 from fabric.api import run, settings, env, cd
-from fabric.contrib.files import append
-from fab_deploy.utils import run_as, detect_os
-from fabric.utils import puts, abort
+from fabric.contrib import files
+from fabric import utils as fabric_utils
+from fab_deploy import utils
+
+
+__all__ = ['create_linux_account', 'ssh_add_key']
+
 
 def prepare_server():
     """ Prepares server: installs system packages. """
     setup_backports()
     install_common_software()
 
-@run_as('root')
+@utils.run_as('root')
 def install_common_software():
     """ Installs common system packages. """
     common_packages = [
         'maverick': ['libmysqlclient-dev'],
     }
 
-    os = detect_os()
+    os = utils.detect_os()
     if os not in extra_packages:
-        abort('Your OS (%s) is unsupported now.' % os)
+        fabric_utils.abort('Your OS (%s) is unsupported now.' % os)
 
     aptitude_install(" ".join(common_packages + extra_packages[os]))
 
     run('pip install -U virtualenv')
 
 
-@run_as('root')
+@utils.run_as('root')
 def setup_backports():
     """ Adds backports repo to apt sources. """
-    os = detect_os()
+    os = utils.detect_os()
     backports = {
         'lenny': 'http://backports.debian.org/debian-backports lenny-backports main contrib non-free',
         'squeeze': 'http://backports.debian.org/debian-backports squeeze-backports main contrib non-free',
     }
 
     if os not in backports:
-        puts("Backports are not available for " + os)
+        fabric_utils.puts("Backports are not available for " + os)
         return
 
     run("echo 'deb %s' > /etc/apt/sources.list.d/backports.sources.list" % backports[os])
     with settings(warn_only=True):
         run('aptitude update')
 
-@run_as('root')
+@utils.run_as('root')
 def create_linux_account(pub_key_file):
     """ Creates linux account and setups ssh access. """
     with open(os.path.normpath(pub_key_file), 'rt') as f:
         run('adduser %s --disabled-password --gecos ""' % username)
         with cd('/home/' + username):
             run('mkdir -p .ssh')
-            append('.ssh/authorized_keys', ssh_key)
+            files.append('.ssh/authorized_keys', ssh_key)
             run('chown -R %s:%s .ssh' % (username, username))
 
 def ssh_add_key(pub_key_file):
     with open(os.path.normpath(pub_key_file), 'rt') as f:
         ssh_key = f.read()
     run('mkdir -p .ssh')
-    append('.ssh/authorized_keys', ssh_key)
+    files.append('.ssh/authorized_keys', ssh_key)
 
 
-@run_as('root')
+@utils.run_as('root')
 def aptitude_install(packages, options=''):
     """ Installs package via aptitude. """
     run('aptitude install %s -y %s' % (options, packages,))
 
 
-#@run_as('root')
+#@utils.run_as('root')
 #def install_backup_system():
 #    run('aptitude install -y s3cmd ruby rubygems libxml2-dev libxslt-dev libopenssl-ruby')
 #    run('gem install rubygems-update')

fab_deploy/utils.py

 import pprint
 from re import match
 from functools import wraps
-from fabric.contrib.files import upload_template
 from fabric.api import *
-from fabric.state import _AttributeDict
-from fabric.network import normalize, join_host_strings
+from fabric.contrib import files
+from fabric import state
+from fabric import network
+
+
+__all__ = ['run_as', 'update_env', 'inside_project', 'inside_virtualenv', 'delete_pyc', 'print_env',
+           'detect_os']
+
 
 def _codename(distname, version, id):
     patterns = [
     def decorator(func):
         @wraps(func)
         def inner(*args, **kwargs):
-            old_user, host, port = normalize(env.host_string)
-            env.host_string = join_host_strings(user, host, port)
+            old_user, host, port = network.normalize(env.host_string)
+            env.host_string = network.join_host_strings(user, host, port)
             result = func(*args, **kwargs)
-            env.host_string = join_host_strings(old_user, host, port)
+            env.host_string = network.join_host_strings(old_user, host, port)
             return result
         return inner
     return decorator
     config_template = _config_template_path(name)
     if check_existence and not os.path.exists(config_template):
         return
-    upload_template(config_template, to, env.conf, True)
+    files.upload_template(config_template, to, env.conf, True)
 
 def update_env():
     """
     Updates :attr:`env.conf` configuration with some defaults and converts
-    it to _AttributeDict (that's a dictionary subclass enabling attribute
+    it to state._AttributeDict (that's a dictionary subclass enabling attribute
     lookup/assignment of keys/values).
 
     Call :func:`update_env` at the end of each server-configuring function.
             update_env()
     """
     assert len(env.hosts)==1, "Multiple hosts in env.hosts are not supported now. (%s)" % env.hosts
-    user, host, port = normalize(env.hosts[0])
+    user, host, port = network.normalize(env.hosts[0])
 
     env.conf = getattr(env, 'conf', {})
 
         env.conf['INSTANCE_NAME'] = user
     SRC_DIR = '%s/src/%s' % (HOME_DIR, env.conf['INSTANCE_NAME'])
 
-    defaults = _AttributeDict(
+    defaults = state._AttributeDict(
         HG_BRANCH='default',
         GIT_BRANCH = 'master',
         DB_NAME=env.conf['INSTANCE_NAME'],

fab_deploy/virtualenv.py

 from __future__ import with_statement
 from fabric.api import run, env, cd
-from fab_deploy.apache import touch
-from fab_deploy.utils import inside_src
+from fab_deploy import apache
 from fab_deploy import utils
 
-@inside_src
+
+__all__ = ['pip', 'pip_install', 'pip_update']
+
+
+@utils.inside_src
 def pip(commands=''):
     """ Runs pip command """
     run('pip ' + commands)
 
-@inside_src
+@utils.inside_src
 def pip_install(what='active', options='', restart=True):
     """ Installs pip requirements listed in ``reqs/<file>.txt`` file. """
     what = utils._pip_req_path(what)
     run('pip install %s -r %s' % (options, what))
     if restart:
-        touch()
+        apache.touch()
 
-@inside_src
+@utils.inside_src
 def pip_update(what='active', options='', restart=True):
     """ Updates pip requirements listed in ``reqs/<file>.txt`` file. """
     what = utils._pip_req_path(what)
     run('pip install %s -U -r %s' % (options, what))
     if restart:
-        touch()
+        apache.touch()
 
 def virtualenv_create():
     run('mkdir -p envs')
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.