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
301
302
|
"""Prepare inactive NetworkManager bond profiles and local recovery commands."""
import configparser
import io
import json
import os
import shlex
import shutil
import subprocess
import sys
import uuid
from pathlib import Path
STATE = Path("/var/lib/dotfiles/bond0")
BOND_UUID = "88919b61-3a22-4cb6-9485-440725990cea"
def nmcli(*args, input=None):
return subprocess.check_output(["nmcli", *args], input=input, text=True).strip()
def keyfile(text):
parser = configparser.ConfigParser(interpolation=None, strict=True)
parser.optionxform = lambda optionstr: optionstr
parser.read_string(text)
return parser
def bond_profile():
return nmcli(
"--offline",
"connection",
"add",
"type",
"bond",
"ifname",
"bond0",
"con-name",
"dotfiles-bond0",
"connection.uuid",
BOND_UUID,
"connection.autoconnect",
"no",
"connection.autoconnect-ports",
"1",
"bond.options",
"mode=active-backup,miimon=1000,fail_over_mac=active,primary_reselect=always",
"ipv4.method",
"auto",
"ipv4.dhcp-client-id",
"duid",
"ipv4.dhcp-iaid",
"1",
"ipv6.method",
"auto",
"ipv6.dhcp-duid",
"stable-uuid",
"ipv6.dhcp-iaid",
"1",
"connection.stable-id",
"dotfiles-bond0",
)
def port_profile(source, kind, identity, priority):
if kind not in ("wifi", "ethernet", "802-11-wireless", "802-3-ethernet"):
raise ValueError(f"Not an Ethernet or Wi-Fi profile: {kind}")
config = keyfile(source)
if config.get("connection", "secondaries", fallback=""):
raise ValueError(
"A profile with automatic VPN connections needs separate review."
)
for section in ("ipv4", "ipv6", "proxy"):
config.remove_section(section)
data = io.StringIO()
config.write(data, space_around_delimiters=False)
setting = (
"802-11-wireless" if kind in ("wifi", "802-11-wireless") else "802-3-ethernet"
)
return nmcli(
"--offline",
"connection",
"modify",
"connection.id",
f"dotfiles-bond-{identity}",
"connection.uuid",
identity,
"connection.autoconnect",
"no",
"connection.controller",
BOND_UUID,
"connection.port-type",
"bond",
"connection.secondaries",
"",
"bond-port.prio",
str(priority),
f"{setting}.cloned-mac-address",
"permanent",
input=data.getvalue(),
)
def profile_source(identity):
for directory in ("/etc", "/run", "/usr/lib"):
for path in Path(directory, "NetworkManager/system-connections").glob("*"):
if path.is_file():
text = path.read_text()
if keyfile(text).get("connection", "uuid", fallback="") == identity:
return text
raise ValueError(f"No saved NetworkManager keyfile for {identity}")
def prepare_port(name):
identity = nmcli("-g", "connection.uuid", "connection", "show", name)
source = profile_source(identity)
config = keyfile(source)
kind = config["connection"]["type"]
interface = config.get("connection", "interface-name", fallback="") or nmcli(
"-g", "GENERAL.DEVICES", "con", "show", "uuid", identity
)
if "/" in interface or not Path("/sys/class/net", interface, "device").exists():
raise ValueError(f"Profile needs one physical interface present: {name}")
config["connection"]["interface-name"] = interface
if config.get("connection", "master", fallback="") or config.get(
"connection", "controller", fallback=""
):
raise ValueError(f"Profile already belongs to a controller: {name}")
wireless = kind in ("wifi", "802-11-wireless")
if wireless and not config.get("wifi-security", "psk", fallback=""):
raise ValueError(
"Wi-Fi needs a system-saved PSK for unattended failover; no profile was changed."
)
clone = str(uuid.uuid4())
data = io.StringIO()
config.write(data, space_around_delimiters=False)
return {
"original": identity,
"autoconnect": nmcli(
"-g", "connection.autoconnect", "con", "show", "uuid", identity
),
"uuid": clone,
"interface": interface,
"profile": port_profile(data.getvalue(), kind, clone, 0 if wireless else 100),
}
def shell_command(*args):
return shlex.join(str(arg) for arg in args)
def recovery_commands(ports):
commands = ["#!/bin/sh", "set -u"]
commands.append("systemctl stop dotfiles-bond-rollback.timer || true")
for identity in [BOND_UUID, *(port["uuid"] for port in ports)]:
commands.append(
shell_command(
"nmcli", "con", "mod", "uuid", identity, "connection.autoconnect", "no"
)
+ " || true"
)
for identity in [BOND_UUID, *(port["uuid"] for port in ports)]:
commands.append(
shell_command("nmcli", "con", "delete", "uuid", identity) + " || true"
)
commands.append(
shell_command(
"rm",
"-f",
f"/etc/NetworkManager/system-connections/dotfiles-{identity}.nmconnection",
)
)
for port in ports:
commands.append(
shell_command(
"nmcli",
"con",
"mod",
"uuid",
port["original"],
"connection.autoconnect",
port["autoconnect"],
)
)
active = set(nmcli("-g", "UUID", "con", "show", "--active").splitlines())
for port in ports:
if port["original"] in active:
commands.append(
shell_command(
"nmcli", "--wait", "0", "con", "up", "uuid", port["original"]
)
)
return "\n".join(commands) + "\n"
def activation_commands(ports, active=()):
commands = ["#!/bin/sh", "set -eu"]
commands.append(
shell_command(
"systemd-run",
"--collect",
"--unit=dotfiles-bond-rollback",
"--on-active=5m",
"/bin/sh",
STATE / "rollback.sh",
)
)
commands.append("trap 'sh /var/lib/dotfiles/bond0/rollback.sh' EXIT")
commands.append(
"install -m 600 /var/lib/dotfiles/bond0/*.nmconnection /etc/NetworkManager/system-connections/"
)
for identity in [BOND_UUID, *(port["uuid"] for port in ports)]:
commands.append(
shell_command(
"nmcli",
"con",
"load",
f"/etc/NetworkManager/system-connections/dotfiles-{identity}.nmconnection",
)
)
for port in ports:
commands.append(
shell_command(
"nmcli",
"con",
"mod",
"uuid",
port["original"],
"connection.autoconnect",
"no",
)
)
if port["original"] in active:
commands.append(
shell_command("nmcli", "con", "down", "uuid", port["original"])
)
for identity in [*(port["uuid"] for port in ports), BOND_UUID]:
commands.append(
shell_command(
"nmcli", "con", "mod", "uuid", identity, "connection.autoconnect", "yes"
)
)
commands.append(
shell_command("nmcli", "--wait", "0", "con", "up", "uuid", BOND_UUID)
)
commands.append("trap - EXIT")
commands.append(
"echo 'Rollback is due in five minutes. Test locally, then run just canonical-bond-keep.'"
)
return "\n".join(commands) + "\n"
def prepare(names):
if os.geteuid() != 0:
raise ValueError("Run preparation through the sudo recipe.")
if STATE.exists():
raise ValueError(
f"Preparation already exists at {STATE}; do not overwrite recovery data."
)
if (
BOND_UUID in nmcli("-g", "UUID", "con", "show").splitlines()
or Path("/sys/class/net/bond0").exists()
):
raise ValueError("A bond already exists; stop before replacing it.")
ports = [prepare_port(name) for name in names]
active = set(nmcli("-g", "UUID", "con", "show", "--active").splitlines())
if len({port["original"] for port in ports}) != len(ports):
raise ValueError("Each source profile must be unique.")
if len({port["interface"] for port in ports}) != len(ports):
raise ValueError("Select only one profile per physical interface.")
profiles = {BOND_UUID: bond_profile(), **{p["uuid"]: p["profile"] for p in ports}}
scripts = {
"activate.sh": activation_commands(ports, active),
"rollback.sh": recovery_commands(ports),
}
os.umask(0o077)
STATE.mkdir(parents=True, mode=0o700)
try:
for identity, text in profiles.items():
(STATE / f"dotfiles-{identity}.nmconnection").write_text(text + "\n")
for name, text in scripts.items():
(STATE / name).write_text(text)
(STATE / "profiles.json").write_text(
json.dumps(
[{k: v for k, v in p.items() if k != "profile"} for p in ports],
indent=2,
)
+ "\n"
)
except Exception:
shutil.rmtree(STATE)
raise
print(f"Prepared inactive profiles in {STATE}. No live connection was changed.")
if __name__ == "__main__":
if not sys.argv[1:]:
sys.exit("Supply the Ethernet and Wi-Fi connection profile names.")
try:
prepare(sys.argv[1:])
except (ValueError, subprocess.CalledProcessError) as error:
sys.exit(str(error))
|