author | ymh <ymh.work@gmail.com> |
Thu, 29 Nov 2012 22:34:27 +0100 | |
changeset 7 | fc1eea0039d8 |
parent 6 | 740d11ae9e28 |
child 9 | 4fb252f37428 |
permissions | -rw-r--r-- |
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 |
||
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
24 |
def do_export_version(path, **export_keys): |
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
25 |
print("Export version %s " % (repr(export_keys))) |
1 | 26 |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
27 |
for export_key, version in export_keys.items(): |
1 | 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 |
||
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
43 |
print("Export version %s done"%repr(export_keys)) |
0 | 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 |
||
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
123 |
def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None): |
0 | 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): |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
128 |
if module_to_uninstall: |
7 | 129 |
with settings(warn_only=True): |
130 |
run("pip uninstall -y %s" % module_to_uninstall) |
|
0 | 131 |
run("pip install \"%s\"" % remotepath) |
132 |
||
133 |
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module): |
|
134 |
print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
135 |
remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT") |
|
136 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
137 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
138 |
#remocve old files optio -c of collect static fail ! |
|
139 |
run("rm -fr \"%s\"" % (remotestaticsitepath)) |
|
140 |
run("python manage.py collectstatic --noinput") |
|
141 |
||
142 |
def create_config(export_path): |
|
143 |
print("Create config from %s" % (export_path,)) |
|
7 | 144 |
remotepath = env.remote_path['src'] |
0 | 145 |
remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py") |
7 | 146 |
template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl") |
0 | 147 |
|
148 |
context = { |
|
7 | 149 |
'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/", |
150 |
'asctime': '%(asctime)s', |
|
151 |
'levelname': '%(levelname)s', |
|
152 |
'message': '%(message)s', |
|
153 |
'module': '%(module)s', |
|
0 | 154 |
} |
6
740d11ae9e28
correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
155 |
context.update(env.config['web']) |
0 | 156 |
|
157 |
if not exists(remote_config_path, verbose=True): |
|
158 |
upload_template(template_path, remote_config_path, context=context) |
|
159 |
||
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
160 |
def export_version(**kwargs): |
6
740d11ae9e28
correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
161 |
print("export version %s" % (repr(kwargs))) |
0 | 162 |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
163 |
export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()])) |
0 | 164 |
|
165 |
clean_export_folder(export_path) |
|
1 | 166 |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
167 |
do_export_version(export_path,**kwargs) |
0 | 168 |
|
169 |
return export_path |
|
170 |
||
171 |
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): |
|
172 |
print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) |
|
173 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
174 |
if "remote_baseline_venv" in env and env.remote_baseline_venv: |
|
175 |
prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") |
|
176 |
else: |
|
177 |
prefix_str = "echo" |
|
178 |
with settings(warn_only=True): |
|
179 |
run("rm -fr \"%s\"" % remotevirtualenvpath) |
|
180 |
run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
181 |
with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
182 |
run("python create_python_env.py") |
|
183 |
run("python project-boot.py \"%s\"" % remotevirtualenvpath) |
|
184 |
with prefix("source \"%s\"" % activate_path): |
|
185 |
run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
186 |
||
187 |
def do_sync_ldt(version, export_path): |
|
188 |
print("do_sync_ldt with version %s and path %s" % (version,export_path)) |
|
1 | 189 |
|
190 |
## |
|
0 | 191 |
src_path = export_path + "/src/ldt" |
192 |
build_src(src_path) |
|
193 |
(_,version_str) = get_src_version(src_path) |
|
194 |
build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str) |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
195 |
sync_install_build(build_path, 'ldt') |
0 | 196 |
|
197 |
||
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
198 |
def sync_install_build(build_path, module_to_uninstall=None): |
0 | 199 |
res_trans = None |
200 |
try: |
|
201 |
res_trans = sync_build(build_path) |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
202 |
install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall) |
0 | 203 |
finally: |
204 |
if res_trans: |
|
205 |
remove_build(res_trans[0]) |
|
206 |
||
207 |
||
208 |
def do_sync_web(version, export_path): |
|
209 |
print("do_sync_web with version %s and path %s" % (version,export_path)) |
|
1 | 210 |
#sync web |
0 | 211 |
web_path = os.path.join(export_path,"web/") |
1 | 212 |
rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web']) |
213 |
#sync src |
|
214 |
src_path = os.path.join(export_path,"src/") |
|
215 |
rsync_export(src_path, env.remote_path['src'], env.rsync_filters['web']) |
|
216 |
||
0 | 217 |
|
218 |
def check_folder_access(): |
|
219 |
print("Check folder access") |
|
220 |
# get remote user |
|
221 |
for folder_path in env.folders: |
|
222 |
if not os.path.isabs(folder_path): |
|
1 | 223 |
folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path |
0 | 224 |
with settings(warn_only=True): |
225 |
if not exists(folder_path): |
|
226 |
run("mkdir -p \"%s\"" % folder_path) |
|
227 |
run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) |
|
228 |
run("chmod -R -c g+w \"%s\"" % folder_path) |
|
229 |
@task |
|
230 |
def relaunch_server(do_collectstatic=True): |
|
231 |
print("Relaunch server") |
|
232 |
check_folder_access() |
|
233 |
if do_collectstatic: |
|
7 | 234 |
collectstatic(env.remote_path['src'], env.remote_path['virtualenv'], env.platform_web_module) |
0 | 235 |
sudo(env.web_relaunch_cmd, shell=False) |
236 |
||
237 |
@task |
|
238 |
def sync_web(version): |
|
239 |
print(green("sync web with version %s" % version)) |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
240 |
export_path = export_version(web=version) |
1 | 241 |
export_path_full = os.path.join(export_path,'web') |
242 |
do_sync_web(version, export_path_full) |
|
243 |
create_config(export_path_full) |
|
0 | 244 |
clean_export_folder(export_path) |
245 |
relaunch_server() |
|
246 |
||
247 |
@task |
|
248 |
def sync_ldt(version): |
|
249 |
print(green("sync ldt with version %s" % version)) |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
250 |
export_path = export_version(ldt=version) |
1 | 251 |
export_path_full = os.path.join(export_path,'ldt') |
252 |
do_sync_ldt(version, export_path_full) |
|
0 | 253 |
clean_export_folder(export_path) |
254 |
relaunch_server() |
|
255 |
||
256 |
@task |
|
257 |
def update_lib(version, package): |
|
258 |
print(green("update ldt with version %s" % version)) |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
259 |
export_path = export_version(web=version) |
1 | 260 |
export_path_full = os.path.join(export_path,'web') |
261 |
lib_path = os.path.join(export_path_full, "virtualenv", "res", "lib") |
|
0 | 262 |
|
263 |
f, pathname, description = imp.find_module("patch", [lib_path]) |
|
264 |
patch = imp.load_module("patch", f, pathname, description) |
|
265 |
f, pathname, description = imp.find_module("lib_create_env", [lib_path]) |
|
266 |
lib_create_env = imp.load_module("lib_create_env", f, pathname, description) |
|
267 |
||
1 | 268 |
package_path = os.path.join(export_path_full, "virtualenv", "res", "src", lib_create_env.URLS[package]['local']) |
0 | 269 |
|
1 | 270 |
sync_install_build(package_path_full) |
0 | 271 |
clean_export_folder(export_path) |
272 |
relaunch_server() |
|
273 |
||
274 |
@task |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
275 |
def sync_platform(version_web, version_ldt): |
6
740d11ae9e28
correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
276 |
print(green("sync platform with version web %s, ldt %s" % (version_web, version_ldt))) |
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
277 |
export_path = export_version(ldt=version_ldt, web=version_web) |
1 | 278 |
export_path_ldt = os.path.join(export_path,'ldt') |
279 |
export_path_web = os.path.join(export_path,'web') |
|
6
740d11ae9e28
correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
280 |
do_sync_ldt(version_ldt, export_path_ldt) |
740d11ae9e28
correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents:
3
diff
changeset
|
281 |
do_sync_web(version_web, export_path_web) |
1 | 282 |
create_config(export_path_web) |
0 | 283 |
clean_export_folder(export_path) |
284 |
relaunch_server() |
|
285 |
||
286 |
@task |
|
287 |
def create_virtualenv(version): |
|
288 |
print(green("create virtualenv with version %s" % version)) |
|
3
800c48d0e3c1
Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents:
1
diff
changeset
|
289 |
export_path = export_version(web=version) |
1 | 290 |
export_path_web = os.path.join(export_path,'web') |
0 | 291 |
venv_remote_export_path = "" |
292 |
try: |
|
1 | 293 |
virtualenv_path = os.path.join(export_path_web, "virtualenv") |
0 | 294 |
|
1 | 295 |
venv_remote_export_path = os.path.join(env.remote_path['venv_export'], env.export_prefix, version,"virtualenv") |
296 |
rsync_export(virtualenv_path, venv_remote_export_path, env.rsync_filters['venv']) |
|
297 |
do_create_virtualenv(venv_remote_export_path, env.remote_path['virtualenv']) |
|
0 | 298 |
finally: |
299 |
clean_export_folder(export_path) |
|
300 |
if venv_remote_export_path: |
|
301 |
clean_rsync_folder(venv_remote_export_path) |
|
1 | 302 |
|
303 |