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