pve-install-hetzner 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pve_install_hetzner/__init__.py +4 -0
- pve_install_hetzner/answer_files.py +192 -0
- pve_install_hetzner/cli.py +804 -0
- pve_install_hetzner/disks.py +93 -0
- pve_install_hetzner/first_boot.py +136 -0
- pve_install_hetzner/guidance.py +580 -0
- pve_install_hetzner/health.py +107 -0
- pve_install_hetzner/hetzner_network.py +403 -0
- pve_install_hetzner/prepare-rescue.sh +40 -0
- pve_install_hetzner/pve_boot.py +259 -0
- pve_install_hetzner/storage.py +106 -0
- pve_install_hetzner/terminal.py +295 -0
- pve_install_hetzner/web_access.py +74 -0
- pve_install_hetzner-2.0.0.dist-info/METADATA +551 -0
- pve_install_hetzner-2.0.0.dist-info/RECORD +18 -0
- pve_install_hetzner-2.0.0.dist-info/WHEEL +4 -0
- pve_install_hetzner-2.0.0.dist-info/entry_points.txt +3 -0
- pve_install_hetzner-2.0.0.dist-info/licenses/LICENSES/GPL-3.0-or-later.txt +232 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: foundata GmbH <https://foundata.com>
|
|
2
|
+
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
3
|
+
|
|
4
|
+
"""Read and save the operator's pve-install-hetzner answer file."""
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import os
|
|
8
|
+
import tomllib
|
|
9
|
+
from collections.abc import Iterator
|
|
10
|
+
from contextlib import contextmanager
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from tempfile import NamedTemporaryFile
|
|
13
|
+
|
|
14
|
+
# Public TOML names and the settings shared by the wizard and installer.
|
|
15
|
+
FIELDS = {
|
|
16
|
+
"host.fqdn": "fqdn",
|
|
17
|
+
"host.email": "email",
|
|
18
|
+
"host.timezone": "timezone",
|
|
19
|
+
"access.ssh-public-key-file": "ssh_key",
|
|
20
|
+
"access.ssh-public-key": "ssh_public_key",
|
|
21
|
+
"access.root-password-file": "root_password_file",
|
|
22
|
+
"access.root-password-hash-file": "root_password_hash_file",
|
|
23
|
+
"access.web": "web_access",
|
|
24
|
+
"access.web-allow-from": "web_allow_from",
|
|
25
|
+
"storage.disks": "disk",
|
|
26
|
+
"storage.layout": "layout",
|
|
27
|
+
"installation.target-firmware": "target_firmware",
|
|
28
|
+
"installation.repository": "repository",
|
|
29
|
+
"installation.iso": "iso",
|
|
30
|
+
"network.uplink-name": "uplink_name",
|
|
31
|
+
"network.guest-subnet": "guest_subnet",
|
|
32
|
+
"network.guest-ipv6": "guest_ipv6",
|
|
33
|
+
"network.nic-pins": "pin_nic",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def field_name(attribute: str) -> str:
|
|
38
|
+
"""Return the answer-file key used in diagnostics."""
|
|
39
|
+
return next(name for name, attr in FIELDS.items() if attr == attribute)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _text(value: object, name: str) -> str:
|
|
43
|
+
if not isinstance(value, str) or not value.strip() or "\0" in value:
|
|
44
|
+
raise ValueError(f"{name} requires a nonempty string")
|
|
45
|
+
return value
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _value(name: str, value: object, directory: Path) -> str | Path | list[str]:
|
|
49
|
+
if name == "network.nic-pins":
|
|
50
|
+
if not isinstance(value, dict):
|
|
51
|
+
raise ValueError(f"{name} requires a table of MAC addresses and names")
|
|
52
|
+
return [f"{mac}={_text(nic, name)}" for mac, nic in value.items()]
|
|
53
|
+
if name in ("storage.disks", "access.web-allow-from"):
|
|
54
|
+
if not isinstance(value, list) or not value:
|
|
55
|
+
raise ValueError(f"{name} requires a nonempty array of strings")
|
|
56
|
+
entries = [_text(entry, name) for entry in value]
|
|
57
|
+
if name == "storage.disks":
|
|
58
|
+
return [str(directory / entry) for entry in entries]
|
|
59
|
+
return entries
|
|
60
|
+
text = _text(value, name)
|
|
61
|
+
if name.endswith("-file") or name == "installation.iso":
|
|
62
|
+
if text == "-" and name in (
|
|
63
|
+
"access.root-password-file",
|
|
64
|
+
"access.root-password-hash-file",
|
|
65
|
+
):
|
|
66
|
+
return Path("-")
|
|
67
|
+
return directory / text
|
|
68
|
+
return text
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def load_answers(args: argparse.Namespace) -> None:
|
|
72
|
+
"""Load typed answers, leaving omitted settings available for the wizard."""
|
|
73
|
+
for attr in FIELDS.values():
|
|
74
|
+
setattr(args, attr, None)
|
|
75
|
+
path: Path | None = args.answer_file
|
|
76
|
+
if path is None:
|
|
77
|
+
return
|
|
78
|
+
with path.open("rb") as source:
|
|
79
|
+
try:
|
|
80
|
+
answers = tomllib.load(source)
|
|
81
|
+
except (UnicodeDecodeError, tomllib.TOMLDecodeError) as exc:
|
|
82
|
+
raise ValueError(f"{path}: invalid TOML: {exc}") from exc
|
|
83
|
+
version = answers.pop("schema-version", 1)
|
|
84
|
+
if type(version) is not int or version != 1:
|
|
85
|
+
raise ValueError(f"{path}: unsupported schema-version (expected integer 1)")
|
|
86
|
+
directory = path.absolute().parent
|
|
87
|
+
for table, items in answers.items():
|
|
88
|
+
if not any(field.startswith(table + ".") for field in FIELDS):
|
|
89
|
+
raise ValueError(f"{path}: unknown answer table {table!r}")
|
|
90
|
+
if not isinstance(items, dict):
|
|
91
|
+
raise ValueError(f"{path}: {table} must be a TOML table")
|
|
92
|
+
for key, value in items.items():
|
|
93
|
+
name = f"{table}.{key}"
|
|
94
|
+
if name not in FIELDS:
|
|
95
|
+
raise ValueError(f"{path}: unknown answer {name!r}")
|
|
96
|
+
setattr(args, FIELDS[name], _value(name, value, directory))
|
|
97
|
+
if args.ssh_key and args.ssh_public_key:
|
|
98
|
+
raise ValueError(
|
|
99
|
+
"choose only one of access.ssh-public-key and access.ssh-public-key-file"
|
|
100
|
+
)
|
|
101
|
+
if args.root_password_file and args.root_password_hash_file:
|
|
102
|
+
raise ValueError("choose only one root password source in [access]")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _is_rescue_storage(device: int) -> bool:
|
|
106
|
+
"""Recognize the live root, memory filesystems and remote shares by device."""
|
|
107
|
+
if device == Path("/").stat().st_dev:
|
|
108
|
+
return True
|
|
109
|
+
number = f"{os.major(device)}:{os.minor(device)}"
|
|
110
|
+
with Path("/proc/self/mountinfo").open(
|
|
111
|
+
encoding="utf-8", errors="surrogateescape"
|
|
112
|
+
) as mounts:
|
|
113
|
+
for line in mounts:
|
|
114
|
+
mount, filesystem = line.split(" - ", 1)
|
|
115
|
+
if mount.split()[2] == number:
|
|
116
|
+
return filesystem.split()[0] in {
|
|
117
|
+
"tmpfs",
|
|
118
|
+
"ramfs",
|
|
119
|
+
"nfs",
|
|
120
|
+
"nfs4",
|
|
121
|
+
"cifs",
|
|
122
|
+
"smb3",
|
|
123
|
+
"fuse.sshfs",
|
|
124
|
+
}
|
|
125
|
+
return False
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@contextmanager
|
|
129
|
+
def _answer_directory(parent: Path) -> Iterator[Path]:
|
|
130
|
+
# Pin the directory so a replaced symlink or a new mount cannot redirect the write.
|
|
131
|
+
fd = os.open(parent, os.O_RDONLY | os.O_DIRECTORY)
|
|
132
|
+
try:
|
|
133
|
+
if not _is_rescue_storage(os.fstat(fd).st_dev):
|
|
134
|
+
raise ValueError(
|
|
135
|
+
f"{parent}: choose a directory on the live rescue filesystem, such as /root"
|
|
136
|
+
)
|
|
137
|
+
yield Path(f"/proc/self/fd/{fd}")
|
|
138
|
+
finally:
|
|
139
|
+
os.close(fd)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def save_answers(args: argparse.Namespace, path: Path) -> None:
|
|
143
|
+
"""Atomically save the reviewed settings, including only credential references."""
|
|
144
|
+
import tomli_w
|
|
145
|
+
|
|
146
|
+
document: dict[str, object] = {"schema-version": 1}
|
|
147
|
+
tables: dict[str, dict[str, object]] = {}
|
|
148
|
+
for field, attr in FIELDS.items():
|
|
149
|
+
value = getattr(args, attr)
|
|
150
|
+
if value is None or value == []:
|
|
151
|
+
continue
|
|
152
|
+
table, key = field.split(".")
|
|
153
|
+
if attr == "pin_nic":
|
|
154
|
+
value = dict(pin.split("=", 1) for pin in value)
|
|
155
|
+
elif isinstance(value, Path):
|
|
156
|
+
value = str(
|
|
157
|
+
value
|
|
158
|
+
if attr.startswith("root_password") and value == Path("-")
|
|
159
|
+
else value.absolute()
|
|
160
|
+
)
|
|
161
|
+
elif attr == "disk":
|
|
162
|
+
value = [str(Path(disk).absolute()) for disk in value]
|
|
163
|
+
tables.setdefault(table, {})[key] = value
|
|
164
|
+
document.update(tables)
|
|
165
|
+
with _answer_directory(path.parent) as directory:
|
|
166
|
+
destination = directory / path.name
|
|
167
|
+
for item in (
|
|
168
|
+
args.iso,
|
|
169
|
+
args.ssh_key,
|
|
170
|
+
args.root_password_file,
|
|
171
|
+
args.root_password_hash_file,
|
|
172
|
+
):
|
|
173
|
+
if (
|
|
174
|
+
item is not None
|
|
175
|
+
and item != Path("-")
|
|
176
|
+
and item.resolve() == destination.resolve()
|
|
177
|
+
):
|
|
178
|
+
raise ValueError(
|
|
179
|
+
"the output answer file must differ from referenced input files"
|
|
180
|
+
)
|
|
181
|
+
if destination.is_symlink() or (
|
|
182
|
+
destination.exists() and not destination.is_file()
|
|
183
|
+
):
|
|
184
|
+
raise ValueError(f"{path}: choose a regular file for the reviewed answers")
|
|
185
|
+
with NamedTemporaryFile(dir=directory, delete=False) as target:
|
|
186
|
+
temp = Path(target.name)
|
|
187
|
+
try:
|
|
188
|
+
tomli_w.dump(document, target)
|
|
189
|
+
target.flush()
|
|
190
|
+
os.replace(temp, destination)
|
|
191
|
+
finally:
|
|
192
|
+
temp.unlink(missing_ok=True)
|