|
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).lstrip("/") |
|
16 return os.path.expanduser(base_path) + "_%s" % (str(version)) |
|
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 #hgui = ui.ui() |
|
27 #repo = hg.repository(hgui, cmdutil.findrepo(os.getcwd())) |
|
28 #commands.archive(hgui, repo, path, rev=str(version)) |
|
29 |
|
30 local("hg archive -r \'%s\' \"%s\"" % (str(version),path)) |
|
31 print("Export version %s done"%str(version)) |
|
32 |
|
33 def rsync_export(path, remotepath, filters): |
|
34 print("Rsync %s to %s",(path,remotepath)) |
|
35 |
|
36 if filters: |
|
37 filter_option_str = " ".join(["--filter \"%s\"" % (f) for f in filters]) |
|
38 else: |
|
39 filter_option_str ="" |
|
40 |
|
41 run("mkdir -p \"%s\"" % remotepath) |
|
42 rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) |
|
43 print("Rsync %s to %s done",(path,remotepath)) |
|
44 |
|
45 def clean_rsync_folder(remotepath): |
|
46 print("clean rsync folder %s" % remotepath) |
|
47 run("rm -fr \"%s\"" % remotepath) |
|
48 |
|
49 def build_src(path): |
|
50 print("Build source dist at %s" % path) |
|
51 f = None |
|
52 try: |
|
53 f, pathname, description = imp.find_module("setup", [path]) |
|
54 print(" 2 Build source dist at %s" % path) |
|
55 setup_mod = imp.load_module("setup", f, pathname, description) |
|
56 print(" 3 Build source dist at %s" % path) |
|
57 finally: |
|
58 if f: |
|
59 f.close() |
|
60 |
|
61 setup_mod.launch_setup("setup.py", ['sdist']) |
|
62 |
|
63 print("Build source dist at %s done" % path) |
|
64 |
|
65 |
|
66 def get_src_version(path): |
|
67 print("get src version at %s" % path) |
|
68 f = None |
|
69 try: |
|
70 f, pathname, description = imp.find_module("ldt", [path]) |
|
71 ldt_mod = imp.load_module("ldt", f, pathname, description) |
|
72 finally: |
|
73 if f: |
|
74 f.close() |
|
75 version = ldt_mod.VERSION |
|
76 version_str = ldt_mod.get_version() |
|
77 |
|
78 return (version, version_str) |
|
79 |
|
80 |
|
81 def sync_build(path): |
|
82 print("Sync build %s" % path) |
|
83 with cd(env.remote_ldt_base_path): |
|
84 filename = os.path.basename(path) |
|
85 res_trans = put(path, os.path.join(env.remote_ldt_base_path, filename)) |
|
86 print("Sync build %s to %s" % (path,repr(res_trans))) |
|
87 return res_trans |
|
88 |
|
89 def remove_build(path): |
|
90 print("remove build build %s" % path) |
|
91 run("rm \"%s\"" % path) |
|
92 |
|
93 |
|
94 def install_build(remotepath, remotevirtualenvpath): |
|
95 print("Install build %s in %s" % (remotepath, remotevirtualenvpath)) |
|
96 activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
97 |
|
98 with prefix("source %s" % activate_path): |
|
99 run("pip install \"%s\"" % remotepath) |
|
100 |
|
101 def collectstatic(remotepath, remotevirtualenvpath): |
|
102 print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
103 activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
104 with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
105 run("python manage.py collectstatic -c --noinput") |
|
106 |
|
107 def create_config(export_path): |
|
108 print("Create config from %s" % (export_path,)) |
|
109 remotepath = env.remote_web_path |
|
110 remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py") |
|
111 template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl") |
|
112 |
|
113 context = { |
|
114 'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/", |
|
115 'base_url': env.base_url, |
|
116 'web_url': env.web_url, |
|
117 'stream_url': env.stream_url, |
|
118 'stream_src_prefix': env.stream_src_prefix, |
|
119 'ffmpeg_path': env.ffmpeg_path, |
|
120 'db_engine': env.db_engine, |
|
121 'db_name': env.db_name, |
|
122 'db_user': env.db_user, |
|
123 'db_password': env.db_password, |
|
124 'db_host': env.db_host, |
|
125 'db_port': env.db_port, |
|
126 'log_file': env.log_file, |
|
127 'index_path': env.index_path, |
|
128 'google_analytics_code': env.google_analytics_code, |
|
129 'email_use_tls': env.email_use_tls, |
|
130 'email_host': env.email_host, |
|
131 'email_host_user': env.email_host_user, |
|
132 'email_host_user': env.email_host_user, |
|
133 'email_port': env.email_port, |
|
134 } |
|
135 |
|
136 if not exists(remote_config_path, verbose=True): |
|
137 upload_template(template_path, remote_config_path, context=context) |
|
138 |
|
139 def export_version(version): |
|
140 print("export version %s" % str(version)) |
|
141 |
|
142 export_path = get_export_path(version) |
|
143 |
|
144 clean_export_folder(export_path) |
|
145 do_export_version(export_path,version) |
|
146 |
|
147 return export_path |
|
148 |
|
149 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): |
|
150 print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) |
|
151 activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
152 if "remote_baseline_venv" in env and env.remote_baseline_venv: |
|
153 prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") |
|
154 else: |
|
155 prefix_str = "echo" |
|
156 with settings(warn_only=True): |
|
157 run("rm -fr \"%s\"" % remotevirtualenvpath) |
|
158 run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
159 with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
160 run("python create_python_env.py") |
|
161 run("python project-boot.py \"%s\"" % remotevirtualenvpath) |
|
162 with prefix("source \"%s\"" % activate_path): |
|
163 run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
164 |
|
165 def do_sync_ldt(version, export_path): |
|
166 print("do_sync_ldt with version %s and path %s" % (version,export_path)) |
|
167 src_path = export_path + "/src/ldt" |
|
168 build_src(src_path) |
|
169 (_,version_str) = get_src_version(src_path) |
|
170 build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str) |
|
171 sync_install_build(build_path) |
|
172 |
|
173 |
|
174 def sync_install_build(build_path): |
|
175 res_trans = None |
|
176 try: |
|
177 res_trans = sync_build(build_path) |
|
178 install_build(res_trans[0], env.remote_virtualenv_path) |
|
179 finally: |
|
180 if res_trans: |
|
181 remove_build(res_trans[0]) |
|
182 |
|
183 |
|
184 def do_sync_web(version, export_path): |
|
185 print("do_sync_web with version %s and path %s" % (version,export_path)) |
|
186 web_path = os.path.join(export_path,"web/") |
|
187 rsync_export(web_path, env.remote_web_path, env.web_rsync_filters) |
|
188 |
|
189 def check_folder_access(): |
|
190 print("Check folder access") |
|
191 # get remote user |
|
192 for folder_path in env.folders: |
|
193 if not os.path.isabs(folder_path): |
|
194 folder_path = env.remote_web_path.rstrip("/")+ "/" + folder_path |
|
195 with settings(warn_only=True): |
|
196 if not exists(folder_path): |
|
197 run("mkdir -p \"%s\"" % folder_path) |
|
198 run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) |
|
199 run("chmod -R -c g+w \"%s\"" % folder_path) |
|
200 |
|
201 def relaunch_server(): |
|
202 print("Relaunch server") |
|
203 collectstatic(env.remote_web_path, env.remote_virtualenv_path) |
|
204 check_folder_access() |
|
205 sudo(env.web_relaunch_cmd, shell=False) |
|
206 |
|
207 @task |
|
208 def sync_web(version): |
|
209 print(green("sync web with version %s" % version)) |
|
210 export_path = export_version(version) |
|
211 do_sync_web(version, export_path) |
|
212 create_config(export_path) |
|
213 clean_export_folder(export_path) |
|
214 relaunch_server() |
|
215 |
|
216 @task |
|
217 def sync_ldt(version): |
|
218 print(green("sync ldt with version %s" % version)) |
|
219 export_path = export_version(version) |
|
220 do_sync_ldt(version, export_path) |
|
221 clean_export_folder(export_path) |
|
222 relaunch_server() |
|
223 |
|
224 @task |
|
225 def update_lib(version, package): |
|
226 print(green("update ldt with version %s" % version)) |
|
227 export_path = export_version(version) |
|
228 lib_path = os.path.join(export_path, "virtualenv", "res", "lib") |
|
229 |
|
230 f, pathname, description = imp.find_module("patch", [lib_path]) |
|
231 patch = imp.load_module("patch", f, pathname, description) |
|
232 f, pathname, description = imp.find_module("lib_create_env", [lib_path]) |
|
233 lib_create_env = imp.load_module("lib_create_env", f, pathname, description) |
|
234 |
|
235 package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local']) |
|
236 |
|
237 sync_install_build(package_path) |
|
238 clean_export_folder(export_path) |
|
239 relaunch_server() |
|
240 |
|
241 @task |
|
242 def sync_platform(version): |
|
243 print(green("sync platform with version %s" % version)) |
|
244 export_path = export_version(version) |
|
245 do_sync_ldt(version, export_path) |
|
246 do_sync_web(version, export_path) |
|
247 create_config(export_path) |
|
248 clean_export_folder(export_path) |
|
249 relaunch_server() |
|
250 |
|
251 @task |
|
252 def create_virtualenv(version): |
|
253 print(green("create virtualenv with version %s" % version)) |
|
254 export_path = export_version(version) |
|
255 venv_remote_export_path = "" |
|
256 try: |
|
257 virtualenv_path = os.path.join(export_path, "virtualenv") |
|
258 |
|
259 venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv") |
|
260 rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters) |
|
261 do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path) |
|
262 finally: |
|
263 clean_export_folder(export_path) |
|
264 if venv_remote_export_path: |
|
265 clean_rsync_folder(venv_remote_export_path) |