-
Notifications
You must be signed in to change notification settings - Fork 0
/
update
executable file
·100 lines (74 loc) · 2.73 KB
/
update
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
#!/usr/bin/env python3
from pathlib import Path, PurePath
import subprocess
import re
import ipaddress
import io
def get_container_macs(directory):
results = {}
pattern = re.compile(r"^Network=.*mac=(([a-zA-Z0-9]{2}:){5}[a-zA-Z0-9]{2}).*$")
for file in directory.glob("*.container"):
name = file.stem
with file.open() as f:
for line in f.readlines():
m = pattern.match(line)
if m is None:
continue
item = m.group(1)
if name in results:
raise Exception(f"{name} has multiple items of the same value")
continue
if item in results.values():
raise Exception(f"{item} does already exist in another unit")
results[name] = item
return results
class Target:
def __init__(self, userhost):
self.userhost = userhost
self.tmpdir = PurePath("~/tmp_config")
def rsync(self, source, target, delete=False):
args = ["rsync"]
if delete:
args.append("--delete")
args.extend(
[
"--compress",
"--recursive",
"--times",
"--links",
"--devices",
"--specials",
"--executability",
f"{source}",
f"{self.userhost}:{target}",
],
)
subprocess.run(args, check=True)
def write_file(self, source, target):
target = self.tmpdir / target
subprocess.run(["ssh", self.userhost, "mkdir", "-p", target.parent], check=True)
p = subprocess.Popen(
["ssh", self.userhost, f"cat > {target}"], stdin=subprocess.PIPE
)
p.communicate(input=source.encode())
if p.returncode != 0:
raise Exception("failed to write file")
def run_script(self, file):
with open("script.sh", "rb") as f:
script = f.read()
p = subprocess.run(["ssh", self.userhost, script], check=True)
def main():
target = Target("[email protected]")
source_dir = Path("./config").resolve()
source_dir_private = Path("./config-private").resolve()
container_macs = get_container_macs(source_dir / "etc/containers/systemd")
target.rsync(f"{source_dir}/", f"{target.tmpdir}/", delete=True)
target.rsync(f"{source_dir_private}/", f"{target.tmpdir}/")
nft_vars = io.StringIO()
for name, mac in container_macs.items():
nft_vars.write(f"define mac_{name} = {mac}\n")
target.write_file(nft_vars.getvalue(), "usr/local/share/nftables/vars.conf")
target.run_script("script.sh")
print("successful")
if __name__ == "__main__":
main()