sbin/sync/fabfile.py
changeset 3 1cb672cb9f9d
child 4 8e3ace50b235
equal deleted inserted replaced
2:06be5588329f 3:1cb672cb9f9d
       
     1 from fabric.api import task, run, local, env, cd, put, prefix, sudo
       
     2 from fabric.colors import green
       
     3 from fabric.contrib.project import rsync_project
       
     4 from fabric.contrib.files import exists, upload_template
       
     5 from fabric.context_managers import settings 
       
     6 from mercurial import commands, ui, hg, cmdutil
       
     7 import imp
       
     8 import os, os.path
       
     9 import shutil
       
    10 import sys
       
    11 
       
    12 import config
       
    13 
       
    14 def get_export_path(version):
       
    15     base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/")
       
    16     return os.path.expanduser(base_path) + "_%s" % (str(version))
       
    17 
       
    18 def clean_export_folder(path):
       
    19     print("Removing %s" % path)
       
    20     if os.path.isdir(path):
       
    21         shutil.rmtree(path, ignore_errors=True)
       
    22 
       
    23 def do_export_version(path, version):
       
    24     print("Export version %s"%str(version))
       
    25         
       
    26     local("hg archive -r \'%s\' \"%s\"" % (str(version),path))
       
    27     print("Export version %s done"%str(version))
       
    28 
       
    29     
       
    30 def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
       
    31     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
       
    32     res = ""
       
    33     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
       
    34         tempfilepath = run("mktemp -t ldtplatform.XXXXXX")
       
    35         with settings(warn_only=True):
       
    36             run("echo \"import os\" > %s" % (tempfilepath))
       
    37             map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)),
       
    38                 ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module),
       
    39                  "from django.conf import settings",
       
    40                  "print settings.%s" % (settings_key)])
       
    41             res = run("python < %s" % (tempfilepath))
       
    42         run("rm -f \"%s\"" % (tempfilepath))
       
    43     return res
       
    44 
       
    45     
       
    46 def rsync_export(path, remotepath, filters):
       
    47     print("Rsync %s to %s",(path,remotepath))
       
    48     
       
    49     if filters:
       
    50         filter_option_str = " ".join(["--filter \"%s\"" % (f) for f in filters])
       
    51     else:
       
    52         filter_option_str =""
       
    53     
       
    54     run("mkdir -p \"%s\"" % remotepath)
       
    55     rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
       
    56     print("Rsync %s to %s done",(path,remotepath))
       
    57     
       
    58 def clean_rsync_folder(remotepath):
       
    59     print("clean rsync folder %s" % remotepath)
       
    60     run("rm -fr \"%s\"" % remotepath)
       
    61     
       
    62 
       
    63 def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
       
    64     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
       
    65     remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT")
       
    66     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
       
    67     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
       
    68         #remocve old files optio -c of collect static fail !
       
    69         run("rm -fr \"%s\"" % (remotestaticsitepath))
       
    70         run("python manage.py collectstatic --noinput")
       
    71         
       
    72 def create_config(export_path):    
       
    73     print("Create config from %s" % (export_path,))
       
    74     remotepath = env.remote_web_path
       
    75     remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
       
    76     template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl")
       
    77     
       
    78     context = {
       
    79      'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
       
    80      'base_url': env.base_url,
       
    81      'web_url': env.web_url,
       
    82      'stream_url': env.stream_url,
       
    83      'stream_src_prefix': env.stream_src_prefix,
       
    84      'ffmpeg_path': env.ffmpeg_path,
       
    85      'db_engine': env.db_engine,
       
    86      'db_name': env.db_name,
       
    87      'db_user': env.db_user,
       
    88      'db_password': env.db_password,
       
    89      'db_host': env.db_host,
       
    90      'db_port': env.db_port,
       
    91      'log_file': env.log_file,
       
    92      'google_analytics_code': env.google_analytics_code,
       
    93      'email_use_tls': env.email_use_tls,
       
    94      'email_host': env.email_host,
       
    95      'email_host_user': env.email_host_user,
       
    96      'email_host_user': env.email_host_user,
       
    97      'email_port': env.email_port,
       
    98      'forbidden_stream_url': env.forbidden_stream_url,
       
    99     }
       
   100     
       
   101     if not exists(remote_config_path, verbose=True):
       
   102         upload_template(template_path, remote_config_path, context=context)
       
   103 
       
   104 def export_version(version):
       
   105     print("export version %s" % str(version))
       
   106     
       
   107     export_path = get_export_path(version)
       
   108     
       
   109     clean_export_folder(export_path)
       
   110     do_export_version(export_path,version)
       
   111     
       
   112     return export_path
       
   113 
       
   114 def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
       
   115     print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
       
   116     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
       
   117     if "remote_baseline_venv" in env and env.remote_baseline_venv:
       
   118         prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate")
       
   119     else:
       
   120         prefix_str = "echo"
       
   121     with settings(warn_only=True):
       
   122         run("rm -fr \"%s\"" % remotevirtualenvpath)
       
   123     run("mkdir -p \"%s\"" % remotevirtualenvpath)
       
   124     with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
       
   125         run("python create_python_env.py")
       
   126         run("python project-boot.py \"%s\"" % remotevirtualenvpath)
       
   127     with prefix("source \"%s\"" % activate_path):
       
   128         run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
       
   129         
       
   130 
       
   131 def do_sync_web(version, export_path):
       
   132     print("do_sync_web with version %s and path %s" % (version,export_path))
       
   133     web_path = os.path.join(export_path,"web/") 
       
   134     rsync_export(web_path, env.remote_web_path, env.web_rsync_filters)    
       
   135     
       
   136 def check_folder_access():
       
   137     print("Check folder access")
       
   138     # get remote user
       
   139     for folder_path in env.folders:
       
   140         if not os.path.isabs(folder_path):
       
   141             folder_path = env.remote_web_path.rstrip("/")+ "/" + folder_path
       
   142             with settings(warn_only=True):
       
   143                 if not exists(folder_path):
       
   144                     run("mkdir -p \"%s\"" % folder_path)
       
   145                 run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path))
       
   146                 run("chmod -R -c g+w \"%s\"" % folder_path)
       
   147 @task
       
   148 def relaunch_server(do_collectstatic=True):
       
   149     print("Relaunch server")
       
   150     check_folder_access()
       
   151     if do_collectstatic:
       
   152         collectstatic(env.remote_web_path, env.remote_virtualenv_path, env.platform_web_module)
       
   153     sudo(env.web_relaunch_cmd, shell=False)
       
   154 
       
   155 @task
       
   156 def sync_web(version):
       
   157     print(green("sync web with version %s" % version))
       
   158     export_path = export_version(version)
       
   159     do_sync_web(version, export_path)
       
   160     create_config(export_path)
       
   161     clean_export_folder(export_path)
       
   162     relaunch_server()
       
   163     
       
   164 @task
       
   165 def update_lib(version, package):
       
   166     print(green("update ldt with version %s" % version))
       
   167     export_path = export_version(version)
       
   168     lib_path = os.path.join(export_path, "virtualenv", "res", "lib")
       
   169     
       
   170     f, pathname, description = imp.find_module("patch", [lib_path])
       
   171     patch = imp.load_module("patch", f, pathname, description)
       
   172     f, pathname, description = imp.find_module("lib_create_env", [lib_path])
       
   173     lib_create_env = imp.load_module("lib_create_env", f, pathname, description)
       
   174     
       
   175     package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
       
   176     
       
   177     sync_install_build(package_path)
       
   178     clean_export_folder(export_path)
       
   179     relaunch_server()
       
   180 
       
   181 
       
   182 @task
       
   183 def create_virtualenv(version):
       
   184     print(green("create virtualenv with version %s" % version))
       
   185     export_path = export_version(version)
       
   186     venv_remote_export_path = ""
       
   187     try:
       
   188         virtualenv_path = os.path.join(export_path, "virtualenv")
       
   189     
       
   190         venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv")
       
   191         rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters)
       
   192         do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path)
       
   193     finally:
       
   194         clean_export_folder(export_path)
       
   195         if venv_remote_export_path:
       
   196             clean_rsync_folder(venv_remote_export_path)