sbin/sync/fabfile.py
author ymh <ymh.work@gmail.com>
Wed, 31 Aug 2016 17:37:28 +0200
changeset 159 a2e6d314da2f
child 162 f014c1593ade
permissions -rw-r--r--
add sync scripts
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
159
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import imp
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import os.path
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import StringIO
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
import config  # @UnusedImport
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from fablib import (export_version, do_sync_web, create_config,
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
                    clean_export_folder, do_sync_comp, sync_install_build, do_create_virtualenv,
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
                    clean_rsync_folder, rsync_export, get_src_version, sync_build,
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
                    do_relaunch_server, install_build, do_create_virtualenv_requirement)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from fabric.api import task, env
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from fabric.colors import green
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
def build_source(version):
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    print(green("build source with version %s" % version))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    export_path = export_version(iconolab=version)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    export_path_full = os.path.join(export_path,'src')
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    build_src(export_path_full)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    (_,version_str) = get_src_version(env.key, src_path)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    return os.path.join(src_path,"dist","%s-%s.tar.gz" % (env.key,version_str))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
@task
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
def relaunch_server(do_collectstatic=True, do_migrate=True, do_check_folder_access=False):
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    print("Relaunch server")
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    do_relaunch_server(do_collectstatic, do_migrate, env.get('check_folder_access',do_check_folder_access))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
@task
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
def sync_site(version):
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    print(green("sync site with version %s" % version))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    build_path = build_source(version)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    res_trans = sync_build(build_path)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    # untar build
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    run('tar zxf %s' % res_trans[0])
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    # rebuild virtualenv
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    requirements_path = os.path.join(res_trans[0][0:-7], env.repos[env.key]['requirements'])
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    do_create_virtualenv_requirement(requirements_path, env.remote_path['virtualenv'], env.repos[env.key]['python_version'])
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    # add setting path to virtualenv
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    ext_path = "import sys; sys.__plen = len(sys.path)\n"
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    for l in env.remote_path.get('pythonpath', []):
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        ext_path += l + "\n"
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    ext_path += "import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)"
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    put(StringIO.StringIO(ext_path), os.path.join(env.remote_path['virtualenv'], 'lib/python%s/site-packages/_virtualenv_path_extensions.pth'%env.repos[env.key]['python_version']))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    # install build
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    install_build(res_trans[0], env.remote_path['virtualenv'])
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    # remove build + untared folder
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    run('rm -fr "%s" "%s" ' % (res_trans[0], res_trans[0][0:-7]))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    clean_export_folder(export_path)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    relaunch_server()
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
@task
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
def create_virtualenv(version):
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
    print(green("create virtualenv with version %s" % version))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    export_path = export_version(web=version)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    export_path_web = os.path.join(export_path,'web')
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
    venv_remote_export_path = ""
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    try:
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        virtualenv_path = os.path.join(export_path_web, "virtualenv")
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        venv_remote_export_path = os.path.join(env.remote_path['venv_export'], env.export_prefix, version,"virtualenv")
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        rsync_export(virtualenv_path, venv_remote_export_path, env.rsync_filters['venv'])
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        do_create_virtualenv(venv_remote_export_path, env.remote_path['virtualenv'])
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    finally:
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        clean_export_folder(export_path)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        if venv_remote_export_path:
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            clean_rsync_folder(venv_remote_export_path)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
if len(env.repos) > 1:
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    @task
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    def sync_platform(version):
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        print(green("sync platform with version web %s" % (version)))
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        export_path = export_version(web=version)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        export_path_web = os.path.join(export_path,'web')
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        do_sync_web(version, export_path_web)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        comp_versions = get_comp_versions_dict(export_path_web)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        for key in [k for k in env.repos if k != 'web']:
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            export_path_key = export_version(**{key: comp_versions[key]})
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            export_path_comp = os.path.join(export_path_key, key)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            do_sync_comp(key, export_path_comp)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            clean_export_folder(export_path_key)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        create_config(export_path_web)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        clean_export_folder(export_path)
a2e6d314da2f add sync scripts
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        relaunch_server()