23
|
1 |
from fabric.api import run, local, env, cd, put, prefix, sudo, lcd |
|
2 |
from fabric.context_managers import settings |
|
3 |
from fabric.contrib.files import exists, upload_template |
|
4 |
from fabric.contrib.project import rsync_project |
|
5 |
from mercurial import commands, ui, hg, cmdutil |
|
6 |
import config |
|
7 |
import imp |
|
8 |
import os |
|
9 |
import os.path |
|
10 |
import re |
|
11 |
import shutil |
|
12 |
import urlparse |
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
__all__ = ["check_folder_access", "syncdb", "collectstatic", |
|
17 |
"export_version", "do_sync_web", "create_config", "clean_export_folder", "relaunch_server", |
|
18 |
"do_sync_ldt", "sync_install_build", "do_create_virtualenv", "clean_rsync_folder", "rsync_export"] |
|
19 |
|
|
20 |
def get_export_path(version): |
|
21 |
base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/") |
|
22 |
return os.path.expanduser(base_path) + "_%s" % (str(version)) |
|
23 |
|
|
24 |
def clean_export_folder(path): |
|
25 |
print("Removing %s" % path) |
|
26 |
if os.path.isdir(path): |
|
27 |
shutil.rmtree(path, ignore_errors=True) |
|
28 |
|
|
29 |
def do_export_version(path, **export_keys): |
|
30 |
print("Export version %s : %s " % (path,repr(export_keys))) |
|
31 |
|
|
32 |
for export_key, version in export_keys.items(): |
|
33 |
export_path = os.path.join(path,export_key) |
|
34 |
|
|
35 |
repo_url = env.repos[export_key] |
|
36 |
url_part = urlparse.urlparse(repo_url) |
|
37 |
if url_part.scheme or url_part.netloc: |
|
38 |
# this is a remote repo. Let's clone first |
|
39 |
clone_path = os.path.join(path,'clone',export_keys) |
|
40 |
os.makedirs(clone_path) |
|
41 |
local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path)) |
|
42 |
else: |
|
43 |
clone_path = repo_url |
|
44 |
|
|
45 |
with lcd(clone_path): |
|
46 |
local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path)) |
|
47 |
|
|
48 |
print("Export version %s done"%repr(export_keys)) |
|
49 |
|
|
50 |
|
|
51 |
def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key): |
|
52 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
53 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
54 |
tempfilepath = run("mktemp -t ldtplatform.XXXXXX") |
|
55 |
with settings(warn_only=True): |
|
56 |
run("echo \"import os\" > %s" % (tempfilepath)) |
|
57 |
map(lambda str_echo: run("echo \"%s\" >> %s" % (str_echo, tempfilepath)), |
|
58 |
["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module), |
|
59 |
"from django.conf import settings", |
|
60 |
"print settings.%s" % (settings_key)]) |
|
61 |
res = run("python < %s" % (tempfilepath)) |
|
62 |
run("rm -f \"%s\"" % (tempfilepath)) |
|
63 |
return res |
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
def rsync_export(path, remotepath, filters): |
|
68 |
print("Rsync %s to %s",(path,remotepath)) |
|
69 |
|
|
70 |
filter_option_str = "--progress --stats" |
|
71 |
if filters: |
|
72 |
filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters]) |
|
73 |
|
|
74 |
run("mkdir -p \"%s\"" % remotepath) |
|
75 |
rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) |
|
76 |
print("Rsync %s to %s done",(path,remotepath)) |
|
77 |
|
|
78 |
def clean_rsync_folder(remotepath): |
|
79 |
print("clean rsync folder %s" % remotepath) |
|
80 |
run("rm -fr \"%s\"" % remotepath) |
|
81 |
|
|
82 |
def build_src(path): |
|
83 |
print("Build source dist at %s" % path) |
|
84 |
f = None |
|
85 |
try: |
|
86 |
f, pathname, description = imp.find_module("setup", [path]) |
|
87 |
print(" 2 Build source dist at %s" % path) |
|
88 |
setup_mod = imp.load_module("setup", f, pathname, description) |
|
89 |
print(" 3 Build source dist at %s" % path) |
|
90 |
finally: |
|
91 |
if f: |
|
92 |
f.close() |
|
93 |
|
|
94 |
setup_mod.launch_setup("setup.py", ['sdist']) |
|
95 |
|
|
96 |
print("Build source dist at %s done" % path) |
|
97 |
|
|
98 |
|
|
99 |
def get_src_version(mod_name, path): |
|
100 |
print("get src version for %s at %s" % (mod_name,path)) |
|
101 |
f = None |
|
102 |
try: |
|
103 |
f, pathname, description = imp.find_module(mod_name, [path]) |
|
104 |
ldt_mod = imp.load_module(mod_name, f, pathname, description) |
|
105 |
finally: |
|
106 |
if f: |
|
107 |
f.close() |
|
108 |
version = ldt_mod.VERSION |
|
109 |
version_str = ldt_mod.get_version() |
|
110 |
|
|
111 |
return (version, version_str) |
|
112 |
|
|
113 |
|
|
114 |
def sync_build(path): |
|
115 |
print("Sync build %s" % path) |
|
116 |
with cd(env.remote_path['ldt_base']): |
|
117 |
filename = os.path.basename(path) |
|
118 |
res_trans = put(path, os.path.join(env.remote_path['ldt_base'], filename)) |
|
119 |
print("Sync build %s to %s" % (path,repr(res_trans))) |
|
120 |
return res_trans |
|
121 |
|
|
122 |
def remove_build(path): |
|
123 |
print("remove build build %s" % path) |
|
124 |
run("rm \"%s\"" % path) |
|
125 |
|
|
126 |
|
|
127 |
def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None): |
|
128 |
print("Install build %s in %s" % (remotepath, remotevirtualenvpath)) |
|
129 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
130 |
|
|
131 |
with prefix("source %s" % activate_path): |
|
132 |
if module_to_uninstall: |
|
133 |
with settings(warn_only=True): |
|
134 |
run("pip uninstall -y %s" % module_to_uninstall) |
|
135 |
run("pip install \"%s\"" % remotepath) |
|
136 |
|
|
137 |
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module): |
|
138 |
print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) |
|
139 |
remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT") |
|
140 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
141 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
142 |
#remocve old files optio -c of collect static fail ! |
|
143 |
run("rm -fr \"%s\"" % (remotestaticsitepath)) |
|
144 |
run("python manage.py collectstatic --noinput") |
|
145 |
|
|
146 |
def syncdb(remotepath, remotevirtualenvpath): |
|
147 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
148 |
with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): |
|
149 |
run("python manage.py syncdb --migrate --noinput") |
|
150 |
|
|
151 |
def create_config(export_path): |
|
152 |
print("Create config from %s" % (export_path,)) |
|
153 |
remotepath = env.remote_path['src'] |
|
154 |
remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py") |
|
155 |
template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl") |
|
156 |
|
|
157 |
context = { |
|
158 |
'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/", |
|
159 |
'asctime': '%(asctime)s', |
|
160 |
'levelname': '%(levelname)s', |
|
161 |
'message': '%(message)s', |
|
162 |
'module': '%(module)s', |
|
163 |
} |
|
164 |
context.update(env.config['web']) |
|
165 |
|
|
166 |
if not exists(remote_config_path, verbose=True): |
|
167 |
upload_template(template_path, remote_config_path, context=context) |
|
168 |
|
|
169 |
def export_version(**kwargs): |
|
170 |
print("export version %s" % (repr(kwargs))) |
|
171 |
|
|
172 |
export_path = kwargs.get('path', None) |
|
173 |
|
|
174 |
if not export_path: |
|
175 |
export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()])) |
|
176 |
|
|
177 |
clean_export_folder(export_path) |
|
178 |
|
|
179 |
do_export_version(export_path,**kwargs) |
|
180 |
|
|
181 |
return export_path |
|
182 |
|
|
183 |
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): |
|
184 |
print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) |
|
185 |
activate_path = os.path.join(remotevirtualenvpath, "bin/activate") |
|
186 |
if "remote_baseline_venv" in env and env.remote_baseline_venv: |
|
187 |
prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") |
|
188 |
else: |
|
189 |
prefix_str = "echo" |
|
190 |
with settings(warn_only=True): |
|
191 |
run("rm -fr \"%s\"" % remotevirtualenvpath) |
|
192 |
run("mkdir -p \"%s\"" % remotevirtualenvpath) |
|
193 |
with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): |
|
194 |
run("python create_python_env.py") |
|
195 |
run("python project-boot.py \"%s\"" % remotevirtualenvpath) |
|
196 |
with prefix("source \"%s\"" % activate_path): |
|
197 |
run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) |
|
198 |
|
|
199 |
def do_sync_comp(key, export_path): |
|
200 |
print("do_sync_comp with path %s" % (export_path)) |
|
201 |
|
|
202 |
src_path = os.path.join(export_path,"src") |
|
203 |
# find setup.py |
|
204 |
for root, _, files in os.walk(src_path): |
|
205 |
if "setup.py" in files: |
|
206 |
src_path = root |
|
207 |
|
|
208 |
build_src(src_path) |
|
209 |
(_,version_str) = get_src_version(key, src_path) |
|
210 |
build_path = os.path.join(src_path,"dist","%s-%s.tar.gz" % (key,version_str)) |
|
211 |
sync_install_build(build_path, key) |
|
212 |
|
|
213 |
|
|
214 |
def sync_install_build(build_path, module_to_uninstall=None): |
|
215 |
res_trans = None |
|
216 |
try: |
|
217 |
res_trans = sync_build(build_path) |
|
218 |
install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall) |
|
219 |
finally: |
|
220 |
if res_trans: |
|
221 |
remove_build(res_trans[0]) |
|
222 |
|
|
223 |
|
|
224 |
def do_sync_web(version, export_path): |
|
225 |
print("do_sync_web with version %s and path %s" % (version,export_path)) |
|
226 |
#sync web |
|
227 |
web_path = os.path.join(export_path,"web/") |
|
228 |
rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web']) |
|
229 |
#sync src |
|
230 |
src_path = os.path.join(export_path,"src/") |
|
231 |
rsync_export(src_path, env.remote_path['src'], env.rsync_filters['src']) |
|
232 |
|
|
233 |
|
|
234 |
def check_folder_access(): |
|
235 |
print("Check folder access") |
|
236 |
# get remote user |
|
237 |
for folder_path in env.folders: |
|
238 |
if not os.path.isabs(folder_path): |
|
239 |
folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path |
|
240 |
with settings(warn_only=True): |
|
241 |
if not exists(folder_path): |
|
242 |
run("mkdir -p \"%s\"" % folder_path) |
|
243 |
run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) |
|
244 |
run("chmod -R -c g+w \"%s\"" % folder_path) |
|
245 |
|
|
246 |
def get_comp_versions_dict(export_path_web): |
|
247 |
comp_versions = {} |
|
248 |
with open(os.path.join(export_path_web, 'src', 'requirement.txt')) as f: |
|
249 |
for line in f: |
|
250 |
m = re.match('^(\w+)\s+\(\s*\=\=\s*([\.\d\w]+)\s*\)', line) |
|
251 |
if m: |
|
252 |
key, version_req = m.groups() |
|
253 |
if "." in version_req: |
|
254 |
version_req = "V" + ".".join(["%02d" % (int(s)) if s.isdigit() else s for s in version_req.split(".")]) |
|
255 |
comp_versions[key] = version_req |
|
256 |
|
|
257 |
return comp_versions |