]> sbz's 6dev Repos - freebsd-maintainer-scripts/.git/blob - poudriere-runner.py
poudriere-runner: update jails
[freebsd-maintainer-scripts/.git] / poudriere-runner.py
1 #!/usr/bin/env python3
2
3 # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
4
5 """
6 sbz poudriere runner
7 """
8
9 import argparse
10 import configparser
11 import os
12 import shlex
13 import subprocess
14 import sys
15
16 from typing import Dict
17
18 import colored
19 from colored import stylize
20
21 LOCALBASE = "/usr/local"
22 PORTSDIR = "/usr/ports" or os.environ["PORTSDIR"]
23
24 default_conf = (
25     """
26 [default]
27 debug=False
28 setup=False
29 disk_path=%s/zfsfs
30 jails=12amd64
31 jails_disabled=11amd64,13amd64,14amd64
32 mdconfig=False
33 mdconfig_cmd=mdconfig -f
34 cpuset=False
35 cpuset_cmd=cpuset -c -l 0-1
36 sets_host=FREEBSD_HOST=https://download.FreeBSD.org
37 port_tree=portsdir
38
39 [11amd64]
40 name=11amd64
41 arch=amd64
42 version=11.4-RELEASE
43
44 [12amd64]
45 name=12amd64
46 arch=amd64
47 version=12.2-RELEASE
48
49 [13amd64]
50 name=13amd64
51 arch=amd64
52 version=13.0-RELEASE
53
54 [14amd64]
55 name=14amd64
56 arch=amd64
57 version=14.0-CURRENT
58 """
59     % LOCALBASE
60 )
61
62
63 class Prunner(object):
64     def __init__(self):
65         loadDisk()
66         self._jails = loadJails()
67         self._port_tree = cfg.get("default", "port_tree")
68         self.is_setup = cfg.getboolean("default", "setup")
69         if self.is_setup:
70             self.setUp()
71
72     def setUp(self, **params: Dict):
73         if debug:
74             print("Creating jail setup...")
75         params["host"] = cfg.get("default", "sets_host")
76         for jail in self._jails:
77             cmd = "poudriere jail -c -j {0} -a {1} -v {2} {3}".format(
78                 jail["name"], jail["arch"], jail["version"], params["host"]
79             )
80             if debug:
81                 print("exec: {0}".format(cmd))
82             sudo(cmd)
83
84     def tearDown(self):
85         if debug:
86             print("Stoping jails teardown()...")
87         for jail in self._jails:
88             cmd = "poudriere jail -k -j {0}".format(jail["name"])
89             if debug:
90                 print("exec: {0}".format(cmd))
91             sudo(cmd)
92
93     @property
94     def jails(self):
95         return self._jails
96
97     @property
98     def port_tree(self):
99         return self._port_tree
100
101     def testPort(self, origin: str, port_tree: str):
102         for jail in self._jails:
103             if debug:
104                 print(
105                     stylize(
106                         "testport o: {0} j: {1}".format(origin, jail["name"]),
107                         colored.fg("magenta"),
108                     )
109                 )
110             cmd = "poudriere testport -o {0} -j {1} -p {2}".format(
111                 origin, jail["name"], port_tree
112             )
113             out, err = sudo(cmd)
114             if debug:
115                 print("j: {0}\nout: {1}\nerr: {2}".format(jail["name"], out, err))
116
117     def testAll(self):
118         for o in self.sbzports():
119             self.test_port(o)
120
121     def bulk(self, jail):
122         raise NotImplemented("not available yet")
123
124     def __del__(self):
125         if hasattr(self, "is_setup") and self.is_setup:
126             self.tearDown()
127
128
129 def run(command: str, use_sudo=False) -> str:
130     cpuset_cmd = cfg.get("default", "cpuset_cmd")
131     use_cpuset = cfg.getboolean("default", "cpuset")
132     if use_cpuset:
133         command = "%s %s" % (cpuset_cmd, command)
134
135     command = "sudo %s" % command if use_sudo else command
136     p = subprocess.Popen(
137         shlex.split(command), stdout=subprocess.PIPE, universal_newlines=True
138     )
139     stdout, stderr = p.communicate()
140     p.wait()
141
142     return stdout.strip(), stderr
143
144
145 def sudo(command: str):
146     return run(command, use_sudo=True)
147
148
149 def loadDisk():
150     if cfg.getboolean("default", "mdconfig") is False:
151         if debug:
152             print("Skip mdconfig...\n")
153         return
154
155     mdconfig_cmd = cfg.get("default", "mdconfig_cmd")
156     disk_path = cfg.get("default", "disk_path")
157     if not os.path.exists("/dev/md0"):
158         out, err = sudo("{0} {1}".format(mdconfig_cmd, disk_path))
159         if err is not None:
160             print("Error: {0}".format(out))
161             sys.exit(1)
162
163
164 def loadJails():
165     jails = []
166     existing_jails = cfg.get("default", "jails").split(",")
167     try:
168         existing_jails.remove(cfg.get("default", "jails_disabled"))
169     except Exception:
170         pass
171
172     for jail in existing_jails:
173         name = cfg.get(jail, "name")
174         arch = cfg.get(jail, "arch")
175         version = cfg.get(jail, "version")
176         jails.append({"name": name, "arch": arch, "version": version})
177
178     return jails
179
180
181 def isValidOrigin(origin: str):
182     abspath = "{0}/{1}".format(PORTSDIR, origin)
183
184     return os.path.isdir(abspath)
185
186
187 def main():
188     global cfg, debug
189     cfg = configparser.ConfigParser()
190     cfg.read_string(default_conf)
191     debug = cfg.getboolean("default", "debug")
192     if debug:
193         print(cfg.get("default", "disk_path"))
194
195     runner = Prunner()
196     if len(sys.argv) == 1:
197         cwd = os.getcwd()
198         origin = "{0}/{1}".format(cwd.split("/")[-2], cwd.split("/")[-1])
199     else:
200         origin = sys.argv[1]
201
202     if isValidOrigin(origin):
203         runner.testPort(origin, runner.port_tree)
204
205     return 0
206
207
208 if __name__ == "__main__":
209     sys.exit(main())