sbin/sync/fabfile.py
changeset 7 fb450f78bfd1
parent 6 9e29d68d48f1
child 8 fd17983fd0bd
equal deleted inserted replaced
6:9e29d68d48f1 7:fb450f78bfd1
    20     if os.path.isdir(path):
    20     if os.path.isdir(path):
    21         shutil.rmtree(path, ignore_errors=True)
    21         shutil.rmtree(path, ignore_errors=True)
    22 
    22 
    23 def do_export_version(path, version):
    23 def do_export_version(path, version):
    24     print("Export version %s"%str(version))
    24     print("Export version %s"%str(version))
    25     
    25         
    26     #hgui = ui.ui() 
       
    27     #repo = hg.repository(hgui, cmdutil.findrepo(os.getcwd()))
       
    28     #commands.archive(hgui, repo, path, rev=str(version))
       
    29     
       
    30     local("hg archive -r \'%s\' \"%s\"" % (str(version),path))
    26     local("hg archive -r \'%s\' \"%s\"" % (str(version),path))
    31     print("Export version %s done"%str(version))
    27     print("Export version %s done"%str(version))
       
    28 
       
    29     
       
    30 def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key):
       
    31     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
       
    32     res = ""
       
    33     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
       
    34         tempfilepath = run("mktemp --tmpdir ldtplatform.XXXXXX")
       
    35         with settings(warn_only=True):
       
    36             run("echo \"import os\" > %s" % (tempfilepath))
       
    37             map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)),
       
    38                 ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module),
       
    39                  "from django.conf import settings",
       
    40                  "print settings.%s" % (settings_key)])
       
    41             res = run("python < %s" % (tempfilepath))
       
    42         run("rm -f \"%s\"" % (tempfilepath))
       
    43     return res
       
    44 
    32     
    45     
    33 def rsync_export(path, remotepath, filters):
    46 def rsync_export(path, remotepath, filters):
    34     print("Rsync %s to %s",(path,remotepath))
    47     print("Rsync %s to %s",(path,remotepath))
    35     
    48     
    36     if filters:
    49     if filters:
    44     
    57     
    45 def clean_rsync_folder(remotepath):
    58 def clean_rsync_folder(remotepath):
    46     print("clean rsync folder %s" % remotepath)
    59     print("clean rsync folder %s" % remotepath)
    47     run("rm -fr \"%s\"" % remotepath)
    60     run("rm -fr \"%s\"" % remotepath)
    48     
    61     
    49 def build_src(path):
       
    50     print("Build source dist at %s" % path)
       
    51     f = None
       
    52     try:
       
    53         f, pathname, description = imp.find_module("setup", [path])
       
    54         print(" 2 Build source dist at %s" % path)
       
    55         setup_mod = imp.load_module("setup", f, pathname, description)
       
    56         print(" 3 Build source dist at %s" % path)
       
    57     finally:
       
    58         if f:
       
    59             f.close()
       
    60         
       
    61     setup_mod.launch_setup("setup.py", ['sdist'])
       
    62     
       
    63     print("Build source dist at %s done" % path)
       
    64     
       
    65 
    62 
    66 def get_src_version(path):
    63 def collectstatic(remotepath, remotevirtualenvpath, platform_web_module):
    67     print("get src version at %s" % path)
       
    68     f = None
       
    69     try:
       
    70         f, pathname, description = imp.find_module("ldt", [path])
       
    71         ldt_mod = imp.load_module("ldt", f, pathname, description)
       
    72     finally:
       
    73         if f:
       
    74             f.close()
       
    75     version = ldt_mod.VERSION
       
    76     version_str = ldt_mod.get_version()
       
    77     
       
    78     return (version, version_str) 
       
    79     
       
    80 
       
    81 def sync_build(path):
       
    82     print("Sync build %s" % path)
       
    83     with cd(env.remote_ldt_base_path):
       
    84         filename = os.path.basename(path)
       
    85         res_trans = put(path, os.path.join(env.remote_ldt_base_path, filename))
       
    86         print("Sync build %s to %s" % (path,repr(res_trans)))
       
    87         return res_trans
       
    88 
       
    89 def remove_build(path):
       
    90     print("remove build build %s" % path)
       
    91     run("rm \"%s\"" % path)
       
    92         
       
    93 
       
    94 def install_build(remotepath, remotevirtualenvpath):
       
    95     print("Install build %s in %s" % (remotepath, remotevirtualenvpath))
       
    96     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
       
    97     
       
    98     with prefix("source %s" % activate_path):
       
    99         run("pip install \"%s\"" % remotepath)
       
   100 
       
   101 def collectstatic(remotepath, remotevirtualenvpath):
       
   102     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
    64     print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath))
       
    65     remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT")
   103     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
    66     activate_path = os.path.join(remotevirtualenvpath, "bin/activate")
   104     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
    67     with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath):
   105         run("python manage.py collectstatic -c --noinput")
    68         #remocve old files optio -c of collect static fail !
       
    69         run("rm -fr \"%s\"" % (remotestaticsitepath))
       
    70         run("python manage.py collectstatic --noinput")
   106         
    71         
   107 def create_config(export_path):    
    72 def create_config(export_path):    
   108     print("Create config from %s" % (export_path,))
    73     print("Create config from %s" % (export_path,))
   109     remotepath = env.remote_web_path
    74     remotepath = env.remote_web_path
   110     remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
    75     remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py")
   159     with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
   124     with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")):
   160         run("python create_python_env.py")
   125         run("python create_python_env.py")
   161         run("python project-boot.py \"%s\"" % remotevirtualenvpath)
   126         run("python project-boot.py \"%s\"" % remotevirtualenvpath)
   162     with prefix("source \"%s\"" % activate_path):
   127     with prefix("source \"%s\"" % activate_path):
   163         run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
   128         run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt"))
   164 
       
   165 def do_sync_ldt(version, export_path):
       
   166     print("do_sync_ldt with version %s and path %s" % (version,export_path))
       
   167     src_path = export_path + "/src/ldt"
       
   168     build_src(src_path)
       
   169     (_,version_str) = get_src_version(src_path)
       
   170     build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str)
       
   171     sync_install_build(build_path)
       
   172         
   129         
   173 
       
   174 def sync_install_build(build_path):
       
   175     res_trans = None
       
   176     try:
       
   177         res_trans = sync_build(build_path)
       
   178         install_build(res_trans[0], env.remote_virtualenv_path)        
       
   179     finally:
       
   180         if res_trans:
       
   181             remove_build(res_trans[0])
       
   182 
       
   183 
   130 
   184 def do_sync_web(version, export_path):
   131 def do_sync_web(version, export_path):
   185     print("do_sync_web with version %s and path %s" % (version,export_path))
   132     print("do_sync_web with version %s and path %s" % (version,export_path))
   186     web_path = os.path.join(export_path,"web/") 
   133     web_path = os.path.join(export_path,"web/") 
   187     rsync_export(web_path, env.remote_web_path, env.web_rsync_filters)    
   134     rsync_export(web_path, env.remote_web_path, env.web_rsync_filters)    
   195             with settings(warn_only=True):
   142             with settings(warn_only=True):
   196                 if not exists(folder_path):
   143                 if not exists(folder_path):
   197                     run("mkdir -p \"%s\"" % folder_path)
   144                     run("mkdir -p \"%s\"" % folder_path)
   198                 run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path))
   145                 run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path))
   199                 run("chmod -R -c g+w \"%s\"" % folder_path)
   146                 run("chmod -R -c g+w \"%s\"" % folder_path)
   200 
   147 @task
   201 def relaunch_server():
   148 def relaunch_server(do_collectstatic=True):
   202     print("Relaunch server")
   149     print("Relaunch server")
   203     collectstatic(env.remote_web_path, env.remote_virtualenv_path)
   150     if do_collectstatic:
       
   151         collectstatic(env.remote_web_path, env.remote_virtualenv_path, env.platform_web_module)
   204     check_folder_access()
   152     check_folder_access()
   205     sudo(env.web_relaunch_cmd, shell=False)
   153     sudo(env.web_relaunch_cmd, shell=False)
   206 
   154 
   207 @task
   155 @task
   208 def sync_web(version):
   156 def sync_web(version):
   209     print(green("sync web with version %s" % version))
   157     print(green("sync web with version %s" % version))
   210     export_path = export_version(version)
   158     export_path = export_version(version)
   211     do_sync_web(version, export_path)
   159     do_sync_web(version, export_path)
   212     create_config(export_path)
   160     create_config(export_path)
   213     clean_export_folder(export_path)
       
   214     relaunch_server()
       
   215 
       
   216 @task
       
   217 def sync_ldt(version):
       
   218     print(green("sync ldt with version %s" % version))
       
   219     export_path = export_version(version)
       
   220     do_sync_ldt(version, export_path)
       
   221     clean_export_folder(export_path)
   161     clean_export_folder(export_path)
   222     relaunch_server()
   162     relaunch_server()
   223     
   163     
   224 @task
   164 @task
   225 def update_lib(version, package):
   165 def update_lib(version, package):
   235     package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
   175     package_path = os.path.join(export_path, "virtualenv", "res", "src", lib_create_env.URLS[package]['local'])
   236     
   176     
   237     sync_install_build(package_path)
   177     sync_install_build(package_path)
   238     clean_export_folder(export_path)
   178     clean_export_folder(export_path)
   239     relaunch_server()
   179     relaunch_server()
   240     
   180 
   241 @task
       
   242 def sync_platform(version):
       
   243     print(green("sync platform with version %s" % version))
       
   244     export_path = export_version(version)
       
   245     do_sync_ldt(version, export_path)
       
   246     do_sync_web(version, export_path)
       
   247     create_config(export_path)
       
   248     clean_export_folder(export_path)
       
   249     relaunch_server()
       
   250 
   181 
   251 @task
   182 @task
   252 def create_virtualenv(version):
   183 def create_virtualenv(version):
   253     print(green("create virtualenv with version %s" % version))
   184     print(green("create virtualenv with version %s" % version))
   254     export_path = export_version(version)
   185     export_path = export_version(version)