sbin/sync/fabfile.py
author ymh <ymh.work@gmail.com>
Tue, 04 Dec 2012 00:17:10 +0100
changeset 23 7df43b86c425
parent 11 fc7cc9667de1
child 24 6b79d5424407
permissions -rw-r--r--
improve fabfile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     1
from fabric.api import task, env, sudo
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from fabric.colors import green
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     3
from fabric.tasks import Task
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     4
from ldt_fablib import (check_folder_access, syncdb, collectstatic, 
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     5
    export_version, do_sync_web, create_config, clean_export_folder, do_sync_comp, 
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     6
    sync_install_build, do_create_virtualenv, clean_rsync_folder, rsync_export, 
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     7
    get_comp_versions_dict)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import imp
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
     9
import os.path
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
@task
11
fc7cc9667de1 add syncdb to relaunch server
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    12
def relaunch_server(do_collectstatic=True, do_syncdb=True):
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    print("Relaunch server")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    check_folder_access()
11
fc7cc9667de1 add syncdb to relaunch server
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    15
    if do_syncdb:
fc7cc9667de1 add syncdb to relaunch server
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
    16
        syncdb(env.remote_path['src'], env.remote_path['virtualenv'])
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    if do_collectstatic:
7
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
    18
        collectstatic(env.remote_path['src'], env.remote_path['virtualenv'], env.platform_web_module)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    sudo(env.web_relaunch_cmd, shell=False)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
def sync_web(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    print(green("sync web with version %s" % version))
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
    24
    export_path = export_version(web=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
    export_path_full = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
    do_sync_web(version, export_path_full)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
    create_config(export_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
def update_lib(version, package):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    print(green("update ldt with version %s" % version))
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
    34
    export_path = export_version(web=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
    export_path_full = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
    lib_path = os.path.join(export_path_full, "virtualenv", "res", "lib")
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    f, pathname, description = imp.find_module("patch", [lib_path])
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    39
    imp.load_module("patch", f, pathname, description)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    f, pathname, description = imp.find_module("lib_create_env", [lib_path])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    lib_create_env = imp.load_module("lib_create_env", f, pathname, description)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    43
    package_path_full = os.path.join(export_path_full, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
    sync_install_build(package_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
@task
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    50
def sync_platform(version):
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    51
    print(green("sync platform with version web %s" % (version)))
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    52
    export_path = export_version(web=version)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    53
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
    export_path_web = os.path.join(export_path,'web')
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    55
    do_sync_web(version, export_path_web)    
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    56
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    57
    comp_versions = get_comp_versions_dict(export_path_web)  
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    58
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    59
    for key in [k for k in env.repos if key != 'web']:
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    60
        export_path_key = export_version(**{key: comp_versions[key]})
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    61
        export_path_comp = os.path.join(export_path_key, key)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    62
        do_sync_comp(key, export_path_comp)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    63
        clean_export_folder(export_path_key)    
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    64
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
    create_config(export_path_web)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
def create_virtualenv(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    print(green("create virtualenv with version %s" % version))
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
    72
    export_path = export_version(web=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
    export_path_web = os.path.join(export_path,'web')
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
    venv_remote_export_path = ""
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    try:
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    76
        virtualenv_path = os.path.join(export_path_web, "virtualenv")
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
        venv_remote_export_path = os.path.join(env.remote_path['venv_export'], env.export_prefix, version,"virtualenv")
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
        rsync_export(virtualenv_path, venv_remote_export_path, env.rsync_filters['venv'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
        do_create_virtualenv(venv_remote_export_path, env.remote_path['virtualenv'])
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        if venv_remote_export_path:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            clean_rsync_folder(venv_remote_export_path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    86
class SyncComp(Task):
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    87
    
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    88
    def __init__(self, key):
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    89
        self.key = key
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
23
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    91
    def __get_name(self):
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    92
        return "sync_" + self.key
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    93
    
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    94
    name = property(__get_name)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    95
    
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    96
    def run(self, version):
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    97
        print(green("sync ldt with version %s" % version))
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    98
        export_path_web = export_version(web=version)    
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
    99
        export_path_web_full = os.path.join(export_path_web,'web')
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   100
        comp_versions = get_comp_versions_dict(export_path_web_full)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   101
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   102
        export_path = export_version(**{self.key:comp_versions[self.key]})
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   103
        export_path_full = os.path.join(export_path,self.key)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   104
        do_sync_comp(self.key, export_path_full)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   105
        clean_export_folder(export_path)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   106
        clean_export_folder(export_path_web)
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   107
        
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   108
        relaunch_server()
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   109
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   110
for sync_name in [key for key in env.repos if key != 'web']:
7df43b86c425 improve fabfile
ymh <ymh.work@gmail.com>
parents: 11
diff changeset
   111
    globals()[sync_name] = SyncComp(sync_name)