1
|
1 |
from fabric.api import task, run, local, env, cd, put, prefix, sudo, lcd |
0
|
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 |
1
|
11 |
import urlparse |
0
|
12 |
|
|
13 |
import config |
|
14 |
|
|
15 |
def get_export_path(version): |
|
16 |
base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/") |
|
17 |
return os.path.expanduser(base_path) + "_%s" % (str(version)) |
|
18 |
|
|
19 |
def clean_export_folder(path): |
|
20 |
print("Removing %s" % path) |
|
21 |
if os.path.isdir(path): |
|
22 |
shutil.rmtree(path, ignore_errors=True) |
|
23 |
|
1
|
24 |
def do_export_version(path, version, *export_keys): |
|
25 |
print("Export version %s with keys %s" % (str(version), repr(export_keys))) |
|
26 |
|
|
27 |
for export_key in export_keys: |
|
28 |
export_path = os.path.join(path,export_key) |
0
|
29 |
|
1
|
30 |
repo_url = env.repos[export_key] |
|
31 |
url_part = urlparse.urlparse(repo_url) |
|
32 |
if url_part.scheme or url_part.netloc: |
|
33 |
# this is a remote repo. Let's clone first |
|
34 |
clone_path = os.path.join(path,'clone',export_keys) |
|
35 |
os.makedirs(clone_path) |
|
36 |
local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path)) |
|
37 |
else: |
|
38 |
clone_path = repo_url |
|
39 |
|
|
40 |
with lcd(clone_path): |
|
41 |
local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path)) |
|
42 |
|
0
|
43 |
print("Export version %s done"%str(version)) |
|
44 |
|
|
45 |
|
|
46 |
def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key): |
|
47 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
48 |
res = "" |
|
49 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
50 |
tempfilepath = run("mktemp -t ldtplatform.XXXXXX") |
|
51 |
with settings(warn_only=True): |
|
52 |
run("echo \"import os\" > %s" % (tempfilepath)) |
|
53 |
map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)), |
|
54 |
["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module), |
|
55 |
"from django.conf import settings", |
|
56 |
"print settings.%s" % (settings_key)]) |
|
57 |
res = run("python < %s" % (tempfilepath)) |
|
58 |
run("rm -f \"%s\"" % (tempfilepath)) |
|
59 |
return res |
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
def rsync_export(path, remotepath, filters): |
|
64 |
print("Rsync %s to %s",(path,remotepath)) |
|
65 |
|
|
66 |
filter_option_str = "--progress --stats" |
|
67 |
if filters: |
|
68 |
filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters]) |
|
69 |
|
|
70 |
run("mkdir -p \"%s\"" % remotepath) |
|
71 |
rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) |
|
72 |
print("Rsync %s to %s done",(path,remotepath)) |
|
73 |
|
|
74 |
def clean_rsync_folder(remotepath): |
|
75 |
print("clean rsync folder %s" % remotepath) |
|
76 |
run("rm -fr \"%s\"" % remotepath) |
|
77 |
|
|
78 |
def build_src(path): |
|
79 |
print("Build source dist at %s" % path) |
|
80 |
f = None |
|
81 |
try: |
|
82 |
f, pathname, description = imp.find_module("setup", [path]) |
|
83 |
print(" 2 Build source dist at %s" % path) |
|
84 |
setup_mod = imp.load_module("setup", f, pathname, description) |
|
85 |
print(" 3 Build source dist at %s" % path) |
|
86 |
finally: |
|
87 |
if f: |
|
88 |
f.close() |
|
89 |
|
|
90 |
setup_mod.launch_setup("setup.py", ['sdist']) |
|
91 |
|
|
92 |
print("Build source dist at %s done" % path) |
|
93 |
|
|
94 |
|
|
95 |
def get_src_version(path): |
|
96 |
print("get src version at %s" % path) |
|
97 |
f = None |
|
98 |
try: |
|
99 |
f, pathname, description = imp.find_module("ldt", [path]) |
|
100 |
ldt_mod = imp.load_module("ldt", f, pathname, description) |
|
101 |
finally: |
|
102 |
if f: |
|
103 |
f.close() |
|
104 |
version = ldt_mod.VERSION |
|
105 |
version_str = ldt_mod.get_version() |
|
106 |
|
|
107 |
return (version, version_str) |
|
108 |
|
|
109 |
|
|
110 |
def sync_build(path): |
|
111 |
print("Sync build %s" % path) |
1
|
112 |
with cd(env.remote_path['ldt_base']): |
0
|
113 |
filename = os.path.basename(path) |
1
|
114 |
res_trans = put(path, os.path.join(env.remote_path['ldt_base'], filename)) |
0
|
115 |
print("Sync build %s to %s" % (path,repr(res_trans))) |
|
116 |
return res_trans |
|
117 |
|
|
118 |
def remove_build(path): |
|
119 |
print("remove build build %s" % path) |
|
120 |
run("rm \"%s\"" % path) |
|
121 |
|
|
122 |
|
|
123 |
def install_build(remotepath, remotevirtualenvpath): |
|
124 |
print("Install build %s in %s" % (remotepath, remotevirtualenvpath)) |
|
125 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
126 |
|
|
127 |
with prefix("source %s" % activate_path): |
|
128 |
run("pip install \"%s\"" % remotepath) |
|
129 |
|
|
130 |
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module): |
|
131 |
print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
132 |
remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT") |
|
133 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
134 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
135 |
#remocve old files optio -c of collect static fail ! |
|
136 |
run("rm -fr \"%s\"" % (remotestaticsitepath)) |
|
137 |
run("python manage.py collectstatic --noinput") |
|
138 |
|
|
139 |
def create_config(export_path): |
|
140 |
print("Create config from %s" % (export_path,)) |
1
|
141 |
remotepath = env.remote_path['web'] |
0
|
142 |
remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py") |
|
143 |
template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl") |
|
144 |
|
|
145 |
context = { |
|
146 |
'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/", |
|
147 |
'base_url': env.base_url, |
|
148 |
'web_url': env.web_url, |
|
149 |
'stream_url': env.stream_url, |
|
150 |
'stream_src_prefix': env.stream_src_prefix, |
|
151 |
'ffmpeg_path': env.ffmpeg_path, |
|
152 |
'db_engine': env.db_engine, |
|
153 |
'db_name': env.db_name, |
|
154 |
'db_user': env.db_user, |
|
155 |
'db_password': env.db_password, |
|
156 |
'db_host': env.db_host, |
|
157 |
'db_port': env.db_port, |
|
158 |
'log_file': env.log_file, |
|
159 |
'google_analytics_code': env.google_analytics_code, |
|
160 |
'email_use_tls': env.email_use_tls, |
|
161 |
'email_host': env.email_host, |
|
162 |
'email_host_user': env.email_host_user, |
|
163 |
'email_host_user': env.email_host_user, |
|
164 |
'email_port': env.email_port, |
|
165 |
'forbidden_stream_url': env.forbidden_stream_url, |
|
166 |
} |
|
167 |
|
|
168 |
if not exists(remote_config_path, verbose=True): |
|
169 |
upload_template(template_path, remote_config_path, context=context) |
|
170 |
|
1
|
171 |
def export_version(version, *args): |
|
172 |
print("export version %s for %s" % (str(version), args)) |
0
|
173 |
|
|
174 |
export_path = get_export_path(version) |
|
175 |
|
|
176 |
clean_export_folder(export_path) |
1
|
177 |
|
|
178 |
do_export_version(export_path,version, *args) |
0
|
179 |
|
|
180 |
return export_path |
|
181 |
|
|
182 |
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): |
|
183 |
print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) |
|
184 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
185 |
if "remote_baseline_venv" in env and env.remote_baseline_venv: |
|
186 |
prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") |
|
187 |
else: |
|
188 |
prefix_str = "echo" |
|
189 |
with settings(warn_only=True): |
|
190 |
run("rm -fr \"%s\"" % remotevirtualenvpath) |
|
191 |
run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
192 |
with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
193 |
run("python create_python_env.py") |
|
194 |
run("python project-boot.py \"%s\"" % remotevirtualenvpath) |
|
195 |
with prefix("source \"%s\"" % activate_path): |
|
196 |
run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
197 |
|
|
198 |
def do_sync_ldt(version, export_path): |
|
199 |
print("do_sync_ldt with version %s and path %s" % (version,export_path)) |
1
|
200 |
|
|
201 |
## |
0
|
202 |
src_path = export_path + "/src/ldt" |
|
203 |
build_src(src_path) |
|
204 |
(_,version_str) = get_src_version(src_path) |
|
205 |
build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str) |
|
206 |
sync_install_build(build_path) |
|
207 |
|
|
208 |
|
|
209 |
def sync_install_build(build_path): |
|
210 |
res_trans = None |
|
211 |
try: |
|
212 |
res_trans = sync_build(build_path) |
1
|
213 |
install_build(res_trans[0], env.remote_path['virtualenv']) |
0
|
214 |
finally: |
|
215 |
if res_trans: |
|
216 |
remove_build(res_trans[0]) |
|
217 |
|
|
218 |
|
|
219 |
def do_sync_web(version, export_path): |
|
220 |
print("do_sync_web with version %s and path %s" % (version,export_path)) |
1
|
221 |
#sync web |
0
|
222 |
web_path = os.path.join(export_path,"web/") |
1
|
223 |
rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web']) |
|
224 |
#sync src |
|
225 |
src_path = os.path.join(export_path,"src/") |
|
226 |
rsync_export(src_path, env.remote_path['src'], env.rsync_filters['web']) |
|
227 |
|
0
|
228 |
|
|
229 |
def check_folder_access(): |
|
230 |
print("Check folder access") |
|
231 |
# get remote user |
|
232 |
for folder_path in env.folders: |
|
233 |
if not os.path.isabs(folder_path): |
1
|
234 |
folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path |
0
|
235 |
with settings(warn_only=True): |
|
236 |
if not exists(folder_path): |
|
237 |
run("mkdir -p \"%s\"" % folder_path) |
|
238 |
run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) |
|
239 |
run("chmod -R -c g+w \"%s\"" % folder_path) |
|
240 |
@task |
|
241 |
def relaunch_server(do_collectstatic=True): |
|
242 |
print("Relaunch server") |
|
243 |
check_folder_access() |
|
244 |
if do_collectstatic: |
1
|
245 |
collectstatic(env.remote_path['web'], env.remote_path['virtualenv'], env.platform_web_module) |
0
|
246 |
sudo(env.web_relaunch_cmd, shell=False) |
|
247 |
|
|
248 |
@task |
|
249 |
def sync_web(version): |
|
250 |
print(green("sync web with version %s" % version)) |
1
|
251 |
export_path = export_version(version, 'web') |
|
252 |
export_path_full = os.path.join(export_path,'web') |
|
253 |
do_sync_web(version, export_path_full) |
|
254 |
create_config(export_path_full) |
0
|
255 |
clean_export_folder(export_path) |
|
256 |
relaunch_server() |
|
257 |
|
|
258 |
@task |
|
259 |
def sync_ldt(version): |
|
260 |
print(green("sync ldt with version %s" % version)) |
1
|
261 |
export_path = export_version(version, 'ldt') |
|
262 |
export_path_full = os.path.join(export_path,'ldt') |
|
263 |
do_sync_ldt(version, export_path_full) |
0
|
264 |
clean_export_folder(export_path) |
|
265 |
relaunch_server() |
|
266 |
|
|
267 |
@task |
|
268 |
def update_lib(version, package): |
|
269 |
print(green("update ldt with version %s" % version)) |
1
|
270 |
export_path = export_version(version,'web') |
|
271 |
export_path_full = os.path.join(export_path,'web') |
|
272 |
lib_path = os.path.join(export_path_full, "virtualenv", "res", "lib") |
0
|
273 |
|
|
274 |
f, pathname, description = imp.find_module("patch", [lib_path]) |
|
275 |
patch = imp.load_module("patch", f, pathname, description) |
|
276 |
f, pathname, description = imp.find_module("lib_create_env", [lib_path]) |
|
277 |
lib_create_env = imp.load_module("lib_create_env", f, pathname, description) |
|
278 |
|
1
|
279 |
package_path = os.path.join(export_path_full, "virtualenv", "res", "src", lib_create_env.URLS[package]['local']) |
0
|
280 |
|
1
|
281 |
sync_install_build(package_path_full) |
0
|
282 |
clean_export_folder(export_path) |
|
283 |
relaunch_server() |
|
284 |
|
|
285 |
@task |
|
286 |
def sync_platform(version): |
|
287 |
print(green("sync platform with version %s" % version)) |
1
|
288 |
export_path = export_version(version, 'ldt', 'web') |
|
289 |
export_path_ldt = os.path.join(export_path,'ldt') |
|
290 |
export_path_web = os.path.join(export_path,'web') |
|
291 |
do_sync_ldt(version, export_path_ldt) |
|
292 |
do_sync_web(version, export_path_web) |
|
293 |
create_config(export_path_web) |
0
|
294 |
clean_export_folder(export_path) |
|
295 |
relaunch_server() |
|
296 |
|
|
297 |
@task |
|
298 |
def create_virtualenv(version): |
|
299 |
print(green("create virtualenv with version %s" % version)) |
1
|
300 |
export_path = export_version(version, 'web') |
|
301 |
export_path_web = os.path.join(export_path,'web') |
0
|
302 |
venv_remote_export_path = "" |
|
303 |
try: |
1
|
304 |
virtualenv_path = os.path.join(export_path_web, "virtualenv") |
0
|
305 |
|
1
|
306 |
venv_remote_export_path = os.path.join(env.remote_path['venv_export'], env.export_prefix, version,"virtualenv") |
|
307 |
rsync_export(virtualenv_path, venv_remote_export_path, env.rsync_filters['venv']) |
|
308 |
do_create_virtualenv(venv_remote_export_path, env.remote_path['virtualenv']) |
0
|
309 |
finally: |
|
310 |
clean_export_folder(export_path) |
|
311 |
if venv_remote_export_path: |
|
312 |
clean_rsync_folder(venv_remote_export_path) |
1
|
313 |
|
|
314 |
|