poudriere-runner: add more options, use types hints and cleanup
[freebsd-maintainer-scripts/.git] / poudriere-runner.py
index 00072039413e797f2be23d731c4d643a8c592b4c..eddeb548660cd197dfc8e5820e79b5f1371b2b06 100755 (executable)
@@ -11,64 +11,57 @@ import shlex
 import subprocess
 import sys
 
+from typing import List, Dict
+
+import colored
+from colored import stylize
+
 LOCALBASE="/usr/local"
 PORTSDIR="/usr/ports" or os.environ['PORTSDIR']
 
 default_conf="""
 [default]
-debug=False
+debug=True
+setup=False
 disk_path=%s/zfsfs
-jails=10i386,10amd64,93amd64,93i386
-jails_disabled=84i386,84amd64
+jails=12amd64,13amd64
+jails_disabled=14amd64
+mdconfig=False
 mdconfig_cmd=mdconfig -f
-cpuset=True
-cpuset_cmd=cpuset -c -l 0-3
-
-[84i386]
-name=84i386
-arch=i386
-version=8.4-RELEASE
+cpuset=False
+cpuset_cmd=cpuset -c -l 0-1
+sets_host=FREEBSD_HOST=https://download.FreeBSD.org
+port_tree=portsdir
 
-[84amd64]
-name=84amd64
+[12amd64]
+name=12amd64
 arch=amd64
-version=8.4-RELEASE
+version=12.2-RELEASE
 
-[93i386]
-name=93i386
-arch=i386
-version=9.3-RELEASE
-
-[93amd64]
-name=93amd64
+[13amd64]
+name=13amd64
 arch=amd64
-version=9.3-RELEASE
-
-[10i386]
-name=10i386
-arch=i386
-version=10.3-RELEASE
+version=13.0-RC1
 
-[10amd64]
-name=10amd64
+[14amd64]
+name=14amd64
 arch=amd64
-version=10.3-RELEASE
+version=14.0-CURRENT
 """ % LOCALBASE
 
 class Prunner(object):
     def __init__(self):
         loadDisk()
         self._jails = loadJails()
-        self._port_trees = []
-        self.is_setup = True
-        if not self.is_setup:
+        self._port_tree = cfg.get("default", "port_tree")
+        self.is_setup = cfg.getboolean("default", "setup")
+        if self.is_setup:
             self.setUp()
 
-    def setUp(self, **params):
+    def setUp(self, **params: Dict):
         if debug:
-            print("setup...")
-        #TODO: test if exists to avoid recreating jails
-        params['host'] = 'FREEBSD_HOST=ftp.fr.freebsd.org'
+            print("Creating jail setup...")
+        params['host'] = cfg.get("default", "sets_host")
         for jail in self._jails:
             cmd = "poudriere jail -c -j {0} -a {1} -v {2} {3}".format(
                 jail['name'],
@@ -80,7 +73,8 @@ class Prunner(object):
             sudo(cmd)
 
     def tearDown(self):
-        #TODO: kill jail (stop them)
+        if debug:
+            print("Stoping jails teardown()...")
         for jail in self._jails:
             cmd = "poudriere jail -k -j {0}".format(jail['name'])
             if debug:
@@ -92,19 +86,19 @@ class Prunner(object):
        return self._jails
 
     @property
-    def port_trees(self):
-       return self._port_trees
+    def port_tree(self):
+       return self._port_tree
 
-    def testPort(self, origin, port_tree="portsdir"):
+    def testPort(self, origin: str, port_tree: str):
         for jail in self._jails:
             if debug:
-                print("testport o: {0}: j: {1}".format(origin, jail['name']))
-            cmd = "poudriere testport -o {0} -j {1} -p {2} -n".format(
-                    origin,
-                    jail['name'], port_tree)
+                print(stylize("testport o: {0} j: {1}".format(origin,
+                    jail['name']), colored.fg("magenta")))
+            cmd = "poudriere testport -o {0} -j {1} -p {2}".format(
+                    origin, jail['name'], port_tree)
             out, err = sudo(cmd)
             if debug:
-                print("j: {0}, out: {1}, err: {2}".format(jail['name'], out,
+                print("j: {0}\nout: {1}\nerr: {2}".format(jail['name'], out,
                     err))
 
     def testAll(self):
@@ -121,27 +115,31 @@ class Prunner(object):
         return ports
 
     def __del__(self):
-        if debug:
-            print("teardown()...")
         if hasattr(self, 'is_setup') and self.is_setup:
             self.tearDown()
 
-def run(command, use_sudo=False):
+def run(command: str, use_sudo=False) -> str:
     cpuset_cmd = cfg.get('default', 'cpuset_cmd')
-    use_cpuset = cfg.get('default', 'cpuset')
+    use_cpuset = cfg.getboolean('default', 'cpuset')
     if use_cpuset:
         command = "%s %s" % (cpuset_cmd, command)
 
     command = "sudo %s" % command if use_sudo else command
-    p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
+    p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
+            universal_newlines=True)
     stdout, stderr = p.communicate()
     p.wait()
+
     return stdout.strip(), stderr
 
-def sudo(command):
+def sudo(command: str):
     return run(command, use_sudo=True)
 
 def loadDisk():
+    if cfg.getboolean('default', 'mdconfig') is False:
+        print("Skip mdconfig...\n")
+        return
+
     mdconfig_cmd = cfg.get('default', 'mdconfig_cmd')
     disk_path = cfg.get('default', 'disk_path')
     if not os.path.exists('/dev/md0'):
@@ -149,7 +147,6 @@ def loadDisk():
         if err is not None:
             print("Error: {0}".format(out))
             sys.exit(1)
-    return True
 
 def loadJails():
     jails = []
@@ -167,15 +164,16 @@ def loadJails():
 
     return jails
 
-def isValidOrigin(origin):
+def isValidOrigin(origin: str):
     abspath = "{0}/{1}".format(PORTSDIR, origin)
+
     return os.path.isdir(abspath)
 
 def main():
     global cfg, debug
     cfg = configparser.ConfigParser()
     cfg.read_string(default_conf)
-    debug = cfg.get('default', 'debug')
+    debug = cfg.getboolean('default', 'debug')
     if debug:
         print(cfg.get('default', 'disk_path'))
 
@@ -187,8 +185,7 @@ def main():
         origin = sys.argv[1]
 
     if isValidOrigin(origin):
-        runner.testPort(origin)
-    #runner.testAll()
+        runner.testPort(origin, runner.port_tree)
 
 if __name__ == '__main__':
     main()