# HG changeset patch # User ymh # Date 1354576630 -3600 # Node ID 7df43b86c425dd4ef4dcde75cbb0d3fdccba23de # Parent cdb1030d63a8febcac4a10a40ef829a2a5a77ea5 improve fabfile diff -r cdb1030d63a8 -r 7df43b86c425 .hgignore --- a/.hgignore Fri Nov 30 14:02:30 2012 +0100 +++ b/.hgignore Tue Dec 04 00:17:10 2012 +0100 @@ -26,4 +26,6 @@ syntax: regexp ^\.pydevproject$ syntax: regexp -^web/static/site$ \ No newline at end of file +^web/static/site$ +syntax: regexp +^\.pydevproject$ \ No newline at end of file diff -r cdb1030d63a8 -r 7df43b86c425 .project --- a/.project Fri Nov 30 14:02:30 2012 +0100 +++ b/.project Tue Dec 04 00:17:10 2012 +0100 @@ -3,6 +3,7 @@ platform_web + platform diff -r cdb1030d63a8 -r 7df43b86c425 .pydevproject --- a/.pydevproject Fri Nov 30 14:02:30 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,13 +0,0 @@ - - - - -Default -python 2.7 - -/platform_web/src - - -/Users/tc/dev/eclipse_workspace/platform/src/ldt - - diff -r cdb1030d63a8 -r 7df43b86c425 sbin/sync/fabfile.py --- a/sbin/sync/fabfile.py Fri Nov 30 14:02:30 2012 +0100 +++ b/sbin/sync/fabfile.py Tue Dec 04 00:17:10 2012 +0100 @@ -1,236 +1,13 @@ -from fabric.api import task, run, local, env, cd, put, prefix, sudo, lcd +from fabric.api import task, env, sudo from fabric.colors import green -from fabric.contrib.project import rsync_project -from fabric.contrib.files import exists, upload_template -from fabric.context_managers import settings -from mercurial import commands, ui, hg, cmdutil +from fabric.tasks import Task +from ldt_fablib import (check_folder_access, syncdb, collectstatic, + export_version, do_sync_web, create_config, clean_export_folder, do_sync_comp, + sync_install_build, do_create_virtualenv, clean_rsync_folder, rsync_export, + get_comp_versions_dict) import imp -import os, os.path -import shutil -import sys -import urlparse - -import config - -def get_export_path(version): - base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/") - return os.path.expanduser(base_path) + "_%s" % (str(version)) - -def clean_export_folder(path): - print("Removing %s" % path) - if os.path.isdir(path): - shutil.rmtree(path, ignore_errors=True) - -def do_export_version(path, **export_keys): - print("Export version %s " % (repr(export_keys))) - - for export_key, version in export_keys.items(): - export_path = os.path.join(path,export_key) - - repo_url = env.repos[export_key] - url_part = urlparse.urlparse(repo_url) - if url_part.scheme or url_part.netloc: - # this is a remote repo. Let's clone first - clone_path = os.path.join(path,'clone',export_keys) - os.makedirs(clone_path) - local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path)) - else: - clone_path = repo_url - - with lcd(clone_path): - local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path)) - - print("Export version %s done"%repr(export_keys)) - - -def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key): - activate_path = os.path.join(remotevirtualenvpath, "bin/activate") - res = "" - with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): - tempfilepath = run("mktemp -t ldtplatform.XXXXXX") - with settings(warn_only=True): - run("echo \"import os\" > %s" % (tempfilepath)) - map(lambda str: run("echo \"%s\" >> %s" % (str, tempfilepath)), - ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module), - "from django.conf import settings", - "print settings.%s" % (settings_key)]) - res = run("python < %s" % (tempfilepath)) - run("rm -f \"%s\"" % (tempfilepath)) - return res - - - -def rsync_export(path, remotepath, filters): - print("Rsync %s to %s",(path,remotepath)) - - filter_option_str = "--progress --stats" - if filters: - filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters]) - - run("mkdir -p \"%s\"" % remotepath) - rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) - print("Rsync %s to %s done",(path,remotepath)) - -def clean_rsync_folder(remotepath): - print("clean rsync folder %s" % remotepath) - run("rm -fr \"%s\"" % remotepath) - -def build_src(path): - print("Build source dist at %s" % path) - f = None - try: - f, pathname, description = imp.find_module("setup", [path]) - print(" 2 Build source dist at %s" % path) - setup_mod = imp.load_module("setup", f, pathname, description) - print(" 3 Build source dist at %s" % path) - finally: - if f: - f.close() - - setup_mod.launch_setup("setup.py", ['sdist']) - - print("Build source dist at %s done" % path) - - -def get_src_version(path): - print("get src version at %s" % path) - f = None - try: - f, pathname, description = imp.find_module("ldt", [path]) - ldt_mod = imp.load_module("ldt", f, pathname, description) - finally: - if f: - f.close() - version = ldt_mod.VERSION - version_str = ldt_mod.get_version() - - return (version, version_str) - - -def sync_build(path): - print("Sync build %s" % path) - with cd(env.remote_path['ldt_base']): - filename = os.path.basename(path) - res_trans = put(path, os.path.join(env.remote_path['ldt_base'], filename)) - print("Sync build %s to %s" % (path,repr(res_trans))) - return res_trans +import os.path -def remove_build(path): - print("remove build build %s" % path) - run("rm \"%s\"" % path) - - -def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None): - print("Install build %s in %s" % (remotepath, remotevirtualenvpath)) - activate_path = os.path.join(remotevirtualenvpath, "bin/activate") - - with prefix("source %s" % activate_path): - if module_to_uninstall: - with settings(warn_only=True): - run("pip uninstall -y %s" % module_to_uninstall) - run("pip install \"%s\"" % remotepath) - -def collectstatic(remotepath, remotevirtualenvpath, platform_web_module): - print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) - remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT") - activate_path = os.path.join(remotevirtualenvpath, "bin/activate") - with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): - #remocve old files optio -c of collect static fail ! - run("rm -fr \"%s\"" % (remotestaticsitepath)) - run("python manage.py collectstatic --noinput") - -def syncdb(remotepath, remotevirtualenvpath): - activate_path = os.path.join(remotevirtualenvpath, "bin/activate") - with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): - run("python manage.py syncdb --migrate --noinput") - -def create_config(export_path): - print("Create config from %s" % (export_path,)) - remotepath = env.remote_path['src'] - remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py") - template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl") - - context = { - 'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/", - 'asctime': '%(asctime)s', - 'levelname': '%(levelname)s', - 'message': '%(message)s', - 'module': '%(module)s', - } - context.update(env.config['web']) - - if not exists(remote_config_path, verbose=True): - upload_template(template_path, remote_config_path, context=context) - -def export_version(**kwargs): - print("export version %s" % (repr(kwargs))) - - export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()])) - - clean_export_folder(export_path) - - do_export_version(export_path,**kwargs) - - return export_path - -def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): - print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) - activate_path = os.path.join(remotevirtualenvpath, "bin/activate") - if "remote_baseline_venv" in env and env.remote_baseline_venv: - prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") - else: - prefix_str = "echo" - with settings(warn_only=True): - run("rm -fr \"%s\"" % remotevirtualenvpath) - run("mkdir -p \"%s\"" % remotevirtualenvpath) - with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): - run("python create_python_env.py") - run("python project-boot.py \"%s\"" % remotevirtualenvpath) - with prefix("source \"%s\"" % activate_path): - run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) - -def do_sync_ldt(version, export_path): - print("do_sync_ldt with version %s and path %s" % (version,export_path)) - - ## - src_path = export_path + "/src/ldt" - build_src(src_path) - (_,version_str) = get_src_version(src_path) - build_path = os.path.join(src_path,"dist","ldt-%s.tar.gz" % version_str) - sync_install_build(build_path, 'ldt') - - -def sync_install_build(build_path, module_to_uninstall=None): - res_trans = None - try: - res_trans = sync_build(build_path) - install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall) - finally: - if res_trans: - remove_build(res_trans[0]) - - -def do_sync_web(version, export_path): - print("do_sync_web with version %s and path %s" % (version,export_path)) - #sync web - web_path = os.path.join(export_path,"web/") - rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web']) - #sync src - src_path = os.path.join(export_path,"src/") - rsync_export(src_path, env.remote_path['src'], env.rsync_filters['src']) - - -def check_folder_access(): - print("Check folder access") - # get remote user - for folder_path in env.folders: - if not os.path.isabs(folder_path): - folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path - with settings(warn_only=True): - if not exists(folder_path): - run("mkdir -p \"%s\"" % folder_path) - run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) - run("chmod -R -c g+w \"%s\"" % folder_path) @task def relaunch_server(do_collectstatic=True, do_syncdb=True): print("Relaunch server") @@ -250,15 +27,6 @@ create_config(export_path_full) clean_export_folder(export_path) relaunch_server() - -@task -def sync_ldt(version): - print(green("sync ldt with version %s" % version)) - export_path = export_version(ldt=version) - export_path_full = os.path.join(export_path,'ldt') - do_sync_ldt(version, export_path_full) - clean_export_folder(export_path) - relaunch_server() @task def update_lib(version, package): @@ -268,24 +36,32 @@ lib_path = os.path.join(export_path_full, "virtualenv", "res", "lib") f, pathname, description = imp.find_module("patch", [lib_path]) - patch = imp.load_module("patch", f, pathname, description) + imp.load_module("patch", f, pathname, description) f, pathname, description = imp.find_module("lib_create_env", [lib_path]) lib_create_env = imp.load_module("lib_create_env", f, pathname, description) - package_path = os.path.join(export_path_full, "virtualenv", "res", "src", lib_create_env.URLS[package]['local']) + package_path_full = os.path.join(export_path_full, "virtualenv", "res", "src", lib_create_env.URLS[package]['local']) sync_install_build(package_path_full) clean_export_folder(export_path) relaunch_server() @task -def sync_platform(version_web, version_ldt): - print(green("sync platform with version web %s, ldt %s" % (version_web, version_ldt))) - export_path = export_version(ldt=version_ldt, web=version_web) - export_path_ldt = os.path.join(export_path,'ldt') +def sync_platform(version): + print(green("sync platform with version web %s" % (version))) + export_path = export_version(web=version) + export_path_web = os.path.join(export_path,'web') - do_sync_ldt(version_ldt, export_path_ldt) - do_sync_web(version_web, export_path_web) + do_sync_web(version, export_path_web) + + comp_versions = get_comp_versions_dict(export_path_web) + + for key in [k for k in env.repos if key != 'web']: + export_path_key = export_version(**{key: comp_versions[key]}) + export_path_comp = os.path.join(export_path_key, key) + do_sync_comp(key, export_path_comp) + clean_export_folder(export_path_key) + create_config(export_path_web) clean_export_folder(export_path) relaunch_server() @@ -307,4 +83,29 @@ if venv_remote_export_path: clean_rsync_folder(venv_remote_export_path) +class SyncComp(Task): + + def __init__(self, key): + self.key = key + def __get_name(self): + return "sync_" + self.key + + name = property(__get_name) + + def run(self, version): + print(green("sync ldt with version %s" % version)) + export_path_web = export_version(web=version) + export_path_web_full = os.path.join(export_path_web,'web') + comp_versions = get_comp_versions_dict(export_path_web_full) + + export_path = export_version(**{self.key:comp_versions[self.key]}) + export_path_full = os.path.join(export_path,self.key) + do_sync_comp(self.key, export_path_full) + clean_export_folder(export_path) + clean_export_folder(export_path_web) + + relaunch_server() + +for sync_name in [key for key in env.repos if key != 'web']: + globals()[sync_name] = SyncComp(sync_name) diff -r cdb1030d63a8 -r 7df43b86c425 sbin/sync/ldt_fablib/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sbin/sync/ldt_fablib/__init__.py Tue Dec 04 00:17:10 2012 +0100 @@ -0,0 +1,257 @@ +from fabric.api import run, local, env, cd, put, prefix, sudo, lcd +from fabric.context_managers import settings +from fabric.contrib.files import exists, upload_template +from fabric.contrib.project import rsync_project +from mercurial import commands, ui, hg, cmdutil +import config +import imp +import os +import os.path +import re +import shutil +import urlparse + + + +__all__ = ["check_folder_access", "syncdb", "collectstatic", + "export_version", "do_sync_web", "create_config", "clean_export_folder", "relaunch_server", + "do_sync_ldt", "sync_install_build", "do_create_virtualenv", "clean_rsync_folder", "rsync_export"] + +def get_export_path(version): + base_path = os.path.join(env.base_export_path,env.export_prefix).rstrip("/") + return os.path.expanduser(base_path) + "_%s" % (str(version)) + +def clean_export_folder(path): + print("Removing %s" % path) + if os.path.isdir(path): + shutil.rmtree(path, ignore_errors=True) + +def do_export_version(path, **export_keys): + print("Export version %s : %s " % (path,repr(export_keys))) + + for export_key, version in export_keys.items(): + export_path = os.path.join(path,export_key) + + repo_url = env.repos[export_key] + url_part = urlparse.urlparse(repo_url) + if url_part.scheme or url_part.netloc: + # this is a remote repo. Let's clone first + clone_path = os.path.join(path,'clone',export_keys) + os.makedirs(clone_path) + local("hg clone \"%s\" \"%s\"" % (repo_url,clone_path)) + else: + clone_path = repo_url + + with lcd(clone_path): + local("hg archive -r \'%s\' \"%s\"" % (str(version),export_path)) + + print("Export version %s done"%repr(export_keys)) + + +def get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, settings_key): + activate_path = os.path.join(remotevirtualenvpath, "bin/activate") + with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): + tempfilepath = run("mktemp -t ldtplatform.XXXXXX") + with settings(warn_only=True): + run("echo \"import os\" > %s" % (tempfilepath)) + map(lambda str_echo: run("echo \"%s\" >> %s" % (str_echo, tempfilepath)), + ["os.environ.setdefault('DJANGO_SETTINGS_MODULE', '%s.settings')" % (platform_web_module), + "from django.conf import settings", + "print settings.%s" % (settings_key)]) + res = run("python < %s" % (tempfilepath)) + run("rm -f \"%s\"" % (tempfilepath)) + return res + + + +def rsync_export(path, remotepath, filters): + print("Rsync %s to %s",(path,remotepath)) + + filter_option_str = "--progress --stats" + if filters: + filter_option_str += " " + " ".join(["--filter \"%s\"" % (f) for f in filters]) + + run("mkdir -p \"%s\"" % remotepath) + rsync_project(remotepath, local_dir=path, extra_opts=filter_option_str, delete=True) + print("Rsync %s to %s done",(path,remotepath)) + +def clean_rsync_folder(remotepath): + print("clean rsync folder %s" % remotepath) + run("rm -fr \"%s\"" % remotepath) + +def build_src(path): + print("Build source dist at %s" % path) + f = None + try: + f, pathname, description = imp.find_module("setup", [path]) + print(" 2 Build source dist at %s" % path) + setup_mod = imp.load_module("setup", f, pathname, description) + print(" 3 Build source dist at %s" % path) + finally: + if f: + f.close() + + setup_mod.launch_setup("setup.py", ['sdist']) + + print("Build source dist at %s done" % path) + + +def get_src_version(mod_name, path): + print("get src version for %s at %s" % (mod_name,path)) + f = None + try: + f, pathname, description = imp.find_module(mod_name, [path]) + ldt_mod = imp.load_module(mod_name, f, pathname, description) + finally: + if f: + f.close() + version = ldt_mod.VERSION + version_str = ldt_mod.get_version() + + return (version, version_str) + + +def sync_build(path): + print("Sync build %s" % path) + with cd(env.remote_path['ldt_base']): + filename = os.path.basename(path) + res_trans = put(path, os.path.join(env.remote_path['ldt_base'], filename)) + print("Sync build %s to %s" % (path,repr(res_trans))) + return res_trans + +def remove_build(path): + print("remove build build %s" % path) + run("rm \"%s\"" % path) + + +def install_build(remotepath, remotevirtualenvpath, module_to_uninstall= None): + print("Install build %s in %s" % (remotepath, remotevirtualenvpath)) + activate_path = os.path.join(remotevirtualenvpath, "bin/activate") + + with prefix("source %s" % activate_path): + if module_to_uninstall: + with settings(warn_only=True): + run("pip uninstall -y %s" % module_to_uninstall) + run("pip install \"%s\"" % remotepath) + +def collectstatic(remotepath, remotevirtualenvpath, platform_web_module): + print("Collect static in %s with %s" % (remotepath, remotevirtualenvpath)) + remotestaticsitepath = get_remote_env(remotepath, remotevirtualenvpath, platform_web_module, "STATIC_ROOT") + activate_path = os.path.join(remotevirtualenvpath, "bin/activate") + with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): + #remocve old files optio -c of collect static fail ! + run("rm -fr \"%s\"" % (remotestaticsitepath)) + run("python manage.py collectstatic --noinput") + +def syncdb(remotepath, remotevirtualenvpath): + activate_path = os.path.join(remotevirtualenvpath, "bin/activate") + with prefix("source \"%s\"" % activate_path), prefix("export PYTHONPATH=\"%s\"" % remotepath), cd(remotepath): + run("python manage.py syncdb --migrate --noinput") + +def create_config(export_path): + print("Create config from %s" % (export_path,)) + remotepath = env.remote_path['src'] + remote_config_path = os.path.join(remotepath, env.platform_web_module, "config.py") + template_path = os.path.join(export_path, "src", env.platform_web_module, "config.py.tmpl") + + context = { + 'base_dir': os.path.join(remotepath, env.platform_web_module).rstrip("/")+"/", + 'asctime': '%(asctime)s', + 'levelname': '%(levelname)s', + 'message': '%(message)s', + 'module': '%(module)s', + } + context.update(env.config['web']) + + if not exists(remote_config_path, verbose=True): + upload_template(template_path, remote_config_path, context=context) + +def export_version(**kwargs): + print("export version %s" % (repr(kwargs))) + + export_path = kwargs.get('path', None) + + if not export_path: + export_path = get_export_path("_".join(["%s_%s" % (k,v) for k,v in kwargs.items()])) + + clean_export_folder(export_path) + + do_export_version(export_path,**kwargs) + + return export_path + +def do_create_virtualenv(remote_venv_export_path, remotevirtualenvpath): + print("Create virtualenv export_path : %s - remote venvpath : %s" % (remote_venv_export_path, remotevirtualenvpath)) + activate_path = os.path.join(remotevirtualenvpath, "bin/activate") + if "remote_baseline_venv" in env and env.remote_baseline_venv: + prefix_str = "source \"%s\"" % os.path.join(env.remote_baseline_venv, "bin/activate") + else: + prefix_str = "echo" + with settings(warn_only=True): + run("rm -fr \"%s\"" % remotevirtualenvpath) + run("mkdir -p \"%s\"" % remotevirtualenvpath) + with prefix(prefix_str), cd(os.path.join(remote_venv_export_path,"virtualenv","web")): + run("python create_python_env.py") + run("python project-boot.py \"%s\"" % remotevirtualenvpath) + with prefix("source \"%s\"" % activate_path): + run("pip install -r \"%s\"" % os.path.join(remote_venv_export_path,"virtualenv","web","res","srvr_requirements.txt")) + +def do_sync_comp(key, export_path): + print("do_sync_comp with path %s" % (export_path)) + + src_path = os.path.join(export_path,"src") + # find setup.py + for root, _, files in os.walk(src_path): + if "setup.py" in files: + src_path = root + + build_src(src_path) + (_,version_str) = get_src_version(key, src_path) + build_path = os.path.join(src_path,"dist","%s-%s.tar.gz" % (key,version_str)) + sync_install_build(build_path, key) + + +def sync_install_build(build_path, module_to_uninstall=None): + res_trans = None + try: + res_trans = sync_build(build_path) + install_build(res_trans[0], env.remote_path['virtualenv'], module_to_uninstall) + finally: + if res_trans: + remove_build(res_trans[0]) + + +def do_sync_web(version, export_path): + print("do_sync_web with version %s and path %s" % (version,export_path)) + #sync web + web_path = os.path.join(export_path,"web/") + rsync_export(web_path, env.remote_path['web'], env.rsync_filters['web']) + #sync src + src_path = os.path.join(export_path,"src/") + rsync_export(src_path, env.remote_path['src'], env.rsync_filters['src']) + + +def check_folder_access(): + print("Check folder access") + # get remote user + for folder_path in env.folders: + if not os.path.isabs(folder_path): + folder_path = env.remote_path['web'].rstrip("/")+ "/" + folder_path + with settings(warn_only=True): + if not exists(folder_path): + run("mkdir -p \"%s\"" % folder_path) + run("chown -R -c :%s \"%s\"" % (env.web_group, folder_path)) + run("chmod -R -c g+w \"%s\"" % folder_path) + +def get_comp_versions_dict(export_path_web): + comp_versions = {} + with open(os.path.join(export_path_web, 'src', 'requirement.txt')) as f: + for line in f: + m = re.match('^(\w+)\s+\(\s*\=\=\s*([\.\d\w]+)\s*\)', line) + if m: + key, version_req = m.groups() + if "." in version_req: + version_req = "V" + ".".join(["%02d" % (int(s)) if s.isdigit() else s for s in version_req.split(".")]) + comp_versions[key] = version_req + + return comp_versions diff -r cdb1030d63a8 -r 7df43b86c425 src/requirement.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/requirement.txt Tue Dec 04 00:17:10 2012 +0100 @@ -0,0 +1,2 @@ +ldt (==1.36) +hashcut (==0.1)