-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrequirements.py
More file actions
259 lines (210 loc) · 8.45 KB
/
requirements.py
File metadata and controls
259 lines (210 loc) · 8.45 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
import sys
from pathlib import Path
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: {' '.join(self.fix_cmd)}")
async def fix(self) -> bool:
assert self.fix_cmd, "Attempted to fix without a fix command"
proc = await self.host.arun(self.fix_cmd)
return await proc.wait() == 0
class LocalRequirement(Requirement):
host = local_host
class PodmanPlatform(Requirement):
async def check(self):
result = False
try:
podman_info = await self.host.podman_info()
except FileNotFoundError:
logger.error("podman not found. Try: dnf install podman")
return False
try:
host_os = (
podman_info["host"].get("Os") or podman_info["host"]["os"]
).lower()
if host_os == "linux":
result = True
except KeyError:
host_os = sys.platform.lower()
result = False
if sys.platform == "darwin":
logger.error(
"The podman machine (VM) is not running. "
"Try: podman machine init --now"
)
else:
logger.error(
"Unknown error trying to query podman. Is podman installed?"
)
return result
if host_os != "linux":
logger.error("The platform '{host_os}' is not currently supported.")
return result
class PodmanGraphDriver(Requirement):
async def check(self):
podman_info = await self.host.podman_info()
storage_conf_path = podman_info["store"]["configFile"]
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(Requirement):
async def check(self):
podman_info = await self.host.podman_info()
storage_conf_path = podman_info["store"]["configFile"]
runtime = podman_info["host"]["ociRuntime"]["name"]
if runtime == "crun":
return True
else:
containers_conf_path = Path(storage_conf_path).parent / "containers.conf"
cmd = host.cmd(["podman", "system", "reset"])
logger.error(
f"The configured runtime is '{runtime}'. "
f"It must be set to 'crun' in {containers_conf_path}. "
f"Afterward, run '{cmd}'."
)
return False
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"
def __init__(self):
os_type = self.host.os_type()
if os_type == "centos":
dns_plugin_path = "/usr/libexec/cni/dnsname"
self.check_cmd = ["test", "-x", dns_plugin_path]
self.fix_cmd = ["sudo", "dnf", "install", "-y", dns_plugin_path]
elif os_type in ["ubuntu", "debian"]:
dns_plugin_path = "/usr/lib/cni/dnsname"
self.check_cmd = ["test", "-x", dns_plugin_path]
self.fix_cmd = [
"sudo",
"apt",
"install",
"-y",
"golang-github-containernetworking-plugin-dnsname",
]
class FuseOverlayfsPresence(FixableRequirement):
check_cmd = ["command", "-v", "fuse-overlayfs"]
suggest_msg = "Could not find fuse-overlayfs"
fix_cmd = ["sudo", "dnf", "install", "-y", "fuse-overlayfs"]
async def check_requirements():
if not await PodmanPlatform().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()
# 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