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