Commits

Peter Sanchez committed 581c083

Made fabfile compatible with Fabric 1.0.1

  • Participants
  • Parent commits acfd763

Comments (0)

Files changed (1)

 def _get_callable(default=run):
     'Return local or default (run) based on hosts variable'
     require('run_type', provided_by=[local, staging, prod])
+    is_local = _is_local()
+    if default == cd:
+        return lcd if is_local else default
+
     def _local(cmd, *args, **kwargs):
         return local(cmd)
-    return _local if _is_local() else default
+    return _local if is_local else default
 
 
 def _get_cmds():
     return (
         _get_callable(),
         _get_callable(default=sudo),
+        _get_callable(default=cd),
     )
 
 
 def localdev():
     'Use the local machine'
     env.hosts = ['localhost']
-    env.path = '/path/to/environments/PROJECT_NAME'
+    env.env_path = '/path/to/environments/PROJECT_NAME'
     env.user = 'username'
     env.run_type = 'local'
     env.repo_path = '/path/to/local/repository'
 def prod():
     'Use the production environment'
     env.hosts = ['yourproject.yourdomain.com']
-    env.path = '/path/to/environments/PROJECT_NAME'
+    env.env_path = '/path/to/environments/PROJECT_NAME'
     env.user = 'username'
     env.run_type = 'prod'
     env.repo_path = 'http://bitbucket.org/someuser/yourproject'
 # Tasks
 def test():
     'Run the test suite and bail out if it fails'
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
-    with cd(env.project_name):
+    with _cd(env.project_name):
         _run('python manage.py test')
 
 
     a full deployment
     '''
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
     
     import time
     env.release = time.strftime('%Y%m%d%H%M%S')
 
-    lpath = '%(path)s/releases/%(release)s' % env
+    lpath = '%(env_path)s/releases/%(release)s' % env
     _run('mkdir -p %s' % lpath)
-    with cd(lpath):
+    with _cd(lpath):
         _run('virtualenv --no-site-packages .')
         _run('mkdir -p shared packages')
     deploy()
 def soft_update():
     'Just update codebase. Do not do a full deploy'
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
+    require('env_path')
     
     checkout_code_repo(update=True)
     touch_wsgi_handler()
     then restart the webserver
     '''
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
+    require('env_path')
 
     checkout_code_repo()
     install_requirements()
 
 def show_versions():
     'List all deployed versions'
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
 
-    with cd('%(path)s/releases' % env):
+    with _cd('%(env_path)s/releases' % env):
         print _run('ls -xt')
 
 
 def rollback_version(version):
     'Specify a specific version to be made live'
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
     
     env.version = version
-    with cd('%(path)s' % env):
+    with _cd('%(env_path)s' % env):
         _run('ln -nfs releases/%(version)s current' % env)
     restart_webserver()
 
 def rollback():
     'Simple GENERIC rollback. Symlink to the second most recent release'
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
 
     releases = _get_releases_list()
     release = releases[-2]
     release directory. BE CAREFUL USING THIS.
     '''
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
 
     releases = _get_releases_list()
     release = releases[-1]
-    with cd('%(path)s/releases' % env):
+    with _cd('%(env_path)s/releases' % env):
         _run('rm -rf %s' % release)
 
 
 def _get_releases_list():
     'Returns sorted list of all current releases'
     require('hosts', provided_by=[localdev, staging, prod])
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
 
-    with cd('%(path)s/releases' % env):
+    with _cd('%(env_path)s/releases' % env):
         releases = _run('ls -xt')
         releases = [x.replace('/', '') for x in releases.split()]
     return sorted(releases)
     'Create an archive from the current Git master branch and upload it'
     require('repo_path', provided_by=[localdev, staging, prod])
     require('repo_rev', provided_by=[localdev, staging, prod])
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
     if update:
-        lpath = '%(path)s/current/%(project_name)s' % env
-        with cd(lpath):
+        lpath = '%(env_path)s/current/%(project_name)s' % env
+        with _cd(lpath):
             _run('hg pull -r %(repo_rev)s -u' % env)
     else:
         require('release', provided_by=[deploy, setup])
-        lpath = '%(path)s/releases/%(release)s' % env
-        with cd(lpath):
+        lpath = '%(env_path)s/releases/%(release)s' % env
+        with _cd(lpath):
             cmd = 'hg clone -r '
             cmd += '%(repo_rev)s %(repo_path)s %(project_name)s' % env
             _run(cmd)
 
     if env.copy_base_settings_local:
         lpath = os.path.join(lpath, '%(project_name)s/%(project_name)s' % env)
-        with cd(lpath):
+        with _cd(lpath):
             with settings(warn_only=True):
                 _run('cp base_settings_local.py settings_local.py')
 
 def install_site():
     'Add the virtualhost file to apache'
     require('release', provided_by=[deploy, setup])
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
     if not _is_local():
-        with cd('%(path)s/releases/%(release)s' % env):
+        with _cd('%(env_path)s/releases/%(release)s' % env):
             cmd = 'cp %(project_name)s/apache/%(project_name)s.conf ' % env + \
                   '/etc/apache2/conf.d/'
             _sudo(cmd)
 def install_requirements():
     'Install the required packages from the requirements file using pip'
     require('release', provided_by=[deploy, setup])
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
-    with cd('%(path)s/releases/%(release)s' % env):
+    with _cd('%(env_path)s/releases/%(release)s' % env):
         cmd = './bin/pip install -E . '
         cmd += '-r ./%(project_name)s/requirements.txt ' % env
         cmd += '--download-cache=~/.pipcache'
 def symlink_current_release():
     'Symlink our current release'
     require('release', provided_by=[deploy, setup])
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
-    with cd('%(path)s' % env):
+    with _cd('%(env_path)s' % env):
         _run('ln -nfs releases/%(release)s current' % env)
 
 
     'Update the database'
     require('project_name')
     require('release', provided_by=[deploy, setup])
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
-    p = '%(path)s/releases/%(release)s/%(project_name)s/%(project_name)s' % env
-    with cd(p):
+    p = '%(env_path)s/releases/%(release)s/%(project_name)s/%(project_name)s' % env
+    with _cd(p):
         _run('../../bin/python manage.py syncdb --noinput')
         if env.run_south_migrate:
             _run('../../bin/python manage.py migrate')
 
 def restart_webserver():
     'Restart the web server'
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
     if not _is_local():
         _sudo('apache2ctl configtest')
     'Touch the mod_wsgi wsgi_handler.py file'
     require('project_name')
     require('hosts', provided_by=[localdev, staging, prod])
-    _run, _sudo = _get_cmds()
+    _run, _sudo, _cd = _get_cmds()
 
-    with cd('%(path)s/current/%(project_name)s/apache' % env):
+    with _cd('%(env_path)s/current/%(project_name)s/apache' % env):
         _run('touch wsgi_handler.py')
 
 
     '''
     require('hosts', provided_by=[localdev, staging, prod])
     require('num_releases')
-    require('path')
-    _run, _sudo = _get_cmds()
+    require('env_path')
+    _run, _sudo, _cd = _get_cmds()
 
     releases = _get_releases_list()
-    with cd('%(path)s/releases' % env):
+    with _cd('%(env_path)s/releases' % env):
         while len(releases) > env.num_releases:
             release = releases.pop(0)
             _run('rm -rf %s' % release)