summaryrefslogtreecommitdiff
path: root/roles/radicle_node/tasks/main.yml
blob: 9741aab771bcb43584431f58ac4e1c6892af9cd4 (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
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
- name: "check radicle_node_version"
  shell: |
    [ "{{ radicle_node_version }}" = "1" ] || \
      (echo "Unexpected version {{ radicle_node_version }}" 1>&2; exit 1)

- name: "check that radicle_node_key is set"
  shell: |
    echo radicle_node_key Ansible variable is not set
    exit 1
  when: radicle_node_key is not defined

- name: "check that radicle_node_key_pub is set"
  shell: |
    echo radicle_node_key_pub Ansible variable is not set
    exit 1
  when: radicle_node_key_pub is not defined

- name: "install important additional packages for Radicle"
  apt:
    name:
      # For the Radicle installer
      - curl

      # Radicle is built on git.
      - git

      # Rsync for backups.
      - rsync

- name: "stop Radicle node if running"
  shell: |
    systemctl stop radicle-node || true

- name: "create directory for Radicle"
  file:
    state: directory
    path: /home/_rad/.radicle
    owner: _rad
    group: _rad
    mode: 0755

- name: "create directory for Radicle backup"
  file:
    state: directory
    path: radicle-backup
    owner: root
    group: root
    mode: 0755

- name: "restore .radicle directory from backup (step 1 or 2)"
  when: radicle_node_backup is defined
  synchronize:
    src: "{{ radicle_node_backup }}/."
    dest: radicle-backup/.
    group: no
    owner: no

- name: "restore .radicle directory from backup (step 2 or 2)"
  when: radicle_node_backup is defined
  shell: |
    rm radicle-backup/node/control.sock
    rsync -a --del radicle-backup/. /home/_rad/.radicle/.
    chown -R _rad:_rad /home/_rad/.radicle/.

- name: "create directory for Radicle keys"
  file:
    state: directory
    path: /home/_rad/.radicle/keys
    owner: _rad
    group: _rad
    mode: 0755

- name: "install Radicle private key"
  copy:
    content: "{{ radicle_node_key }}"
    dest: /home/_rad/.radicle/keys/radicle
    owner: _rad
    group: _rad
    mode: 0600

- name: "install Radicle public key"
  copy:
    content: "{{ radicle_node_key_pub }}"
    dest: /home/_rad/.radicle/keys/radicle.pub
    owner: _rad
    group: _rad
    mode: 0644

- name: "install of upgrade Radicle using installer"
  shell: |
    # Can't use "set -o pipefail" here, because shell may not be
    # bash. So we don't use a pipe from curl to bash, and download
    # as one command and run script as a second command. If the
    # download fails, the task fails.

    curl -sSf https://radicle.xyz/install > radicle-install
    install -m 0644 -o _rad -g _rad radicle-install /home/_rad/radicle-install
    sudo -u _rad -i bash ./radicle-install

- name: "install systemd unit for Radicle node"
  copy:
    content: |
      [Unit]
      After=syslog.target network.target
      Description=Radicle Node

      [Service]
      Type=simple
      ExecStart=/home/_rad/.radicle/bin/radicle-node --listen 0.0.0.0:8776
      Environment=RAD_HOME=/home/_rad/.radicle
      KillMode=process
      Restart=on-failure
      RestartSec=1
      User=_rad
      Group=_rad

      [Install]
      WantedBy=default.target
    dest: /lib/systemd/system/radicle-node.service

# # Ansible does not seem to always actually start the unit, so do
# # it manually. This seems to only happen on the first run on a
# # freshly created host.
# - name: "actually start node"
#   shell: |
#     systemctl restart radicle-node
#     if [ "$(systemctl is-active radicle-node)" != active ]; then
#         systemctl status radicle-node
#         false    
#     fi

- name: "install script to add nodes to connect to the Radicle config file"
  when: radicle_node_connections is defined
  copy:
    content: |
      #!/usr/bin/python3

      import json, os, subprocess, sys

      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())
      nodes = config["node"]["connect"]
      for new in sys.argv[1:]:
          if new not in nodes:
              nodes.append(new)

      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
    owner: _rad
    group: _rad
    mode: 0755

- name: "connect to other Radicle nodes"
  when: radicle_node_connections is defined
  with_items: "{{ radicle_node_connections }}"
  shell: |
    cat <<'EOF' > /tmp/connect.sh
    export PATH="$HOME/.radicle/bin:$PATH"
    ./radicle-perma-connect "{{ item.nid }}@{{ item.host }}:{{ item.port }}"
    EOF
    sudo -u _rad -i bash -ex /tmp/connect.sh

- name: "(re)start systemd unit for Radicle node"
  systemd:
    name: radicle-node
    state: restarted
    masked: no
    enabled: yes
    daemon_reload: yes

- name: "seed Radicle repositories"
  when: radicle_node_repositories is defined
  with_items: "{{ radicle_node_repositories }}"
  shell: |
    cat <<'EOF' > seed.sh
    export PATH="$HOME/.radicle/bin:$PATH"
    rad node status
    rad seed "{{ item.rid }}"
    EOF
    sudo -u _rad bash -ex seed.sh