|
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 mercurial import commands, ui, hg, cmdutil |
|
5 import imp |
|
6 import os, os.path |
|
7 import shutil |
|
8 import sys |
|
9 |
|
10 import config |
|
11 |
|
12 def get_export_path(version): |
|
13 base_path = os.path.join(env.base_export_path,env.export_prefix).lstrip("/") |
|
14 return os.path.expanduser(base_path) + "_%s" % (str(version)) |
|
15 |
|
16 def clean_export_folder(path): |
|
17 print("Removing %s" % path) |
|
18 if os.path.isdir(path): |
|
19 shutil.rmtree(path, ignore_errors=True) |
|
20 |
|
21 def do_export_version(path, version): |
|
22 print("Export version %s"%str(version)) |
|
23 |
|
24 #hgui = ui.ui() |
|
25 #repo = hg.repository(hgui, cmdutil.findrepo(os.getcwd())) |
|
26 #commands.archive(hgui, repo, path, rev=str(version)) |
|
27 |
|
28 local("hg archive -r \'%s\' \"%s\"" % (str(version),path)) |
|
29 print("Export version %s done"%str(version)) |
|
30 |
|
31 def rsync_export(path, remotepath, filters): |
|
32 print("Rsync %s to %s",(path,remotepath)) |
|
33 |
|
34 if filters: |
|
35 filter_option_str = " ".join(["--filter \"%s\"" % (f) for f in filters]) |
|
36 else: |
|
37 filter_option_str ="" |
|
38 |
|
39 run("mkdir -p \"%s\"" % remotepath) |
|
40 rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) |
|
41 print("Rsync %s to %s done",(path,remotepath)) |
|
42 |
|
43 def clean_rsync_folder(remotepath): |
|
44 print("clean rsync folder %s" % remotepath) |
|
45 run("rm -fr \"%s\"" % remotepath) |
|
46 |
|
47 def build_src(path): |
|
48 print("Build source dist at %s" % path) |
|
49 f = None |
|
50 try: |
|
51 f, pathname, description = imp.find_module("setup", [path]) |
|
52 print(" 2 Build source dist at %s" % path) |
|
53 setup_mod = imp.load_module("setup", f, pathname, description) |
|
54 print(" 3 Build source dist at %s" % path) |
|
55 finally: |
|
56 if f: |
|
57 f.close() |
|
58 |
|
59 setup_mod.launch_setup("setup.py", ['sdist']) |
|
60 |
|
61 print("Build source dist at %s done" % path) |
|
62 |
|
63 |
|
64 def get_src_version(path): |
|
65 print("get src version at %s" % path) |
|
66 f = None |
|
67 try: |
|
68 f, pathname, description = imp.find_module("ldt", [path]) |
|
69 ldt_mod = imp.load_module("ldt", f, pathname, description) |
|
70 finally: |
|
71 if f: |
|
72 f.close() |
|
73 version = ldt_mod.VERSION |
|
74 version_str = ldt_mod.get_version() |
|
75 |
|
76 return (version, version_str) |
|
77 |
|
78 |
|
79 def sync_build(path): |
|
80 print("Sync build %s" % path) |
|
81 with cd(env.remote_ldt_base_path): |
|
82 filename = os.path.basename(path) |
|
83 res_trans = put(path, os.path.join(env.remote_ldt_base_path, filename)) |
|
84 print("Sync build %s to %s" % (path,repr(res_trans))) |
|
85 return res_trans |
|
86 |
|
87 def remove_build(path): |
|
88 print("remove build build %s" % path) |
|
89 run("rm \"%s\"" % path) |
|
90 |
|
91 |
|
92 def install_build(remotepath, remotevirtualenvpath): |
|
93 print("Install build %s in %s" % (remotepath, remotevirtualenvpath)) |
|
94 activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
95 |
|
96 with prefix("source %s" % activate_path): |
|
97 run("pip install \"%s\"" % remotepath) |
|
98 |
|
99 def collectstatic(remotepath, remotevirtualenvpath): |
|
100 print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
101 activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
102 with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(os.path.join(remotepath, env.platform_web_module)): |
|
103 run("python manage.py collectstatic --noinput") |
|
104 |
|
105 |
|
106 |
|
107 def export_version(version): |
|
108 print("export version %s" % str(version)) |
|
109 |
|
110 export_path = get_export_path(version) |
|
111 |
|
112 clean_export_folder(export_path) |
|
113 do_export_version(export_path,version) |
|
114 |
|
115 return export_path |
|
116 |
|
117 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): |
|
118 print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) |
|
119 activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
120 if "remote_baseline_venv" in env and env.remote_baseline_venv: |
|
121 prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") |
|
122 else: |
|
123 prefix_str = "echo" |
|
124 run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
125 with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
126 run("python create_python_env.py") |
|
127 run("python project-boot.py --unzip-setuptools --no-site-packages --clear --type-install=local \"%s\"" % remotevirtualenvpath) |
|
128 with prefix("source \"%s\"" % activate_path): |
|
129 run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
130 |
|
131 def do_sync_ldt(version, export_path): |
|
132 print("do_sync_ldt with version %s and path %s" % (version,export_path)) |
|
133 src_path = export_path + "/src/ldt" |
|
134 build_src(src_path) |
|
135 (_,version_str) = get_src_version(src_path) |
|
136 build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str) |
|
137 res_trans = None |
|
138 try: |
|
139 res_trans = sync_build(build_path) |
|
140 install_build(res_trans[0], env.remote_virtualenv_path) |
|
141 finally: |
|
142 if res_trans: |
|
143 remove_build(res_trans[0]) |
|
144 |
|
145 def do_sync_web(version, export_path): |
|
146 print("do_sync_web with version %s and path %s" % (version,export_path)) |
|
147 web_path = os.path.join(export_path,"web/") |
|
148 rsync_export(web_path, env.remote_web_path, env.web_rsync_filters) |
|
149 |
|
150 def relaunch_server(): |
|
151 print("Relaunch server") |
|
152 collectstatic(env.remote_web_path, env.remote_virtualenv_path) |
|
153 sudo(env.web_relaunch_cmd, shell=False) |
|
154 |
|
155 @task |
|
156 def sync_web(version): |
|
157 print(green("sync web with version %s" % version)) |
|
158 export_path = export_version(version) |
|
159 do_sync_web(version, export_path) |
|
160 clean_export_folder(export_path) |
|
161 relaunch_server() |
|
162 |
|
163 @task |
|
164 def sync_ldt(version): |
|
165 print(green("sync ldt with version %s" % version)) |
|
166 export_path = export_version(version) |
|
167 do_sync_ldt(version, export_path) |
|
168 clean_export_folder(export_path) |
|
169 relaunch_server() |
|
170 |
|
171 @task |
|
172 def sync_platform(version): |
|
173 print(green("sync platform with version %s" % version)) |
|
174 export_path = export_version(version) |
|
175 do_sync_ldt(version, export_path) |
|
176 do_sync_web(version, export_path) |
|
177 clean_export_folder(export_path) |
|
178 relaunch_server() |
|
179 |
|
180 @task |
|
181 def create_virtualenv(version): |
|
182 print(green("create virtualenv with version %s" % version)) |
|
183 export_path = export_version(version) |
|
184 venv_remote_export_path = "" |
|
185 try: |
|
186 virtualenv_path = os.path.join(export_path, "virtualenv") |
|
187 |
|
188 venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv") |
|
189 rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters) |
|
190 do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path) |
|
191 finally: |
|
192 clean_export_folder(export_path) |
|
193 if venv_remote_export_path: |
|
194 clean_rsync_folder(venv_remote_export_path) |