Skip to content

Commit 9f48e2c

Browse files
samoht9277klaus993JuArce
authored
Explorer ansible (#1550)
Co-authored-by: Klaus Lungwitz <[email protected]> Co-authored-by: Julian Arce <[email protected]>
1 parent d6004dc commit 9f48e2c

File tree

17 files changed

+285
-54
lines changed

17 files changed

+285
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ config-files/*.last_processed_batch.json
1616
nonce_*.bin
1717

1818
infra/ansible/playbooks/ini/**.ini
19+
infra/ansible/playbooks/files/**.pem

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,10 @@ ansible_operator_deploy: ## Deploy the Operator. Parameters: INVENTORY
12681268
-e "ecdsa_keystore_path=$(ECDSA_KEYSTORE)" \
12691269
-e "bls_keystore_path=$(BLS_KEYSTORE)"
12701270

1271+
ansible_explorer_deploy:
1272+
@ansible-playbook infra/ansible/playbooks/explorer.yaml \
1273+
-i $(INVENTORY)
1274+
12711275
ansible_telemetry_create_env:
12721276
@cp -n infra/ansible/playbooks/ini/config-telemetry.ini.example infra/ansible/playbooks/ini/config-telemetry.ini
12731277
@echo "Config files for Telemetry created in infra/ansible/playbooks/ini"

explorer/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ explorer-*.tar
3333

3434
# In case you use Node.js/npm, you want to ignore these.
3535
npm-debug.log
36-
/assets/node_modules/
36+
assets/node_modules/
3737

3838
# Environment Variables
3939
/.env

explorer/lib/explorer/release.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule Explorer.Release do
2+
@moduledoc """
3+
Used for executing DB release tasks when run in production without Mix
4+
installed.
5+
"""
6+
@app :explorer
7+
8+
def migrate do
9+
load_app()
10+
11+
for repo <- repos() do
12+
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
13+
end
14+
end
15+
16+
def rollback(repo, version) do
17+
load_app()
18+
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
19+
end
20+
21+
defp repos do
22+
Application.fetch_env!(@app, :ecto_repos)
23+
end
24+
25+
defp load_app do
26+
Application.load(@app)
27+
end
28+
end

explorer/rel/overlays/bin/migrate

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
cd -P -- "$(dirname -- "$0")"
5+
exec ./explorer eval Explorer.Release.migrate
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
call "%~dp0\explorer" eval Explorer.Release.migrate

explorer/rel/overlays/bin/server

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
cd -P -- "$(dirname -- "$0")"
5+
PHX_SERVER=true exec ./explorer start
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set PHX_SERVER=true
2+
call "%~dp0\explorer" start

infra/ansible/playbooks/elixir.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
deb: "{{ download_libssl.dest }}"
5050
when: libssl_check.rc != 0
5151

52+
########## Install Erlang 26.2.1-1 ##########
5253
- name: Check if Erlang 26.2.1-1 is installed
5354
become: true
5455
ansible.builtin.shell:
@@ -57,6 +58,7 @@
5758
changed_when: false
5859
failed_when: erlang_check.rc not in [0, 1]
5960

61+
6062
- name: Download Erlang 26.2.1-1
6163
become: true
6264
register: download_erlang
Lines changed: 128 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,135 @@
1-
- import_playbook: setup.yaml
2-
- import_playbook: webserver.yaml
3-
#- import_playbook: elixir.yaml # Not working
4-
#- import_playbook: docker.yaml
1+
- name: Run setup playbook
2+
ansible.builtin.import_playbook: setup.yaml
3+
vars:
4+
host: explorer
5+
6+
- name: Run elixir playbook
7+
ansible.builtin.import_playbook: elixir.yaml
8+
vars:
9+
host: explorer
10+
11+
- name: Run nodejs playbook
12+
ansible.builtin.import_playbook: nodejs.yaml
13+
vars:
14+
host: explorer
515

6-
- hosts: aligned-holesky-explorer
7-
become: true
16+
- name: Run postgres playbook
17+
ansible.builtin.import_playbook: postgres.yaml
818
vars:
9-
user: "{{ user }}"
19+
host: explorer
20+
ini_file: ini/config-explorer.ini
21+
22+
- name: Setup Explorer
23+
hosts: explorer
24+
vars:
25+
service: "explorer"
26+
27+
pre_tasks:
28+
- name: Install pnpm
29+
become: true
30+
ansible.builtin.shell:
31+
cmd: npm install -g pnpm
32+
vars:
33+
ansible_ssh_user: "{{ admin_user }}"
34+
35+
- name: Allow all access to tcp port 443
36+
become: true
37+
ufw:
38+
rule: allow
39+
port: 443
40+
proto: tcp
41+
vars:
42+
ansible_ssh_user: "{{ admin_user }}"
43+
44+
- name: Clone the aligned_layer repository
45+
ansible.builtin.git:
46+
repo: https://github.com/yetanotherco/aligned_layer
47+
dest: "/home/{{ ansible_user }}/repos/explorer/aligned_layer"
48+
update: yes
49+
50+
- name: Create .ssl directory
51+
file:
52+
path: /home/{{ ansible_user }}/.ssl/
53+
state: directory
54+
55+
- name: Upload SSL key to server (infra/ansible/playbooks/files/key.pem)
56+
copy:
57+
src: key.pem
58+
dest: /home/{{ ansible_user }}/.ssl/key.pem
59+
60+
- name: Upload SSL certificate to server (infra/ansible/playbooks/files/cert.pem)
61+
copy:
62+
src: cert.pem
63+
dest: /home/{{ ansible_user }}/.ssl/cert.pem
1064

1165
tasks:
12-
# Install required packages
13-
- name: Update apt and install required system packages
14-
apt:
15-
pkg:
16-
- unzip
17-
state: latest
18-
update_cache: true
19-
20-
# Create directories for each service
21-
- name: Create directories for each service if do not exist
22-
ansible.builtin.file:
23-
path: /home/{{ user }}/repos/{{ item }}
66+
- name: Add environment file for Explorer
67+
template:
68+
src: explorer/explorer_env.j2
69+
dest: /home/{{ ansible_user }}/repos/explorer/aligned_layer/explorer/.env
70+
vars:
71+
MIX_ENV: prod
72+
RPC_URL: "{{ lookup('ini', 'RPC_URL file=ini/config-explorer.ini') }}"
73+
ENVIRONMENT: "{{ lookup('ini', 'ENVIRONMENT file=ini/config-explorer.ini') }}"
74+
ALIGNED_CONFIG_FILE: "{{ lookup('ini', 'ALIGNED_CONFIG_FILE file=ini/config-explorer.ini') }}"
75+
PHX_HOST: "{{ lookup('ini', 'PHX_HOST file=ini/config-explorer.ini') }}"
76+
PHX_SERVER: true
77+
ELIXIR_HOSTNAME: "{{ lookup('ini', 'ELIXIR_HOSTNAME file=ini/config-explorer.ini') }}"
78+
DB_NAME: "{{ lookup('ini', 'DB_NAME file=ini/config-explorer.ini') }}"
79+
DB_USER: "{{ lookup('ini', 'DB_USER file=ini/config-explorer.ini') }}"
80+
DB_PASS: "{{ lookup('ini', 'DB_PASS file=ini/config-explorer.ini') }}"
81+
DB_HOST: "{{ lookup('ini', 'DB_HOST file=ini/config-explorer.ini') }}"
82+
TRACKER_API_URL: "{{ lookup('ini', 'TRACKER_API_URL file=ini/config-explorer.ini') }}"
83+
SECRET_KEY_BASE: "{{ lookup('ini', 'SECRET_KEY_BASE file=ini/config-explorer.ini') }}"
84+
KEYFILE_PATH: "{{ lookup('ini', 'KEYFILE_PATH file=ini/config-explorer.ini') }}"
85+
CERTFILE_PATH: "{{ lookup('ini', 'CERTFILE_PATH file=ini/config-explorer.ini') }}"
86+
BATCH_TTL_MINUTES: "{{ lookup('ini', 'BATCH_TTL_MINUTES file=ini/config-explorer.ini') }}"
87+
SCHEDULED_BATCH_INTERVAL_MINUTES: "{{ lookup('ini', 'SCHEDULED_BATCH_INTERVAL_MINUTES file=ini/config-explorer.ini') }}"
88+
LATEST_RELEASE: "{{ lookup('ini', 'LATEST_RELEASE file=ini/config-explorer.ini') }}"
89+
90+
- name: Build the explorer release
91+
args:
92+
chdir: "/home/{{ ansible_user }}/repos/explorer/aligned_layer/explorer"
93+
environment:
94+
MIX_ENV: prod
95+
shell:
96+
executable: /bin/bash
97+
cmd: |
98+
set -ex
99+
source .env
100+
mix local.hex --force
101+
mix local.rebar --force
102+
mix deps.get --only $MIX_ENV
103+
mix compile
104+
pnpm --prefix=assets/ install
105+
mix phx.digest
106+
mix assets.deploy
107+
mix release --overwrite
108+
mix ecto.migrate
109+
110+
- name: Set CAP_NET_BIND_SERVICE to beam
111+
shell:
112+
cmd: sudo setcap CAP_NET_BIND_SERVICE=+eip /home/app/repos/explorer/aligned_layer/explorer/_build/prod/rel/explorer/erts-14.2.1/bin/beam.smp
113+
vars:
114+
ansible_ssh_user: "{{ admin_user }}"
115+
116+
- name: Create .env for Explorer systemd service
117+
shell: cat /home/{{ ansible_user }}/repos/explorer/aligned_layer/explorer/.env | sed 's/export //g' > /home/{{ ansible_user }}/config/.env.explorer
118+
119+
- name: Create systemd services directory
120+
file:
121+
path: "/home/{{ ansible_user }}/.config/systemd/user/"
24122
state: directory
25-
mode: '0755'
26-
become_user: "{{ user }}"
27-
loop:
28-
- explorer
29123

30-
# Clone Aligned repository for each service
31-
- name: Clone Aligned repository
32-
ansible.builtin.git:
33-
repo: https://github.com/yetanotherco/aligned_layer.git
34-
dest: /home/{{ user }}/repos/{{ item }}/aligned_layer
35-
version: v0.10.2
36-
become_user: "{{ user }}"
37-
loop:
38-
- explorer
39-
register: repo_clone
40-
failed_when:
41-
- repo_clone.failed
42-
- not 'Local modifications exist in the destination' in repo_clone.msg
124+
- name: Add service to systemd
125+
template:
126+
src: services/explorer.service.j2
127+
dest: "/home/{{ ansible_user }}/.config/systemd/user/explorer.service"
128+
force: no
43129

130+
- name: Start explorer service
131+
systemd_service:
132+
name: explorer
133+
state: started
134+
enabled: true
135+
scope: user

0 commit comments

Comments
 (0)