|
1 import imp |
|
2 import os.path |
|
3 import io |
|
4 |
|
5 # import config # @UnusedImport |
|
6 # from fablib import (export_version, do_sync_web, create_config, |
|
7 # clean_export_folder, do_sync_comp, sync_install_build, do_create_virtualenv, |
|
8 # clean_rsync_folder, rsync_export, get_src_version, sync_build, |
|
9 # do_relaunch_server, install_build, do_create_virtualenv_requirement, build_src) |
|
10 from core import (export_version, build_src, get_src_version, sync_build, |
|
11 do_create_virtualenv_requirement, get_src_dependencies, |
|
12 do_relaunch_server, clean_export_folder) |
|
13 # from fabric import task, env, run, cd, put |
|
14 from fabric import Connection |
|
15 from invoke import task |
|
16 from blessings import Terminal |
|
17 # from fabric.colors import green |
|
18 |
|
19 # env.use_ssh_config = True |
|
20 |
|
21 t = Terminal() |
|
22 |
|
23 def build_source(c, key, version): |
|
24 print(t.green("build source with version %s" % version)) |
|
25 export_path = export_version(c, **{ key: version }) |
|
26 export_path_full = os.path.join(export_path, key, c.env.repos[key]['src_root']) |
|
27 build_src(c, export_path_full) |
|
28 (_,version_str) = get_src_version(c, key, export_path_full) |
|
29 src_dep = get_src_dependencies(c, key, export_path_full) |
|
30 return (os.path.join(export_path_full,"dist","%s-%s.tar.gz" % (key,version_str)), src_dep) |
|
31 |
|
32 |
|
33 def do_create_virtualenv(c, remote_build_path, dep_remote_build_path_list): |
|
34 env = c.env |
|
35 requirements_path = os.path.join(remote_build_path, env['repos'][env.key]['requirements']) |
|
36 remotevirtualenvpath = env['remote_path']['virtualenv'] |
|
37 do_create_virtualenv_requirement(c, requirements_path, remotevirtualenvpath, env['repos'][env.key]['python_version']) |
|
38 # add setting path to virtualenv |
|
39 ext_path = "import sys; sys.__plen = len(sys.path)\n" |
|
40 for l in env['remote_path'].get('pythonpath', []): |
|
41 ext_path += l + "\n" |
|
42 ext_path += "import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)" |
|
43 with Connection(env['hosts'][0]) as rconnection: |
|
44 rconnection.put(io.StringIO(ext_path), os.path.join(env['remote_path']['virtualenv'], 'lib/python%s/site-packages/_virtualenv_path_extensions.pth'%env['repos'][env.key]['python_version'])) |
|
45 for dep_remote_build_path in dep_remote_build_path_list: |
|
46 with rconnection.prefix("echo $SHELL && . \"%s\"" % os.path.join(remotevirtualenvpath, "bin/activate")): |
|
47 rconnection.run("pip install \"%s\"" % dep_remote_build_path) |
|
48 with rconnection.prefix("echo $SHELL && . \"%s\"" % os.path.join(remotevirtualenvpath, "bin/activate")): |
|
49 rconnection.run("pip install \"%s.tar.gz\"" % remote_build_path) |
|
50 |
|
51 |
|
52 @task |
|
53 def relaunch_server(c, collectstatic=True, migrate=True): |
|
54 print("Relaunch server") |
|
55 do_relaunch_server(c, collectstatic, migrate) |
|
56 |
|
57 |
|
58 @task |
|
59 def create_virtualenv(c, version): |
|
60 |
|
61 print(t.green("create virtualenv for version ") + version) |
|
62 build_path, source_dep_list = build_source(c, c.env.key, version) |
|
63 print(t.green("BUILD PATH: ") + build_path + " - %r" % source_dep_list) |
|
64 |
|
65 source_dep_build_path_list = [] |
|
66 print("Build dependencies : %r" % source_dep_list) |
|
67 for source_dep_key, source_dep_version in source_dep_list: |
|
68 source_dep_build_path, _ = build_source(c, source_dep_key, source_dep_version) |
|
69 source_dep_build_path_list.append(source_dep_build_path) |
|
70 |
|
71 host_connection = Connection(c.env['hosts'][0]) |
|
72 host_connection.run('mkdir -p "%s"' % c.env['remote_path']['build_export']) |
|
73 |
|
74 res_trans = sync_build(c, build_path) |
|
75 res_trans_dep = [ sync_build(c, source_dep_build_path).remote for source_dep_build_path in source_dep_build_path_list] |
|
76 # untar build |
|
77 print("Untar %s on remote host"%res_trans.remote) |
|
78 with host_connection.cd(c.env['remote_path']['build_export']): |
|
79 host_connection.run('tar zxf %s' % res_trans.remote) |
|
80 |
|
81 do_create_virtualenv(c, res_trans.remote[0:-7], res_trans_dep) |
|
82 |
|
83 host_connection.run('rm -fr "%s/*"' % (c.env['remote_path']['build_export'])) |
|
84 clean_export_folder(c.env.remote_path['build_export']) |
|
85 |
|
86 |
|
87 @task |
|
88 def publish_version(c, version): |
|
89 create_virtualenv(c, version) |
|
90 relaunch_server(c, True, True) |