-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrequirements.py
More file actions
300 lines (234 loc) · 9.4 KB
/
Copy pathrequirements.py
File metadata and controls
300 lines (234 loc) · 9.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import shlex
from packaging.version import parse as parse_version, Version
from typing import List
from ceph_devstack import config, logger
from ceph_devstack.host import Host, host, local_host
class Requirement:
host: Host = host
check_cmd: List[str]
async def evaluate(self) -> bool:
return await self.check()
async def check(self) -> bool:
proc = await self.host.arun(self.check_cmd)
return await proc.wait() == 0
class FixableRequirement(Requirement):
fix_cmd: List[str]
suggest_msg: str
async def evaluate(self) -> bool:
if await self.check() is True:
return True
if config["args"].get("fix", False):
return await self.fix()
else:
await self.suggest()
return False
async def suggest(self):
if hasattr(self, "suggest_msg"):
logger.error(f"{self.suggest_msg}. Try: {shlex.join(self.fix_cmd)}")
async def fix(self) -> bool:
assert self.fix_cmd, "Attempted to fix without a fix command"
stream_output = config["args"].get("verbose", False)
proc = await self.host.arun(self.fix_cmd, stream_output=stream_output)
return await proc.wait() == 0
class LocalRequirement(Requirement):
host = local_host
class LocalFixableRequirement(FixableRequirement):
host = local_host
class PodmanPlatform(LocalFixableRequirement):
suggest_msg = "podman not found"
@property
def fix_cmd(self):
host_os = self.host.os_type
if host_os == "darwin":
return ["brew", "install", "podman"]
return ["sudo", host.package_manager(), "install", "-y", "podman"]
async def check(self):
try:
await self.host.podman_info()
return True
except FileNotFoundError:
logger.error("podman not found. Try: dnf install podman")
return False
class PodmanMachinePresent(FixableRequirement):
suggest_msg = "podman machine (VM) not present"
fix_cmd = ["podman", "machine", "init", "--now"]
async def check(self):
machine_infos = await host.podman_machine_info()
if machine_infos and (machine_info := machine_infos[-1]):
return machine_info.get("Created") is not None
return False
class PodmanMachineRunning(LocalFixableRequirement):
suggest_msg = "podman machine (VM) not running"
fix_cmd = ["podman", "machine", "start"]
async def check(self):
machine_infos = await host.podman_machine_info()
if machine_infos and (machine_info := machine_infos[-1]):
return machine_info.get("Running", False)
return False
class PodmanGraphDriver(Requirement):
async def check(self):
podman_info = await self.host.podman_info()
storage_conf_path = podman_info["store"].get("configFile", "storage.conf")
graph_driver = podman_info["store"]["graphDriverName"]
if graph_driver == "overlay":
return True
else:
self.suggest_msg = (
f"The configured graph driver is '{graph_driver}'. "
f"It must be set to 'overlay' in {storage_conf_path}."
)
return False
class KernelVersionForOverlay(Requirement):
async def check(self):
kernel_version = self.host.kernel_version()
version_for_overlay = Version("5.12")
if kernel_version < version_for_overlay:
self.suggest_msg = (
f"Kernel version ({kernel_version}) is too old to support native rootless "
f"overlayfs (needs {version_for_overlay})"
)
return False
return True
class KernelVersionForCgroupV2(Requirement):
async def check(self):
version_for_cgroup = Version("4.15")
kernel_version = self.host.kernel_version()
if not kernel_version >= version_for_cgroup:
self.suggest_msg = (
f"Kernel version ({kernel_version}) is too old to support cgroup v2 "
f"(needs {version_for_cgroup})"
)
return False
class CgroupV2(FixableRequirement):
suggest_msg = "cgroup v2 is not enabled"
fix_cmd = [
"sudo",
"grubby",
"--update-kernel=ALL",
"--args='systemd.unified_cgroup_hierarchy=1'",
]
async def check(self):
podman_info = await self.host.podman_info()
return podman_info["host"]["cgroupVersion"] == "v2"
class PodmanVersion(Requirement):
def __init__(self, version: str, msg: str = ""):
self.required_version = parse_version(version)
self.msg = msg
async def check(self):
podman_info = await self.host.podman_info()
podman_version = parse_version(podman_info["version"]["Version"])
if podman_version < self.required_version:
if self.msg:
logger.warning(self.msg)
return False
return True
class PodmanRuntime(FixableRequirement):
suggest_msg = "Could not find the 'crun' container runtime"
@property
def fix_cmd(self):
if self.host.os_type != "darwin":
return ["sudo", self.host.package_manager(), "install", "-y", "crun"]
return []
async def check(self):
podman_info = await self.host.podman_info()
runtime = podman_info["host"]["ociRuntime"]["name"]
return runtime == "crun"
class SELinuxBoolean(FixableRequirement):
def __init__(self, boolean_name: str):
super().__init__()
self.boolean_name = boolean_name
self.fix_cmd = ["sudo", "setsebool", "-P", f"{self.boolean_name}=true"]
self.suggest_msg = f"SELinux boolean '{self.boolean_name}' must be enabled"
async def check(self):
return await self.host.check_selinux_bool(self.boolean_name)
class SysctlValue(FixableRequirement):
def __init__(self, name: str, min_value: int):
super().__init__()
self.key = name
self.min_value = min_value
self.fix_cmd = ["sudo", "sysctl", f"{name}={min_value}"]
async def check(self):
current_value = await self.host.get_sysctl_value(self.key)
self.suggest_msg = f"sysctl setting {self.key} ({current_value}) is too low"
return current_value >= self.min_value
class PodmanDNSPlugin(FixableRequirement):
suggest_msg = "Could not find the podman DNS plugin"
@property
def dns_plugin_path(self):
os_type = self.host.os_type
if os_type in ["ubuntu", "debian"]:
return "/usr/lib/cni/dnsname"
return "/usr/libexec/cni/dnsname"
@property
def check_cmd(self):
return ["test", "-x", self.dns_plugin_path]
@property
def fix_cmd(self):
os_type = self.host.os_type
if os_type == "centos":
return ["sudo", "dnf", "install", "-y", self.dns_plugin_path]
elif os_type in ["ubuntu", "debian"]:
return [
"sudo",
"apt",
"install",
"-y",
"golang-github-containernetworking-plugin-dnsname",
]
return []
class FuseOverlayfsPresence(FixableRequirement):
check_cmd = ["command", "-v", "fuse-overlayfs"]
suggest_msg = "Could not find fuse-overlayfs"
fix_cmd = ["sudo", "dnf", "install", "-y", "fuse-overlayfs"]
class AppArmorProfile(FixableRequirement):
_profile_path = "/etc/apparmor.d/local/unix-chkpwd"
_profile_content = '"capability dac_override,"'
check_cmd = ["test", "-f", _profile_path]
suggest_msg = "Did not find required apparmor profile"
fix_cmd = [
"sudo",
"bash",
"-c",
f"echo -e {_profile_content} > {_profile_path} && systemctl reload apparmor",
]
async def check_requirements():
if not await PodmanPlatform().evaluate():
return False
if local_host.os_type == "darwin":
if not await PodmanMachinePresent().evaluate():
return False
if not await PodmanMachineRunning().evaluate():
return False
result = True
# kernel and podman versions for native overlay filesystem
result = result and await PodmanGraphDriver().evaluate()
podman_overlay_version = "3.10"
podman_version_overlay = await PodmanVersion(
podman_overlay_version,
"Podman version is too old for rootless native overlayfs (needs {podman_overlay_version})",
).evaluate()
needs_fuse = not (
await KernelVersionForOverlay().evaluate() and podman_version_overlay
)
# if not using native overlay, we need fuse-overlayfs
if needs_fuse:
result = result and await FuseOverlayfsPresence().evaluate()
# cgroup v2
if not await CgroupV2().evaluate():
result = result and await KernelVersionForCgroupV2().evaluate()
# runtime
result = result and await PodmanRuntime().evaluate()
# SELinux
if await host.selinux_enforcing():
result = result and await SELinuxBoolean("container_manage_cgroup").evaluate()
result = result and await SELinuxBoolean("container_use_devices").evaluate()
# AppArmor
if await host.apparmor_enabled():
result = result and await AppArmorProfile().evaluate()
# podman DNS plugin
if not await PodmanVersion("5.0").evaluate():
result = result and await PodmanDNSPlugin().evaluate()
# sysctl settings for OSD
result = result and await SysctlValue("fs.aio-max-nr", 2097152).evaluate()
result = result and await SysctlValue("kernel.pid_max", 4194304).evaluate()
return result