summaryrefslogtreecommitdiff
path: root/roles/radicle_node/tasks/main.yml
blob: 8b07eab33c60024cdf25f6149878186d29b10d92 (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
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
303
304
305
306
307
- 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

      # Web server for the web UI.
      - caddy

      # Radicle components.
      - radicle
      - radicle-ci-broker
      - radicle-native-ci

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

- name: "create directory for Radicle for the _rad user"
  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]
      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
    dest: /lib/systemd/system/radicle-node.service

- 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' > /tmp/seed.sh
    export PATH="$HOME/.radicle/bin:$PATH"
    rad node status
    rad seed "{{ item.rid }}"
    EOF
    sudo -u _rad bash -ex /tmp/seed.sh

- name: "install Caddy configuation file"
  template:
    src: Caddyfile.j2
    dest: /etc/caddy/Caddyfile

- name: "create directory for CI logs"
  file:
    state: directory
    path: /srv/http
    owner: _rad
    group: _rad

- name: "restart Caddy"
  systemd:
    name: caddy
    state: restarted
    masked: no
    enabled: yes
    daemon_reload: yes

- name: "install systemd unit for Radicle HTTPD"
  copy:
    content: |
      [Unit]
      Description=Radicle HTTP Daemon
      After=network.target network-online.target
      Requires=network-online.target

      [Service]
      User=_rad
      Group=_rad
      ExecStart=/usr/bin/radicle-httpd --listen 127.0.0.1:8080
      Environment=RAD_HOME=/home/_rad/.radicle RUST_BACKTRACE=1 RUST_LOG=info
      KillMode=process
      Restart=always
      RestartSec=1

      [Install]
      WantedBy=multi-user.target
    dest: /lib/systemd/system/radicle-httpd.service

- name: "enable systemd unit for Radicle HTTPD"
  systemd:
    name: radicle-httpd
    state: restarted
    masked: no
    enabled: yes
    daemon_reload: yes

- name: "install Radicle CI broker config"
  copy:
    content: |
      default_adapter: native
      adapters:
        native:
          command: /home/bin/radicle-native-ci
          env:
            RADICLE_NATIVE_CI: /home/_rad/native-ci.yaml
      filters:
        - !Or
          - !And
            - !Repository "rad:zZnk3hS8C3WAhnv7mWcCUToCqpBs"
            - !AnyPatch
          - !And
            - !Repository "rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5"
            - !AnyPatch
    dest: /home/_rad/ci-broker.yaml
    owner: _rad
    group: _rad
    mode: 0644

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

- name: "install Radicle native CI config"
  copy:
    content: |
      state: /srv/http
      log: /home/_rad/native-ci.log
    dest: /home/_rad/native-ci.yaml
    owner: _rad
    group: _rad
    mode: 0644

- name: "install systemd unit for Radicle CI broker"
  copy:
    content: |
      [Unit]
      After=radicle-node.service
      Description=Radicle CI broker

      [Service]
      Type=simple
      Environment=RAD_HOME=/home/_rad/.radicle
      Environment=PATH=/bin:/usr/bin:/sbin:/usr/sbin
      ExecStart=bash -c 'ci-broker /home/_rad/ci-broker.yaml >> /srv/http/broker.log'
      KillMode=process
      Restart=always
      RestartSec=1
      User=_rad
      Group=_rad

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

- name: "enable systemd unit for Radicle CI broker"
  systemd:
    name: radicle-ci-broker
    state: restarted
    masked: no
    enabled: yes
    daemon_reload: yes