| author | nowmad@23.1.168.192.in-addr.arpa |
| Wed, 02 Dec 2015 19:39:16 +0100 | |
| changeset 203 | 786cdb826f09 |
| parent 141 | 0d0883c6ec09 |
| permissions | -rw-r--r-- |
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
1 |
from distutils.util import strtobool |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
2 |
import os |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
3 |
import shutil |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
4 |
import settings |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
5 |
import tempfile |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
6 |
|
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
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
|
8 |
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
|
9 |
from fabric.colors import green |
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
10 |
|
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
11 |
def _prep_bool_arg(arg): |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
12 |
return bool(strtobool(str(arg))) |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
13 |
|
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
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
|
16 |
|
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
17 |
#TODO: add collectatic |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
18 |
|
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
def __init(): |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
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
|
21 |
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
|
22 |
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
|
23 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
def __clean(): |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
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
|
26 |
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
|
27 |
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
|
28 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
def get_version_path(): |
|
123
bd83fbd445ca
src are not at repo root folder, again
ymh <ymh.work@gmail.com>
parents:
121
diff
changeset
|
30 |
return os.path.join(env['temp_folder'], env['version']) |
|
118
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 export(): |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
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
|
34 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
def pack(): |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
# create a new source distribution as tarball |
|
123
bd83fbd445ca
src are not at repo root folder, again
ymh <ymh.work@gmail.com>
parents:
121
diff
changeset
|
37 |
with lcd(os.path.join(get_version_path(),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
|
38 |
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
|
39 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
def create_virtualenv(): |
|
123
bd83fbd445ca
src are not at repo root folder, again
ymh <ymh.work@gmail.com>
parents:
121
diff
changeset
|
41 |
with lcd(os.path.join(get_version_path(),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
|
42 |
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
|
43 |
put('virtualenv/*.txt', tmpd) |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
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
|
45 |
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
|
46 |
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
|
47 |
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
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
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
|
53 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
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
|
55 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
|
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
57 |
def deploy(installonly=False): |
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
# figure out the release name and version |
|
123
bd83fbd445ca
src are not at repo root folder, again
ymh <ymh.work@gmail.com>
parents:
121
diff
changeset
|
59 |
with lcd(os.path.join(get_version_path(),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
|
60 |
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
|
61 |
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
|
62 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
# 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
|
64 |
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
|
65 |
# 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
|
66 |
# 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
|
67 |
# 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
|
68 |
# python interpreter |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
# 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
|
72 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
run('rm -rf /tmp/%s.tar.gz' % dist) |
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
74 |
|
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
75 |
def restart_srv(): |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
76 |
sudo(env.srv_restart_cmd, shell=False) |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
77 |
|
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
78 |
def apply_deploy(): |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
79 |
with prefix('source %s/bin/activate' % env.srv_venv_path): |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
80 |
run("django-admin migrate --noinput") |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
81 |
run("django-admin collectstatic --noinput --clear") |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
82 |
|
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
|
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
@task(default=True) |
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
85 |
def deploy_version(version='tip', installonly=False): |
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
env['version'] = version |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
__init() |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
export() |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
pack() |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
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
|
91 |
create_virtualenv() |
|
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
deploy() |
|
141
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
93 |
if not _prep_bool_arg(installonly): |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
94 |
apply_deploy() |
|
0d0883c6ec09
add collecstatic and migrate to fabfile
ymh <ymh.work@gmail.com>
parents:
123
diff
changeset
|
95 |
restart_srv() |
|
118
fea47f1054e2
prepare for server install, mv settings, add setup, correct various problems
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
__clean() |