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