sbin/sync/fabfile.py
changeset 3 800c48d0e3c1
parent 1 5778de052a1b
child 6 740d11ae9e28
equal deleted inserted replaced
2:fb621e89f9f1 3:800c48d0e3c1
    19 def clean_export_folder(path):
    19 def clean_export_folder(path):
    20     print("Removing %s" % path)
    20     print("Removing %s" % path)
    21     if os.path.isdir(path):
    21     if os.path.isdir(path):
    22         shutil.rmtree(path, ignore_errors=True)
    22         shutil.rmtree(path, ignore_errors=True)
    23 
    23 
    24 def do_export_version(path, version, *export_keys):
    24 def do_export_version(path, **export_keys):
    25     print("Export version %s with keys %s" % (str(version), repr(export_keys)))
    25     print("Export version %s " % (repr(export_keys)))
    26 
    26 
    27     for export_key in export_keys:
    27     for export_key, version in export_keys.items():
    28         export_path = os.path.join(path,export_key)
    28         export_path = os.path.join(path,export_key)
    29         
    29         
    30         repo_url = env.repos[export_key]
    30         repo_url = env.repos[export_key]
    31         url_part = urlparse.urlparse(repo_url)
    31         url_part = urlparse.urlparse(repo_url)
    32         if url_part.scheme or url_part.netloc:
    32         if url_part.scheme or url_part.netloc:
    38             clone_path = repo_url
    38             clone_path = repo_url
    39         
    39         
    40         with lcd(clone_path):
    40         with lcd(clone_path):
    41             local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path))
    41             local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path))
    42     
    42     
    43     print("Export version %s done"%str(version))
    43     print("Export version %s done"%repr(export_keys))
    44 
    44 
    45     
    45     
    46 def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
    46 def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
    47     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
    47     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
    48     res = ""
    48     res = ""
   118 def remove_build(path):
   118 def remove_build(path):
   119     print("remove build build %s" % path)
   119     print("remove build build %s" % path)
   120     run("rm \"%s\"" % path)
   120     run("rm \"%s\"" % path)
   121         
   121         
   122 
   122 
   123 def install_build(remotepath, remotevirtualenvpath):
   123 def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None):
   124     print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
   124     print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
   125     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   125     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   126     
   126     
   127     with prefix("source %s" % activate_path):
   127     with prefix("source %s" % activate_path):
       
   128         if module_to_uninstall:
       
   129             run("pip uninstall %s" % module_to_uninstall)
   128         run("pip install \"%s\"" % remotepath)
   130         run("pip install \"%s\"" % remotepath)
   129 
   131 
   130 def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
   132 def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
   131     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
   133     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
   132     remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT")
   134     remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT")
   166     }
   168     }
   167     
   169     
   168     if not exists(remote_config_path, verbose=True):
   170     if not exists(remote_config_path, verbose=True):
   169         upload_template(template_path, remote_config_path, context=context)
   171         upload_template(template_path, remote_config_path, context=context)
   170 
   172 
   171 def export_version(version, *args):
   173 def export_version(**kwargs):
   172     print("export version %s for %s" % (str(version), args))
   174     print("export version %s for %s" % (repr(kwargs)))
   173     
   175     
   174     export_path = get_export_path(version)
   176     export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()]))
   175     
   177     
   176     clean_export_folder(export_path)
   178     clean_export_folder(export_path)
   177     
   179     
   178     do_export_version(export_path,version, *args)
   180     do_export_version(export_path,**kwargs)
   179     
   181     
   180     return export_path
   182     return export_path
   181 
   183 
   182 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
   184 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
   183     print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
   185     print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
   201     ##
   203     ##
   202     src_path = export_path + "/src/ldt"
   204     src_path = export_path + "/src/ldt"
   203     build_src(src_path)
   205     build_src(src_path)
   204     (_,version_str) = get_src_version(src_path)
   206     (_,version_str) = get_src_version(src_path)
   205     build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
   207     build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
   206     sync_install_build(build_path)
   208     sync_install_build(build_path, 'ldt')
   207         
   209         
   208 
   210 
   209 def sync_install_build(build_path):
   211 def sync_install_build(build_path, module_to_uninstall=None):
   210     res_trans = None
   212     res_trans = None
   211     try:
   213     try:
   212         res_trans = sync_build(build_path)
   214         res_trans = sync_build(build_path)
   213         install_build(res_trans[0], env.remote_path['virtualenv'])        
   215         install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall)        
   214     finally:
   216     finally:
   215         if res_trans:
   217         if res_trans:
   216             remove_build(res_trans[0])
   218             remove_build(res_trans[0])
   217 
   219 
   218 
   220 
   246     sudo(env.web_relaunch_cmd, shell=False)
   248     sudo(env.web_relaunch_cmd, shell=False)
   247 
   249 
   248 @task
   250 @task
   249 def sync_web(version):
   251 def sync_web(version):
   250     print(green("sync web with version %s" % version))
   252     print(green("sync web with version %s" % version))
   251     export_path = export_version(version, 'web')
   253     export_path = export_version(web=version)
   252     export_path_full = os.path.join(export_path,'web')
   254     export_path_full = os.path.join(export_path,'web')
   253     do_sync_web(version, export_path_full)
   255     do_sync_web(version, export_path_full)
   254     create_config(export_path_full)
   256     create_config(export_path_full)
   255     clean_export_folder(export_path)
   257     clean_export_folder(export_path)
   256     relaunch_server()
   258     relaunch_server()
   257 
   259 
   258 @task
   260 @task
   259 def sync_ldt(version):
   261 def sync_ldt(version):
   260     print(green("sync ldt with version %s" % version))
   262     print(green("sync ldt with version %s" % version))
   261     export_path = export_version(version, 'ldt')
   263     export_path = export_version(ldt=version)
   262     export_path_full = os.path.join(export_path,'ldt')
   264     export_path_full = os.path.join(export_path,'ldt')
   263     do_sync_ldt(version, export_path_full)
   265     do_sync_ldt(version, export_path_full)
   264     clean_export_folder(export_path)
   266     clean_export_folder(export_path)
   265     relaunch_server()
   267     relaunch_server()
   266     
   268     
   267 @task
   269 @task
   268 def update_lib(version, package):
   270 def update_lib(version, package):
   269     print(green("update ldt with version %s" % version))
   271     print(green("update ldt with version %s" % version))
   270     export_path = export_version(version,'web')
   272     export_path = export_version(web=version)
   271     export_path_full = os.path.join(export_path,'web')
   273     export_path_full = os.path.join(export_path,'web')
   272     lib_path = os.path.join(export_path_full, "virtualenv", "res", "lib")
   274     lib_path = os.path.join(export_path_full, "virtualenv", "res", "lib")
   273     
   275     
   274     f, pathname, description = imp.find_module("patch", [lib_path])
   276     f, pathname, description = imp.find_module("patch", [lib_path])
   275     patch = imp.load_module("patch", f, pathname, description)
   277     patch = imp.load_module("patch", f, pathname, description)
   281     sync_install_build(package_path_full)
   283     sync_install_build(package_path_full)
   282     clean_export_folder(export_path)
   284     clean_export_folder(export_path)
   283     relaunch_server()
   285     relaunch_server()
   284     
   286     
   285 @task
   287 @task
   286 def sync_platform(version):
   288 def sync_platform(version_web, version_ldt):
   287     print(green("sync platform with version %s" % version))
   289     print(green("sync platform with version %s" % version))
   288     export_path = export_version(version, 'ldt', 'web')
   290     export_path = export_version(ldt=version_ldt, web=version_web)
   289     export_path_ldt = os.path.join(export_path,'ldt')
   291     export_path_ldt = os.path.join(export_path,'ldt')
   290     export_path_web = os.path.join(export_path,'web')
   292     export_path_web = os.path.join(export_path,'web')
   291     do_sync_ldt(version, export_path_ldt)
   293     do_sync_ldt(version, export_path_ldt)
   292     do_sync_web(version, export_path_web)
   294     do_sync_web(version, export_path_web)
   293     create_config(export_path_web)
   295     create_config(export_path_web)
   295     relaunch_server()
   297     relaunch_server()
   296 
   298 
   297 @task
   299 @task
   298 def create_virtualenv(version):
   300 def create_virtualenv(version):
   299     print(green("create virtualenv with version %s" % version))
   301     print(green("create virtualenv with version %s" % version))
   300     export_path = export_version(version, 'web')
   302     export_path = export_version(web=version)
   301     export_path_web = os.path.join(export_path,'web')
   303     export_path_web = os.path.join(export_path,'web')
   302     venv_remote_export_path = ""
   304     venv_remote_export_path = ""
   303     try:
   305     try:
   304         virtualenv_path = os.path.join(export_path_web, "virtualenv")
   306         virtualenv_path = os.path.join(export_path_web, "virtualenv")
   305     
   307