summaryrefslogtreecommitdiff
path: root/roles/radicle_node/files/rad-config-update
blob: 7737dea541ce0eaa19a84ad08bc32fa33030bc65 (plain)
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
#!/usr/bin/python3

import json, os, subprocess, sys

alias = sys.argv[1]
ext = sys.argv[2]
peer = sys.argv[3]

p = subprocess.run(["rad", "config", "show"], check=True, capture_output=True)
if p.returncode != 0:
    sys.exit("rad config show failed")
config = json.loads(p.stdout.decode())

config["node"]["alias"] = alias
config["node"]["externalAddresses"] = [ext]
config["node"]["policy"] = "allow"

nodes = config["node"]["connect"]
if peer not in nodes:
    nodes.append(peer)

p = subprocess.run(["rad", "self", "--home"], check=True, capture_output=True)
if p.returncode != 0:
    sys.exit("rad self --home failed")

home = p.stdout.decode().strip()
filename = os.path.join(home, "config.json")
if os.path.exists(filename):
    os.rename(filename, filename + ".bak")
with open(filename, "w") as f:
    f.write(json.dumps(config, indent=4))