sbin/sync/fabfile.py
author ymh <ymh.work@gmail.com>
Thu, 29 Nov 2012 22:34:27 +0100
changeset 7 fc1eea0039d8
parent 6 740d11ae9e28
child 9 4fb252f37428
permissions -rw-r--r--
small correction on fabric file
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     1
from fabric.api import task, run, local, env, cd, put, prefix, sudo, lcd
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from fabric.colors import green
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from fabric.contrib.project import rsync_project
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from fabric.contrib.files import exists, upload_template
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from fabric.context_managers import settings 
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from mercurial import commands, ui, hg, cmdutil
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
import imp
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
import os, os.path
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import shutil
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import sys
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
import urlparse
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
import config
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
def get_export_path(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    return os.path.expanduser(base_path) + "_%s" % (str(version))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
def clean_export_folder(path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    print("Removing %s" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    if os.path.isdir(path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        shutil.rmtree(path, ignore_errors=True)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
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
def do_export_version(path, **export_keys):
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
    25
    print("Export version %s " % (repr(export_keys)))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
    27
    for export_key, version in export_keys.items():
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    28
        export_path = os.path.join(path,export_key)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
        repo_url = env.repos[export_key]
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    31
        url_part = urlparse.urlparse(repo_url)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
        if url_part.scheme or url_part.netloc:
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    33
            # this is a remote repo. Let's clone first
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    34
            clone_path = os.path.join(path,'clone',export_keys)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
            os.makedirs(clone_path)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    36
            local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path))
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
        else:
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
            clone_path = repo_url
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
        
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
        with lcd(clone_path):
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
            local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path))
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
    
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
    43
    print("Export version %s done"%repr(export_keys))
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    res = ""
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        tempfilepath = run("mktemp -t ldtplatform.XXXXXX")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        with settings(warn_only=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            run("echo \"import os\" > %s" % (tempfilepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)),
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
                ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module),
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
                 "from django.conf import settings",
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
                 "print settings.%s" % (settings_key)])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            res = run("python < %s" % (tempfilepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        run("rm -f \"%s\"" % (tempfilepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    return res
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
def rsync_export(path, remotepath, filters):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    print("Rsync %s to %s",(path,remotepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    filter_option_str = "--progress --stats"
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    if filters:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    run("mkdir -p \"%s\"" % remotepath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
    print("Rsync %s to %s done",(path,remotepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
def clean_rsync_folder(remotepath):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
    print("clean rsync folder %s" % remotepath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
    run("rm -fr \"%s\"" % remotepath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
def build_src(path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
    print("Build source dist at %s" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
    f = None
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    try:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        f, pathname, description = imp.find_module("setup", [path])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        print(" 2 Build source dist at %s" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
        setup_mod = imp.load_module("setup", f, pathname, description)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        print(" 3 Build source dist at %s" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        if f:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
            f.close()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    setup_mod.launch_setup("setup.py", ['sdist'])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
    print("Build source dist at %s done" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
def get_src_version(path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    print("get src version at %s" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    f = None
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
    try:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        f, pathname, description = imp.find_module("ldt", [path])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        ldt_mod = imp.load_module("ldt", f, pathname, description)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        if f:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
            f.close()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
    version = ldt_mod.VERSION
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
    version_str = ldt_mod.get_version()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    return (version, version_str) 
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
def sync_build(path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    print("Sync build %s" % path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   112
    with cd(env.remote_path['ldt_base']):
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        filename = os.path.basename(path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   114
        res_trans = put(path, os.path.join(env.remote_path['ldt_base'], filename))
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        print("Sync build %s to %s" % (path,repr(res_trans)))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        return res_trans
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
def remove_build(path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
    print("remove build build %s" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
    run("rm \"%s\"" % path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
        
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   123
def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None):
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
    with prefix("source %s" % activate_path):
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   128
        if module_to_uninstall:
7
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   129
            with settings(warn_only=True):
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   130
                run("pip uninstall -y %s" % module_to_uninstall)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
        run("pip install \"%s\"" % remotepath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
    with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        #remocve old files optio -c of collect static fail !
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
        run("rm -fr \"%s\"" % (remotestaticsitepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
        run("python manage.py collectstatic --noinput")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
        
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
def create_config(export_path):    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    print("Create config from %s" % (export_path,))
7
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   144
    remotepath = env.remote_path['src']
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
7
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   146
    template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl")
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    context = {
7
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   149
        'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   150
        'asctime': '%(asctime)s',
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   151
        'levelname': '%(levelname)s',
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   152
        'message': '%(message)s',
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   153
        'module': '%(module)s',
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
    }
6
740d11ae9e28 correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   155
    context.update(env.config['web'])
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
    if not exists(remote_config_path, verbose=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
        upload_template(template_path, remote_config_path, context=context)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   160
def export_version(**kwargs):
6
740d11ae9e28 correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   161
    print("export version %s" % (repr(kwargs)))
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
    
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   163
    export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()]))
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
    clean_export_folder(export_path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   166
    
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   167
    do_export_version(export_path,**kwargs)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
    return export_path
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
    print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
    if "remote_baseline_venv" in env and env.remote_baseline_venv:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    else:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
        prefix_str = "echo"
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    with settings(warn_only=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        run("rm -fr \"%s\"" % remotevirtualenvpath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
    run("mkdir -p \"%s\"" % remotevirtualenvpath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
    with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
        run("python create_python_env.py")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
        run("python project-boot.py \"%s\"" % remotevirtualenvpath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
    with prefix("source \"%s\"" % activate_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
        run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
def do_sync_ldt(version, export_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
    print("do_sync_ldt with version %s and path %s" % (version,export_path))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   189
    
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   190
    ##
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    src_path = export_path + "/src/ldt"
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
    build_src(src_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
    (_,version_str) = get_src_version(src_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
    build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   195
    sync_install_build(build_path, 'ldt')
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
        
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   198
def sync_install_build(build_path, module_to_uninstall=None):
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    res_trans = None
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
    try:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
        res_trans = sync_build(build_path)
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   202
        install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall)        
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        if res_trans:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
            remove_build(res_trans[0])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
def do_sync_web(version, export_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
    print("do_sync_web with version %s and path %s" % (version,export_path))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   210
    #sync web
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    web_path = os.path.join(export_path,"web/") 
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   212
    rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
    #sync src
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   214
    src_path = os.path.join(export_path,"src/") 
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   215
    rsync_export(src_path, env.remote_path['src'], env.rsync_filters['web'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   216
        
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
def check_folder_access():
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
    print("Check folder access")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    # get remote user
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
    for folder_path in env.folders:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
        if not os.path.isabs(folder_path):
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
            folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
            with settings(warn_only=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
                if not exists(folder_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
                    run("mkdir -p \"%s\"" % folder_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
                run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
                run("chmod -R -c g+w \"%s\"" % folder_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
def relaunch_server(do_collectstatic=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    print("Relaunch server")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
    check_folder_access()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
    if do_collectstatic:
7
fc1eea0039d8 small correction on fabric file
ymh <ymh.work@gmail.com>
parents: 6
diff changeset
   234
        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
   235
    sudo(env.web_relaunch_cmd, shell=False)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
def sync_web(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
    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
   240
    export_path = export_version(web=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   241
    export_path_full = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   242
    do_sync_web(version, export_path_full)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   243
    create_config(export_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
def sync_ldt(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
    print(green("sync 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
   250
    export_path = export_version(ldt=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
    export_path_full = os.path.join(export_path,'ldt')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
    do_sync_ldt(version, export_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
def update_lib(version, package):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
    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
   259
    export_path = export_version(web=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   260
    export_path_full = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
    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
   262
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
    f, pathname, description = imp.find_module("patch", [lib_path])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
    patch = imp.load_module("patch", f, pathname, description)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
    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
   266
    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
   267
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
    package_path = 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
   269
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
    sync_install_build(package_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
@task
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   275
def sync_platform(version_web, version_ldt):
6
740d11ae9e28 correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   276
    print(green("sync platform with version web %s, ldt %s" % (version_web, version_ldt)))
3
800c48d0e3c1 Correct fab file for being able to pass 2 versions : ldt and web
ymh <ymh.work@gmail.com>
parents: 1
diff changeset
   277
    export_path = export_version(ldt=version_ldt, web=version_web)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   278
    export_path_ldt = os.path.join(export_path,'ldt')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
    export_path_web = os.path.join(export_path,'web')
6
740d11ae9e28 correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   280
    do_sync_ldt(version_ldt, export_path_ldt)
740d11ae9e28 correct fabric + virtualenv creation (remove LDT)
ymh <ymh.work@gmail.com>
parents: 3
diff changeset
   281
    do_sync_web(version_web, export_path_web)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   282
    create_config(export_path_web)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
def create_virtualenv(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
    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
   289
    export_path = export_version(web=version)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
    export_path_web = os.path.join(export_path,'web')
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
    venv_remote_export_path = ""
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
    try:
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
        virtualenv_path = os.path.join(export_path_web, "virtualenv")
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   295
        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
   296
        rsync_export(virtualenv_path, venv_remote_export_path, env.rsync_filters['venv'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   297
        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
   298
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
        if venv_remote_export_path:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
            clean_rsync_folder(venv_remote_export_path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   302
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   303