From a961cb524c540c6e5afa9e629bad543d9e8ed313 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 13 Feb 2024 19:16:19 +0200 Subject: refactor(radicle_node): move things into files/templates For easier editing. Signed-off-by: Lars Wirzenius Sponsored-by: author --- roles/radicle_node/files/rad-config-update | 31 ++++++++++ roles/radicle_node/tasks/main.yml | 68 ++-------------------- .../radicle_node/templates/radicle-node.service.j2 | 16 +++++ 3 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 roles/radicle_node/files/rad-config-update create mode 100644 roles/radicle_node/templates/radicle-node.service.j2 diff --git a/roles/radicle_node/files/rad-config-update b/roles/radicle_node/files/rad-config-update new file mode 100644 index 0000000..7737dea --- /dev/null +++ b/roles/radicle_node/files/rad-config-update @@ -0,0 +1,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)) diff --git a/roles/radicle_node/tasks/main.yml b/roles/radicle_node/tasks/main.yml index a86a67a..13c49eb 100644 --- a/roles/radicle_node/tasks/main.yml +++ b/roles/radicle_node/tasks/main.yml @@ -95,62 +95,15 @@ mode: 0644 - name: "install systemd unit for Radicle node" - copy: - content: | - [Unit] - Description=Radicle Node - After=network.target network-online.target - Requires=network-online.target - - [Service] - User=_rad - Group=_rad - ExecStart=/usr/bin/radicle-node --listen 0.0.0.0:8776 --force - Environment=RAD_HOME=/home/_rad/.radicle RUST_BACKTRACE=1 RUST_LOG=info - KillMode=process - Restart=always - RestartSec=3 - - [Install] - WantedBy=multi-user.target + template: + src: radicle-node.service.j2 dest: /lib/systemd/system/radicle-node.service - name: "install script to add update Radicle config file" when: radicle_node_connections is defined copy: - content: | - #!/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)) - dest: /home/_rad/radicle-perma-connect + src: rad-config-update + dest: /home/_rad/rad-config-update owner: _rad group: _rad mode: 0755 @@ -159,22 +112,13 @@ when: radicle_node_connections is defined with_items: "{{ radicle_node_connections }}" shell: | - cat <<'EOF' > /home/_rad/connect.sh - export PATH="$HOME/.radicle/bin:$PATH" - ./radicle-perma-connect "{{ radicle_node_domain_name }}" "{{ radicle_node_domain_name }}:8776" "{{ item.nid }}@{{ item.host }}:{{ item.port }}" - EOF - sudo -u _rad -i bash -ex ./connect.sh + sudo -u _rad -i ./rad-config-update "{{ radicle_node_domain_name }}" "{{ radicle_node_domain_name }}:8776" "{{ item.nid }}@{{ item.host }}:{{ item.port }}" - name: "seed Radicle repositories" when: radicle_node_repositories is defined with_items: "{{ radicle_node_repositories }}" shell: | - cat <<'EOF' > /home/_rad/seed.sh - export PATH="$HOME/.radicle/bin:$PATH" - rad node status - rad seed "{{ item.rid }}" - EOF - sudo -u _rad bash -ex /home/_rad/seed.sh + sudo -u _rad rad seed "{{ item.rid }}" - name: "(re)start systemd unit for Radicle node" systemd: diff --git a/roles/radicle_node/templates/radicle-node.service.j2 b/roles/radicle_node/templates/radicle-node.service.j2 new file mode 100644 index 0000000..ae2af8c --- /dev/null +++ b/roles/radicle_node/templates/radicle-node.service.j2 @@ -0,0 +1,16 @@ +[Unit] +Description=Radicle Node +After=network.target network-online.target +Requires=network-online.target + +[Service] +User=_rad +Group=_rad +ExecStart=/usr/bin/radicle-node --listen 0.0.0.0:8776 --force +Environment=RAD_HOME=/home/_rad/.radicle RUST_BACKTRACE=1 RUST_LOG=info +KillMode=process +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target -- cgit v1.2.1