Upload the contribution of vstf as bottleneck network framework.
[bottlenecks.git] / vstf / vstf / agent / env / basic / source_manager.py
diff --git a/vstf/vstf/agent/env/basic/source_manager.py b/vstf/vstf/agent/env/basic/source_manager.py
new file mode 100755 (executable)
index 0000000..4267cbd
--- /dev/null
@@ -0,0 +1,74 @@
+"""
+Created on 2015-8-27
+
+@author: y00228926
+"""
+import os
+import logging
+import contextlib
+from subprocess import CalledProcessError
+from vstf.common.utils import check_call
+
+LOG = logging.getLogger(__name__)
+
+
+@contextlib.contextmanager
+def my_chdir(file_path):
+    old_cwd = os.path.realpath(os.curdir)
+    os.chdir(file_path)
+    LOG.info("cd %s", file_path)
+    yield
+    os.chdir(old_cwd)
+    LOG.info("cd %s", old_cwd)
+
+
+class SourceCodeManager(object):
+    def __init__(self):
+        super(SourceCodeManager, self).__init__()
+        self.base_path = '/opt/vstf/'
+
+    @staticmethod
+    def _git_pull(url, dest):
+        if not os.path.isdir(dest):
+            check_call("git clone %s %s" % (url, dest), shell=True)
+        else:
+            with my_chdir(dest):
+                check_call("git pull", shell=True)
+
+    @staticmethod
+    def _install(dest):
+        with my_chdir(dest):
+            try:
+                check_call("make && make install", shell=True)
+            except CalledProcessError:
+                LOG.info("retry make again")
+                check_call("make clean; make && make install", shell=True)
+
+    def src_install(self, cfg):
+        for key, item in cfg.items():
+            repo_type = item['repo_type']
+            url = item['url']
+            install = item['install']
+            if install is True:
+                LOG.info("installing src repo:%s", key)
+                if repo_type == "git":
+                    target = self.base_path + key
+                    self._git_pull(url, target)
+                    self._install(target)
+                else:
+                    raise Exception("unsupported repo type:%s" % repo_type)
+            else:
+                LOG.info("skip src repo:%s", key)
+        return True
+
+
+if __name__ == '__main__':
+    import argparse
+    import json
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--config', help='config file to parse')
+    args = parser.parse_args()
+    logging.basicConfig(level=logging.INFO)
+    cfg = json.load(open(args.config))
+    mgr = SourceCodeManager()
+    mgr.src_install(cfg)