|
1 from fabric.api import env, local, put, run, lcd, task, prefix, sudo |
|
2 from fabric.contrib.files import exists, append, upload_template |
|
3 from fabric.colors import green |
|
4 import tempfile |
|
5 import shutil |
|
6 import os |
|
7 import settings |
|
8 |
|
9 current_path = os.path.dirname(os.path.realpath(__file__)) |
|
10 |
|
11 def __init(): |
|
12 if not env.get('temp_folder', None): |
|
13 env['temp_folder'] = tempfile.mkdtemp() |
|
14 print(green("working folder is %s" % env['temp_folder'])) |
|
15 |
|
16 def __clean(): |
|
17 if env.get('temp_folder', None): |
|
18 print(green("Removing %s" % env['temp_folder'])) |
|
19 shutil.rmtree(env['temp_folder']) |
|
20 |
|
21 def get_version_path(): |
|
22 return os.path.join(env['temp_folder'], env['version']) |
|
23 |
|
24 def export(): |
|
25 local('hg archive -r %s %s' % (env['version'], get_version_path()) ) |
|
26 |
|
27 def pack(): |
|
28 # create a new source distribution as tarball |
|
29 with lcd(get_version_path()): |
|
30 local('python setup.py sdist --formats=gztar', capture=False) |
|
31 |
|
32 def create_virtualenv(): |
|
33 with lcd(get_version_path()): |
|
34 tmpd = run('mktemp -d').strip() |
|
35 put('virtualenv/*.txt', tmpd) |
|
36 run('virtualenv -p %s %s' % (env.srv_python_location,env.srv_venv_path)) |
|
37 with prefix('source %s/bin/activate' % env.srv_venv_path): |
|
38 run('pip install -r %s/requirements.txt' % tmpd) |
|
39 run('pip install -r %s/requirements_srvr.txt' % tmpd) |
|
40 with prefix('source %s/bin/activate' % env.srv_venv_path): |
|
41 site_package_location = run('python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"').strip() |
|
42 upload_template(os.path.join(current_path,'config_path.pth'), os.path.join(site_package_location,'config_path.pth'),context={"srv_config_path": env.srv_config_path}) |
|
43 |
|
44 append(os.path.join(env.srv_venv_path, "bin/activate"), "export DJANGO_SETTINGS_MODULE="+env.srv_django_settings_module) |
|
45 |
|
46 run('rm -fr %s' % tmpd) |
|
47 |
|
48 |
|
49 def deploy(): |
|
50 # figure out the release name and version |
|
51 with lcd(get_version_path()): |
|
52 dist = local('python setup.py --fullname', capture=True).strip() |
|
53 print(green("dist is %s" % dist)) |
|
54 |
|
55 # upload the source tarball to the temporary folder on the server |
|
56 put('dist/%s.tar.gz' % dist, '/tmp/%s.tar.gz' % dist) |
|
57 # create a place where we can unzip the tarball, then enter |
|
58 # that directory and unzip it |
|
59 # now setup the package with our virtual environment's |
|
60 # python interpreter |
|
61 with prefix('source %s/bin/activate' % env.srv_venv_path): |
|
62 run('pip install -U --force-reinstall /tmp/%s.tar.gz' % dist) |
|
63 # now that all is set up, delete the folder again |
|
64 |
|
65 run('rm -rf /tmp/%s.tar.gz' % dist) |
|
66 sudo(env.srv_restart_cmd, shell=False) |
|
67 |
|
68 @task(default=True) |
|
69 def deploy_version(version='tip'): |
|
70 env['version'] = version |
|
71 __init() |
|
72 export() |
|
73 pack() |
|
74 if not exists(env.srv_venv_path): |
|
75 create_virtualenv() |
|
76 deploy() |
|
77 __clean() |