Add sync script files
authorymh <ymh.work@gmail.com>
Wed, 19 Sep 2012 10:31:06 +0900
changeset 3 1cb672cb9f9d
parent 2 06be5588329f
child 4 8e3ace50b235
Add sync script files
.hgignore
sbin/sync/config.py.tmpl
sbin/sync/fabfile.py
--- a/.hgignore	Wed Sep 19 01:58:34 2012 +0900
+++ b/.hgignore	Wed Sep 19 10:31:06 2012 +0900
@@ -2,5 +2,5 @@
 syntax: regexp
 ^virtualenv/.*/project-boot.py
 ^virtualenv/.*/env/.*$
-syntax: regexp
-^\.settings/org\.eclipse\.core\.resources\.prefs$
\ No newline at end of file
+^\.settings/org\.eclipse\.core\.resources\.prefs$
+^sbin/sync/config\.py$
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/sync/config.py.tmpl	Wed Sep 19 10:31:06 2012 +0900
@@ -0,0 +1,59 @@
+from fabric.api import env
+
+env.hosts = ['<user>@<server1>']
+
+env.web_group = '<web_user_group>'
+env.folders = ['log', 'static/media']
+
+env.base_export_path = "<local path used to export hg version>"
+env.export_prefix = "hyperplateau"
+env.remote_web_path = "<path of website root folder on the server>"
+env.platform_web_module = "hyperplateau"
+env.remote_virtualenv_path = "<path of the virtualenv on the server>"
+env.remote_venv_export_path = "<path for temporary export path of virtualenv resources>" 
+env.remote_baseline_venv = "<path of virtualenv baseline used for virtualenv creation>"
+env.web_rsync_filters = [
+    "+ core",
+    "P .htpasswd",
+    "P .htaccess",
+    "P hyperplateau/.htaccess",
+    "P hyperplateau/config.py",
+    "P hyperplateau/modwsgi.wsgi",
+    "P robots.txt",
+    "P env/***",
+    "P log/***",
+    "P index/***",
+    "P static/media/***",
+    "P crossdomain.xml",
+]
+env.venv_rsync_filters = [
+    "+ core",
+]
+env.web_relaunch_cmd = "supervisorctl restart hyperplateau"
+
+env.base_url = "/"
+env.web_url = "http://<base_url>"
+env.stream_url = "<base_streaming_url>" #optional
+
+env.stream_src_prefix = ""
+env.ffmpeg_path = "<path_to_ffmpeg>" #optional
+
+env.db_engine = '<database driver>' #cf django documentation
+env.db_name = '<database name>'
+env.db_user = '<database user>'
+env.db_password = ''
+env.db_host = '<database host>'
+env.db_port = <database port>
+
+env.log_file = env.remote_web_path + '/log/log.txt'
+
+env.google_analytics_code = ""
+
+env.email_use_tls = True
+env.email_host = ""
+env.email_host_user = ""
+env.email_host_user = ""
+env.email_port = ""
+
+env.forbidden_stream_url = "rtmp://media.iri.centrepompidou.fr/ddc_player/mp4:video/forbidden_stream.mp4?old_path="
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sbin/sync/fabfile.py	Wed Sep 19 10:31:06 2012 +0900
@@ -0,0 +1,196 @@
+from fabric.api import task, run, local, env, cd, put, prefix, sudo
+from fabric.colors import green
+from fabric.contrib.project import rsync_project
+from fabric.contrib.files import exists, upload_template
+from fabric.context_managers import settings 
+from mercurial import commands, ui, hg, cmdutil
+import imp
+import os, os.path
+import shutil
+import sys
+
+import config
+
+def get_export_path(version):
+    base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/")
+    return os.path.expanduser(base_path) + "_%s" % (str(version))
+
+def clean_export_folder(path):
+    print("Removing %s" % path)
+    if os.path.isdir(path):
+        shutil.rmtree(path, ignore_errors=True)
+
+def do_export_version(path, version):
+    print("Export version %s"%str(version))
+        
+    local("hg archive -r \'%s\' \"%s\"" % (str(version),path))
+    print("Export version %s done"%str(version))
+
+    
+def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
+    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
+    res = ""
+    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
+        tempfilepath = run("mktemp -t ldtplatform.XXXXXX")
+        with settings(warn_only=True):
+            run("echo \"import os\" > %s" % (tempfilepath))
+            map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)),
+                ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module),
+                 "from django.conf import settings",
+                 "print settings.%s" % (settings_key)])
+            res = run("python < %s" % (tempfilepath))
+        run("rm -f \"%s\"" % (tempfilepath))
+    return res
+
+    
+def rsync_export(path, remotepath, filters):
+    print("Rsync %s to %s",(path,remotepath))
+    
+    if filters:
+        filter_option_str = " ".join(["--filter \"%s\"" % (f) for f in filters])
+    else:
+        filter_option_str =""
+    
+    run("mkdir -p \"%s\"" % remotepath)
+    rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
+    print("Rsync %s to %s done",(path,remotepath))
+    
+def clean_rsync_folder(remotepath):
+    print("clean rsync folder %s" % remotepath)
+    run("rm -fr \"%s\"" % remotepath)
+    
+
+def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
+    print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
+    remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT")
+    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
+    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
+        #remocve old files optio -c of collect static fail !
+        run("rm -fr \"%s\"" % (remotestaticsitepath))
+        run("python manage.py collectstatic --noinput")
+        
+def create_config(export_path):    
+    print("Create config from %s" % (export_path,))
+    remotepath = env.remote_web_path
+    remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
+    template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl")
+    
+    context = {
+     'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
+     'base_url': env.base_url,
+     'web_url': env.web_url,
+     'stream_url': env.stream_url,
+     'stream_src_prefix': env.stream_src_prefix,
+     'ffmpeg_path': env.ffmpeg_path,
+     'db_engine': env.db_engine,
+     'db_name': env.db_name,
+     'db_user': env.db_user,
+     'db_password': env.db_password,
+     'db_host': env.db_host,
+     'db_port': env.db_port,
+     'log_file': env.log_file,
+     'google_analytics_code': env.google_analytics_code,
+     'email_use_tls': env.email_use_tls,
+     'email_host': env.email_host,
+     'email_host_user': env.email_host_user,
+     'email_host_user': env.email_host_user,
+     'email_port': env.email_port,
+     'forbidden_stream_url': env.forbidden_stream_url,
+    }
+    
+    if not exists(remote_config_path, verbose=True):
+        upload_template(template_path, remote_config_path, context=context)
+
+def export_version(version):
+    print("export version %s" % str(version))
+    
+    export_path = get_export_path(version)
+    
+    clean_export_folder(export_path)
+    do_export_version(export_path,version)
+    
+    return export_path
+
+def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
+    print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
+    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
+    if "remote_baseline_venv" in env and env.remote_baseline_venv:
+        prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate")
+    else:
+        prefix_str = "echo"
+    with settings(warn_only=True):
+        run("rm -fr \"%s\"" % remotevirtualenvpath)
+    run("mkdir -p \"%s\"" % remotevirtualenvpath)
+    with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
+        run("python create_python_env.py")
+        run("python project-boot.py \"%s\"" % remotevirtualenvpath)
+    with prefix("source \"%s\"" % activate_path):
+        run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
+        
+
+def do_sync_web(version, export_path):
+    print("do_sync_web with version %s and path %s" % (version,export_path))
+    web_path = os.path.join(export_path,"web/") 
+    rsync_export(web_path, env.remote_web_path, env.web_rsync_filters)    
+    
+def check_folder_access():
+    print("Check folder access")
+    # get remote user
+    for folder_path in env.folders:
+        if not os.path.isabs(folder_path):
+            folder_path = env.remote_web_path.rstrip("/")+ "/" + folder_path
+            with settings(warn_only=True):
+                if not exists(folder_path):
+                    run("mkdir -p \"%s\"" % folder_path)
+                run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path))
+                run("chmod -R -c g+w \"%s\"" % folder_path)
+@task
+def relaunch_server(do_collectstatic=True):
+    print("Relaunch server")
+    check_folder_access()
+    if do_collectstatic:
+        collectstatic(env.remote_web_path, env.remote_virtualenv_path, env.platform_web_module)
+    sudo(env.web_relaunch_cmd, shell=False)
+
+@task
+def sync_web(version):
+    print(green("sync web with version %s" % version))
+    export_path = export_version(version)
+    do_sync_web(version, export_path)
+    create_config(export_path)
+    clean_export_folder(export_path)
+    relaunch_server()
+    
+@task
+def update_lib(version, package):
+    print(green("update ldt with version %s" % version))
+    export_path = export_version(version)
+    lib_path = os.path.join(export_path, "virtualenv", "res", "lib")
+    
+    f, pathname, description = imp.find_module("patch", [lib_path])
+    patch = imp.load_module("patch", f, pathname, description)
+    f, pathname, description = imp.find_module("lib_create_env", [lib_path])
+    lib_create_env = imp.load_module("lib_create_env", f, pathname, description)
+    
+    package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
+    
+    sync_install_build(package_path)
+    clean_export_folder(export_path)
+    relaunch_server()
+
+
+@task
+def create_virtualenv(version):
+    print(green("create virtualenv with version %s" % version))
+    export_path = export_version(version)
+    venv_remote_export_path = ""
+    try:
+        virtualenv_path = os.path.join(export_path, "virtualenv")
+    
+        venv_remote_export_path = os.path.join(env.remote_venv_export_path, env.export_prefix, version,"virtualenv")
+        rsync_export(virtualenv_path, venv_remote_export_path, env.venv_rsync_filters)
+        do_create_virtualenv(venv_remote_export_path, env.remote_virtualenv_path)
+    finally:
+        clean_export_folder(export_path)
+        if venv_remote_export_path:
+            clean_rsync_folder(venv_remote_export_path)