|
3
|
1 |
from fabric.api import task, run, local, env, cd, put, prefix, sudo |
|
|
2 |
from fabric.colors import green |
|
|
3 |
from fabric.contrib.project import rsync_project |
|
|
4 |
from fabric.contrib.files import exists, upload_template |
|
|
5 |
from fabric.context_managers import settings |
|
|
6 |
from mercurial import commands, ui, hg, cmdutil |
|
|
7 |
import imp |
|
|
8 |
import os, os.path |
|
|
9 |
import shutil |
|
|
10 |
import sys |
|
|
11 |
|
|
|
12 |
import config |
|
|
13 |
|
|
|
14 |
def get_export_path(version): |
|
|
15 |
base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/") |
|
17
|
16 |
return os.path.expanduser(base_path) + "_%s" % (str(version)) |
|
3
|
17 |
|
|
|
18 |
def clean_export_folder(path): |
|
|
19 |
print("Removing %s" % path) |
|
|
20 |
if os.path.isdir(path): |
|
|
21 |
shutil.rmtree(path, ignore_errors=True) |
|
|
22 |
|
|
|
23 |
def do_export_version(path, version): |
|
|
24 |
print("Export version %s"%str(version)) |
|
|
25 |
|
|
|
26 |
local("hg archive -r \'%s\' \"%s\"" % (str(version),path)) |
|
|
27 |
print("Export version %s done"%str(version)) |
|
|
28 |
|
|
|
29 |
|
|
|
30 |
def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key): |
|
|
31 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
|
32 |
res = "" |
|
|
33 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
|
34 |
tempfilepath = run("mktemp -t ldtplatform.XXXXXX") |
|
|
35 |
with settings(warn_only=True): |
|
|
36 |
run("echo \"import os\" > %s" % (tempfilepath)) |
|
|
37 |
map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)), |
|
|
38 |
["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module), |
|
|
39 |
"from django.conf import settings", |
|
|
40 |
"print settings.%s" % (settings_key)]) |
|
|
41 |
res = run("python < %s" % (tempfilepath)) |
|
|
42 |
run("rm -f \"%s\"" % (tempfilepath)) |
|
|
43 |
return res |
|
|
44 |
|
|
|
45 |
def rsync_export(path, remotepath, filters): |
|
|
46 |
print("Rsync %s to %s",(path,remotepath)) |
|
|
47 |
|
|
6
|
48 |
filter_option_str = "--progress --stats" |
|
3
|
49 |
if filters: |
|
6
|
50 |
filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters]) |
|
3
|
51 |
|
|
|
52 |
run("mkdir -p \"%s\"" % remotepath) |
|
|
53 |
rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) |
|
|
54 |
print("Rsync %s to %s done",(path,remotepath)) |
|
|
55 |
|
|
|
56 |
def clean_rsync_folder(remotepath): |
|
|
57 |
print("clean rsync folder %s" % remotepath) |
|
|
58 |
run("rm -fr \"%s\"" % remotepath) |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module): |
|
|
62 |
print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
|
63 |
remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT") |
|
|
64 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
|
65 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
|
66 |
#remocve old files optio -c of collect static fail ! |
|
|
67 |
run("rm -fr \"%s\"" % (remotestaticsitepath)) |
|
|
68 |
run("python manage.py collectstatic --noinput") |
|
27
|
69 |
|
|
|
70 |
def migrate(remotepath, remotevirtualenvpath, platform_web_module): |
|
|
71 |
print("migrate in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
|
72 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
|
73 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
|
74 |
#remocve old files optio -c of collect static fail ! |
|
50
|
75 |
run("python manage.py syncdb --migrate --noinput") |
|
3
|
76 |
|
|
50
|
77 |
def create_config(export_path, key): |
|
|
78 |
print("Create config from %s with key %s" % (export_path,key)) |
|
|
79 |
remotepath = env.remote_web_path[key] |
|
|
80 |
remote_config_path = os.path.join(remotepath, env.web_module[key], "config.py") |
|
|
81 |
template_path = os.path.join(export_path, env.local_folders[key], env.web_module[key], "config.py.tmpl") |
|
3
|
82 |
|
|
50
|
83 |
context = env.config[key] |
|
|
84 |
context['base_dir'] = os.path.join(remotepath, env.web_module[key]).rstrip("/")+"/" |
|
3
|
85 |
|
|
|
86 |
if not exists(remote_config_path, verbose=True): |
|
|
87 |
upload_template(template_path, remote_config_path, context=context) |
|
|
88 |
|
|
|
89 |
def export_version(version): |
|
|
90 |
print("export version %s" % str(version)) |
|
|
91 |
|
|
|
92 |
export_path = get_export_path(version) |
|
|
93 |
|
|
|
94 |
clean_export_folder(export_path) |
|
|
95 |
do_export_version(export_path,version) |
|
|
96 |
|
|
|
97 |
return export_path |
|
|
98 |
|
|
|
99 |
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): |
|
|
100 |
print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) |
|
|
101 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
|
102 |
if "remote_baseline_venv" in env and env.remote_baseline_venv: |
|
|
103 |
prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") |
|
|
104 |
else: |
|
|
105 |
prefix_str = "echo" |
|
|
106 |
with settings(warn_only=True): |
|
|
107 |
run("rm -fr \"%s\"" % remotevirtualenvpath) |
|
|
108 |
run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
|
109 |
with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
|
110 |
run("python create_python_env.py") |
|
|
111 |
run("python project-boot.py \"%s\"" % remotevirtualenvpath) |
|
|
112 |
with prefix("source \"%s\"" % activate_path): |
|
|
113 |
run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
|
114 |
|
|
|
115 |
|
|
50
|
116 |
def do_sync_web(version, export_path, key): |
|
|
117 |
print("do_sync_web with version %s and path %s and key %s" % (version,export_path, key)) |
|
|
118 |
web_path = os.path.join(export_path,env.local_folders[key].rstrip("/")+"/") |
|
|
119 |
rsync_export(web_path, env.remote_web_path[key], env.web_rsync_filters[key]) |
|
27
|
120 |
|
|
|
121 |
def remove_lib(path): |
|
|
122 |
print("remove build build %s" % path) |
|
|
123 |
run("rm \"%s\"" % path) |
|
|
124 |
|
|
|
125 |
def install_lib(remotepath, remotevirtualenvpath): |
|
|
126 |
print("Install lib %s in %s" % (remotepath, remotevirtualenvpath)) |
|
|
127 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
|
128 |
|
|
|
129 |
with prefix("source %s" % activate_path): |
|
|
130 |
run("pip install \"%s\"" % remotepath) |
|
|
131 |
|
|
|
132 |
def sync_lib(path): |
|
|
133 |
print("Sync build %s" % path) |
|
|
134 |
run("mkdir -p \"%s\"" % env.remote_venv_export_path) |
|
|
135 |
with cd(env.remote_venv_export_path): |
|
|
136 |
filename = os.path.basename(path) |
|
|
137 |
dest_path = os.path.join(env.remote_venv_export_path, filename) |
|
|
138 |
res_trans = put(path, dest_path) |
|
|
139 |
print("Sync build %s to %s" % (path,repr(res_trans))) |
|
|
140 |
return res_trans |
|
|
141 |
|
|
|
142 |
def sync_install_lib(lib_path): |
|
|
143 |
res_trans = None |
|
|
144 |
try: |
|
|
145 |
res_trans = sync_lib(lib_path) |
|
|
146 |
install_lib(res_trans[0], env.remote_virtualenv_path) |
|
|
147 |
finally: |
|
|
148 |
if res_trans: |
|
|
149 |
remove_lib(res_trans[0]) |
|
|
150 |
|
|
3
|
151 |
|
|
50
|
152 |
def check_folder_access(key): |
|
3
|
153 |
print("Check folder access") |
|
|
154 |
# get remote user |
|
50
|
155 |
for folder_path in env.folders[key]: |
|
3
|
156 |
if not os.path.isabs(folder_path): |
|
50
|
157 |
folder_path = env.remote_web_path[key].rstrip("/")+ "/" + folder_path |
|
3
|
158 |
with settings(warn_only=True): |
|
|
159 |
if not exists(folder_path): |
|
|
160 |
run("mkdir -p \"%s\"" % folder_path) |
|
|
161 |
run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) |
|
|
162 |
run("chmod -R -c g+w \"%s\"" % folder_path) |
|
|
163 |
@task |
|
50
|
164 |
def relaunch_server(key,do_collectstatic=True, do_migrate=True): |
|
3
|
165 |
print("Relaunch server") |
|
50
|
166 |
check_folder_access(key) |
|
3
|
167 |
if do_collectstatic: |
|
50
|
168 |
collectstatic(env.remote_web_path[key], env.remote_virtualenv_path, env.web_module[key]) |
|
27
|
169 |
if do_migrate: |
|
50
|
170 |
migrate(env.remote_web_path[key], env.remote_virtualenv_path, env.web_module[key]) |
|
|
171 |
sudo(env.relaunch_cmd[key], shell=False) |
|
|
172 |
|
|
3
|
173 |
|
|
|
174 |
@task |
|
50
|
175 |
def sync_web(version, key): |
|
3
|
176 |
print(green("sync web with version %s" % version)) |
|
|
177 |
export_path = export_version(version) |
|
50
|
178 |
do_sync_web(version, export_path, key) |
|
|
179 |
create_config(export_path, key) |
|
3
|
180 |
clean_export_folder(export_path) |
|
50
|
181 |
relaunch_server(key) |
|
3
|
182 |
|
|
|
183 |
@task |
|
|
184 |
def update_lib(version, package): |
|
|
185 |
print(green("update ldt with version %s" % version)) |
|
|
186 |
export_path = export_version(version) |
|
17
|
187 |
lib_path = os.path.join(export_path, env.local_folders.get("virtualenv","virtualenv"), "res", "lib") |
|
3
|
188 |
|
|
|
189 |
f, pathname, description = imp.find_module("patch", [lib_path]) |
|
|
190 |
patch = imp.load_module("patch", f, pathname, description) |
|
|
191 |
f, pathname, description = imp.find_module("lib_create_env", [lib_path]) |
|
|
192 |
lib_create_env = imp.load_module("lib_create_env", f, pathname, description) |
|
|
193 |
|
|
17
|
194 |
package_path = os.path.join(export_path, env.local_folders.get("virtualenv","virtualenv"), "res", "src", lib_create_env.URLS[package]['local']) |
|
3
|
195 |
|
|
27
|
196 |
sync_install_lib(package_path) |
|
3
|
197 |
clean_export_folder(export_path) |
|
|
198 |
relaunch_server() |
|
|
199 |
|
|
|
200 |
|
|
|
201 |
@task |
|
|
202 |
def create_virtualenv(version): |
|
|
203 |
print(green("create virtualenv with version %s" % version)) |
|
|
204 |
export_path = export_version(version) |
|
|
205 |
venv_remote_export_path = "" |
|
|
206 |
try: |
|
17
|
207 |
virtualenv_path = os.path.join(export_path, env.local_folders.get("virtualenv","virtualenv")) |
|
3
|
208 |
|
|
|
209 |
venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv") |
|
|
210 |
rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters) |
|
|
211 |
do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path) |
|
|
212 |
finally: |
|
|
213 |
clean_export_folder(export_path) |
|
|
214 |
if venv_remote_export_path: |
|
|
215 |
clean_rsync_folder(venv_remote_export_path) |
|
50
|
216 |
|
|
|
217 |
|
|
|
218 |
#sync hp |
|
|
219 |
# sync files |
|
|
220 |
# create virtualenv |
|
|
221 |
# create config |
|
|
222 |
# syncdb + migrate |
|
|
223 |
# collectstatic |
|
|
224 |
# relaunch web |
|
|
225 |
|
|
|
226 |
#sync ldt |
|
|
227 |
# sync files |
|
|
228 |
# create virtualenv |
|
|
229 |
# create config |
|
|
230 |
# syncdb + migrate |
|
|
231 |
# collectstatic |
|
|
232 |
# relaunch web |