| author | ymh <ymh.work@gmail.com> |
| Fri, 22 Jun 2012 20:44:32 +0200 | |
| changeset 86 | bb39eafd5293 |
| parent 82 | cb842369c5ca |
| child 96 | 900b29a370ae |
| permissions | -rw-r--r-- |
| 16 | 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 |
|
| 69 | 4 |
from fabric.contrib.files import exists, upload_template |
| 86 | 5 |
from fabric.context_managers import settings |
| 16 | 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") |
|
| 66 | 104 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
| 86 | 105 |
run("python manage.py collectstatic -c --noinput") |
| 16 | 106 |
|
| 69 | 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, |
|
| 71 | 116 |
'web_url': env.web_url, |
| 69 | 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) |
|
| 16 | 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 |
run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
157 |
with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
158 |
run("python create_python_env.py") |
|
159 |
run("python project-boot.py --unzip-setuptools --no-site-packages --clear --type-install=local \"%s\"" % remotevirtualenvpath) |
|
160 |
with prefix("source \"%s\"" % activate_path): |
|
161 |
run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
162 |
||
163 |
def do_sync_ldt(version, export_path): |
|
164 |
print("do_sync_ldt with version %s and path %s" % (version,export_path)) |
|
165 |
src_path = export_path + "/src/ldt" |
|
166 |
build_src(src_path) |
|
167 |
(_,version_str) = get_src_version(src_path) |
|
168 |
build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str) |
|
|
79
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
169 |
sync_install_build(build_path) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
170 |
|
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
171 |
|
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
172 |
def sync_install_build(build_path): |
| 16 | 173 |
res_trans = None |
174 |
try: |
|
175 |
res_trans = sync_build(build_path) |
|
176 |
install_build(res_trans[0], env.remote_virtualenv_path) |
|
177 |
finally: |
|
178 |
if res_trans: |
|
179 |
remove_build(res_trans[0]) |
|
180 |
||
|
79
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
181 |
|
| 16 | 182 |
def do_sync_web(version, export_path): |
183 |
print("do_sync_web with version %s and path %s" % (version,export_path)) |
|
184 |
web_path = os.path.join(export_path,"web/") |
|
185 |
rsync_export(web_path, env.remote_web_path, env.web_rsync_filters) |
|
186 |
||
187 |
def relaunch_server(): |
|
188 |
print("Relaunch server") |
|
189 |
collectstatic(env.remote_web_path, env.remote_virtualenv_path) |
|
190 |
sudo(env.web_relaunch_cmd, shell=False) |
|
| 86 | 191 |
|
192 |
def check_folder_access(): |
|
193 |
print("Check folder access") |
|
194 |
# get remote user |
|
195 |
for folder_path in env.folders: |
|
196 |
if not os.path.isabs(folder_path): |
|
197 |
folder_path = env.remote_web_path.rstrip("/")+ "/" + folder_path |
|
198 |
with settings(warn_only=True): |
|
199 |
if not exists(folder_path): |
|
200 |
run("mkdir -p \"%s\"" % folder_path) |
|
201 |
run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) |
|
202 |
run("chown -R -c g+w \"%s\"" % folder_path) |
|
| 16 | 203 |
|
204 |
@task |
|
205 |
def sync_web(version): |
|
206 |
print(green("sync web with version %s" % version)) |
|
207 |
export_path = export_version(version) |
|
| 69 | 208 |
do_sync_web(version, export_path) |
| 86 | 209 |
check_folder_access() |
210 |
create_config(export_path) |
|
| 16 | 211 |
clean_export_folder(export_path) |
212 |
relaunch_server() |
|
213 |
||
214 |
@task |
|
215 |
def sync_ldt(version): |
|
216 |
print(green("sync ldt with version %s" % version)) |
|
217 |
export_path = export_version(version) |
|
218 |
do_sync_ldt(version, export_path) |
|
219 |
clean_export_folder(export_path) |
|
|
79
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
220 |
relaunch_server() |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
221 |
|
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
222 |
@task |
| 82 | 223 |
def update_lib(version, package): |
|
79
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
224 |
print(green("update ldt with version %s" % version)) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
225 |
export_path = export_version(version) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
226 |
lib_path = os.path.join(export_path, "virtualenv", "res", "lib") |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
227 |
|
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
228 |
f, pathname, description = imp.find_module("patch", [lib_path]) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
229 |
patch = imp.load_module("patch", f, pathname, description) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
230 |
f, pathname, description = imp.find_module("lib_create_env", [lib_path]) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
231 |
lib_create_env = imp.load_module("lib_create_env", f, pathname, description) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
232 |
|
| 82 | 233 |
package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local']) |
|
79
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
234 |
|
| 82 | 235 |
sync_install_build(package_path) |
|
79
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
236 |
clean_export_folder(export_path) |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
237 |
relaunch_server() |
|
4afe089760bc
add a method to update ldt platform without release
ymh <ymh.work@gmail.com>
parents:
71
diff
changeset
|
238 |
|
| 16 | 239 |
@task |
240 |
def sync_platform(version): |
|
241 |
print(green("sync platform with version %s" % version)) |
|
242 |
export_path = export_version(version) |
|
243 |
do_sync_ldt(version, export_path) |
|
244 |
do_sync_web(version, export_path) |
|
| 86 | 245 |
check_folder_access() |
246 |
create_config(export_path) |
|
| 16 | 247 |
clean_export_folder(export_path) |
248 |
relaunch_server() |
|
249 |
||
250 |
@task |
|
251 |
def create_virtualenv(version): |
|
252 |
print(green("create virtualenv with version %s" % version)) |
|
253 |
export_path = export_version(version) |
|
254 |
venv_remote_export_path = "" |
|
255 |
try: |
|
256 |
virtualenv_path = os.path.join(export_path, "virtualenv") |
|
257 |
||
258 |
venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv") |
|
259 |
rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters) |
|
260 |
do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path) |
|
261 |
finally: |
|
262 |
clean_export_folder(export_path) |
|
263 |
if venv_remote_export_path: |
|
264 |
clean_rsync_folder(venv_remote_export_path) |