sbin/sync/fabfile.py
author ymh <ymh.work@gmail.com>
Thu, 29 Nov 2012 15:57:49 +0100
changeset 1 5778de052a1b
parent 0 87104b7cb3d6
child 3 800c48d0e3c1
permissions -rw-r--r--
- update virtualenv - allow the use of runcserver - add a conf folder to hold web server config - update fabric
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
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
def do_export_version(path, version, *export_keys):
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    25
    print("Export version %s with keys %s" % (str(version), repr(export_keys)))
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    26
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
    for export_key in export_keys:
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
    
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    print("Export version %s done"%str(version))
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
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
def install_build(remotepath, remotevirtualenvpath):
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):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
        run("pip install \"%s\"" % remotepath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
    print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
    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
   133
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    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
   135
        #remocve old files optio -c of collect static fail !
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
        run("rm -fr \"%s\"" % (remotestaticsitepath))
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
        run("python manage.py collectstatic --noinput")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
        
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
def create_config(export_path):    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
    print("Create config from %s" % (export_path,))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
    remotepath = env.remote_path['web']
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
    template_path = os.path.join(export_path, "web", env.platform_web_module, "config.py.tmpl")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    context = {
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
     'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/",
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
     'base_url': env.base_url,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
     'web_url': env.web_url,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
     'stream_url': env.stream_url,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
     'stream_src_prefix': env.stream_src_prefix,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
     'ffmpeg_path': env.ffmpeg_path,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
     'db_engine': env.db_engine,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
     'db_name': env.db_name,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
     'db_user': env.db_user,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
     'db_password': env.db_password,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
     'db_host': env.db_host,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
     'db_port': env.db_port,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
     'log_file': env.log_file,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
     'google_analytics_code': env.google_analytics_code,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
     'email_use_tls': env.email_use_tls,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
     'email_host': env.email_host,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
     'email_host_user': env.email_host_user,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
     'email_host_user': env.email_host_user,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
     'email_port': env.email_port,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
     'forbidden_stream_url': env.forbidden_stream_url,
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
    }
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
    if not exists(remote_config_path, verbose=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        upload_template(template_path, remote_config_path, context=context)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   171
def export_version(version, *args):
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   172
    print("export version %s for %s" % (str(version), args))
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
    export_path = get_export_path(version)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
    clean_export_folder(export_path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   177
    
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   178
    do_export_version(export_path,version, *args)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
    return export_path
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
    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
   184
    activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    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
   186
        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
   187
    else:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
        prefix_str = "echo"
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
    with settings(warn_only=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
        run("rm -fr \"%s\"" % remotevirtualenvpath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
    run("mkdir -p \"%s\"" % remotevirtualenvpath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
    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
   193
        run("python create_python_env.py")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
        run("python project-boot.py \"%s\"" % remotevirtualenvpath)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    with prefix("source \"%s\"" % activate_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
        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
   197
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
def do_sync_ldt(version, export_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
    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
   200
    
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   201
    ##
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
    src_path = export_path + "/src/ldt"
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
    build_src(src_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
    (_,version_str) = get_src_version(src_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
    build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
    sync_install_build(build_path)
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
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
def sync_install_build(build_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
    res_trans = None
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
    try:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
        res_trans = sync_build(build_path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   213
        install_build(res_trans[0], env.remote_path['virtualenv'])        
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
        if res_trans:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
            remove_build(res_trans[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
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
def do_sync_web(version, export_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
    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
   221
    #sync web
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    web_path = os.path.join(export_path,"web/") 
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
    rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   224
    #sync src
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   225
    src_path = os.path.join(export_path,"src/") 
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   226
    rsync_export(src_path, env.remote_path['src'], env.rsync_filters['web'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   227
        
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
def check_folder_access():
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    print("Check folder access")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
    # get remote user
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
    for folder_path in env.folders:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        if not os.path.isabs(folder_path):
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   234
            folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
            with settings(warn_only=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
                if not exists(folder_path):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
                    run("mkdir -p \"%s\"" % folder_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
                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
   239
                run("chmod -R -c g+w \"%s\"" % folder_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
def relaunch_server(do_collectstatic=True):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    print("Relaunch server")
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    check_folder_access()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    if do_collectstatic:
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   245
        collectstatic(env.remote_path['web'], env.remote_path['virtualenv'], env.platform_web_module)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
    sudo(env.web_relaunch_cmd, shell=False)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
def sync_web(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
    print(green("sync web with version %s" % version))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   251
    export_path = export_version(version, 'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   252
    export_path_full = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   253
    do_sync_web(version, export_path_full)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   254
    create_config(export_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
def sync_ldt(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
    print(green("sync ldt with version %s" % version))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
    export_path = export_version(version, 'ldt')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
    export_path_full = os.path.join(export_path,'ldt')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
    do_sync_ldt(version, export_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
def update_lib(version, package):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
    print(green("update ldt with version %s" % version))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   270
    export_path = export_version(version,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   271
    export_path_full = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   272
    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
   273
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
    f, pathname, description = imp.find_module("patch", [lib_path])
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
    patch = imp.load_module("patch", f, pathname, description)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
    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
   277
    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
   278
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
    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
   280
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
    sync_install_build(package_path_full)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
    
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
def sync_platform(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
    print(green("sync platform with version %s" % version))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
    export_path = export_version(version, 'ldt', 'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   289
    export_path_ldt = os.path.join(export_path,'ldt')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   290
    export_path_web = os.path.join(export_path,'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   291
    do_sync_ldt(version, export_path_ldt)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   292
    do_sync_web(version, export_path_web)
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   293
    create_config(export_path_web)
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
    clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
    relaunch_server()
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
@task
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
def create_virtualenv(version):
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
    print(green("create virtualenv with version %s" % version))
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
    export_path = export_version(version, 'web')
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   301
    export_path_web = os.path.join(export_path,'web')
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
    venv_remote_export_path = ""
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
    try:
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   304
        virtualenv_path = os.path.join(export_path_web, "virtualenv")
0
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
    
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   306
        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
   307
        rsync_export(virtualenv_path, venv_remote_export_path, env.rsync_filters['venv'])
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   308
        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
   309
    finally:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
        clean_export_folder(export_path)
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
        if venv_remote_export_path:
87104b7cb3d6 first version of file organization
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
            clean_rsync_folder(venv_remote_export_path)
1
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   313
5778de052a1b - update virtualenv
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314