sbin/sync/fabfile.py
changeset 96 900b29a370ae
parent 86 bb39eafd5293
equal deleted inserted replaced
95:04db1cae7afc 96:900b29a370ae
     1 from fabric.api import task, run, local, env, cd, put, prefix, sudo
     1 from fabric.api import task, run, local, env, cd, put, prefix, sudo
     2 from fabric.colors import green
     2 from fabric.colors import green
     3 from fabric.contrib.project import rsync_project
     3 from fabric.contrib.project import rsync_project
     4 from fabric.contrib.files import exists, upload_template
     4 from fabric.contrib.files import exists, upload_template
     5 from fabric.context_managers import settings 
     5 from fabric.context_managers import settings
     6 from mercurial import commands, ui, hg, cmdutil
     6 from mercurial import commands, ui, hg, cmdutil
     7 import imp
     7 import imp
     8 import os, os.path
     8 import os, os.path
     9 import shutil
     9 import shutil
    10 import sys
    10 import sys
    20     if os.path.isdir(path):
    20     if os.path.isdir(path):
    21         shutil.rmtree(path, ignore_errors=True)
    21         shutil.rmtree(path, ignore_errors=True)
    22 
    22 
    23 def do_export_version(path, version):
    23 def do_export_version(path, version):
    24     print("Export version %s"%str(version))
    24     print("Export version %s"%str(version))
    25     
    25 
    26     #hgui = ui.ui() 
    26     #hgui = ui.ui()
    27     #repo = hg.repository(hgui, cmdutil.findrepo(os.getcwd()))
    27     #repo = hg.repository(hgui, cmdutil.findrepo(os.getcwd()))
    28     #commands.archive(hgui, repo, path, rev=str(version))
    28     #commands.archive(hgui, repo, path, rev=str(version))
    29     
    29 
    30     local("hg archive -r \'%s\' \"%s\"" % (str(version),path))
    30     local("hg archive -r \'%s\' \"%s\"" % (str(version),path))
    31     print("Export version %s done"%str(version))
    31     print("Export version %s done"%str(version))
    32     
    32 
    33 def rsync_export(path, remotepath, filters):
    33 def rsync_export(path, remotepath, filters):
    34     print("Rsync %s to %s",(path,remotepath))
    34     print("Rsync %s to %s",(path,remotepath))
    35     
    35 
    36     if filters:
    36     if filters:
    37         filter_option_str = " ".join(["--filter \"%s\"" % (f) for f in filters])
    37         filter_option_str = " ".join(["--filter \"%s\"" % (f) for f in filters])
    38     else:
    38     else:
    39         filter_option_str =""
    39         filter_option_str =""
    40     
    40 
    41     run("mkdir -p \"%s\"" % remotepath)
    41     run("mkdir -p \"%s\"" % remotepath)
    42     rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
    42     rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
    43     print("Rsync %s to %s done",(path,remotepath))
    43     print("Rsync %s to %s done",(path,remotepath))
    44     
    44 
    45 def clean_rsync_folder(remotepath):
    45 def clean_rsync_folder(remotepath):
    46     print("clean rsync folder %s" % remotepath)
    46     print("clean rsync folder %s" % remotepath)
    47     run("rm -fr \"%s\"" % remotepath)
    47     run("rm -fr \"%s\"" % remotepath)
    48     
    48 
    49 def build_src(path):
    49 def build_src(path):
    50     print("Build source dist at %s" % path)
    50     print("Build source dist at %s" % path)
    51     f = None
    51     f = None
    52     try:
    52     try:
    53         f, pathname, description = imp.find_module("setup", [path])
    53         f, pathname, description = imp.find_module("setup", [path])
    55         setup_mod = imp.load_module("setup", f, pathname, description)
    55         setup_mod = imp.load_module("setup", f, pathname, description)
    56         print(" 3 Build source dist at %s" % path)
    56         print(" 3 Build source dist at %s" % path)
    57     finally:
    57     finally:
    58         if f:
    58         if f:
    59             f.close()
    59             f.close()
    60         
    60 
    61     setup_mod.launch_setup("setup.py", ['sdist'])
    61     setup_mod.launch_setup("setup.py", ['sdist'])
    62     
    62 
    63     print("Build source dist at %s done" % path)
    63     print("Build source dist at %s done" % path)
    64     
    64 
    65 
    65 
    66 def get_src_version(path):
    66 def get_src_version(path):
    67     print("get src version at %s" % path)
    67     print("get src version at %s" % path)
    68     f = None
    68     f = None
    69     try:
    69     try:
    72     finally:
    72     finally:
    73         if f:
    73         if f:
    74             f.close()
    74             f.close()
    75     version = ldt_mod.VERSION
    75     version = ldt_mod.VERSION
    76     version_str = ldt_mod.get_version()
    76     version_str = ldt_mod.get_version()
    77     
    77 
    78     return (version, version_str) 
    78     return (version, version_str)
    79     
    79 
    80 
    80 
    81 def sync_build(path):
    81 def sync_build(path):
    82     print("Sync build %s" % path)
    82     print("Sync build %s" % path)
    83     with cd(env.remote_ldt_base_path):
    83     with cd(env.remote_ldt_base_path):
    84         filename = os.path.basename(path)
    84         filename = os.path.basename(path)
    87         return res_trans
    87         return res_trans
    88 
    88 
    89 def remove_build(path):
    89 def remove_build(path):
    90     print("remove build build %s" % path)
    90     print("remove build build %s" % path)
    91     run("rm \"%s\"" % path)
    91     run("rm \"%s\"" % path)
    92         
    92 
    93 
    93 
    94 def install_build(remotepath, remotevirtualenvpath):
    94 def install_build(remotepath, remotevirtualenvpath):
    95     print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
    95     print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
    96     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
    96     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
    97     
    97 
    98     with prefix("source %s" % activate_path):
    98     with prefix("source %s" % activate_path):
    99         run("pip install \"%s\"" % remotepath)
    99         run("pip install \"%s\"" % remotepath)
   100 
   100 
   101 def collectstatic(remotepath, remotevirtualenvpath):
   101 def collectstatic(remotepath, remotevirtualenvpath):
   102     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
   102     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
   103     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   103     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   104     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
   104     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
   105         run("python manage.py collectstatic -c --noinput")
   105         run("python manage.py collectstatic -c --noinput")
   106         
   106 
   107 def create_config(export_path):    
   107 def create_config(export_path):
   108     print("Create config from %s" % (export_path,))
   108     print("Create config from %s" % (export_path,))
   109     remotepath = env.remote_web_path
   109     remotepath = env.remote_web_path
   110     remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
   110     remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
   111     template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl")
   111     template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl")
   112     
   112 
   113     context = {
   113     context = {
   114      'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
   114      'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
   115      'base_url': env.base_url,
   115      'base_url': env.base_url,
   116      'web_url': env.web_url,
   116      'web_url': env.web_url,
   117      'stream_url': env.stream_url,
   117      'stream_url': env.stream_url,
   130      'email_host': env.email_host,
   130      'email_host': env.email_host,
   131      'email_host_user': env.email_host_user,
   131      'email_host_user': env.email_host_user,
   132      'email_host_user': env.email_host_user,
   132      'email_host_user': env.email_host_user,
   133      'email_port': env.email_port,
   133      'email_port': env.email_port,
   134     }
   134     }
   135     
   135 
   136     if not exists(remote_config_path, verbose=True):
   136     if not exists(remote_config_path, verbose=True):
   137         upload_template(template_path, remote_config_path, context=context)
   137         upload_template(template_path, remote_config_path, context=context)
   138 
   138 
   139 def export_version(version):
   139 def export_version(version):
   140     print("export version %s" % str(version))
   140     print("export version %s" % str(version))
   141     
   141 
   142     export_path = get_export_path(version)
   142     export_path = get_export_path(version)
   143     
   143 
   144     clean_export_folder(export_path)
   144     clean_export_folder(export_path)
   145     do_export_version(export_path,version)
   145     do_export_version(export_path,version)
   146     
   146 
   147     return export_path
   147     return export_path
   148 
   148 
   149 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
   149 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
   150     print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
   150     print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
   151     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   151     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   156     run("mkdir -p \"%s\"" % remotevirtualenvpath)
   156     run("mkdir -p \"%s\"" % remotevirtualenvpath)
   157     with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
   157     with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
   158         run("python create_python_env.py")
   158         run("python create_python_env.py")
   159         run("python project-boot.py --unzip-setuptools --no-site-packages --clear --type-install=local \"%s\"" % remotevirtualenvpath)
   159         run("python project-boot.py --unzip-setuptools --no-site-packages --clear --type-install=local \"%s\"" % remotevirtualenvpath)
   160     with prefix("source \"%s\"" % activate_path):
   160     with prefix("source \"%s\"" % activate_path):
   161         run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
   161         run("pip install -i https://pypi.python.org/simple -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
   162 
   162 
   163 def do_sync_ldt(version, export_path):
   163 def do_sync_ldt(version, export_path):
   164     print("do_sync_ldt with version %s and path %s" % (version,export_path))
   164     print("do_sync_ldt with version %s and path %s" % (version,export_path))
   165     src_path = export_path + "/src/ldt"
   165     src_path = export_path + "/src/ldt"
   166     build_src(src_path)
   166     build_src(src_path)
   167     (_,version_str) = get_src_version(src_path)
   167     (_,version_str) = get_src_version(src_path)
   168     build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
   168     build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
   169     sync_install_build(build_path)
   169     sync_install_build(build_path)
   170         
   170 
   171 
   171 
   172 def sync_install_build(build_path):
   172 def sync_install_build(build_path):
   173     res_trans = None
   173     res_trans = None
   174     try:
   174     try:
   175         res_trans = sync_build(build_path)
   175         res_trans = sync_build(build_path)
   176         install_build(res_trans[0], env.remote_virtualenv_path)        
   176         install_build(res_trans[0], env.remote_virtualenv_path)
   177     finally:
   177     finally:
   178         if res_trans:
   178         if res_trans:
   179             remove_build(res_trans[0])
   179             remove_build(res_trans[0])
   180 
   180 
   181 
   181 
   182 def do_sync_web(version, export_path):
   182 def do_sync_web(version, export_path):
   183     print("do_sync_web with version %s and path %s" % (version,export_path))
   183     print("do_sync_web with version %s and path %s" % (version,export_path))
   184     web_path = os.path.join(export_path,"web/") 
   184     web_path = os.path.join(export_path,"web/")
   185     rsync_export(web_path, env.remote_web_path, env.web_rsync_filters)    
   185     rsync_export(web_path, env.remote_web_path, env.web_rsync_filters)
   186 
   186 
   187 def relaunch_server():
   187 def relaunch_server():
   188     print("Relaunch server")
   188     print("Relaunch server")
   189     collectstatic(env.remote_web_path, env.remote_virtualenv_path)
   189     collectstatic(env.remote_web_path, env.remote_virtualenv_path)
   190     sudo(env.web_relaunch_cmd, shell=False)
   190     sudo(env.web_relaunch_cmd, shell=False)
   191     
   191 
   192 def check_folder_access():
   192 def check_folder_access():
   193     print("Check folder access")
   193     print("Check folder access")
   194     # get remote user
   194     # get remote user
   195     for folder_path in env.folders:
   195     for folder_path in env.folders:
   196         if not os.path.isabs(folder_path):
   196         if not os.path.isabs(folder_path):
   216     print(green("sync ldt with version %s" % version))
   216     print(green("sync ldt with version %s" % version))
   217     export_path = export_version(version)
   217     export_path = export_version(version)
   218     do_sync_ldt(version, export_path)
   218     do_sync_ldt(version, export_path)
   219     clean_export_folder(export_path)
   219     clean_export_folder(export_path)
   220     relaunch_server()
   220     relaunch_server()
   221     
   221 
   222 @task
   222 @task
   223 def update_lib(version, package):
   223 def update_lib(version, package):
   224     print(green("update ldt with version %s" % version))
   224     print(green("update ldt with version %s" % version))
   225     export_path = export_version(version)
   225     export_path = export_version(version)
   226     lib_path = os.path.join(export_path, "virtualenv", "res", "lib")
   226     lib_path = os.path.join(export_path, "virtualenv", "res", "lib")
   227     
   227 
   228     f, pathname, description = imp.find_module("patch", [lib_path])
   228     f, pathname, description = imp.find_module("patch", [lib_path])
   229     patch = imp.load_module("patch", f, pathname, description)
   229     patch = imp.load_module("patch", f, pathname, description)
   230     f, pathname, description = imp.find_module("lib_create_env", [lib_path])
   230     f, pathname, description = imp.find_module("lib_create_env", [lib_path])
   231     lib_create_env = imp.load_module("lib_create_env", f, pathname, description)
   231     lib_create_env = imp.load_module("lib_create_env", f, pathname, description)
   232     
   232 
   233     package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
   233     package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
   234     
   234 
   235     sync_install_build(package_path)
   235     sync_install_build(package_path)
   236     clean_export_folder(export_path)
   236     clean_export_folder(export_path)
   237     relaunch_server()
   237     relaunch_server()
   238     
   238 
   239 @task
   239 @task
   240 def sync_platform(version):
   240 def sync_platform(version):
   241     print(green("sync platform with version %s" % version))
   241     print(green("sync platform with version %s" % version))
   242     export_path = export_version(version)
   242     export_path = export_version(version)
   243     do_sync_ldt(version, export_path)
   243     do_sync_ldt(version, export_path)
   252     print(green("create virtualenv with version %s" % version))
   252     print(green("create virtualenv with version %s" % version))
   253     export_path = export_version(version)
   253     export_path = export_version(version)
   254     venv_remote_export_path = ""
   254     venv_remote_export_path = ""
   255     try:
   255     try:
   256         virtualenv_path = os.path.join(export_path, "virtualenv")
   256         virtualenv_path = os.path.join(export_path, "virtualenv")
   257     
   257 
   258         venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv")
   258         venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv")
   259         rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters)
   259         rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters)
   260         do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path)
   260         do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path)
   261     finally:
   261     finally:
   262         clean_export_folder(export_path)
   262         clean_export_folder(export_path)