From da597ca3eef67afc0e158c7aa07ff297a062d762 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Thu, 26 Sep 2024 18:40:51 +0200 Subject: [PATCH 1/9] besu updates --- .../roles/ethereum_besu/defaults/main.yml | 6 + .../ethereum_besu/tasks/commands/install.yml | 72 +++++++++ .../ethereum_besu/tasks/commands/reset.yml | 7 + .../ethereum_besu/tasks/commands/restart.yml | 7 + .../ethereum_besu/tasks/commands/start.yml | 31 ++++ .../ethereum_besu/tasks/commands/stop.yml | 21 +++ .../tasks/commands/uninstall.yml | 16 ++ .../roles/ethereum_besu/tasks/main.yml | 8 + .../roles/ethereum_besu/tasks/metrics.yml | 63 ++++++++ .../ethereum_besu/templates/collector.py.j2 | 143 ++++++++++++++++++ .../roles/ethereum_besu/templates/config | 8 + .../templates/metrics.service.j2 | 18 +++ .../templates/systemd.service.j2 | 16 ++ 13 files changed, 416 insertions(+) create mode 100644 depin/services/roles/ethereum_besu/defaults/main.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/commands/install.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/commands/reset.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/commands/restart.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/commands/start.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/commands/stop.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/main.yml create mode 100644 depin/services/roles/ethereum_besu/tasks/metrics.yml create mode 100644 depin/services/roles/ethereum_besu/templates/collector.py.j2 create mode 100644 depin/services/roles/ethereum_besu/templates/config create mode 100644 depin/services/roles/ethereum_besu/templates/metrics.service.j2 create mode 100644 depin/services/roles/ethereum_besu/templates/systemd.service.j2 diff --git a/depin/services/roles/ethereum_besu/defaults/main.yml b/depin/services/roles/ethereum_besu/defaults/main.yml new file mode 100644 index 00000000..fa51a4ff --- /dev/null +++ b/depin/services/roles/ethereum_besu/defaults/main.yml @@ -0,0 +1,6 @@ +--- + +ethereum_besu_cmd: "{{ depin_cmd | default('uninstall') }}" +ethereum_besu_version: 24.9.1 +ethereum_besu_metrics_port: 33006 +ethereum_besu_download_url: "https://github.com/hyperledger/besu/releases/download/24.9.1/besu-24.9.1.zip" diff --git a/depin/services/roles/ethereum_besu/tasks/commands/install.yml b/depin/services/roles/ethereum_besu/tasks/commands/install.yml new file mode 100644 index 00000000..8408241b --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/commands/install.yml @@ -0,0 +1,72 @@ +--- +- name: Install dependencies + become: true + ansible.builtin.apt: + name: + - openjdk-21-jdk + state: present + update_cache: true + +- name: Create program folder + become: true + ansible.builtin.file: + state: directory + owner: root + group: root + mode: '0755' + dest: /opt/ethereum_besu/{{ ethereum_besu_version }} + + +- name: Download binary to tmp + become: true + ansible.builtin.unarchive: + remote_src: true + src: "{{ ethereum_besu_download_url }}" + dest: /tmp + failed_when: false + +- name: Find binary + become: true + ansible.builtin.find: + paths: + - /tmp + - "{{ ethereum_besu_download_path | default('/tmp') }}" + patterns: 'besu$' + use_regex: true + get_checksum: true + recurse: true + register: _download + +- name: Install node + when: _download['files'] | length > 0 + become: true + vars: + _files: | + {{ + _download['files'] | + selectattr('checksum', 'equalto', ethereum_besu_checksum | default('')) + }} + block: + - name: Copy required files + ansible.builtin.copy: + remote_src: true + src: /tmp/besu-{{ ethereum_besu_version }}/ + dest: /opt/ethereum_besu/{{ ethereum_besu_version }} + mode: '0755' + + - name: Copy config file + become: true + ansible.builtin.copy: + src: templates/config + dest: /opt/ethereum_besu/{{ ethereum_besu_version }} + mode: '0755' + + - name: Create systemd file + ansible.builtin.template: + src: templates/systemd.service.j2 + dest: /etc/systemd/system/ethereum_besu.service + mode: '0644' + +- name: Start node + ansible.builtin.include_tasks: + file: commands/start.yml \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/commands/reset.yml b/depin/services/roles/ethereum_besu/tasks/commands/reset.yml new file mode 100644 index 00000000..d967402b --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/commands/reset.yml @@ -0,0 +1,7 @@ +--- +- name: Uninstall + ansible.builtin.include_tasks: commands/uninstall.yml + +- name: Install + ansible.builtin.include_tasks: commands/install.yml + diff --git a/depin/services/roles/ethereum_besu/tasks/commands/restart.yml b/depin/services/roles/ethereum_besu/tasks/commands/restart.yml new file mode 100644 index 00000000..8705cfba --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/commands/restart.yml @@ -0,0 +1,7 @@ +--- +## stop and start service +- name: Stop + ansible.builtin.include_tasks: commands/stop.yml + +- name: Start + ansible.builtin.include_tasks: commands/start.yml diff --git a/depin/services/roles/ethereum_besu/tasks/commands/start.yml b/depin/services/roles/ethereum_besu/tasks/commands/start.yml new file mode 100644 index 00000000..5b548287 --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/commands/start.yml @@ -0,0 +1,31 @@ +--- + # Run the ethereum_besu +- name: Start node + become: true + ansible.builtin.service: + name: ethereum_besu + enabled: true + daemon_reload: true + state: started + +- name: Assert + when: "'molecule' in groups" + block: + - name: Get service info + become: true + ansible.builtin.systemd: + name: "ethereum_besu" + register: _ethereum_besu + + - debug: + var: _ethereum_besu.status.ActiveState + + - name: Check service + ansible.builtin.assert: + that: + - _ethereum_besu['status']['ActiveState']=='active' + quiet: true + + - name: Set pid + ansible.builtin.set_fact: + _presearch_pid: "{{ _ethereum_besu['status']['ExecMainPID'] }}" \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml b/depin/services/roles/ethereum_besu/tasks/commands/stop.yml new file mode 100644 index 00000000..9c821aa5 --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/commands/stop.yml @@ -0,0 +1,21 @@ +--- +# Stop ethereum_besu +- name: Stop ethereum_besu Sentry Node + ansible.builtin.systemd: + name: ethereum_besu + state: stopped + +- name: Assert + when: "'molecule' in groups" + block: + - name: Get service info + become: true + ansible.builtin.systemd: + name: "ethereum_besu" + register: _ethereum_besu + + - name: Check service + ansible.builtin.assert: + that: + - _ethereum_besu['status']['ActiveState']=='inactive' + quiet: true \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml new file mode 100644 index 00000000..9903808a --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml @@ -0,0 +1,16 @@ +--- +# Stop the service before uninstalling +- name: Stop + ansible.builtin.include_tasks: commands/stop.yml + +# Remove the binary file for ethereum_besu Sentry Node +- name: Remove systemd service for ethereum_besu Sentry + file: + path: /opt/ethereum_besu + state: absent + +# Remove the systemd service for ethereum_besu Sentry Node +- name: Remove systemd service for ethereum_besu Sentry + file: + path: /etc/systemd/system/ethereum_besu.service + state: absent diff --git a/depin/services/roles/ethereum_besu/tasks/main.yml b/depin/services/roles/ethereum_besu/tasks/main.yml new file mode 100644 index 00000000..ccaa37a1 --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/main.yml @@ -0,0 +1,8 @@ +--- +- name: "ethereum_besu {{ ethereum_besu_cmd }}" + ansible.builtin.include_tasks: + file: "commands/{{ ethereum_besu_cmd }}.yml" + +- name: Ensure metrics is installed + ansible.builtin.include_tasks: + file: metrics.yml \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/metrics.yml b/depin/services/roles/ethereum_besu/tasks/metrics.yml new file mode 100644 index 00000000..7a63e8eb --- /dev/null +++ b/depin/services/roles/ethereum_besu/tasks/metrics.yml @@ -0,0 +1,63 @@ +--- +- name: Install metrics + vars: + _name: "{{ role_name | replace('_', '-') }}" + when: vars[role_name + '_cmd'] != 'uninstall' + become: true + block: + - name: Ensure directory exists + ansible.builtin.file: + path: /etc/deeep-network/collectors + state: directory + mode: '0755' + recurse: true + + - name: Install script + ansible.builtin.template: + src: templates/collector.py.j2 + dest: /etc/deeep-network/collectors/{{ _name }}.py + mode: '0744' + + - name: Install service + ansible.builtin.template: + src: templates/metrics.service.j2 + dest: /etc/systemd/system/collector-{{ _name }}.service + mode: '0644' + + + - name: Update prometheus.conf + vars: + _block: | + - name: {{ _name }} + url: 'http://127.0.0.1:{{ vars[role_name + '_metrics_port'] }}/metrics' + update_every: 900 + ansible.builtin.blockinfile: + path: /etc/netdata/go.d/prometheus.conf + append_newline: true + block: | + {{ _block | indent(4, first=true) }} + + - name: Start service + ansible.builtin.systemd_service: + name: collector-{{ _name }} + enabled: true + daemon_reload: true + state: started + +- name: Uninstall metrics + when: vars[role_name + '_cmd'] == 'uninstall' + become: true + block: + - name: Stop service + ansible.builtin.systemd_service: + name: "{{ _name }}" + enabled: false + state: stopped + + - name: Remove files + ansible.builtin.file: + path: "{{ item }}" + state: absent + loop: + - /etc/systemd/system/collector-{{ _name }}.service + - /etc/deeep-network/collectors/{{ _name }}.py \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/templates/collector.py.j2 b/depin/services/roles/ethereum_besu/templates/collector.py.j2 new file mode 100644 index 00000000..f96315a9 --- /dev/null +++ b/depin/services/roles/ethereum_besu/templates/collector.py.j2 @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 +import os +import socket +import time +import os +import time +import subprocess +from subprocess import check_output +import json + + +from prometheus_client import start_http_server, REGISTRY, GC_COLLECTOR, PLATFORM_COLLECTOR, PROCESS_COLLECTOR +from prometheus_client.core import GaugeMetricFamily +from prometheus_client.registry import Collector + +try: + from utils_lxd import lxd_get +except ImportError as imp_exc: + LXD_UTILS_IMPORT_ERROR = imp_exc +else: + LXD_UTILS_IMPORT_ERROR = None + +class MetricsCollector(Collector): + """Collector for Gala Node information""" + def total_uptime_count(sefl, besu_service_date): + total_uptime=0 + for x in besu_service_date: + state_uptime_list = besu_service_date[x].split(":") + hours=0 + if len(state_uptime_list) <= 2: + minutes = int(state_uptime_list[0]) + seconds = int(state_uptime_list[1]) + else: + hours = int(state_uptime_list[0]) + minutes = int(state_uptime_list[1]) + seconds = int(state_uptime_list[2]) + total_uptime=total_uptime+seconds+minutes*60+hours*3600 + return total_uptime + + def collect(self): + result = [] + service_metrics_collected = 1 + + service_name='java' + try: + pid_id= str(check_output(["pidof",service_name]).decode("utf-8")).replace("\n","") + print("PID of process: " + pid_id) + state = 1 + proc = subprocess.Popen( + ["ps", "-eo", "pid,comm,lstart,etime,time"], stdout=subprocess.PIPE + ) + proc.wait() + services_pids = proc.stdout.readlines() + + try: + f = open("total_uptime.txt", "r") + besu_service_date = json.loads(f.read().replace("'", '"')) + os.remove("total_uptime.txt") + except: + print("No file") + besu_service_date={} + + for pid_decription in services_pids: + pid_decription = pid_decription.decode("utf-8") + p = pid_decription.split()[0] + if p == pid_id: + print(pid_decription) + mounth = pid_decription.split()[3] + day = pid_decription.split()[4] + time_str = pid_decription.split()[5] + years = pid_decription.split()[6] + run_uptime = pid_decription.split()[7] + state_uptime_dic=run_uptime.split(":") + if len(state_uptime_dic)<=2: + hours=0 + minutes = int(state_uptime_dic[0]) + seconds = int(state_uptime_dic[1]) + else: + hours = int(state_uptime_dic[0]) + minutes = int(state_uptime_dic[1]) + seconds = int(state_uptime_dic[2]) + state_uptime=seconds+minutes*60+hours*3600 + date_str = mounth + "-" + day + "-" + years + " " + time_str + besu_service_date[date_str] = run_uptime + total_uptime=self.total_uptime_count(besu_service_date) + f = open("total_uptime.txt", "w") + f.write(str(besu_service_date)) + f.close() + + except: + + print("Service: "+ service_name + " wasn't found") + state = 0 + state_uptime =0 + try: + if os.path.isfile('./total_uptime.txt'): + f = open("total_uptime.txt", "r") + besu_service_date = json.loads(f.read().replace("'", '"')) + total_uptime=self.total_uptime_count(besu_service_date) + print(total_uptime) + else: + print("No file") + total_uptime=0 + except: + print("The service has never been launched") + total_uptime=0 + service_metrics_collected = 0 + besu_service_date + + + #Mark for tests + device = 'molecule' + service = '{{ _name }}' + hostname = socket.gethostname() + _labels = ['device', 'instance', 'service'] + _label_values = [device, hostname, service] + + g1 = GaugeMetricFamily('state','current state', labels=_labels) + g1.add_metric(_label_values, value=state) + + g2 = GaugeMetricFamily('current_state_uptime','seconds in current state', labels=_labels) + g2.add_metric(_label_values, value=state_uptime) + + g3 = GaugeMetricFamily('total_uptime','seconds in last 24 hours', labels=_labels) + g3.add_metric(_label_values, value=total_uptime) + + result.extend([g1, g2, g3]) + + success = GaugeMetricFamily('service_metrics_collected','service metrics collected successfully', labels=_labels) + success.add_metric(_label_values, value=service_metrics_collected) + + result.extend([success]) + return result + +if __name__ == "__main__": + REGISTRY.unregister(GC_COLLECTOR) + REGISTRY.unregister(PLATFORM_COLLECTOR) + REGISTRY.unregister(PROCESS_COLLECTOR) + REGISTRY.register(MetricsCollector()) + + start_http_server({{ vars[role_name + '_metrics_port'] | int }}) + while True: + time.sleep(30) \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/templates/config b/depin/services/roles/ethereum_besu/templates/config new file mode 100644 index 00000000..02d270ab --- /dev/null +++ b/depin/services/roles/ethereum_besu/templates/config @@ -0,0 +1,8 @@ +network="dev" +miner-enabled=true +miner-coinbase="0xfe3b557e8fb62b89f4916b721be55ceb828dbd73" +rpc-http-cors-origins=["all"] +host-allowlist=["*"] +rpc-ws-enabled=true +rpc-http-enabled=true +data-path="/tmp/tmpdata-path" \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/templates/metrics.service.j2 b/depin/services/roles/ethereum_besu/templates/metrics.service.j2 new file mode 100644 index 00000000..14757e65 --- /dev/null +++ b/depin/services/roles/ethereum_besu/templates/metrics.service.j2 @@ -0,0 +1,18 @@ +{{ ansible_managed | comment }} + +[Unit] +Description=Prometheus collector - {{ _name }} +After=network-online.target + +[Service] +Type=simple +User=nerdnode +ExecStart=sudo /opt/pipx/venvs/ansible-core/bin/python3 /etc/deeep-network/collectors/{{ _name }}.py +KillSignal=SIGINT +Restart=on-failure + +StandardOutput=syslog +StandardError=syslog + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/templates/systemd.service.j2 b/depin/services/roles/ethereum_besu/templates/systemd.service.j2 new file mode 100644 index 00000000..0506164c --- /dev/null +++ b/depin/services/roles/ethereum_besu/templates/systemd.service.j2 @@ -0,0 +1,16 @@ +[Unit] +Description=ethereum_besu service +After=network.target + +[Service] +User=root +Group=root +Type=simple +WorkingDirectory=/opt/ethereum_besu/{{ ethereum_besu_version }} +ExecStart=/opt/ethereum_besu/{{ ethereum_besu_version }}/bin/besu --config-file=/opt/ethereum_besu/{{ ethereum_besu_version }}/config +Restart=on-failure +RestartSec=20s +PIDFile=/var/run/ethereum_besu.pid + +[Install] +WantedBy=multi-user.target \ No newline at end of file From 1c804cd585dc1c19f3ea54146bbb8f03ad9db0e2 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Fri, 27 Sep 2024 15:42:39 +0200 Subject: [PATCH 2/9] Update metric collector --- depin/services/roles/ethereum_besu/defaults/main.yml | 2 +- .../roles/ethereum_besu/tasks/commands/stop.yml | 2 +- .../roles/ethereum_besu/tasks/commands/uninstall.yml | 8 ++++---- .../roles/ethereum_besu/templates/collector.py.j2 | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/depin/services/roles/ethereum_besu/defaults/main.yml b/depin/services/roles/ethereum_besu/defaults/main.yml index fa51a4ff..f5b36c6a 100644 --- a/depin/services/roles/ethereum_besu/defaults/main.yml +++ b/depin/services/roles/ethereum_besu/defaults/main.yml @@ -1,6 +1,6 @@ --- -ethereum_besu_cmd: "{{ depin_cmd | default('uninstall') }}" +ethereum_besu_cmd: "{{ depin_cmd | default('install') }}" ethereum_besu_version: 24.9.1 ethereum_besu_metrics_port: 33006 ethereum_besu_download_url: "https://github.com/hyperledger/besu/releases/download/24.9.1/besu-24.9.1.zip" diff --git a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml b/depin/services/roles/ethereum_besu/tasks/commands/stop.yml index 9c821aa5..2844eff7 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml +++ b/depin/services/roles/ethereum_besu/tasks/commands/stop.yml @@ -1,6 +1,6 @@ --- # Stop ethereum_besu -- name: Stop ethereum_besu Sentry Node +- name: Stop ethereum_besu ansible.builtin.systemd: name: ethereum_besu state: stopped diff --git a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml index 9903808a..9a9b7795 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml +++ b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml @@ -3,14 +3,14 @@ - name: Stop ansible.builtin.include_tasks: commands/stop.yml -# Remove the binary file for ethereum_besu Sentry Node -- name: Remove systemd service for ethereum_besu Sentry +# Remove the binary file for ethereum_besu +- name: Remove systemd service for ethereum_besu file: path: /opt/ethereum_besu state: absent -# Remove the systemd service for ethereum_besu Sentry Node -- name: Remove systemd service for ethereum_besu Sentry +# Remove the systemd service for ethereum_besu +- name: Remove systemd service for ethereum_besu file: path: /etc/systemd/system/ethereum_besu.service state: absent diff --git a/depin/services/roles/ethereum_besu/templates/collector.py.j2 b/depin/services/roles/ethereum_besu/templates/collector.py.j2 index f96315a9..35ec5a7c 100644 --- a/depin/services/roles/ethereum_besu/templates/collector.py.j2 +++ b/depin/services/roles/ethereum_besu/templates/collector.py.j2 @@ -53,9 +53,9 @@ class MetricsCollector(Collector): services_pids = proc.stdout.readlines() try: - f = open("total_uptime.txt", "r") + f = open("ethereum_besu_total_uptime.txt", "r") besu_service_date = json.loads(f.read().replace("'", '"')) - os.remove("total_uptime.txt") + os.remove("ethereum_besu_total_uptime.txt") except: print("No file") besu_service_date={} @@ -83,7 +83,7 @@ class MetricsCollector(Collector): date_str = mounth + "-" + day + "-" + years + " " + time_str besu_service_date[date_str] = run_uptime total_uptime=self.total_uptime_count(besu_service_date) - f = open("total_uptime.txt", "w") + f = open("ethereum_besu_total_uptime.txt", "w") f.write(str(besu_service_date)) f.close() @@ -93,8 +93,8 @@ class MetricsCollector(Collector): state = 0 state_uptime =0 try: - if os.path.isfile('./total_uptime.txt'): - f = open("total_uptime.txt", "r") + if os.path.isfile('./ethereum_besu_total_uptime.txt'): + f = open("ethereum_besu_total_uptime.txt", "r") besu_service_date = json.loads(f.read().replace("'", '"')) total_uptime=self.total_uptime_count(besu_service_date) print(total_uptime) From 5d1b8171ceebd785592497ef9c146e8363f8f1d9 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Thu, 3 Oct 2024 12:54:52 +0200 Subject: [PATCH 3/9] Bug fixes --- depin/services/roles/ethereum_besu/tasks/commands/stop.yml | 5 +++++ .../roles/ethereum_besu/tasks/commands/uninstall.yml | 2 ++ .../roles/ethereum_besu/templates/systemd.service.j2 | 1 + 3 files changed, 8 insertions(+) diff --git a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml b/depin/services/roles/ethereum_besu/tasks/commands/stop.yml index 2844eff7..af10cbe2 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml +++ b/depin/services/roles/ethereum_besu/tasks/commands/stop.yml @@ -1,6 +1,7 @@ --- # Stop ethereum_besu - name: Stop ethereum_besu + become: true ansible.builtin.systemd: name: ethereum_besu state: stopped @@ -14,6 +15,10 @@ name: "ethereum_besu" register: _ethereum_besu + - name: Debug service status + debug: + var: _ethereum_besu.status + - name: Check service ansible.builtin.assert: that: diff --git a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml index 9a9b7795..629ef616 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml +++ b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml @@ -5,12 +5,14 @@ # Remove the binary file for ethereum_besu - name: Remove systemd service for ethereum_besu + become: true file: path: /opt/ethereum_besu state: absent # Remove the systemd service for ethereum_besu - name: Remove systemd service for ethereum_besu + become: true file: path: /etc/systemd/system/ethereum_besu.service state: absent diff --git a/depin/services/roles/ethereum_besu/templates/systemd.service.j2 b/depin/services/roles/ethereum_besu/templates/systemd.service.j2 index 0506164c..2a06d434 100644 --- a/depin/services/roles/ethereum_besu/templates/systemd.service.j2 +++ b/depin/services/roles/ethereum_besu/templates/systemd.service.j2 @@ -8,6 +8,7 @@ Group=root Type=simple WorkingDirectory=/opt/ethereum_besu/{{ ethereum_besu_version }} ExecStart=/opt/ethereum_besu/{{ ethereum_besu_version }}/bin/besu --config-file=/opt/ethereum_besu/{{ ethereum_besu_version }}/config +SuccessExitStatus=143 Restart=on-failure RestartSec=20s PIDFile=/var/run/ethereum_besu.pid From 5b18cdfecb074e3ccc9294b098a9de01fcc64450 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Mon, 7 Oct 2024 11:43:14 +0200 Subject: [PATCH 4/9] Update tests --- .../tasks/commands/uninstall.yml | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml index 629ef616..cdc13286 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml +++ b/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml @@ -11,8 +11,33 @@ state: absent # Remove the systemd service for ethereum_besu -- name: Remove systemd service for ethereum_besu +- name: Delete content & directory become: true - file: - path: /etc/systemd/system/ethereum_besu.service + ansible.builtin.file: state: absent + path: "{{ item }}" + loop: + - /etc/systemd/system/ethereum_besu.service + - /opt/ethereum/{{ ethereum_besu_version }} + loop_control: + loop_var: item + +- name: Verify that the ethereum service file is removed + ansible.builtin.stat: + path: /etc/systemd/system/ethereum.service + register: ethereum_service_file + +- name: Assert that the ethereumGo service file does not exist + ansible.builtin.assert: + that: + - not ethereum_service_file.stat.exists + +- name: Verify that the program directory is removed + ansible.builtin.stat: + path: /opt/ethereum/{{ ethereum_besu_version }} + register: ethereum_directory + +- name: Assert that the program directory does not exist + ansible.builtin.assert: + that: + - not ethereum_directory.stat.exists \ No newline at end of file From 5c71819532d8a2deb50d8830cd07f4e10410f622 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Thu, 10 Oct 2024 10:41:41 +0200 Subject: [PATCH 5/9] Update ethereun with consensus client --- .../services/roles/ethereum/defaults/main.yml | 16 ++ .../roles/ethereum/tasks/commands/install.yml | 162 ++++++++++++++++++ .../tasks/commands/reset.yml | 0 .../tasks/commands/restart.yml | 0 .../tasks/commands/start.yml | 31 +++- .../tasks/commands/stop.yml | 24 ++- .../tasks/commands/uninstall.yml | 6 + .../tasks/main.yml | 6 +- .../tasks/metrics.yml | 0 .../templates/collector.py.j2 | 0 .../ethereum/templates/config_besu.toml.j2 | 10 ++ .../ethereum/templates/config_teku.yaml.j2 | 8 + .../templates/metrics.service.j2 | 0 .../templates/systemd.besu_service.j2} | 2 +- .../templates/systemd.teku_service.j2 | 18 ++ .../roles/ethereum_besu/defaults/main.yml | 6 - .../ethereum_besu/tasks/commands/install.yml | 72 -------- .../roles/ethereum_besu/templates/config | 8 - 18 files changed, 276 insertions(+), 93 deletions(-) create mode 100644 depin/services/roles/ethereum/defaults/main.yml create mode 100644 depin/services/roles/ethereum/tasks/commands/install.yml rename depin/services/roles/{ethereum_besu => ethereum}/tasks/commands/reset.yml (100%) rename depin/services/roles/{ethereum_besu => ethereum}/tasks/commands/restart.yml (100%) rename depin/services/roles/{ethereum_besu => ethereum}/tasks/commands/start.yml (50%) rename depin/services/roles/{ethereum_besu => ethereum}/tasks/commands/stop.yml (52%) rename depin/services/roles/{ethereum_besu => ethereum}/tasks/commands/uninstall.yml (90%) rename depin/services/roles/{ethereum_besu => ethereum}/tasks/main.yml (59%) rename depin/services/roles/{ethereum_besu => ethereum}/tasks/metrics.yml (100%) rename depin/services/roles/{ethereum_besu => ethereum}/templates/collector.py.j2 (100%) create mode 100644 depin/services/roles/ethereum/templates/config_besu.toml.j2 create mode 100644 depin/services/roles/ethereum/templates/config_teku.yaml.j2 rename depin/services/roles/{ethereum_besu => ethereum}/templates/metrics.service.j2 (100%) rename depin/services/roles/{ethereum_besu/templates/systemd.service.j2 => ethereum/templates/systemd.besu_service.j2} (87%) create mode 100644 depin/services/roles/ethereum/templates/systemd.teku_service.j2 delete mode 100644 depin/services/roles/ethereum_besu/defaults/main.yml delete mode 100644 depin/services/roles/ethereum_besu/tasks/commands/install.yml delete mode 100644 depin/services/roles/ethereum_besu/templates/config diff --git a/depin/services/roles/ethereum/defaults/main.yml b/depin/services/roles/ethereum/defaults/main.yml new file mode 100644 index 00000000..774007ba --- /dev/null +++ b/depin/services/roles/ethereum/defaults/main.yml @@ -0,0 +1,16 @@ +--- + +ethereum_besu_cmd: "{{ depin_cmd | default('install') }}" +ethereum_besu_version: 24.9.1 +ethereum_teku_version: 24.8.0 +ethereum_besu_metrics_port: 33006 +ethereum_tesk_metrics_port: 33007 +ethereum_besu_download_url: "https://github.com/hyperledger/besu/releases/download/24.9.1/besu-24.9.1.zip" +ethereum_teku_download_url: "https://artifacts.consensys.net/public/teku/raw/names/teku.zip/versions/24.8.0/teku-24.8.0.zip" + +# Node configuration + +ethereum_network: holesky +ethereum_besu_rpc_http_host: 0.0.0.0 +ethereum_public_ip: "" +ethereum_besu_log_dir: "/var/log/besu" \ No newline at end of file diff --git a/depin/services/roles/ethereum/tasks/commands/install.yml b/depin/services/roles/ethereum/tasks/commands/install.yml new file mode 100644 index 00000000..ef874411 --- /dev/null +++ b/depin/services/roles/ethereum/tasks/commands/install.yml @@ -0,0 +1,162 @@ +--- +- name: Install dependencies + become: true + ansible.builtin.apt: + name: + - openjdk-21-jdk + state: present + update_cache: true + +- name: Create program folder for besu + become: true + ansible.builtin.file: + state: directory + owner: root + group: root + mode: '0755' + dest: /opt/ethereum_besu/{{ ethereum_besu_version }} + +- name: Create program folder for teku + become: true + ansible.builtin.file: + state: directory + owner: root + group: root + mode: '0755' + dest: /opt/ethereum_teku/{{ ethereum_teku_version }} + +- name: Download besu binary to tmp + become: true + ansible.builtin.unarchive: + remote_src: true + src: "{{ ethereum_besu_download_url }}" + dest: /tmp + failed_when: false + +- name: Download teku binary to tmp + become: true + ansible.builtin.unarchive: + remote_src: true + src: "{{ ethereum_teku_download_url }}" + dest: /tmp + failed_when: false + +- name: Find besu binary + become: true + ansible.builtin.find: + paths: + - /tmp + - "{{ ethereum_besu_download_path | default('/tmp') }}" + patterns: 'besu$' + use_regex: true + get_checksum: true + recurse: true + register: _download_besu + +- name: Find teku binary + become: true + ansible.builtin.find: + paths: + - /tmp + - "{{ ethereum_teku_download_path | default('/tmp') }}" + patterns: 'besu$' + use_regex: true + get_checksum: true + recurse: true + register: _download_teku + +- name: Generate the shared secret + become: true + ansible.builtin.shell: openssl rand -hex 32 | tr -d "\n" > /tmp/jwtsecret.hex + +- name: Copy the shared secret to execution client + become: true + ansible.builtin.copy: + remote_src: true + src: /tmp/jwtsecret.hex + dest: /opt/ethereum_besu/{{ ethereum_besu_version }}/jwtsecret.hex + +- name: Copy the shared secret to consensus client + become: true + ansible.builtin.copy: + remote_src: true + src: /tmp/jwtsecret.hex + dest: /opt/ethereum_teku/{{ ethereum_teku_version }}/jwtsecret.hex + +- name: Get public IP + uri: + url: https://ipecho.net/plain + method: GET + return_content: yes + status_code: 200 + register: public_ip + ignore_errors: True + +- name: Set the public ip + set_fact: + ethereum_public_ip: "{{ public_ip.content }}" + when: public_ip.status == 200 + +- name: Install execution client + when: _download_besu['files'] | length > 0 + become: true + vars: + _files: | + {{ + _download_besu['files'] | + selectattr('checksum', 'equalto', ethereum_besu_checksum | default('')) + }} + block: + - name: Copy required files + ansible.builtin.copy: + remote_src: true + src: /tmp/besu-{{ ethereum_besu_version }}/ + dest: /opt/ethereum_besu/{{ ethereum_besu_version }} + mode: '0755' + + - name: Copy besu config file + become: true + ansible.builtin.template: + src: templates/config_besu.toml.j2 + dest: /opt/ethereum_besu/{{ ethereum_besu_version }}/config_besu.toml + mode: '0777' + + - name: Create besu systemd file + ansible.builtin.template: + src: templates/systemd.besu_service.j2 + dest: /etc/systemd/system/ethereum_besu.service + mode: '0644' + +- name: Install consensus client + when: _download_teku['files'] | length > 0 + become: true + vars: + _files: | + {{ + _download_teku['files'] | + selectattr('checksum', 'equalto', ethereum_teku_checksum | default('')) + }} + block: + - name: Copy required files + ansible.builtin.copy: + remote_src: true + src: /tmp/teku-{{ ethereum_teku_version }}/ + dest: /opt/ethereum_teku/{{ ethereum_teku_version }} + mode: '0755' + + - name: Copy teku config file + become: true + ansible.builtin.template: + src: templates/config_teku.yaml.j2 + dest: /opt/ethereum_teku/{{ ethereum_teku_version }}/config_teku.yaml + mode: '0777' + + - name: Create teku systemd file + ansible.builtin.template: + src: templates/systemd.teku_service.j2 + dest: /etc/systemd/system/ethereum_teku.service + mode: '0644' + +- name: Start node + ansible.builtin.include_tasks: + file: commands/start.yml \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/commands/reset.yml b/depin/services/roles/ethereum/tasks/commands/reset.yml similarity index 100% rename from depin/services/roles/ethereum_besu/tasks/commands/reset.yml rename to depin/services/roles/ethereum/tasks/commands/reset.yml diff --git a/depin/services/roles/ethereum_besu/tasks/commands/restart.yml b/depin/services/roles/ethereum/tasks/commands/restart.yml similarity index 100% rename from depin/services/roles/ethereum_besu/tasks/commands/restart.yml rename to depin/services/roles/ethereum/tasks/commands/restart.yml diff --git a/depin/services/roles/ethereum_besu/tasks/commands/start.yml b/depin/services/roles/ethereum/tasks/commands/start.yml similarity index 50% rename from depin/services/roles/ethereum_besu/tasks/commands/start.yml rename to depin/services/roles/ethereum/tasks/commands/start.yml index 5b548287..1f16102b 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/start.yml +++ b/depin/services/roles/ethereum/tasks/commands/start.yml @@ -1,6 +1,6 @@ --- # Run the ethereum_besu -- name: Start node +- name: Start execution client become: true ansible.builtin.service: name: ethereum_besu @@ -8,6 +8,14 @@ daemon_reload: true state: started +- name: Start consensus client + become: true + ansible.builtin.service: + name: ethereum_teku + enabled: true + daemon_reload: true + state: started + - name: Assert when: "'molecule' in groups" block: @@ -28,4 +36,23 @@ - name: Set pid ansible.builtin.set_fact: - _presearch_pid: "{{ _ethereum_besu['status']['ExecMainPID'] }}" \ No newline at end of file + _presearch_pid: "{{ _ethereum_besu['status']['ExecMainPID'] }}" + + - name: Get service info + become: true + ansible.builtin.systemd: + name: "ethereum_teku" + register: _ethereum_teku + + - debug: + var: _ethereum_teku.status.ActiveState + + - name: Check service + ansible.builtin.assert: + that: + - _ethereum_teku['status']['ActiveState']=='active' + quiet: true + + - name: Set pid + ansible.builtin.set_fact: + _presearch_pid: "{{ _ethereum_teku['status']['ExecMainPID'] }}" \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml b/depin/services/roles/ethereum/tasks/commands/stop.yml similarity index 52% rename from depin/services/roles/ethereum_besu/tasks/commands/stop.yml rename to depin/services/roles/ethereum/tasks/commands/stop.yml index af10cbe2..f8f99ba7 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/stop.yml +++ b/depin/services/roles/ethereum/tasks/commands/stop.yml @@ -5,7 +5,13 @@ ansible.builtin.systemd: name: ethereum_besu state: stopped - + +- name: Stop ethereum_besu + become: true + ansible.builtin.systemd: + name: ethereum_teku + state: stopped + - name: Assert when: "'molecule' in groups" block: @@ -23,4 +29,20 @@ ansible.builtin.assert: that: - _ethereum_besu['status']['ActiveState']=='inactive' + quiet: true + + - name: Get teku service info + become: true + ansible.builtin.systemd: + name: "ethereum_teku" + register: ethereum_teku + + - name: Debug teku service status + debug: + var: ethereum_teku.status + + - name: Check teku ervice + ansible.builtin.assert: + that: + - ethereum_teku['status']['ActiveState']=='inactive' quiet: true \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml b/depin/services/roles/ethereum/tasks/commands/uninstall.yml similarity index 90% rename from depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml rename to depin/services/roles/ethereum/tasks/commands/uninstall.yml index cdc13286..299c9da1 100644 --- a/depin/services/roles/ethereum_besu/tasks/commands/uninstall.yml +++ b/depin/services/roles/ethereum/tasks/commands/uninstall.yml @@ -10,6 +10,12 @@ path: /opt/ethereum_besu state: absent +- name: Remove systemd service for ethereum_besu + become: true + file: + path: /opt/ethereum_teku + state: absent + # Remove the systemd service for ethereum_besu - name: Delete content & directory become: true diff --git a/depin/services/roles/ethereum_besu/tasks/main.yml b/depin/services/roles/ethereum/tasks/main.yml similarity index 59% rename from depin/services/roles/ethereum_besu/tasks/main.yml rename to depin/services/roles/ethereum/tasks/main.yml index ccaa37a1..685deb7a 100644 --- a/depin/services/roles/ethereum_besu/tasks/main.yml +++ b/depin/services/roles/ethereum/tasks/main.yml @@ -3,6 +3,6 @@ ansible.builtin.include_tasks: file: "commands/{{ ethereum_besu_cmd }}.yml" -- name: Ensure metrics is installed - ansible.builtin.include_tasks: - file: metrics.yml \ No newline at end of file +#- name: Ensure metrics is installed +# ansible.builtin.include_tasks: +# file: metrics.yml \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/tasks/metrics.yml b/depin/services/roles/ethereum/tasks/metrics.yml similarity index 100% rename from depin/services/roles/ethereum_besu/tasks/metrics.yml rename to depin/services/roles/ethereum/tasks/metrics.yml diff --git a/depin/services/roles/ethereum_besu/templates/collector.py.j2 b/depin/services/roles/ethereum/templates/collector.py.j2 similarity index 100% rename from depin/services/roles/ethereum_besu/templates/collector.py.j2 rename to depin/services/roles/ethereum/templates/collector.py.j2 diff --git a/depin/services/roles/ethereum/templates/config_besu.toml.j2 b/depin/services/roles/ethereum/templates/config_besu.toml.j2 new file mode 100644 index 00000000..aa2707b4 --- /dev/null +++ b/depin/services/roles/ethereum/templates/config_besu.toml.j2 @@ -0,0 +1,10 @@ +network="{{ ethereum_network }}" +rpc-http-enabled=true +rpc-http-host="{{ ethereum_besu_rpc_http_host }}" +rpc-http-cors-origins=["*"] +rpc-ws-enabled=true +rpc-ws-host="0.0.0.0" +host-allowlist=["*"] +engine-host-allowlist=["*"] +engine-rpc-enabled=true +engine-jwt-secret="/opt/ethereum_besu/{{ ethereum_besu_version }}/jwtsecret.hex" \ No newline at end of file diff --git a/depin/services/roles/ethereum/templates/config_teku.yaml.j2 b/depin/services/roles/ethereum/templates/config_teku.yaml.j2 new file mode 100644 index 00000000..20bc65cd --- /dev/null +++ b/depin/services/roles/ethereum/templates/config_teku.yaml.j2 @@ -0,0 +1,8 @@ +network: {{ ethereum_network }} +ee-endpoint: "http://localhost:8551" +ee-jwt-secret-file: "/opt/ethereum_teku/{{ ethereum_teku_version }}/jwtsecret.hex" +metrics-enabled: true +rest-api-enabled: true +checkpoint-sync-url: "https://checkpoint-sync.holesky.ethpandaops.io" +p2p-advertised-ip: "{{ ethereum_public_ip }}" +metrics-enabled: true \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/templates/metrics.service.j2 b/depin/services/roles/ethereum/templates/metrics.service.j2 similarity index 100% rename from depin/services/roles/ethereum_besu/templates/metrics.service.j2 rename to depin/services/roles/ethereum/templates/metrics.service.j2 diff --git a/depin/services/roles/ethereum_besu/templates/systemd.service.j2 b/depin/services/roles/ethereum/templates/systemd.besu_service.j2 similarity index 87% rename from depin/services/roles/ethereum_besu/templates/systemd.service.j2 rename to depin/services/roles/ethereum/templates/systemd.besu_service.j2 index 2a06d434..7855f851 100644 --- a/depin/services/roles/ethereum_besu/templates/systemd.service.j2 +++ b/depin/services/roles/ethereum/templates/systemd.besu_service.j2 @@ -7,7 +7,7 @@ User=root Group=root Type=simple WorkingDirectory=/opt/ethereum_besu/{{ ethereum_besu_version }} -ExecStart=/opt/ethereum_besu/{{ ethereum_besu_version }}/bin/besu --config-file=/opt/ethereum_besu/{{ ethereum_besu_version }}/config +ExecStart=/opt/ethereum_besu/{{ ethereum_besu_version }}/bin/besu --config-file=/opt/ethereum_besu/{{ ethereum_besu_version }}/config_besu.toml >> {{ethereum_besu_log_dir}}/besu.log 2>&1 SuccessExitStatus=143 Restart=on-failure RestartSec=20s diff --git a/depin/services/roles/ethereum/templates/systemd.teku_service.j2 b/depin/services/roles/ethereum/templates/systemd.teku_service.j2 new file mode 100644 index 00000000..533b2228 --- /dev/null +++ b/depin/services/roles/ethereum/templates/systemd.teku_service.j2 @@ -0,0 +1,18 @@ +[Unit] +Description=ethereum_teku service +After=network.target + +[Service] +User=root +Group=root +Type=simple +WorkingDirectory=/opt/ethereum_teku/{{ ethereum_teku_version }} +ExecStart=/opt/ethereum_teku/{{ ethereum_teku_version }}/bin/teku --config-file=/opt/ethereum_teku/{{ ethereum_teku_version }}/config_teku.yaml +RestartForceExitStatus=1 +RestartPreventExitStatus=2 +Restart=on-failure +RestartSec=20s + + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/defaults/main.yml b/depin/services/roles/ethereum_besu/defaults/main.yml deleted file mode 100644 index f5b36c6a..00000000 --- a/depin/services/roles/ethereum_besu/defaults/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- - -ethereum_besu_cmd: "{{ depin_cmd | default('install') }}" -ethereum_besu_version: 24.9.1 -ethereum_besu_metrics_port: 33006 -ethereum_besu_download_url: "https://github.com/hyperledger/besu/releases/download/24.9.1/besu-24.9.1.zip" diff --git a/depin/services/roles/ethereum_besu/tasks/commands/install.yml b/depin/services/roles/ethereum_besu/tasks/commands/install.yml deleted file mode 100644 index 8408241b..00000000 --- a/depin/services/roles/ethereum_besu/tasks/commands/install.yml +++ /dev/null @@ -1,72 +0,0 @@ ---- -- name: Install dependencies - become: true - ansible.builtin.apt: - name: - - openjdk-21-jdk - state: present - update_cache: true - -- name: Create program folder - become: true - ansible.builtin.file: - state: directory - owner: root - group: root - mode: '0755' - dest: /opt/ethereum_besu/{{ ethereum_besu_version }} - - -- name: Download binary to tmp - become: true - ansible.builtin.unarchive: - remote_src: true - src: "{{ ethereum_besu_download_url }}" - dest: /tmp - failed_when: false - -- name: Find binary - become: true - ansible.builtin.find: - paths: - - /tmp - - "{{ ethereum_besu_download_path | default('/tmp') }}" - patterns: 'besu$' - use_regex: true - get_checksum: true - recurse: true - register: _download - -- name: Install node - when: _download['files'] | length > 0 - become: true - vars: - _files: | - {{ - _download['files'] | - selectattr('checksum', 'equalto', ethereum_besu_checksum | default('')) - }} - block: - - name: Copy required files - ansible.builtin.copy: - remote_src: true - src: /tmp/besu-{{ ethereum_besu_version }}/ - dest: /opt/ethereum_besu/{{ ethereum_besu_version }} - mode: '0755' - - - name: Copy config file - become: true - ansible.builtin.copy: - src: templates/config - dest: /opt/ethereum_besu/{{ ethereum_besu_version }} - mode: '0755' - - - name: Create systemd file - ansible.builtin.template: - src: templates/systemd.service.j2 - dest: /etc/systemd/system/ethereum_besu.service - mode: '0644' - -- name: Start node - ansible.builtin.include_tasks: - file: commands/start.yml \ No newline at end of file diff --git a/depin/services/roles/ethereum_besu/templates/config b/depin/services/roles/ethereum_besu/templates/config deleted file mode 100644 index 02d270ab..00000000 --- a/depin/services/roles/ethereum_besu/templates/config +++ /dev/null @@ -1,8 +0,0 @@ -network="dev" -miner-enabled=true -miner-coinbase="0xfe3b557e8fb62b89f4916b721be55ceb828dbd73" -rpc-http-cors-origins=["all"] -host-allowlist=["*"] -rpc-ws-enabled=true -rpc-http-enabled=true -data-path="/tmp/tmpdata-path" \ No newline at end of file From 9bf470651fcc8e5ee5c21bc5fe1d8a72f7e4b9fd Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Thu, 10 Oct 2024 11:15:14 +0200 Subject: [PATCH 6/9] Fix log --- depin/services/roles/ethereum/templates/systemd.besu_service.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/depin/services/roles/ethereum/templates/systemd.besu_service.j2 b/depin/services/roles/ethereum/templates/systemd.besu_service.j2 index 7855f851..9f3de0e8 100644 --- a/depin/services/roles/ethereum/templates/systemd.besu_service.j2 +++ b/depin/services/roles/ethereum/templates/systemd.besu_service.j2 @@ -7,7 +7,7 @@ User=root Group=root Type=simple WorkingDirectory=/opt/ethereum_besu/{{ ethereum_besu_version }} -ExecStart=/opt/ethereum_besu/{{ ethereum_besu_version }}/bin/besu --config-file=/opt/ethereum_besu/{{ ethereum_besu_version }}/config_besu.toml >> {{ethereum_besu_log_dir}}/besu.log 2>&1 +ExecStart=/opt/ethereum_besu/{{ ethereum_besu_version }}/bin/besu --config-file=/opt/ethereum_besu/{{ ethereum_besu_version }}/config_besu.toml SuccessExitStatus=143 Restart=on-failure RestartSec=20s From 527a75b33dde2cdbffe369d8392716aa0b30a48c Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Thu, 10 Oct 2024 12:05:47 +0200 Subject: [PATCH 7/9] Fix mistype --- depin/services/roles/ethereum/tasks/commands/install.yml | 2 +- depin/services/roles/ethereum/tasks/commands/stop.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/depin/services/roles/ethereum/tasks/commands/install.yml b/depin/services/roles/ethereum/tasks/commands/install.yml index ef874411..c12a6510 100644 --- a/depin/services/roles/ethereum/tasks/commands/install.yml +++ b/depin/services/roles/ethereum/tasks/commands/install.yml @@ -59,7 +59,7 @@ paths: - /tmp - "{{ ethereum_teku_download_path | default('/tmp') }}" - patterns: 'besu$' + patterns: 'teku$' use_regex: true get_checksum: true recurse: true diff --git a/depin/services/roles/ethereum/tasks/commands/stop.yml b/depin/services/roles/ethereum/tasks/commands/stop.yml index f8f99ba7..e582738e 100644 --- a/depin/services/roles/ethereum/tasks/commands/stop.yml +++ b/depin/services/roles/ethereum/tasks/commands/stop.yml @@ -41,7 +41,7 @@ debug: var: ethereum_teku.status - - name: Check teku ervice + - name: Check teku service ansible.builtin.assert: that: - ethereum_teku['status']['ActiveState']=='inactive' From f18d026339563e5a36e7ad92d18bf387f88502aa Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Fri, 11 Oct 2024 12:13:08 +0200 Subject: [PATCH 8/9] Update collector for ethereum_besu service --- .../services/roles/ethereum/defaults/main.yml | 12 ++++--- depin/services/roles/ethereum/tasks/main.yml | 10 +++--- .../services/roles/ethereum/tasks/metrics.yml | 9 +++--- .../roles/ethereum/templates/collector.py.j2 | 31 +++++++++++++------ .../ethereum/templates/config_besu.toml.j2 | 7 +++-- .../ethereum/templates/config_teku.yaml.j2 | 8 +++-- 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/depin/services/roles/ethereum/defaults/main.yml b/depin/services/roles/ethereum/defaults/main.yml index 774007ba..f8d87e5e 100644 --- a/depin/services/roles/ethereum/defaults/main.yml +++ b/depin/services/roles/ethereum/defaults/main.yml @@ -1,10 +1,9 @@ --- -ethereum_besu_cmd: "{{ depin_cmd | default('install') }}" +ethereum_cmd: "{{ depin_cmd | default('install') }}" ethereum_besu_version: 24.9.1 ethereum_teku_version: 24.8.0 -ethereum_besu_metrics_port: 33006 -ethereum_tesk_metrics_port: 33007 +ethereum_custom_metrics_port: 33006 ethereum_besu_download_url: "https://github.com/hyperledger/besu/releases/download/24.9.1/besu-24.9.1.zip" ethereum_teku_download_url: "https://artifacts.consensys.net/public/teku/raw/names/teku.zip/versions/24.8.0/teku-24.8.0.zip" @@ -13,4 +12,9 @@ ethereum_teku_download_url: "https://artifacts.consensys.net/public/teku/raw/nam ethereum_network: holesky ethereum_besu_rpc_http_host: 0.0.0.0 ethereum_public_ip: "" -ethereum_besu_log_dir: "/var/log/besu" \ No newline at end of file +ethereum_besu_rpc_ws_host: 0.0.0.0 +ethereum_besu_metrics_host: 0.0.0.0 +ethereum_besu_metrics_port: 9545 + +ethereum_teku_ee_endpoint: http://localhost:8551 +ethereum_teku_checkpoint_sync_url: https://checkpoint-sync.holesky.ethpandaops.io diff --git a/depin/services/roles/ethereum/tasks/main.yml b/depin/services/roles/ethereum/tasks/main.yml index 685deb7a..f3f9d1bc 100644 --- a/depin/services/roles/ethereum/tasks/main.yml +++ b/depin/services/roles/ethereum/tasks/main.yml @@ -1,8 +1,8 @@ --- -- name: "ethereum_besu {{ ethereum_besu_cmd }}" +- name: "ethereum_besu {{ ethereum_cmd }}" ansible.builtin.include_tasks: - file: "commands/{{ ethereum_besu_cmd }}.yml" + file: "commands/{{ ethereum_cmd }}.yml" -#- name: Ensure metrics is installed -# ansible.builtin.include_tasks: -# file: metrics.yml \ No newline at end of file +- name: Ensure metrics is installed + ansible.builtin.include_tasks: + file: metrics.yml \ No newline at end of file diff --git a/depin/services/roles/ethereum/tasks/metrics.yml b/depin/services/roles/ethereum/tasks/metrics.yml index 7a63e8eb..b23d13b9 100644 --- a/depin/services/roles/ethereum/tasks/metrics.yml +++ b/depin/services/roles/ethereum/tasks/metrics.yml @@ -2,6 +2,7 @@ - name: Install metrics vars: _name: "{{ role_name | replace('_', '-') }}" + _metrics_port: ethereum_custom_metrics_port when: vars[role_name + '_cmd'] != 'uninstall' become: true block: @@ -28,14 +29,12 @@ - name: Update prometheus.conf vars: _block: | - - name: {{ _name }} - url: 'http://127.0.0.1:{{ vars[role_name + '_metrics_port'] }}/metrics' - update_every: 900 + - name: {{ _name }} + url: 'http://127.0.0.1:{{ vars[_metrics_port] }}/metrics' ansible.builtin.blockinfile: path: /etc/netdata/go.d/prometheus.conf append_newline: true - block: | - {{ _block | indent(4, first=true) }} + block: "{{ _block | indent(4, first=true) }}" - name: Start service ansible.builtin.systemd_service: diff --git a/depin/services/roles/ethereum/templates/collector.py.j2 b/depin/services/roles/ethereum/templates/collector.py.j2 index 35ec5a7c..9629c206 100644 --- a/depin/services/roles/ethereum/templates/collector.py.j2 +++ b/depin/services/roles/ethereum/templates/collector.py.j2 @@ -7,7 +7,7 @@ import time import subprocess from subprocess import check_output import json - +import re from prometheus_client import start_http_server, REGISTRY, GC_COLLECTOR, PLATFORM_COLLECTOR, PROCESS_COLLECTOR from prometheus_client.core import GaugeMetricFamily @@ -22,7 +22,7 @@ else: class MetricsCollector(Collector): """Collector for Gala Node information""" - def total_uptime_count(sefl, besu_service_date): + def total_uptime_count(self, besu_service_date): total_uptime=0 for x in besu_service_date: state_uptime_list = besu_service_date[x].split(":") @@ -41,11 +41,24 @@ class MetricsCollector(Collector): result = [] service_metrics_collected = 1 - service_name='java' + service_name = "ethereum_besu" try: - pid_id= str(check_output(["pidof",service_name]).decode("utf-8")).replace("\n","") - print("PID of process: " + pid_id) - state = 1 + p = subprocess.Popen(["systemctl", "status", service_name], stdout=subprocess.PIPE) + (output, err) = p.communicate() + output = output.decode('utf-8') + print() + y= re.search("Active: .*active", output) + status= y.group().split(" ") + if status[1] == 'active': + state = 1 + else: + print("exit") + state = 0 + sys.exit(1) + + x = re.search("Main PID: [0-9]+", output) + pid= x.group().split(" ") + pid_id=pid[2] proc = subprocess.Popen( ["ps", "-eo", "pid,comm,lstart,etime,time"], stdout=subprocess.PIPE ) @@ -65,7 +78,7 @@ class MetricsCollector(Collector): p = pid_decription.split()[0] if p == pid_id: print(pid_decription) - mounth = pid_decription.split()[3] + month = pid_decription.split()[3] day = pid_decription.split()[4] time_str = pid_decription.split()[5] years = pid_decription.split()[6] @@ -80,7 +93,7 @@ class MetricsCollector(Collector): minutes = int(state_uptime_dic[1]) seconds = int(state_uptime_dic[2]) state_uptime=seconds+minutes*60+hours*3600 - date_str = mounth + "-" + day + "-" + years + " " + time_str + date_str = month + "-" + day + "-" + years + " " + time_str besu_service_date[date_str] = run_uptime total_uptime=self.total_uptime_count(besu_service_date) f = open("ethereum_besu_total_uptime.txt", "w") @@ -138,6 +151,6 @@ if __name__ == "__main__": REGISTRY.unregister(PROCESS_COLLECTOR) REGISTRY.register(MetricsCollector()) - start_http_server({{ vars[role_name + '_metrics_port'] | int }}) + start_http_server({{ vars[role_name + '_custom_metrics_port'] | int }}) while True: time.sleep(30) \ No newline at end of file diff --git a/depin/services/roles/ethereum/templates/config_besu.toml.j2 b/depin/services/roles/ethereum/templates/config_besu.toml.j2 index aa2707b4..6b7d03e0 100644 --- a/depin/services/roles/ethereum/templates/config_besu.toml.j2 +++ b/depin/services/roles/ethereum/templates/config_besu.toml.j2 @@ -3,8 +3,11 @@ rpc-http-enabled=true rpc-http-host="{{ ethereum_besu_rpc_http_host }}" rpc-http-cors-origins=["*"] rpc-ws-enabled=true -rpc-ws-host="0.0.0.0" +rpc-ws-host="{{ ethereum_besu_rpc_ws_host }}" host-allowlist=["*"] engine-host-allowlist=["*"] engine-rpc-enabled=true -engine-jwt-secret="/opt/ethereum_besu/{{ ethereum_besu_version }}/jwtsecret.hex" \ No newline at end of file +engine-jwt-secret="/opt/ethereum_besu/{{ ethereum_besu_version }}/jwtsecret.hex" +metrics-enabled=true +metrics-host="{{ ethereum_besu_metrics_host }}" +metrics-port={{ ethereum_besu_metrics_port }} \ No newline at end of file diff --git a/depin/services/roles/ethereum/templates/config_teku.yaml.j2 b/depin/services/roles/ethereum/templates/config_teku.yaml.j2 index 20bc65cd..5872ae81 100644 --- a/depin/services/roles/ethereum/templates/config_teku.yaml.j2 +++ b/depin/services/roles/ethereum/templates/config_teku.yaml.j2 @@ -1,8 +1,10 @@ network: {{ ethereum_network }} -ee-endpoint: "http://localhost:8551" +ee-endpoint: "{{ ethereum_teku_ee_endpoint }}" ee-jwt-secret-file: "/opt/ethereum_teku/{{ ethereum_teku_version }}/jwtsecret.hex" metrics-enabled: true rest-api-enabled: true -checkpoint-sync-url: "https://checkpoint-sync.holesky.ethpandaops.io" +checkpoint-sync-url: "{{ ethereum_teku_checkpoint_sync_url }}" p2p-advertised-ip: "{{ ethereum_public_ip }}" -metrics-enabled: true \ No newline at end of file +metrics-enabled: true +metrics-host-allowlist: ["*"] +metrics-port: 6174 \ No newline at end of file From 7b1fe2088013c0bc8d808cd7f24e2b485b43e19d Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Fri, 25 Oct 2024 15:11:08 +0200 Subject: [PATCH 9/9] Updates after PR review --- .../roles/ethereum/tasks/commands/start.yml | 8 ++- .../roles/ethereum/tasks/commands/stop.yml | 4 +- depin/services/roles/ethereum/tasks/main.yml | 11 ++-- .../services/roles/ethereum/tasks/metrics.yml | 62 ------------------- .../ethereum/templates/metrics.service.j2 | 18 ------ 5 files changed, 13 insertions(+), 90 deletions(-) delete mode 100644 depin/services/roles/ethereum/tasks/metrics.yml delete mode 100644 depin/services/roles/ethereum/templates/metrics.service.j2 diff --git a/depin/services/roles/ethereum/tasks/commands/start.yml b/depin/services/roles/ethereum/tasks/commands/start.yml index 1f16102b..80e0373a 100644 --- a/depin/services/roles/ethereum/tasks/commands/start.yml +++ b/depin/services/roles/ethereum/tasks/commands/start.yml @@ -8,6 +8,10 @@ daemon_reload: true state: started +- name: Pause for 1 minute for ensure execution client + ansible.builtin.pause: + minutes: 1 + - name: Start consensus client become: true ansible.builtin.service: @@ -28,7 +32,7 @@ - debug: var: _ethereum_besu.status.ActiveState - - name: Check service + - name: Ensure besu is active state ansible.builtin.assert: that: - _ethereum_besu['status']['ActiveState']=='active' @@ -47,7 +51,7 @@ - debug: var: _ethereum_teku.status.ActiveState - - name: Check service + - name: Ensure teku is active state ansible.builtin.assert: that: - _ethereum_teku['status']['ActiveState']=='active' diff --git a/depin/services/roles/ethereum/tasks/commands/stop.yml b/depin/services/roles/ethereum/tasks/commands/stop.yml index e582738e..9f46dcf9 100644 --- a/depin/services/roles/ethereum/tasks/commands/stop.yml +++ b/depin/services/roles/ethereum/tasks/commands/stop.yml @@ -25,7 +25,7 @@ debug: var: _ethereum_besu.status - - name: Check service + - name: Ensure besu is inactive state ansible.builtin.assert: that: - _ethereum_besu['status']['ActiveState']=='inactive' @@ -41,7 +41,7 @@ debug: var: ethereum_teku.status - - name: Check teku service + - name: Ensure teku is inactive state ansible.builtin.assert: that: - ethereum_teku['status']['ActiveState']=='inactive' diff --git a/depin/services/roles/ethereum/tasks/main.yml b/depin/services/roles/ethereum/tasks/main.yml index f3f9d1bc..f299493b 100644 --- a/depin/services/roles/ethereum/tasks/main.yml +++ b/depin/services/roles/ethereum/tasks/main.yml @@ -1,8 +1,7 @@ --- -- name: "ethereum_besu {{ ethereum_cmd }}" +- name: Ethereum {{ ethereum_cmd }} + vars: + cmd_file: "{{ role_path }}/tasks/commands/{{ ethereum_cmd }}.yml" + when: cmd_file is file ansible.builtin.include_tasks: - file: "commands/{{ ethereum_cmd }}.yml" - -- name: Ensure metrics is installed - ansible.builtin.include_tasks: - file: metrics.yml \ No newline at end of file + file: "{{ cmd_file }}" \ No newline at end of file diff --git a/depin/services/roles/ethereum/tasks/metrics.yml b/depin/services/roles/ethereum/tasks/metrics.yml deleted file mode 100644 index b23d13b9..00000000 --- a/depin/services/roles/ethereum/tasks/metrics.yml +++ /dev/null @@ -1,62 +0,0 @@ ---- -- name: Install metrics - vars: - _name: "{{ role_name | replace('_', '-') }}" - _metrics_port: ethereum_custom_metrics_port - when: vars[role_name + '_cmd'] != 'uninstall' - become: true - block: - - name: Ensure directory exists - ansible.builtin.file: - path: /etc/deeep-network/collectors - state: directory - mode: '0755' - recurse: true - - - name: Install script - ansible.builtin.template: - src: templates/collector.py.j2 - dest: /etc/deeep-network/collectors/{{ _name }}.py - mode: '0744' - - - name: Install service - ansible.builtin.template: - src: templates/metrics.service.j2 - dest: /etc/systemd/system/collector-{{ _name }}.service - mode: '0644' - - - - name: Update prometheus.conf - vars: - _block: | - - name: {{ _name }} - url: 'http://127.0.0.1:{{ vars[_metrics_port] }}/metrics' - ansible.builtin.blockinfile: - path: /etc/netdata/go.d/prometheus.conf - append_newline: true - block: "{{ _block | indent(4, first=true) }}" - - - name: Start service - ansible.builtin.systemd_service: - name: collector-{{ _name }} - enabled: true - daemon_reload: true - state: started - -- name: Uninstall metrics - when: vars[role_name + '_cmd'] == 'uninstall' - become: true - block: - - name: Stop service - ansible.builtin.systemd_service: - name: "{{ _name }}" - enabled: false - state: stopped - - - name: Remove files - ansible.builtin.file: - path: "{{ item }}" - state: absent - loop: - - /etc/systemd/system/collector-{{ _name }}.service - - /etc/deeep-network/collectors/{{ _name }}.py \ No newline at end of file diff --git a/depin/services/roles/ethereum/templates/metrics.service.j2 b/depin/services/roles/ethereum/templates/metrics.service.j2 deleted file mode 100644 index 14757e65..00000000 --- a/depin/services/roles/ethereum/templates/metrics.service.j2 +++ /dev/null @@ -1,18 +0,0 @@ -{{ ansible_managed | comment }} - -[Unit] -Description=Prometheus collector - {{ _name }} -After=network-online.target - -[Service] -Type=simple -User=nerdnode -ExecStart=sudo /opt/pipx/venvs/ansible-core/bin/python3 /etc/deeep-network/collectors/{{ _name }}.py -KillSignal=SIGINT -Restart=on-failure - -StandardOutput=syslog -StandardError=syslog - -[Install] -WantedBy=multi-user.target \ No newline at end of file