sbin/sync/fabfile.py
changeset 668 b52724db32ab
parent 642 b419e891b0bd
child 677 fe9cfbb91909
equal deleted inserted replaced
667:9e64be35704f 668:b52724db32ab
     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
       
     5 from fabric.context_managers import settings 
     4 from mercurial import commands, ui, hg, cmdutil
     6 from mercurial import commands, ui, hg, cmdutil
     5 import imp
     7 import imp
     6 import os, os.path
     8 import os, os.path
     7 import shutil
     9 import shutil
     8 import sys
    10 import sys
    97         run("pip install \"%s\"" % remotepath)
    99         run("pip install \"%s\"" % remotepath)
    98 
   100 
    99 def collectstatic(remotepath, remotevirtualenvpath):
   101 def collectstatic(remotepath, remotevirtualenvpath):
   100     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
   102     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
   101     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   103     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)):
   104     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
   103         run("python manage.py collectstatic --noinput")
   105         run("python manage.py collectstatic --noinput")
   104         
   106         
   105         
   107 def create_config(export_path):    
       
   108     print("Create config from %s" % (export_path,))
       
   109     remotepath = env.remote_web_path
       
   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")
       
   112     
       
   113     context = {
       
   114      'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
       
   115      'base_url': env.base_url,
       
   116      'web_url': env.web_url,
       
   117      'stream_url': env.stream_url,
       
   118      'stream_src_prefix': env.stream_src_prefix,
       
   119      'ffmpeg_path': env.ffmpeg_path,
       
   120      'db_engine': env.db_engine,
       
   121      'db_name': env.db_name,
       
   122      'db_user': env.db_user,
       
   123      'db_password': env.db_password,
       
   124      'db_host': env.db_host,
       
   125      'db_port': env.db_port,
       
   126      'log_file': env.log_file,
       
   127      'index_path': env.index_path,
       
   128      'google_analytics_code': env.google_analytics_code,
       
   129      'email_use_tls': env.email_use_tls,
       
   130      'email_host': env.email_host,
       
   131      'email_host_user': env.email_host_user,
       
   132      'email_host_user': env.email_host_user,
       
   133      'email_port': env.email_port,
       
   134     }
       
   135     
       
   136     if not exists(remote_config_path, verbose=True):
       
   137         upload_template(template_path, remote_config_path, context=context)
   106 
   138 
   107 def export_version(version):
   139 def export_version(version):
   108     print("export version %s" % str(version))
   140     print("export version %s" % str(version))
   109     
   141     
   110     export_path = get_export_path(version)
   142     export_path = get_export_path(version)
   132     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))
   133     src_path = export_path + "/src/ldt"
   165     src_path = export_path + "/src/ldt"
   134     build_src(src_path)
   166     build_src(src_path)
   135     (_,version_str) = get_src_version(src_path)
   167     (_,version_str) = get_src_version(src_path)
   136     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)
       
   170         
       
   171 
       
   172 def sync_install_build(build_path):
   137     res_trans = None
   173     res_trans = None
   138     try:
   174     try:
   139         res_trans = sync_build(build_path)
   175         res_trans = sync_build(build_path)
   140         install_build(res_trans[0], env.remote_virtualenv_path)        
   176         install_build(res_trans[0], env.remote_virtualenv_path)        
   141     finally:
   177     finally:
   142         if res_trans:
   178         if res_trans:
   143             remove_build(res_trans[0])
   179             remove_build(res_trans[0])
       
   180 
   144 
   181 
   145 def do_sync_web(version, export_path):
   182 def do_sync_web(version, export_path):
   146     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))
   147     web_path = os.path.join(export_path,"web/") 
   184     web_path = os.path.join(export_path,"web/") 
   148     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)    
   149 
   186 
   150 def relaunch_server():
   187 def relaunch_server():
   151     print("Relaunch server")
   188     print("Relaunch server")
   152     collectstatic(env.remote_web_path, env.remote_virtualenv_path)
   189     collectstatic(env.remote_web_path, env.remote_virtualenv_path)
   153     sudo(env.web_relaunch_cmd, shell=False)
   190     sudo(env.web_relaunch_cmd, shell=False)
       
   191     
       
   192 def check_folder_access():
       
   193     print("Check folder access")
       
   194     # get remote user
       
   195     for folder_path in env.folders:
       
   196         if not os.path.isabs(folder_path):
       
   197             folder_path = env.remote_web_path.rstrip("/")+ "/" + folder_path
       
   198             with settings(warn_only=True):
       
   199                 if not exists(folder_path):
       
   200                     run("mkdir -p \"%s\"" % folder_path)
       
   201                 run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path))
       
   202                 run("chown -R -c g+w \"%s\"" % folder_path)
   154 
   203 
   155 @task
   204 @task
   156 def sync_web(version):
   205 def sync_web(version):
   157     print(green("sync web with version %s" % version))
   206     print(green("sync web with version %s" % version))
   158     export_path = export_version(version)
   207     export_path = export_version(version)
   159     do_sync_web(version, export_path)    
   208     do_sync_web(version, export_path)
       
   209     check_folder_access()
       
   210     create_config(export_path)
   160     clean_export_folder(export_path)
   211     clean_export_folder(export_path)
   161     relaunch_server()
   212     relaunch_server()
   162 
   213 
   163 @task
   214 @task
   164 def sync_ldt(version):
   215 def sync_ldt(version):
   165     print(green("sync ldt with version %s" % version))
   216     print(green("sync ldt with version %s" % version))
   166     export_path = export_version(version)
   217     export_path = export_version(version)
   167     do_sync_ldt(version, export_path)
   218     do_sync_ldt(version, export_path)
   168     clean_export_folder(export_path)
   219     clean_export_folder(export_path)
   169     relaunch_server()    
   220     relaunch_server()
   170 
   221     
       
   222 @task
       
   223 def update_lib(version, package):
       
   224     print(green("update ldt with version %s" % version))
       
   225     export_path = export_version(version)
       
   226     lib_path = os.path.join(export_path, "virtualenv", "res", "lib")
       
   227     
       
   228     f, pathname, description = imp.find_module("patch", [lib_path])
       
   229     patch = imp.load_module("patch", f, pathname, description)
       
   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)
       
   232     
       
   233     package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
       
   234     
       
   235     sync_install_build(package_path)
       
   236     clean_export_folder(export_path)
       
   237     relaunch_server()
       
   238     
   171 @task
   239 @task
   172 def sync_platform(version):
   240 def sync_platform(version):
   173     print(green("sync platform with version %s" % version))
   241     print(green("sync platform with version %s" % version))
   174     export_path = export_version(version)
   242     export_path = export_version(version)
   175     do_sync_ldt(version, export_path)
   243     do_sync_ldt(version, export_path)
   176     do_sync_web(version, export_path)
   244     do_sync_web(version, export_path)
       
   245     check_folder_access()
       
   246     create_config(export_path)
   177     clean_export_folder(export_path)
   247     clean_export_folder(export_path)
   178     relaunch_server()
   248     relaunch_server()
   179 
   249 
   180 @task
   250 @task
   181 def create_virtualenv(version):
   251 def create_virtualenv(version):