From b9fc3fef3ab2862a0e24572d153d96ac1c427220 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Fri, 23 Aug 2024 14:11:06 +0200 Subject: [PATCH 01/18] feat: add hychain service --- depin/services/roles/hychain/README.md | 38 ++++++++++++++++ .../services/roles/hychain/defaults/main.yml | 5 +++ .../roles/hychain/files/hychain.service | 15 +++++++ .../roles/hychain/tasks/commands/install.yml | 43 ++++++++++++++++++ .../roles/hychain/tasks/commands/restart.yml | 5 +++ .../roles/hychain/tasks/commands/start.yml | 5 +++ .../roles/hychain/tasks/commands/stop.yml | 5 +++ .../hychain/tasks/commands/uninstall.yml | 20 +++++++++ .../roles/hychain/tasks/commands/update.yml | 44 +++++++++++++++++++ depin/services/roles/hychain/tasks/main.yml | 5 +++ molecule/default/vars/hychain.yml | 4 ++ 11 files changed, 189 insertions(+) create mode 100644 depin/services/roles/hychain/README.md create mode 100644 depin/services/roles/hychain/defaults/main.yml create mode 100644 depin/services/roles/hychain/files/hychain.service create mode 100644 depin/services/roles/hychain/tasks/commands/install.yml create mode 100644 depin/services/roles/hychain/tasks/commands/restart.yml create mode 100644 depin/services/roles/hychain/tasks/commands/start.yml create mode 100644 depin/services/roles/hychain/tasks/commands/stop.yml create mode 100644 depin/services/roles/hychain/tasks/commands/uninstall.yml create mode 100644 depin/services/roles/hychain/tasks/commands/update.yml create mode 100644 depin/services/roles/hychain/tasks/main.yml create mode 100644 molecule/default/vars/hychain.yml diff --git a/depin/services/roles/hychain/README.md b/depin/services/roles/hychain/README.md new file mode 100644 index 00000000..225dd44b --- /dev/null +++ b/depin/services/roles/hychain/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/depin/services/roles/hychain/defaults/main.yml b/depin/services/roles/hychain/defaults/main.yml new file mode 100644 index 00000000..225abd54 --- /dev/null +++ b/depin/services/roles/hychain/defaults/main.yml @@ -0,0 +1,5 @@ +--- +hychain_version: 0.0.1 +hychain_code: "{{ notdefined | mandatory }}" + +hychain_mode: install` diff --git a/depin/services/roles/hychain/files/hychain.service b/depin/services/roles/hychain/files/hychain.service new file mode 100644 index 00000000..9f668b52 --- /dev/null +++ b/depin/services/roles/hychain/files/hychain.service @@ -0,0 +1,15 @@ +[Unit] +Description=hychain service +After=network.target + +[Service] +User=root +Group=root +Type=simple +ExecStart=/bin/bash /home/hychain/cli/linux/hychain-start.sh +Restart=always +RestartSec=5 +PIDFile=/tmp/hychain.pid + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/install.yml b/depin/services/roles/hychain/tasks/commands/install.yml new file mode 100644 index 00000000..9b8bce9a --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/install.yml @@ -0,0 +1,43 @@ +--- +- name: Update&Upgrade + ansible.builtin.apt: + name: aptitude + state: present + update_cache: true + +- name: Install dependencies + ansible.builtin.apt: + name: + - git + state: present + update_cache: true + +- name: Clone a repo with separate git directory + ansible.builtin.git: + repo: https://github.com/HYCHAIN/guardian-node-software.git + dest: /home/hychain + single_branch: yes + version: main + +- name: Copy start file + ansible.builtin.copy: + content: | + #!/bin/bash + cd /home/hychain/cli/linux/ + ./guardian-cli-linux guardian run {{ hychain_code }} --loop-interval-ms 3600000 + dest: /home/hychain/cli/linux/hychain-start.sh + +- name: Hychain service file + ansible.builtin.copy: + src: ../files/hychain.service + dest: /etc/systemd/system/hychain.service + +- name: Enable service hychain.service + ansible.builtin.service: + name: hychain.service + enabled: yes + +- name: Start service hychain.service, if not started + ansible.builtin.service: + name: hychain.service + state: started \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/restart.yml b/depin/services/roles/hychain/tasks/commands/restart.yml new file mode 100644 index 00000000..c0e9527a --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/restart.yml @@ -0,0 +1,5 @@ +--- +- name: Restarted node + ansible.builtin.service: + name: hychain.service + state: restarted \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/start.yml b/depin/services/roles/hychain/tasks/commands/start.yml new file mode 100644 index 00000000..8daeaf1d --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/start.yml @@ -0,0 +1,5 @@ +--- +- name: Start node + ansible.builtin.service: + name: hychain.service + state: started \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/stop.yml b/depin/services/roles/hychain/tasks/commands/stop.yml new file mode 100644 index 00000000..e6b5fa93 --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/stop.yml @@ -0,0 +1,5 @@ +--- +- name: Stop node + ansible.builtin.service: + name: hychain.service + state: stopped \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/uninstall.yml b/depin/services/roles/hychain/tasks/commands/uninstall.yml new file mode 100644 index 00000000..50094cbd --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/uninstall.yml @@ -0,0 +1,20 @@ +--- +- name: Stop service hychain.service + ansible.builtin.service: + name: hychain.service + state: stopped + +- name: Disable service hychain.service + ansible.builtin.service: + name: hychain.service + enabled: false + +- name: Delete content & directory + ansible.builtin.file: + state: absent + path: /home/hychain + +- name: Delete hychain.service + ansible.builtin.file: + state: absent + path: /etc/systemd/system/hychain.service \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/update.yml b/depin/services/roles/hychain/tasks/commands/update.yml new file mode 100644 index 00000000..d77a437e --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/update.yml @@ -0,0 +1,44 @@ +--- +- name: Stop service hychain.service + ansible.builtin.service: + name: hychain.service + state: stopped + +- name: Delete content & directory + ansible.builtin.file: + state: absent + path: /home/hychain + +- name: Delete hychain.service + ansible.builtin.file: + state: absent + path: /etc/systemd/system/hychain.service + +- name: Clone a repo with separate git directory + ansible.builtin.git: + repo: https://github.com/HYCHAIN/guardian-node-software.git + dest: /home/hychain + single_branch: yes + version: main + +- name: Hychain service file + ansible.builtin.copy: + src: hychain.service + dest: /etc/systemd/system/hychain.service + +- name: Copy start file + ansible.builtin.copy: + content: | + #!/bin/bash + cd /home/hychain/cli/linux/ + ./guardian-cli-linux guardian run {{ hychain_code }} --loop-interval-ms 3600000 + dest: /home/hychain/cli/linux/hychain-start.sh + +- name: Just force systemd to reread configs (2.4 and above) + ansible.builtin.systemd_service: + daemon_reload: true + +- name: Start service hychain.service, if not started + ansible.builtin.service: + name: hychain.service + state: started \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/main.yml b/depin/services/roles/hychain/tasks/main.yml new file mode 100644 index 00000000..6e6ddc54 --- /dev/null +++ b/depin/services/roles/hychain/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: Hychain {{ hychain_mode }} + ansible.builtin.include_tasks: + file: commands/{{ hychain_mode }}.yml + diff --git a/molecule/default/vars/hychain.yml b/molecule/default/vars/hychain.yml new file mode 100644 index 00000000..0d1119da --- /dev/null +++ b/molecule/default/vars/hychain.yml @@ -0,0 +1,4 @@ +test_role: depin.services.hychain + +# override role variables +hychain_code: 0x64c077bf4de3b020f874e8a9384cbdd98dafeb49cc9e78f0996771f86c0c48e2 From 3e3241c7589d0f46bc1c3d18487b434982a5678d Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Mon, 9 Sep 2024 16:53:26 +0200 Subject: [PATCH 02/18] Updates with hychain service --- .../services/roles/hychain/defaults/main.yml | 7 +- .../roles/hychain/files/hychain.service | 15 ---- .../services/roles/hychain/handlers/main.yml | 2 + .../roles/hychain/tasks/commands/install.yml | 90 +++++++++++++------ .../roles/hychain/tasks/commands/reset.yml | 9 ++ .../roles/hychain/tasks/commands/restart.yml | 1 + .../roles/hychain/tasks/commands/start.yml | 1 + .../roles/hychain/tasks/commands/stop.yml | 1 + .../hychain/tasks/commands/uninstall.yml | 19 ++-- .../roles/hychain/tasks/commands/update.yml | 48 ++-------- depin/services/roles/hychain/tasks/main.yml | 1 - .../hychain/templates/systemd.service.j2 | 12 +++ depin/services/roles/hychain/vars/main.yml | 9 ++ 13 files changed, 114 insertions(+), 101 deletions(-) delete mode 100644 depin/services/roles/hychain/files/hychain.service create mode 100644 depin/services/roles/hychain/handlers/main.yml create mode 100644 depin/services/roles/hychain/tasks/commands/reset.yml create mode 100644 depin/services/roles/hychain/templates/systemd.service.j2 create mode 100644 depin/services/roles/hychain/vars/main.yml diff --git a/depin/services/roles/hychain/defaults/main.yml b/depin/services/roles/hychain/defaults/main.yml index 225abd54..a883c9bb 100644 --- a/depin/services/roles/hychain/defaults/main.yml +++ b/depin/services/roles/hychain/defaults/main.yml @@ -1,5 +1,6 @@ --- +# defaults file for hychain hychain_version: 0.0.1 -hychain_code: "{{ notdefined | mandatory }}" - -hychain_mode: install` +hychain_private_key: "{{ notdefined | mandatory }}" +hychain_download_url: "https://github.com/HYCHAIN/guardian-node-software/archive/refs/heads/main.zip" +hychain_mode: install diff --git a/depin/services/roles/hychain/files/hychain.service b/depin/services/roles/hychain/files/hychain.service deleted file mode 100644 index 9f668b52..00000000 --- a/depin/services/roles/hychain/files/hychain.service +++ /dev/null @@ -1,15 +0,0 @@ -[Unit] -Description=hychain service -After=network.target - -[Service] -User=root -Group=root -Type=simple -ExecStart=/bin/bash /home/hychain/cli/linux/hychain-start.sh -Restart=always -RestartSec=5 -PIDFile=/tmp/hychain.pid - -[Install] -WantedBy=multi-user.target \ No newline at end of file diff --git a/depin/services/roles/hychain/handlers/main.yml b/depin/services/roles/hychain/handlers/main.yml new file mode 100644 index 00000000..61e51c3d --- /dev/null +++ b/depin/services/roles/hychain/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for skeleton diff --git a/depin/services/roles/hychain/tasks/commands/install.yml b/depin/services/roles/hychain/tasks/commands/install.yml index 9b8bce9a..5d1b84d4 100644 --- a/depin/services/roles/hychain/tasks/commands/install.yml +++ b/depin/services/roles/hychain/tasks/commands/install.yml @@ -1,43 +1,79 @@ --- -- name: Update&Upgrade - ansible.builtin.apt: - name: aptitude - state: present - update_cache: true - +## install dependencies and start service +--- - name: Install dependencies ansible.builtin.apt: name: - - git + - zip state: present update_cache: true -- name: Clone a repo with separate git directory - ansible.builtin.git: - repo: https://github.com/HYCHAIN/guardian-node-software.git - dest: /home/hychain - single_branch: yes - version: main +- name: Create program folder + become: true + ansible.builtin.file: + state: directory + owner: root + group: root + mode: '0755' + dest: /opt/hychain/{{ hychain_version }} + +- name: Download binary to tmp + ansible.builtin.get_url: + url: "{{ hychain_download_url }}" + dest: /tmp/hychain_service_{{ hychain_version }}.zip + + +- name: Find binary + become: true + ansible.builtin.find: + paths: + - /tmp + - "{{ hychain_download_path | default('/tmp') }}" + patterns: 'hychain_service_{{ hychain_version }}.zip$' + use_regex: true + get_checksum: true + recurse: true + register: _download + +- name: Extract binary + become: true + ansible.builtin.unarchive: + src: /tmp/hychain_service_{{ hychain_version }}.zip + dest: /opt/hychain/{{ hychain_version }} + remote_src: yes - name: Copy start file ansible.builtin.copy: content: | #!/bin/bash - cd /home/hychain/cli/linux/ - ./guardian-cli-linux guardian run {{ hychain_code }} --loop-interval-ms 3600000 - dest: /home/hychain/cli/linux/hychain-start.sh + cd /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/ + ./guardian-cli-linux guardian run {{ hychain_private_key }} + dest: /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/hychain-start.sh -- name: Hychain service file +- name: Create systemd file ansible.builtin.copy: - src: ../files/hychain.service + content: | + [Unit] + Description=hychain service + After=network.target + + [Service] + User=root + Group=root + Type=simple + ExecStart=/bin/bash /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/hychain-start.sh + Restart=always + RestartSec=5 + RuntimeMaxSec=6h + PIDFile=/tmp/hychain.pid + + [Install] + WantedBy=multi-user.target dest: /etc/systemd/system/hychain.service - -- name: Enable service hychain.service - ansible.builtin.service: - name: hychain.service - enabled: yes - + - name: Start service hychain.service, if not started - ansible.builtin.service: - name: hychain.service - state: started \ No newline at end of file + ansible.builtin.systemd_service: + name: hychain + state: started + enabled: true + daemon_reload: true \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/reset.yml b/depin/services/roles/hychain/tasks/commands/reset.yml new file mode 100644 index 00000000..b7af2305 --- /dev/null +++ b/depin/services/roles/hychain/tasks/commands/reset.yml @@ -0,0 +1,9 @@ +--- +## uninstall and install service +- name: Uninstall node + ansible.builtin.include_tasks: + file: commands/uninstall.yml + +- name: Install node + ansible.builtin.include_tasks: + file: commands/install.yml \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/restart.yml b/depin/services/roles/hychain/tasks/commands/restart.yml index c0e9527a..c63271f0 100644 --- a/depin/services/roles/hychain/tasks/commands/restart.yml +++ b/depin/services/roles/hychain/tasks/commands/restart.yml @@ -1,4 +1,5 @@ --- +## stop and start service - name: Restarted node ansible.builtin.service: name: hychain.service diff --git a/depin/services/roles/hychain/tasks/commands/start.yml b/depin/services/roles/hychain/tasks/commands/start.yml index 8daeaf1d..aa11aaca 100644 --- a/depin/services/roles/hychain/tasks/commands/start.yml +++ b/depin/services/roles/hychain/tasks/commands/start.yml @@ -1,4 +1,5 @@ --- +## start service - name: Start node ansible.builtin.service: name: hychain.service diff --git a/depin/services/roles/hychain/tasks/commands/stop.yml b/depin/services/roles/hychain/tasks/commands/stop.yml index e6b5fa93..3fc753d1 100644 --- a/depin/services/roles/hychain/tasks/commands/stop.yml +++ b/depin/services/roles/hychain/tasks/commands/stop.yml @@ -1,4 +1,5 @@ --- +## stop service - name: Stop node ansible.builtin.service: name: hychain.service diff --git a/depin/services/roles/hychain/tasks/commands/uninstall.yml b/depin/services/roles/hychain/tasks/commands/uninstall.yml index 50094cbd..67ede722 100644 --- a/depin/services/roles/hychain/tasks/commands/uninstall.yml +++ b/depin/services/roles/hychain/tasks/commands/uninstall.yml @@ -1,20 +1,13 @@ --- -- name: Stop service hychain.service - ansible.builtin.service: - name: hychain.service +## stop and uninstall service (remove all associated files) +- name: Start service hychain.service, if not started + ansible.builtin.systemd_service: + name: hychain state: stopped - -- name: Disable service hychain.service - ansible.builtin.service: - name: hychain.service enabled: false + daemon_reload: true - name: Delete content & directory ansible.builtin.file: state: absent - path: /home/hychain - -- name: Delete hychain.service - ansible.builtin.file: - state: absent - path: /etc/systemd/system/hychain.service \ No newline at end of file + path: /opt/hychain \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/update.yml b/depin/services/roles/hychain/tasks/commands/update.yml index d77a437e..50d55047 100644 --- a/depin/services/roles/hychain/tasks/commands/update.yml +++ b/depin/services/roles/hychain/tasks/commands/update.yml @@ -1,44 +1,8 @@ --- -- name: Stop service hychain.service - ansible.builtin.service: - name: hychain.service - state: stopped +- name: Uninstall node + ansible.builtin.include_tasks: + file: commands/uninstall.yml -- name: Delete content & directory - ansible.builtin.file: - state: absent - path: /home/hychain - -- name: Delete hychain.service - ansible.builtin.file: - state: absent - path: /etc/systemd/system/hychain.service - -- name: Clone a repo with separate git directory - ansible.builtin.git: - repo: https://github.com/HYCHAIN/guardian-node-software.git - dest: /home/hychain - single_branch: yes - version: main - -- name: Hychain service file - ansible.builtin.copy: - src: hychain.service - dest: /etc/systemd/system/hychain.service - -- name: Copy start file - ansible.builtin.copy: - content: | - #!/bin/bash - cd /home/hychain/cli/linux/ - ./guardian-cli-linux guardian run {{ hychain_code }} --loop-interval-ms 3600000 - dest: /home/hychain/cli/linux/hychain-start.sh - -- name: Just force systemd to reread configs (2.4 and above) - ansible.builtin.systemd_service: - daemon_reload: true - -- name: Start service hychain.service, if not started - ansible.builtin.service: - name: hychain.service - state: started \ No newline at end of file +- name: Install node + ansible.builtin.include_tasks: + file: commands/install.yml \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/main.yml b/depin/services/roles/hychain/tasks/main.yml index 6e6ddc54..8304a017 100644 --- a/depin/services/roles/hychain/tasks/main.yml +++ b/depin/services/roles/hychain/tasks/main.yml @@ -2,4 +2,3 @@ - name: Hychain {{ hychain_mode }} ansible.builtin.include_tasks: file: commands/{{ hychain_mode }}.yml - diff --git a/depin/services/roles/hychain/templates/systemd.service.j2 b/depin/services/roles/hychain/templates/systemd.service.j2 new file mode 100644 index 00000000..06649ceb --- /dev/null +++ b/depin/services/roles/hychain/templates/systemd.service.j2 @@ -0,0 +1,12 @@ +[Unit] +Description= +After=network-online.target +Wants=network.target + +[Service] +Type=simple +ExecStart= +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/depin/services/roles/hychain/vars/main.yml b/depin/services/roles/hychain/vars/main.yml new file mode 100644 index 00000000..131b70bb --- /dev/null +++ b/depin/services/roles/hychain/vars/main.yml @@ -0,0 +1,9 @@ +--- +deb_architecture: { # noqa var-naming[no-role-prefix] + "aarch64": "arm64", + "x86_64": "amd64", + "arm": "arm", + "i386": "386" +} + +system_architecture: "{{ [ansible_architecture] | map('extract', deb_architecture) | first }}" # noqa var-naming[no-role-prefix] From 24e1f244d010c443e2c3cd8d84096f87a8e08334 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Wed, 11 Sep 2024 10:20:57 +0200 Subject: [PATCH 03/18] Autonomi service --- .../services/roles/autonomi/defaults/main.yml | 5 ++ .../roles/autonomi/tasks/commands/install.yml | 79 +++++++++++++++++++ .../roles/autonomi/tasks/commands/reset.yml | 7 ++ .../roles/autonomi/tasks/commands/restart.yml | 8 ++ .../roles/autonomi/tasks/commands/start.yml | 6 ++ .../roles/autonomi/tasks/commands/stop.yml | 6 ++ .../autonomi/tasks/commands/uninstall.yml | 26 ++++++ depin/services/roles/autonomi/tasks/main.yml | 4 + .../autonomi/templates/systemd.service.j2 | 12 +++ depin/services/roles/autonomi/vars/main.yml | 9 +++ 10 files changed, 162 insertions(+) create mode 100644 depin/services/roles/autonomi/defaults/main.yml create mode 100644 depin/services/roles/autonomi/tasks/commands/install.yml create mode 100644 depin/services/roles/autonomi/tasks/commands/reset.yml create mode 100644 depin/services/roles/autonomi/tasks/commands/restart.yml create mode 100644 depin/services/roles/autonomi/tasks/commands/start.yml create mode 100644 depin/services/roles/autonomi/tasks/commands/stop.yml create mode 100644 depin/services/roles/autonomi/tasks/commands/uninstall.yml create mode 100644 depin/services/roles/autonomi/tasks/main.yml create mode 100644 depin/services/roles/autonomi/templates/systemd.service.j2 create mode 100644 depin/services/roles/autonomi/vars/main.yml diff --git a/depin/services/roles/autonomi/defaults/main.yml b/depin/services/roles/autonomi/defaults/main.yml new file mode 100644 index 00000000..d217236c --- /dev/null +++ b/depin/services/roles/autonomi/defaults/main.yml @@ -0,0 +1,5 @@ +--- +autonomi_cmd: install +autonomi_version: 0.10.3 +autonomi_count: 3 +autonomi_safeup_download_url: https://raw.githubusercontent.com/maidsafe/safeup/main/install.sh diff --git a/depin/services/roles/autonomi/tasks/commands/install.yml b/depin/services/roles/autonomi/tasks/commands/install.yml new file mode 100644 index 00000000..8909eaa6 --- /dev/null +++ b/depin/services/roles/autonomi/tasks/commands/install.yml @@ -0,0 +1,79 @@ +--- +## install dependencies and service +- name: Create program folder + become: true + ansible.builtin.file: + state: directory + owner: root + group: root + mode: '0755' + dest: /opt/autonomi/{{ autonomi_version }} + +- name: Download binary to tmp + ansible.builtin.get_url: + url: "{{ autonomi_safeup_download_url }}" + dest: /tmp/safeup_service_install.sh + +- name: Install SafeUp + become: true + command: bash /tmp/safeup_service_install.sh + +- name: Install Safenode Manager + become: true + command: safeup node-manager + +- name: Copy safenode-manager binary + become: true + ansible.builtin.copy: + src: /root/.local/bin/safenode-manager + dest: /opt/autonomi/{{ autonomi_version }}/safenode-manager + remote_src: yes + +- name: Copy safenode-manager binary + become: true + command: chmod +x /opt/autonomi/{{ autonomi_version }}/safenode-manager + +- name: Create Services + become: true + command: /opt/autonomi/{{ autonomi_version }}/safenode-manager add --count {{ autonomi_count }} --peer /ip4/139.59.168.228/udp/56309/quic-v1/p2p/12D3KooWFTMtaqu24ddDSXk9v5YxnuhJmTLFRunER1CG4wZ2XLUU + +- name: Create systemd file + ansible.builtin.copy: + content: | + [Unit] + Description=autonomi service + After=network.target + + [Service] + User=root + Group=root + Type=simple + ExecStart=/opt/autonomi/{{ autonomi_version }}/safenode-manager start + ExecStop=/opt/autonomi/{{ autonomi_version }}/safenode-manager stop + Restart=always + RestartSec=5 + PIDFile=/tmp/autonomi.pid + + [Install] + WantedBy=multi-user.target + dest: /etc/systemd/system/autonomi.service + +- name: Start service hychain.service, if not started + ansible.builtin.systemd_service: + name: hychain + state: stopped + enabled: true + daemon_reload: true + +- name: Start + ansible.builtin.include_tasks: commands/start.yml + +# relative to playbook (molecule - default) +- name: Assert Install + when: "'molecule' in groups" + block: + - name: Common install asserts + ansible.builtin.include_tasks: + file: tasks/asserts_install.yml + + # unique asserts go here diff --git a/depin/services/roles/autonomi/tasks/commands/reset.yml b/depin/services/roles/autonomi/tasks/commands/reset.yml new file mode 100644 index 00000000..d967402b --- /dev/null +++ b/depin/services/roles/autonomi/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/autonomi/tasks/commands/restart.yml b/depin/services/roles/autonomi/tasks/commands/restart.yml new file mode 100644 index 00000000..1fa0088d --- /dev/null +++ b/depin/services/roles/autonomi/tasks/commands/restart.yml @@ -0,0 +1,8 @@ +--- +## 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/autonomi/tasks/commands/start.yml b/depin/services/roles/autonomi/tasks/commands/start.yml new file mode 100644 index 00000000..5b112f28 --- /dev/null +++ b/depin/services/roles/autonomi/tasks/commands/start.yml @@ -0,0 +1,6 @@ +--- +## start service +- name: Start node + ansible.builtin.service: + name: autonomi.service + state: started \ No newline at end of file diff --git a/depin/services/roles/autonomi/tasks/commands/stop.yml b/depin/services/roles/autonomi/tasks/commands/stop.yml new file mode 100644 index 00000000..fa7ef088 --- /dev/null +++ b/depin/services/roles/autonomi/tasks/commands/stop.yml @@ -0,0 +1,6 @@ +--- +## stop service +- name: Stop node + ansible.builtin.service: + name: autonomi.service + state: stopped \ No newline at end of file diff --git a/depin/services/roles/autonomi/tasks/commands/uninstall.yml b/depin/services/roles/autonomi/tasks/commands/uninstall.yml new file mode 100644 index 00000000..7d9df1f4 --- /dev/null +++ b/depin/services/roles/autonomi/tasks/commands/uninstall.yml @@ -0,0 +1,26 @@ +--- +- name: Disable systemd service + ansible.builtin.include_tasks: commands/stop.yml + +## uninstall service and remove all associated files +- name: Delete content & directory + become: true + ansible.builtin.file: + state: absent + path: /opt/autonomi + +- name: Delete serviceS + become: true + ansible.builtin.file: + state: absent + path: /etc/systemd/system/autonomi.service + +# relative to playbook (molecule - default) +- name: Assert Uninstall + when: "'molecule' in groups" + block: + - name: Common install asserts + ansible.builtin.include_tasks: + file: tasks/asserts_uninstall.yml + + # unique asserts go here diff --git a/depin/services/roles/autonomi/tasks/main.yml b/depin/services/roles/autonomi/tasks/main.yml new file mode 100644 index 00000000..ddf5738a --- /dev/null +++ b/depin/services/roles/autonomi/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- name: Autonomi {{ service_cmd | default(autonomi_cmd) }} + ansible.builtin.include_tasks: + file: commands/{{ service_cmd | default(autonomi_cmd) }}.yml diff --git a/depin/services/roles/autonomi/templates/systemd.service.j2 b/depin/services/roles/autonomi/templates/systemd.service.j2 new file mode 100644 index 00000000..06649ceb --- /dev/null +++ b/depin/services/roles/autonomi/templates/systemd.service.j2 @@ -0,0 +1,12 @@ +[Unit] +Description= +After=network-online.target +Wants=network.target + +[Service] +Type=simple +ExecStart= +Restart=always + +[Install] +WantedBy=multi-user.target diff --git a/depin/services/roles/autonomi/vars/main.yml b/depin/services/roles/autonomi/vars/main.yml new file mode 100644 index 00000000..544d9bda --- /dev/null +++ b/depin/services/roles/autonomi/vars/main.yml @@ -0,0 +1,9 @@ +--- +deb_architecture: # noqa var-naming[no-role-prefix] + aarch64: arm64 + x86_64: amd64 + arm: arm + i386: "386" + +system_architecture: |- # noqa var-naming[no-role-prefix] + {{ [ansible_architecture] | map('extract', deb_architecture) | first }} From f732952e72bbd0aed8ab61fa7dcde850440a7ccd Mon Sep 17 00:00:00 2001 From: alex-klikatech <125446221+alex-klikatech@users.noreply.github.com> Date: Wed, 11 Sep 2024 12:41:07 +0200 Subject: [PATCH 04/18] Update install.yml --- depin/services/roles/hychain/tasks/commands/install.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/depin/services/roles/hychain/tasks/commands/install.yml b/depin/services/roles/hychain/tasks/commands/install.yml index 8d00caac..b3e95a62 100644 --- a/depin/services/roles/hychain/tasks/commands/install.yml +++ b/depin/services/roles/hychain/tasks/commands/install.yml @@ -1,6 +1,5 @@ --- ## install dependencies and start service ---- - name: Install dependencies ansible.builtin.apt: name: @@ -86,4 +85,4 @@ ansible.builtin.include_tasks: file: tasks/asserts_install.yml - # unique asserts go here \ No newline at end of file + # unique asserts go here From f2a87b0db1e61d391da1af1820b95553d1011dba Mon Sep 17 00:00:00 2001 From: alex-klikatech <125446221+alex-klikatech@users.noreply.github.com> Date: Wed, 11 Sep 2024 13:34:21 +0200 Subject: [PATCH 05/18] Update install.yml --- depin/services/roles/autonomi/tasks/commands/install.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/depin/services/roles/autonomi/tasks/commands/install.yml b/depin/services/roles/autonomi/tasks/commands/install.yml index 8909eaa6..19d2f206 100644 --- a/depin/services/roles/autonomi/tasks/commands/install.yml +++ b/depin/services/roles/autonomi/tasks/commands/install.yml @@ -58,9 +58,9 @@ WantedBy=multi-user.target dest: /etc/systemd/system/autonomi.service -- name: Start service hychain.service, if not started +- name: Start service autonomi.service, if not started ansible.builtin.systemd_service: - name: hychain + name: autonomi state: stopped enabled: true daemon_reload: true From dc8c781507b4bc0bf7b2e34269889a8649babc97 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Wed, 11 Sep 2024 16:13:35 +0200 Subject: [PATCH 06/18] Update hychain service with molecule integration --- .../services/roles/hychain/defaults/main.yml | 2 +- .../roles/hychain/tasks/commands/install.yml | 21 +++++++------------ .../roles/hychain/tasks/commands/restart.yml | 1 + .../roles/hychain/tasks/commands/start.yml | 21 ++++++++++++++++++- .../roles/hychain/tasks/commands/stop.yml | 18 +++++++++++++++- .../hychain/tasks/commands/uninstall.yml | 4 +++- 6 files changed, 49 insertions(+), 18 deletions(-) diff --git a/depin/services/roles/hychain/defaults/main.yml b/depin/services/roles/hychain/defaults/main.yml index 155a4e23..898b1747 100644 --- a/depin/services/roles/hychain/defaults/main.yml +++ b/depin/services/roles/hychain/defaults/main.yml @@ -2,6 +2,6 @@ # defaults file for hychain hychain_cmd: install hychain_version: 0.0.1 -hychain_private_key: "{{ notdefined | mandatory }}" +hychain_private_key: "0x64c077bf4de3b020f874e8a9384cbdd98dafeb49cc9e78f0996771f86c0c48e2" hychain_download_url: "https://github.com/HYCHAIN/guardian-node-software/archive/refs/heads/main.zip" diff --git a/depin/services/roles/hychain/tasks/commands/install.yml b/depin/services/roles/hychain/tasks/commands/install.yml index 8d00caac..3e63acab 100644 --- a/depin/services/roles/hychain/tasks/commands/install.yml +++ b/depin/services/roles/hychain/tasks/commands/install.yml @@ -1,7 +1,7 @@ --- ## install dependencies and start service ---- - name: Install dependencies + become: true ansible.builtin.apt: name: - zip @@ -43,6 +43,7 @@ remote_src: yes - name: Copy start file + become: true ansible.builtin.copy: content: | #!/bin/bash @@ -51,6 +52,7 @@ dest: /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/hychain-start.sh - name: Create systemd file + become: true ansible.builtin.copy: content: | [Unit] @@ -71,19 +73,10 @@ WantedBy=multi-user.target dest: /etc/systemd/system/hychain.service -- name: Start service hychain.service, if not started - ansible.builtin.systemd_service: - name: hychain - state: started - enabled: true - daemon_reload: true - +- name: Start node + ansible.builtin.include_tasks: + file: commands/start.yml + # relative to playbook (molecule - default) -- name: Assert Install - when: "'molecule' in groups" - block: - - name: Common install asserts - ansible.builtin.include_tasks: - file: tasks/asserts_install.yml # unique asserts go here \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/restart.yml b/depin/services/roles/hychain/tasks/commands/restart.yml index c63271f0..416fa528 100644 --- a/depin/services/roles/hychain/tasks/commands/restart.yml +++ b/depin/services/roles/hychain/tasks/commands/restart.yml @@ -1,6 +1,7 @@ --- ## stop and start service - name: Restarted node + become: true ansible.builtin.service: name: hychain.service state: restarted \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/start.yml b/depin/services/roles/hychain/tasks/commands/start.yml index aa11aaca..fe93052c 100644 --- a/depin/services/roles/hychain/tasks/commands/start.yml +++ b/depin/services/roles/hychain/tasks/commands/start.yml @@ -1,6 +1,25 @@ --- ## start service - name: Start node + become: true ansible.builtin.service: name: hychain.service - state: started \ No newline at end of file + state: started + +- name: Assert + when: "'molecule' in groups" + block: + - name: Get service info + become: true + ansible.builtin.systemd: + name: "hychain" + register: _hychain + + - debug: + var: _hychain.status.ActiveState + + - name: Check service + ansible.builtin.assert: + that: + - _hychain['status']['ActiveState']=='active' + quiet: true \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/stop.yml b/depin/services/roles/hychain/tasks/commands/stop.yml index 3fc753d1..e2a86154 100644 --- a/depin/services/roles/hychain/tasks/commands/stop.yml +++ b/depin/services/roles/hychain/tasks/commands/stop.yml @@ -1,6 +1,22 @@ --- ## stop service - name: Stop node + become: true ansible.builtin.service: name: hychain.service - state: stopped \ No newline at end of file + state: stopped + +- name: Assert + when: "'molecule' in groups" + block: + - name: Get service info + become: true + ansible.builtin.systemd: + name: "hychain" + register: _hychain + + - name: Check service + ansible.builtin.assert: + that: + - _hychain['status']['ActiveState']=='inactive' + quiet: true \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/uninstall.yml b/depin/services/roles/hychain/tasks/commands/uninstall.yml index 67ede722..2b951244 100644 --- a/depin/services/roles/hychain/tasks/commands/uninstall.yml +++ b/depin/services/roles/hychain/tasks/commands/uninstall.yml @@ -1,6 +1,7 @@ --- ## stop and uninstall service (remove all associated files) -- name: Start service hychain.service, if not started +- name: Stop service hychain.service + become: true ansible.builtin.systemd_service: name: hychain state: stopped @@ -8,6 +9,7 @@ daemon_reload: true - name: Delete content & directory + become: true ansible.builtin.file: state: absent path: /opt/hychain \ No newline at end of file From eeec2f0555a4440406c1ccc831086a0c2ef4b9c1 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Wed, 11 Sep 2024 17:01:43 +0200 Subject: [PATCH 07/18] Update autonomi service with molecule integration --- .../roles/autonomi/tasks/commands/install.yml | 15 ++------------- .../roles/autonomi/tasks/commands/restart.yml | 1 - .../roles/autonomi/tasks/commands/start.yml | 19 ++++++++++++++++++- .../roles/autonomi/tasks/commands/stop.yml | 1 + .../autonomi/tasks/commands/uninstall.yml | 9 ++------- 5 files changed, 23 insertions(+), 22 deletions(-) diff --git a/depin/services/roles/autonomi/tasks/commands/install.yml b/depin/services/roles/autonomi/tasks/commands/install.yml index 19d2f206..6d2a90e5 100644 --- a/depin/services/roles/autonomi/tasks/commands/install.yml +++ b/depin/services/roles/autonomi/tasks/commands/install.yml @@ -10,6 +10,7 @@ dest: /opt/autonomi/{{ autonomi_version }} - name: Download binary to tmp + become: true ansible.builtin.get_url: url: "{{ autonomi_safeup_download_url }}" dest: /tmp/safeup_service_install.sh @@ -38,6 +39,7 @@ command: /opt/autonomi/{{ autonomi_version }}/safenode-manager add --count {{ autonomi_count }} --peer /ip4/139.59.168.228/udp/56309/quic-v1/p2p/12D3KooWFTMtaqu24ddDSXk9v5YxnuhJmTLFRunER1CG4wZ2XLUU - name: Create systemd file + become: true ansible.builtin.copy: content: | [Unit] @@ -58,22 +60,9 @@ WantedBy=multi-user.target dest: /etc/systemd/system/autonomi.service -- name: Start service autonomi.service, if not started - ansible.builtin.systemd_service: - name: autonomi - state: stopped - enabled: true - daemon_reload: true - - name: Start ansible.builtin.include_tasks: commands/start.yml # relative to playbook (molecule - default) -- name: Assert Install - when: "'molecule' in groups" - block: - - name: Common install asserts - ansible.builtin.include_tasks: - file: tasks/asserts_install.yml # unique asserts go here diff --git a/depin/services/roles/autonomi/tasks/commands/restart.yml b/depin/services/roles/autonomi/tasks/commands/restart.yml index 1fa0088d..8705cfba 100644 --- a/depin/services/roles/autonomi/tasks/commands/restart.yml +++ b/depin/services/roles/autonomi/tasks/commands/restart.yml @@ -1,6 +1,5 @@ --- ## stop and start service ---- - name: Stop ansible.builtin.include_tasks: commands/stop.yml diff --git a/depin/services/roles/autonomi/tasks/commands/start.yml b/depin/services/roles/autonomi/tasks/commands/start.yml index 5b112f28..a5219ed9 100644 --- a/depin/services/roles/autonomi/tasks/commands/start.yml +++ b/depin/services/roles/autonomi/tasks/commands/start.yml @@ -1,6 +1,23 @@ --- ## start service - name: Start node + become: true ansible.builtin.service: name: autonomi.service - state: started \ No newline at end of file + state: started + +- name: Assert + when: "'molecule' in groups" + block: + - name: Get service info + become: true + ansible.builtin.systemd: + name: "autonomi" + register: _autonomi + + - name: Check service + ansible.builtin.assert: + that: + - _autonomi['status']['ActiveState']=='active' + + quiet: true \ No newline at end of file diff --git a/depin/services/roles/autonomi/tasks/commands/stop.yml b/depin/services/roles/autonomi/tasks/commands/stop.yml index fa7ef088..888bd3fc 100644 --- a/depin/services/roles/autonomi/tasks/commands/stop.yml +++ b/depin/services/roles/autonomi/tasks/commands/stop.yml @@ -1,6 +1,7 @@ --- ## stop service - name: Stop node + become: true ansible.builtin.service: name: autonomi.service state: stopped \ No newline at end of file diff --git a/depin/services/roles/autonomi/tasks/commands/uninstall.yml b/depin/services/roles/autonomi/tasks/commands/uninstall.yml index 7d9df1f4..bcdebe23 100644 --- a/depin/services/roles/autonomi/tasks/commands/uninstall.yml +++ b/depin/services/roles/autonomi/tasks/commands/uninstall.yml @@ -9,18 +9,13 @@ state: absent path: /opt/autonomi -- name: Delete serviceS +- name: Delete services become: true ansible.builtin.file: state: absent path: /etc/systemd/system/autonomi.service # relative to playbook (molecule - default) -- name: Assert Uninstall - when: "'molecule' in groups" - block: - - name: Common install asserts - ansible.builtin.include_tasks: - file: tasks/asserts_uninstall.yml + # unique asserts go here From 49ea4484f641e44763de497e0ad5e5db1d097ab1 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Mon, 16 Sep 2024 12:19:58 +0200 Subject: [PATCH 08/18] Update hychain service --- .../services/roles/hychain/defaults/main.yml | 1 + .../roles/hychain/tasks/commands/restart.yml | 24 +++- .../roles/hychain/tasks/commands/start.yml | 6 +- .../services/roles/hychain/tasks/metrics.yml | 63 ++++++++++ .../roles/hychain/templates/collector.py.j2 | 113 ++++++++++++++++++ .../hychain/templates/metrics.service.j2 | 18 +++ .../hychain/templates/systemd.service.j2 | 12 -- 7 files changed, 223 insertions(+), 14 deletions(-) create mode 100644 depin/services/roles/hychain/tasks/metrics.yml create mode 100644 depin/services/roles/hychain/templates/collector.py.j2 create mode 100644 depin/services/roles/hychain/templates/metrics.service.j2 delete mode 100644 depin/services/roles/hychain/templates/systemd.service.j2 diff --git a/depin/services/roles/hychain/defaults/main.yml b/depin/services/roles/hychain/defaults/main.yml index 898b1747..351efea9 100644 --- a/depin/services/roles/hychain/defaults/main.yml +++ b/depin/services/roles/hychain/defaults/main.yml @@ -1,6 +1,7 @@ --- # defaults file for hychain hychain_cmd: install +hychain_metrics_port: 33002 hychain_version: 0.0.1 hychain_private_key: "0x64c077bf4de3b020f874e8a9384cbdd98dafeb49cc9e78f0996771f86c0c48e2" hychain_download_url: "https://github.com/HYCHAIN/guardian-node-software/archive/refs/heads/main.zip" diff --git a/depin/services/roles/hychain/tasks/commands/restart.yml b/depin/services/roles/hychain/tasks/commands/restart.yml index 416fa528..871bf6ec 100644 --- a/depin/services/roles/hychain/tasks/commands/restart.yml +++ b/depin/services/roles/hychain/tasks/commands/restart.yml @@ -4,4 +4,26 @@ become: true ansible.builtin.service: name: hychain.service - state: restarted \ No newline at end of file + state: restarted + +- name: Assert + when: "'molecule' in groups" + block: + - name: Get service info + become: true + ansible.builtin.systemd: + name: "hychain" + register: _hychain + + - debug: + var: _hychain.status.ActiveState + + - name: Check service + ansible.builtin.assert: + that: + - _hychain['status']['ActiveState']=='active' + quiet: true + + - name: Set pid + ansible.builtin.set_fact: + _presearch_pid: "{{ _hychain['status']['ExecMainPID'] }}" \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/commands/start.yml b/depin/services/roles/hychain/tasks/commands/start.yml index fe93052c..8d7244c8 100644 --- a/depin/services/roles/hychain/tasks/commands/start.yml +++ b/depin/services/roles/hychain/tasks/commands/start.yml @@ -22,4 +22,8 @@ ansible.builtin.assert: that: - _hychain['status']['ActiveState']=='active' - quiet: true \ No newline at end of file + quiet: true + + - name: Set pid + ansible.builtin.set_fact: + _presearch_pid: "{{ _hychain['status']['ExecMainPID'] }}" \ No newline at end of file diff --git a/depin/services/roles/hychain/tasks/metrics.yml b/depin/services/roles/hychain/tasks/metrics.yml new file mode 100644 index 00000000..14b1a6ab --- /dev/null +++ b/depin/services/roles/hychain/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 + + # # https://docs.presearch.io/nodes/api + # # Requests with stats (stats=true): Up to 4 requests per hour + #- 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: + src: "{{ item }}" + state: absent + loop: + - /etc/systemd/system/collector-{{ _name }}.service + - /etc/deeep-network/collectors/{{ _name }}.py diff --git a/depin/services/roles/hychain/templates/collector.py.j2 b/depin/services/roles/hychain/templates/collector.py.j2 new file mode 100644 index 00000000..693856c2 --- /dev/null +++ b/depin/services/roles/hychain/templates/collector.py.j2 @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 +import os +import requests +import socket +import time + +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 collect(self): + result = [] + service_metrics_collected = 1 + + device = 'molecule' + # fetch device hostname + # @note - LXD specific feature + {% if not molecule_inventory is defined -%} + if LXD_UTILS_IMPORT_ERROR is None: + device = lxd_get("1.0/config/user.location")['value'] + device = device['value'] + {% endif %} + + hostname = socket.gethostname() + service = '{{ _name }}' + + _labels = ['device', 'instance', 'service'] + _label_values = [device, hostname, service] + + # fetch public key + try: + file = open(pub_key_file, 'rb') + pub_key = file.read() + pub_key = pub_key.decode("utf-8") + pub_key = pub_key.splitlines() + pub_key = " ".join(pub_key[1:-1]) + file.close() + except FileNotFoundError: + service_metrics_collected = 0 + + api_key = os.getenv("PRESEARCH_API_KEY") + + if api_key is None: + service_metrics_collected = 0 + + if service_metrics_collected == 1: + # https://docs.presearch.io/nodes/api + # no local status or CLI + params = { + 'connected': True, + 'disconnected': True, + 'inactive': True, + 'stats': True, + 'public_keys': pub_key + } + url = 'https://nodes.presearch.com/api/nodes/status/' + api_key + + try: + response = requests.get(url, params=params) + response.raise_for_status() + + data = response.json() + data = data['nodes'][pub_key.replace(' ', '\n')] + + state = 1 if data['status']['connected'] else 0 + # even if connected if blocked = false + state = 1 if not data['status']['blocked'] else 0 + + g1 = GaugeMetricFamily('state','current state', labels=_labels) + g1.add_metric(_label_values, value=state) + + state_uptime = int(data['status']['minutes_in_current_state']) * 60 ## to seconds + + g2 = GaugeMetricFamily('current_state_uptime','seconds in current state', labels=_labels) + g2.add_metric(_label_values, value=state_uptime) + + total_uptime = data['period']['total_uptime_seconds'] + + g3 = GaugeMetricFamily('total_uptime','seconds in last 24 hours', labels=_labels) + g3.add_metric(_label_values, value=total_uptime) + + result.extend([g1, g2, g3]) + service_metrics_collected = 1 + except requests.exceptions.HTTPError as err: + service_metrics_collected = 0 + + 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) diff --git a/depin/services/roles/hychain/templates/metrics.service.j2 b/depin/services/roles/hychain/templates/metrics.service.j2 new file mode 100644 index 00000000..aaf99814 --- /dev/null +++ b/depin/services/roles/hychain/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 diff --git a/depin/services/roles/hychain/templates/systemd.service.j2 b/depin/services/roles/hychain/templates/systemd.service.j2 deleted file mode 100644 index 06649ceb..00000000 --- a/depin/services/roles/hychain/templates/systemd.service.j2 +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description= -After=network-online.target -Wants=network.target - -[Service] -Type=simple -ExecStart= -Restart=always - -[Install] -WantedBy=multi-user.target From f7277594d7d87be6968cfc8fd8283c566abd9f24 Mon Sep 17 00:00:00 2001 From: Anthony Anderson Date: Mon, 16 Sep 2024 11:24:33 -0400 Subject: [PATCH 09/18] chore(hychain): bug fixes --- depin/core/galaxy.yml | 2 +- depin/services/roles/hychain/README.md | 38 ------- .../services/roles/hychain/defaults/main.yml | 6 +- .../services/roles/hychain/handlers/main.yml | 2 - .../roles/hychain/tasks/commands/install.yml | 107 +++++++++--------- .../roles/hychain/tasks/commands/reset.yml | 2 +- .../roles/hychain/tasks/commands/restart.yml | 4 +- .../roles/hychain/tasks/commands/start.yml | 6 +- .../roles/hychain/tasks/commands/stop.yml | 3 +- .../hychain/tasks/commands/uninstall.yml | 24 ++-- depin/services/roles/hychain/tasks/main.yml | 4 +- .../services/roles/hychain/tasks/metrics.yml | 31 +++-- .../roles/hychain/templates/collector.py.j2 | 59 ---------- .../hychain/templates/systemd.service.j2 | 14 +++ depin/services/roles/hychain/vars/main.yml | 9 -- .../presearch/tasks/commands/install.yml | 6 + .../presearch/tasks/commands/uninstall.yml | 6 + .../roles/presearch/tasks/metrics.yml | 19 ++-- .../storagechain/tasks/commands/install.yml | 6 + .../storagechain/tasks/commands/uninstall.yml | 6 + .../roles/storagechain}/tasks/metrics.yml | 17 ++- molecule/default/tasks/tests.yml | 5 - molecule/default/tasks/vars/hychain.yml | 5 + skeletons/role/tasks/commands/install.yml | 7 ++ skeletons/role/tasks/commands/uninstall.yml | 6 + skeletons/role/tasks/metrics.yml.j2 | 60 ++++++++++ 26 files changed, 230 insertions(+), 224 deletions(-) delete mode 100644 depin/services/roles/hychain/README.md delete mode 100644 depin/services/roles/hychain/handlers/main.yml create mode 100644 depin/services/roles/hychain/templates/systemd.service.j2 delete mode 100644 depin/services/roles/hychain/vars/main.yml rename {skeletons/role => depin/services/roles/storagechain}/tasks/metrics.yml (77%) create mode 100644 molecule/default/tasks/vars/hychain.yml create mode 100644 skeletons/role/tasks/metrics.yml.j2 diff --git a/depin/core/galaxy.yml b/depin/core/galaxy.yml index e755da7a..c51ecf07 100644 --- a/depin/core/galaxy.yml +++ b/depin/core/galaxy.yml @@ -8,7 +8,7 @@ namespace: depin name: core # The version of the collection. Must be compatible with semantic versioning -version: 1.1.0 +version: 1.2.0 # The path to the Markdown (.md) readme file. This path is relative to the root of the collection readme: README.md diff --git a/depin/services/roles/hychain/README.md b/depin/services/roles/hychain/README.md deleted file mode 100644 index 225dd44b..00000000 --- a/depin/services/roles/hychain/README.md +++ /dev/null @@ -1,38 +0,0 @@ -Role Name -========= - -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. - -Role Variables --------------- - -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. - -Dependencies ------------- - -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -Example Playbook ----------------- - -Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - - hosts: servers - roles: - - { role: username.rolename, x: 42 } - -License -------- - -BSD - -Author Information ------------------- - -An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/depin/services/roles/hychain/defaults/main.yml b/depin/services/roles/hychain/defaults/main.yml index 351efea9..bd6c395f 100644 --- a/depin/services/roles/hychain/defaults/main.yml +++ b/depin/services/roles/hychain/defaults/main.yml @@ -1,8 +1,8 @@ --- -# defaults file for hychain -hychain_cmd: install +hychain_cmd: "{{ depin_cmd | default('install') }}" hychain_metrics_port: 33002 + hychain_version: 0.0.1 -hychain_private_key: "0x64c077bf4de3b020f874e8a9384cbdd98dafeb49cc9e78f0996771f86c0c48e2" hychain_download_url: "https://github.com/HYCHAIN/guardian-node-software/archive/refs/heads/main.zip" +hychain_private_key: "{{ notdefined | mandatory }}" diff --git a/depin/services/roles/hychain/handlers/main.yml b/depin/services/roles/hychain/handlers/main.yml deleted file mode 100644 index 61e51c3d..00000000 --- a/depin/services/roles/hychain/handlers/main.yml +++ /dev/null @@ -1,2 +0,0 @@ ---- -# handlers file for skeleton diff --git a/depin/services/roles/hychain/tasks/commands/install.yml b/depin/services/roles/hychain/tasks/commands/install.yml index 58eacf36..bd3a932b 100644 --- a/depin/services/roles/hychain/tasks/commands/install.yml +++ b/depin/services/roles/hychain/tasks/commands/install.yml @@ -1,13 +1,4 @@ --- -## install dependencies and start service -- name: Install dependencies - become: true - ansible.builtin.apt: - name: - - zip - state: present - update_cache: true - - name: Create program folder become: true ansible.builtin.file: @@ -18,10 +9,12 @@ dest: /opt/hychain/{{ hychain_version }} - name: Download binary to tmp - ansible.builtin.get_url: - url: "{{ hychain_download_url }}" - dest: /tmp/hychain_service_{{ hychain_version }}.zip - + become: true + ansible.builtin.unarchive: + remote_src: true + src: "{{ hychain_download_url }}" + dest: /tmp + failed_when: false - name: Find binary become: true @@ -29,54 +22,58 @@ paths: - /tmp - "{{ hychain_download_path | default('/tmp') }}" - patterns: 'hychain_service_{{ hychain_version }}.zip$' + patterns: 'guardian-cli-linux$' use_regex: true get_checksum: true recurse: true register: _download -- name: Extract binary +- name: Install node + when: _download['files'] | length > 0 become: true - ansible.builtin.unarchive: - src: /tmp/hychain_service_{{ hychain_version }}.zip - dest: /opt/hychain/{{ hychain_version }} - remote_src: yes + vars: + _files: | + {{ + _download['files'] | + selectattr('checksum', 'equalto', hychain_checksum | default('')) + }} + _path: "{{ (_files | first | default(_download['files'] | first))['path'] }}" + block: + - name: Copy required files + ansible.builtin.copy: + remote_src: true + src: "{{ hychain_file }}" + dest: /opt/hychain/{{ hychain_version }} + mode: '0755' + loop: + - "{{ _path | dirname }}/validation-engine" + - "{{ _path }}" + loop_control: + loop_var: hychain_file -- name: Copy start file - become: true - ansible.builtin.copy: - content: | - #!/bin/bash - cd /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/ - ./guardian-cli-linux guardian run {{ hychain_private_key }} - dest: /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/hychain-start.sh + - name: Symlink binary to path + become: true + ansible.builtin.file: + src: /opt/hychain/{{ hychain_version }}/{{ _path | basename }} + path: /usr/local/bin/hychain + mode: '0755' + owner: root + group: root + state: hard + force: true -- name: Create systemd file - become: true - ansible.builtin.copy: - content: | - [Unit] - Description=hychain service - After=network.target - - [Service] - User=root - Group=root - Type=simple - ExecStart=/bin/bash /opt/hychain/{{ hychain_version }}/guardian-node-software-main/cli/linux/hychain-start.sh - Restart=always - RestartSec=5 - RuntimeMaxSec=6h - PIDFile=/tmp/hychain.pid - - [Install] - WantedBy=multi-user.target - dest: /etc/systemd/system/hychain.service - -- name: Start node - ansible.builtin.include_tasks: - file: commands/start.yml - -# relative to playbook (molecule - default) + - name: Create systemd file + ansible.builtin.template: + src: templates/systemd.service.j2 + dest: /etc/systemd/system/hychain.service + mode: '0644' + + - name: Start node + ansible.builtin.include_tasks: + file: commands/start.yml - # unique asserts go here +- name: Install metrics + vars: + depin_cmd: install + ansible.builtin.include_tasks: + file: tasks/metrics.yml diff --git a/depin/services/roles/hychain/tasks/commands/reset.yml b/depin/services/roles/hychain/tasks/commands/reset.yml index b7af2305..7a48419b 100644 --- a/depin/services/roles/hychain/tasks/commands/reset.yml +++ b/depin/services/roles/hychain/tasks/commands/reset.yml @@ -6,4 +6,4 @@ - name: Install node ansible.builtin.include_tasks: - file: commands/install.yml \ No newline at end of file + file: commands/install.yml diff --git a/depin/services/roles/hychain/tasks/commands/restart.yml b/depin/services/roles/hychain/tasks/commands/restart.yml index 871bf6ec..21367e4e 100644 --- a/depin/services/roles/hychain/tasks/commands/restart.yml +++ b/depin/services/roles/hychain/tasks/commands/restart.yml @@ -1,5 +1,4 @@ --- -## stop and start service - name: Restarted node become: true ansible.builtin.service: @@ -22,8 +21,9 @@ ansible.builtin.assert: that: - _hychain['status']['ActiveState']=='active' + - _hychain_pid | int != _hychain['status']['ExecMainPID'] | int quiet: true - name: Set pid ansible.builtin.set_fact: - _presearch_pid: "{{ _hychain['status']['ExecMainPID'] }}" \ No newline at end of file + _hychain_pid: "{{ _hychain['status']['ExecMainPID'] }}" diff --git a/depin/services/roles/hychain/tasks/commands/start.yml b/depin/services/roles/hychain/tasks/commands/start.yml index 8d7244c8..077c7479 100644 --- a/depin/services/roles/hychain/tasks/commands/start.yml +++ b/depin/services/roles/hychain/tasks/commands/start.yml @@ -4,6 +4,8 @@ become: true ansible.builtin.service: name: hychain.service + enabled: true + daemon_reload: true state: started - name: Assert @@ -12,7 +14,7 @@ - name: Get service info become: true ansible.builtin.systemd: - name: "hychain" + name: hychain register: _hychain - debug: @@ -26,4 +28,4 @@ - name: Set pid ansible.builtin.set_fact: - _presearch_pid: "{{ _hychain['status']['ExecMainPID'] }}" \ No newline at end of file + _hychain_pid: "{{ _hychain['status']['ExecMainPID'] }}" diff --git a/depin/services/roles/hychain/tasks/commands/stop.yml b/depin/services/roles/hychain/tasks/commands/stop.yml index e2a86154..b8f42156 100644 --- a/depin/services/roles/hychain/tasks/commands/stop.yml +++ b/depin/services/roles/hychain/tasks/commands/stop.yml @@ -4,6 +4,7 @@ become: true ansible.builtin.service: name: hychain.service + enabled: false state: stopped - name: Assert @@ -19,4 +20,4 @@ ansible.builtin.assert: that: - _hychain['status']['ActiveState']=='inactive' - quiet: true \ No newline at end of file + quiet: true diff --git a/depin/services/roles/hychain/tasks/commands/uninstall.yml b/depin/services/roles/hychain/tasks/commands/uninstall.yml index 2b951244..ac871c9d 100644 --- a/depin/services/roles/hychain/tasks/commands/uninstall.yml +++ b/depin/services/roles/hychain/tasks/commands/uninstall.yml @@ -1,15 +1,21 @@ --- -## stop and uninstall service (remove all associated files) -- name: Stop service hychain.service - become: true - ansible.builtin.systemd_service: - name: hychain - state: stopped - enabled: false - daemon_reload: true +- name: Disable systemd service + ansible.builtin.include_tasks: commands/stop.yml + +- name: Uninstall metrics + vars: + depin_cmd: uninstall + ansible.builtin.include_tasks: + file: tasks/metrics.yml - name: Delete content & directory become: true ansible.builtin.file: state: absent - path: /opt/hychain \ No newline at end of file + path: "{{ uninstall_file }}" + loop: + - /etc/systemd/system/hychain.service + - /usr/local/bin/hychain + - /opt/hychain/{{ hychain_version }} + loop_control: + loop_var: uninstall_file diff --git a/depin/services/roles/hychain/tasks/main.yml b/depin/services/roles/hychain/tasks/main.yml index 918387ca..916f4c80 100644 --- a/depin/services/roles/hychain/tasks/main.yml +++ b/depin/services/roles/hychain/tasks/main.yml @@ -1,4 +1,4 @@ --- -- name: Hychain {{ service_cmd | default(hychain_cmd) }} +- name: Hychain {{ hychain_cmd }} ansible.builtin.include_tasks: - file: commands/{{ service_cmd | default(hychain_cmd) }}.yml \ No newline at end of file + file: commands/{{ hychain_cmd }}.yml diff --git a/depin/services/roles/hychain/tasks/metrics.yml b/depin/services/roles/hychain/tasks/metrics.yml index 14b1a6ab..7875ee7b 100644 --- a/depin/services/roles/hychain/tasks/metrics.yml +++ b/depin/services/roles/hychain/tasks/metrics.yml @@ -2,7 +2,8 @@ - name: Install metrics vars: _name: "{{ role_name | replace('_', '-') }}" - when: vars[role_name + '_cmd'] != 'uninstall' + _metrics_port: hychain_metrics_port + when: hychain_cmd != 'uninstall' become: true block: - name: Ensure directory exists @@ -23,19 +24,15 @@ src: templates/metrics.service.j2 dest: /etc/systemd/system/collector-{{ _name }}.service - # # https://docs.presearch.io/nodes/api - # # Requests with stats (stats=true): Up to 4 requests per hour - #- 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: 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: @@ -45,7 +42,9 @@ state: started - name: Uninstall metrics - when: vars[role_name + '_cmd'] == 'uninstall' + vars: + _name: "{{ role_name | replace('_', '-') }}" + when: hychain_cmd == 'uninstall' become: true block: - name: Stop service @@ -56,7 +55,7 @@ - name: Remove files ansible.builtin.file: - src: "{{ item }}" + path: "{{ item }}" state: absent loop: - /etc/systemd/system/collector-{{ _name }}.service diff --git a/depin/services/roles/hychain/templates/collector.py.j2 b/depin/services/roles/hychain/templates/collector.py.j2 index 693856c2..b7bc8611 100644 --- a/depin/services/roles/hychain/templates/collector.py.j2 +++ b/depin/services/roles/hychain/templates/collector.py.j2 @@ -15,8 +15,6 @@ except ImportError as imp_exc: else: LXD_UTILS_IMPORT_ERROR = None - - class MetricsCollector(Collector): """Collector for Gala Node information""" @@ -39,63 +37,6 @@ class MetricsCollector(Collector): _labels = ['device', 'instance', 'service'] _label_values = [device, hostname, service] - # fetch public key - try: - file = open(pub_key_file, 'rb') - pub_key = file.read() - pub_key = pub_key.decode("utf-8") - pub_key = pub_key.splitlines() - pub_key = " ".join(pub_key[1:-1]) - file.close() - except FileNotFoundError: - service_metrics_collected = 0 - - api_key = os.getenv("PRESEARCH_API_KEY") - - if api_key is None: - service_metrics_collected = 0 - - if service_metrics_collected == 1: - # https://docs.presearch.io/nodes/api - # no local status or CLI - params = { - 'connected': True, - 'disconnected': True, - 'inactive': True, - 'stats': True, - 'public_keys': pub_key - } - url = 'https://nodes.presearch.com/api/nodes/status/' + api_key - - try: - response = requests.get(url, params=params) - response.raise_for_status() - - data = response.json() - data = data['nodes'][pub_key.replace(' ', '\n')] - - state = 1 if data['status']['connected'] else 0 - # even if connected if blocked = false - state = 1 if not data['status']['blocked'] else 0 - - g1 = GaugeMetricFamily('state','current state', labels=_labels) - g1.add_metric(_label_values, value=state) - - state_uptime = int(data['status']['minutes_in_current_state']) * 60 ## to seconds - - g2 = GaugeMetricFamily('current_state_uptime','seconds in current state', labels=_labels) - g2.add_metric(_label_values, value=state_uptime) - - total_uptime = data['period']['total_uptime_seconds'] - - g3 = GaugeMetricFamily('total_uptime','seconds in last 24 hours', labels=_labels) - g3.add_metric(_label_values, value=total_uptime) - - result.extend([g1, g2, g3]) - service_metrics_collected = 1 - except requests.exceptions.HTTPError as err: - service_metrics_collected = 0 - success = GaugeMetricFamily('service_metrics_collected','service metrics collected successfully', labels=_labels) success.add_metric(_label_values, value=service_metrics_collected) diff --git a/depin/services/roles/hychain/templates/systemd.service.j2 b/depin/services/roles/hychain/templates/systemd.service.j2 new file mode 100644 index 00000000..b1228b78 --- /dev/null +++ b/depin/services/roles/hychain/templates/systemd.service.j2 @@ -0,0 +1,14 @@ +[Unit] +Description=hychain service +After=network.target + +[Service] +Type=simple +WorkingDirectory=/opt/hychain/{{ hychain_version }} +ExecStart=/usr/local/bin/hychain guardian run {{ hychain_private_key }} +Restart=always +RuntimeMaxSec=6h +PIDFile=/var/run/hychain.pid + +[Install] +WantedBy=multi-user.target diff --git a/depin/services/roles/hychain/vars/main.yml b/depin/services/roles/hychain/vars/main.yml deleted file mode 100644 index 131b70bb..00000000 --- a/depin/services/roles/hychain/vars/main.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -deb_architecture: { # noqa var-naming[no-role-prefix] - "aarch64": "arm64", - "x86_64": "amd64", - "arm": "arm", - "i386": "386" -} - -system_architecture: "{{ [ansible_architecture] | map('extract', deb_architecture) | first }}" # noqa var-naming[no-role-prefix] diff --git a/depin/services/roles/presearch/tasks/commands/install.yml b/depin/services/roles/presearch/tasks/commands/install.yml index d56cbfff..81ac67d5 100644 --- a/depin/services/roles/presearch/tasks/commands/install.yml +++ b/depin/services/roles/presearch/tasks/commands/install.yml @@ -7,5 +7,11 @@ ansible.builtin.include_tasks: file: commands/start.yml +- name: Install metrics + vars: + depin_cmd: install + ansible.builtin.include_tasks: + file: tasks/metrics.yml + ## should we backup keys # https://docs.presearch.io/nodes/troubleshooting-and-maintenance/backup-method-for-linux diff --git a/depin/services/roles/presearch/tasks/commands/uninstall.yml b/depin/services/roles/presearch/tasks/commands/uninstall.yml index 1eca954a..68996f3c 100644 --- a/depin/services/roles/presearch/tasks/commands/uninstall.yml +++ b/depin/services/roles/presearch/tasks/commands/uninstall.yml @@ -5,6 +5,12 @@ name: presearch state: absent +- name: Uninstall metrics + vars: + depin_cmd: uninstall + ansible.builtin.include_tasks: + file: tasks/metrics.yml + - name: Assert when: "'molecule' in groups" block: diff --git a/depin/services/roles/presearch/tasks/metrics.yml b/depin/services/roles/presearch/tasks/metrics.yml index 2e804e10..99c648c6 100644 --- a/depin/services/roles/presearch/tasks/metrics.yml +++ b/depin/services/roles/presearch/tasks/metrics.yml @@ -2,7 +2,8 @@ - name: Install metrics vars: _name: "{{ role_name | replace('_', '-') }}" - when: vars[role_name + '_cmd'] != 'uninstall' + _metrics_port: presearch_metrics_port + when: presearch_cmd != 'uninstall' become: true block: - name: Ensure directory exists @@ -23,19 +24,15 @@ src: templates/metrics.service.j2 dest: /etc/systemd/system/collector-{{ _name }}.service - # # https://docs.presearch.io/nodes/api - # # Requests with stats (stats=true): Up to 4 requests per hour - 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: @@ -45,7 +42,9 @@ state: started - name: Uninstall metrics - when: vars[role_name + '_cmd'] == 'uninstall' + vars: + _name: "{{ role_name | replace('_', '-') }}" + when: presearch_cmd == 'uninstall' become: true block: - name: Stop service @@ -56,7 +55,7 @@ - name: Remove files ansible.builtin.file: - src: "{{ item }}" + path: "{{ item }}" state: absent loop: - /etc/systemd/system/collector-{{ _name }}.service diff --git a/depin/services/roles/storagechain/tasks/commands/install.yml b/depin/services/roles/storagechain/tasks/commands/install.yml index 5f160d8a..faa84f78 100644 --- a/depin/services/roles/storagechain/tasks/commands/install.yml +++ b/depin/services/roles/storagechain/tasks/commands/install.yml @@ -142,6 +142,12 @@ delay: 5 until: send_ipv4_address is not failed +- name: Install metrics + vars: + depin_cmd: install + ansible.builtin.include_tasks: + file: tasks/metrics.yml + - name: Assert when: "'molecule' in groups" block: diff --git a/depin/services/roles/storagechain/tasks/commands/uninstall.yml b/depin/services/roles/storagechain/tasks/commands/uninstall.yml index 45e9fce7..a7887699 100644 --- a/depin/services/roles/storagechain/tasks/commands/uninstall.yml +++ b/depin/services/roles/storagechain/tasks/commands/uninstall.yml @@ -2,6 +2,12 @@ - name: Disable systemd service ansible.builtin.include_tasks: commands/stop.yml +- name: Uninstall metrics + vars: + depin_cmd: uninstall + ansible.builtin.include_tasks: + file: tasks/metrics.yml + - name: Uninstall dependencies vars: depin_cmd: uninstall # should be set but if not diff --git a/skeletons/role/tasks/metrics.yml b/depin/services/roles/storagechain/tasks/metrics.yml similarity index 77% rename from skeletons/role/tasks/metrics.yml rename to depin/services/roles/storagechain/tasks/metrics.yml index 2e821c93..c8c7927a 100644 --- a/skeletons/role/tasks/metrics.yml +++ b/depin/services/roles/storagechain/tasks/metrics.yml @@ -2,7 +2,8 @@ - name: Install metrics vars: _name: "{{ role_name | replace('_', '-') }}" - when: vars[role_name + '_cmd'] != 'uninstall' + _metrics_port: storagechain_metrics_port + when: storagechain_cmd != 'uninstall' become: true block: - name: Ensure directory exists @@ -23,19 +24,15 @@ src: templates/metrics.service.j2 dest: /etc/systemd/system/collector-{{ _name }}.service - # # https://docs.presearch.io/nodes/api - # # Requests with stats (stats=true): Up to 4 requests per hour - name: Update prometheus.conf vars: _block: | - name: {{ _name }} - url: 'http://127.0.0.1:{{ vars[role_name + '_metrics_port'] }}/metrics' - update_every: 900 + 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: @@ -45,7 +42,9 @@ state: started - name: Uninstall metrics - when: vars[role_name + '_cmd'] == 'uninstall' + vars: + _name: "{{ role_name | replace('_', '-') }}" + when: storagechain_cmd == 'uninstall' become: true block: - name: Stop service @@ -56,7 +55,7 @@ - name: Remove files ansible.builtin.file: - src: "{{ item }}" + path: "{{ item }}" state: absent loop: - /etc/systemd/system/collector-{{ _name }}.service diff --git a/molecule/default/tasks/tests.yml b/molecule/default/tasks/tests.yml index 9ba2ff2a..c5d96c32 100644 --- a/molecule/default/tasks/tests.yml +++ b/molecule/default/tasks/tests.yml @@ -8,11 +8,6 @@ - name: Block for error handling block: - - name: Metrics - ansible.builtin.include_role: - name: "{{ test_role }}" - tasks_from: metrics.yml - - name: Install ansible.builtin.include_role: name: "{{ test_role }}" diff --git a/molecule/default/tasks/vars/hychain.yml b/molecule/default/tasks/vars/hychain.yml new file mode 100644 index 00000000..237a2212 --- /dev/null +++ b/molecule/default/tasks/vars/hychain.yml @@ -0,0 +1,5 @@ +test_role: depin.services.hychain + +# override role variables +# https://explorer.hytopia.com/address/0x488b60121887a638026B5a8dEa70B7200E4D8Eb0 +hychain_private_key: "0x64c077bf4de3b020f874e8a9384cbdd98dafeb49cc9e78f0996771f86c0c48e2" diff --git a/skeletons/role/tasks/commands/install.yml b/skeletons/role/tasks/commands/install.yml index 7df08a79..ba989c57 100644 --- a/skeletons/role/tasks/commands/install.yml +++ b/skeletons/role/tasks/commands/install.yml @@ -1,9 +1,16 @@ --- ## install dependencies and service + - name: Start ansible.builtin.include_tasks: commands/start.yml +- name: Install metrics + vars: + depin_cmd: install + ansible.builtin.include_tasks: + file: tasks/metrics.yml + # @todo - assert correct version installed # relative to playbook (molecule - default) - name: Assert Install diff --git a/skeletons/role/tasks/commands/uninstall.yml b/skeletons/role/tasks/commands/uninstall.yml index 7609ce43..d0bbed9e 100644 --- a/skeletons/role/tasks/commands/uninstall.yml +++ b/skeletons/role/tasks/commands/uninstall.yml @@ -2,6 +2,12 @@ - name: Disable systemd service ansible.builtin.include_tasks: commands/stop.yml +- name: Uninstall metrics + vars: + depin_cmd: uninstall + ansible.builtin.include_tasks: + file: tasks/metrics.yml + ## uninstall service and remove all associated files # relative to playbook (molecule - default) diff --git a/skeletons/role/tasks/metrics.yml.j2 b/skeletons/role/tasks/metrics.yml.j2 new file mode 100644 index 00000000..64362951 --- /dev/null +++ b/skeletons/role/tasks/metrics.yml.j2 @@ -0,0 +1,60 @@ +--- +- name: Install metrics + vars: + _name: "{{ "{{ role_name | replace('_', '-') }}" }}" + _metrics_port: {{ role_name | replace('_', '-') }}_metrics_port + when: {{ role_name | replace('_', '-') }}_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 + + - 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: {{ role_name | replace('_', '-') }}_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 From f7bd70e393fd0d8ecf5f443800b5d8005f49c20c Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Tue, 17 Sep 2024 16:33:28 +0700 Subject: [PATCH 10/18] add xai node --- depin/services/roles/xai/defaults/main.yml | 13 +++++++ .../roles/xai/tasks/commands/install.yml | 39 +++++++++++++++++++ .../roles/xai/tasks/commands/reset.yml | 7 ++++ .../roles/xai/tasks/commands/restart.yml | 7 ++++ .../roles/xai/tasks/commands/start.yml | 21 ++++++++++ .../roles/xai/tasks/commands/stop.yml | 6 +++ .../roles/xai/tasks/commands/uninstall.yml | 18 +++++++++ depin/services/roles/xai/tasks/main.yml | 5 +++ 8 files changed, 116 insertions(+) create mode 100644 depin/services/roles/xai/defaults/main.yml create mode 100644 depin/services/roles/xai/tasks/commands/install.yml create mode 100644 depin/services/roles/xai/tasks/commands/reset.yml create mode 100644 depin/services/roles/xai/tasks/commands/restart.yml create mode 100644 depin/services/roles/xai/tasks/commands/start.yml create mode 100644 depin/services/roles/xai/tasks/commands/stop.yml create mode 100644 depin/services/roles/xai/tasks/commands/uninstall.yml create mode 100644 depin/services/roles/xai/tasks/main.yml diff --git a/depin/services/roles/xai/defaults/main.yml b/depin/services/roles/xai/defaults/main.yml new file mode 100644 index 00000000..2bfbeae8 --- /dev/null +++ b/depin/services/roles/xai/defaults/main.yml @@ -0,0 +1,13 @@ +xai_version: "latest" + +xai_download_url: "https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip" + +xai_install_path: "/opt/xai/{{ xai_version }}" + +xai_node_count: 1 + +xai_node_port: 8545 + +xai_start_command: "/opt/xai/{{ xai_version }}/sentry-node-cli-linux start" + +xai_stop_command: "/opt/xai/{{ xai_version }}/sentry-node-cli-linux stop" diff --git a/depin/services/roles/xai/tasks/commands/install.yml b/depin/services/roles/xai/tasks/commands/install.yml new file mode 100644 index 00000000..6e3059a1 --- /dev/null +++ b/depin/services/roles/xai/tasks/commands/install.yml @@ -0,0 +1,39 @@ +--- +# Update the system package list +- name: System update + apt: + update_cache: yes + force_apt_get: yes + cache_valid_time: 3600 + +# Install required dependencies +- name: Install dependencies + apt: + name: + - curl + - unzip + state: present + +# Download XAI Sentry Node CLI +- name: Download XAI Sentry Node CLI + get_url: + url: https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip + dest: /tmp/sentry-node-cli-linux.zip + +# Extract the CLI package to the destination folder +- name: Extract XAI Sentry Node CLI + unarchive: + src: /tmp/sentry-node-cli-linux.zip + dest: /usr/local/bin/ + remote_src: yes + +# Set executable permissions for the CLI binary +- name: Set execute permissions + file: + path: /usr/local/bin/sentry-node-cli-linux + mode: '0755' + state: file + +# Run the installation script for XAI Sentry Node CLI +- name: Run XAI Sentry Node CLI installation + command: /usr/local/bin/sentry-node-cli-linux --install diff --git a/depin/services/roles/xai/tasks/commands/reset.yml b/depin/services/roles/xai/tasks/commands/reset.yml new file mode 100644 index 00000000..d967402b --- /dev/null +++ b/depin/services/roles/xai/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/xai/tasks/commands/restart.yml b/depin/services/roles/xai/tasks/commands/restart.yml new file mode 100644 index 00000000..8705cfba --- /dev/null +++ b/depin/services/roles/xai/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/xai/tasks/commands/start.yml b/depin/services/roles/xai/tasks/commands/start.yml new file mode 100644 index 00000000..8c513608 --- /dev/null +++ b/depin/services/roles/xai/tasks/commands/start.yml @@ -0,0 +1,21 @@ +--- +## Start XAI Sentry Node service in test mode +- name: Start XAI Sentry Node in test mode + become: true + command: ./sentry-node-cli-linux --test-mode + +# Assert that XAI Sentry Node is running +- name: Assert XAI Sentry Node service is running in test mode + when: "'molecule' in groups" + block: + - name: Get service info for XAI Sentry Node + become: true + ansible.builtin.systemd: + name: "xai-sentry" + register: _xai_sentry + + - name: Verify XAI Sentry Node service is active in test mode + ansible.builtin.assert: + that: + - _xai_sentry.status.ActiveState == 'active' + quiet: true diff --git a/depin/services/roles/xai/tasks/commands/stop.yml b/depin/services/roles/xai/tasks/commands/stop.yml new file mode 100644 index 00000000..7254af50 --- /dev/null +++ b/depin/services/roles/xai/tasks/commands/stop.yml @@ -0,0 +1,6 @@ +--- +# Stop XAI Sentry Node service +- name: Stop XAI Sentry Node + ansible.builtin.systemd: + name: xai-sentry + state: stopped diff --git a/depin/services/roles/xai/tasks/commands/uninstall.yml b/depin/services/roles/xai/tasks/commands/uninstall.yml new file mode 100644 index 00000000..05fbda35 --- /dev/null +++ b/depin/services/roles/xai/tasks/commands/uninstall.yml @@ -0,0 +1,18 @@ +--- +# Stop the service before uninstalling +- name: Stop XAI Sentry Node + ansible.builtin.systemd: + name: xai-sentry + state: stopped + +# Remove the binary file for XAI Sentry Node +- name: Remove XAI Sentry Node binary + file: + path: /usr/local/bin/sentry-node-cli-linux + state: absent + +# Remove the systemd service for XAI Sentry Node +- name: Remove systemd service for XAI Sentry + file: + path: /etc/systemd/system/xai-sentry.service + state: absent diff --git a/depin/services/roles/xai/tasks/main.yml b/depin/services/roles/xai/tasks/main.yml new file mode 100644 index 00000000..7e103192 --- /dev/null +++ b/depin/services/roles/xai/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: "XAI {{ service_cmd | default(xai_cmd) }}" + ansible.builtin.include_tasks: + file: "commands/{{ service_cmd | default(xai_cmd) }}.yml" + From f5f8302ff6e06e295e3d6d8e944fd48836881830 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Tue, 17 Sep 2024 17:15:59 +0700 Subject: [PATCH 11/18] add xai node --- .../roles/xai/tasks/commands/install.yml | 67 ++++++++++--------- .../roles/xai/tasks/commands/start.yml | 28 +++----- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/depin/services/roles/xai/tasks/commands/install.yml b/depin/services/roles/xai/tasks/commands/install.yml index 6e3059a1..b039e06d 100644 --- a/depin/services/roles/xai/tasks/commands/install.yml +++ b/depin/services/roles/xai/tasks/commands/install.yml @@ -1,39 +1,40 @@ --- -# Update the system package list -- name: System update - apt: - update_cache: yes - force_apt_get: yes - cache_valid_time: 3600 +- name: Install XAI Sentry Node CLI + hosts: all + become: yes -# Install required dependencies -- name: Install dependencies - apt: - name: - - curl - - unzip - state: present + tasks: + # Update the system package list + - name: System update + apt: + update_cache: yes + force_apt_get: yes + cache_valid_time: 3600 -# Download XAI Sentry Node CLI -- name: Download XAI Sentry Node CLI - get_url: - url: https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip - dest: /tmp/sentry-node-cli-linux.zip + # Install required dependencies + - name: Install dependencies + apt: + name: + - curl + - unzip + state: present -# Extract the CLI package to the destination folder -- name: Extract XAI Sentry Node CLI - unarchive: - src: /tmp/sentry-node-cli-linux.zip - dest: /usr/local/bin/ - remote_src: yes + # Download XAI Sentry Node CLI + - name: Download XAI Sentry Node CLI + get_url: + url: https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip + dest: /tmp/sentry-node-cli-linux.zip -# Set executable permissions for the CLI binary -- name: Set execute permissions - file: - path: /usr/local/bin/sentry-node-cli-linux - mode: '0755' - state: file + # Extract the CLI package to the destination folder + - name: Extract XAI Sentry Node CLI + unarchive: + src: /tmp/sentry-node-cli-linux.zip + dest: /usr/local/bin/ + remote_src: yes -# Run the installation script for XAI Sentry Node CLI -- name: Run XAI Sentry Node CLI installation - command: /usr/local/bin/sentry-node-cli-linux --install + # Set executable permissions for the CLI binary + - name: Set execute permissions + file: + path: /usr/local/bin/sentry-node-cli-linux + mode: '0755' + state: file diff --git a/depin/services/roles/xai/tasks/commands/start.yml b/depin/services/roles/xai/tasks/commands/start.yml index 8c513608..7c470037 100644 --- a/depin/services/roles/xai/tasks/commands/start.yml +++ b/depin/services/roles/xai/tasks/commands/start.yml @@ -1,21 +1,11 @@ --- -## Start XAI Sentry Node service in test mode -- name: Start XAI Sentry Node in test mode - become: true - command: ./sentry-node-cli-linux --test-mode +- name: Start XAI Sentry Node + hosts: all + become: yes -# Assert that XAI Sentry Node is running -- name: Assert XAI Sentry Node service is running in test mode - when: "'molecule' in groups" - block: - - name: Get service info for XAI Sentry Node - become: true - ansible.builtin.systemd: - name: "xai-sentry" - register: _xai_sentry - - - name: Verify XAI Sentry Node service is active in test mode - ansible.builtin.assert: - that: - - _xai_sentry.status.ActiveState == 'active' - quiet: true + tasks: + # Run the XAI Sentry Node CLI + - name: Start XAI Sentry Node CLI + command: /usr/local/bin/sentry-node-cli-linux + args: + chdir: /usr/local/bin/ From d418523f88148a751262100d0372a7d13cc9ec92 Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Mon, 23 Sep 2024 16:37:23 +0200 Subject: [PATCH 12/18] Update xai service --- depin/services/roles/xai/defaults/main.yml | 5 +- .../roles/xai/tasks/commands/install.yml | 90 ++++++++++++------- .../roles/xai/tasks/commands/start.yml | 9 +- .../roles/xai/tasks/commands/stop.yml | 4 - .../roles/xai/tasks/commands/uninstall.yml | 12 +-- depin/services/roles/xai/tasks/main.yml | 4 +- 6 files changed, 72 insertions(+), 52 deletions(-) diff --git a/depin/services/roles/xai/defaults/main.yml b/depin/services/roles/xai/defaults/main.yml index 2bfbeae8..267f7bad 100644 --- a/depin/services/roles/xai/defaults/main.yml +++ b/depin/services/roles/xai/defaults/main.yml @@ -1,5 +1,8 @@ -xai_version: "latest" +--- +xai_cmd: "{{ depin_cmd | default('install') }}" +xai_version: 1.1.14 +xai_metrics_port: 33002 xai_download_url: "https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip" xai_install_path: "/opt/xai/{{ xai_version }}" diff --git a/depin/services/roles/xai/tasks/commands/install.yml b/depin/services/roles/xai/tasks/commands/install.yml index b039e06d..55db54b5 100644 --- a/depin/services/roles/xai/tasks/commands/install.yml +++ b/depin/services/roles/xai/tasks/commands/install.yml @@ -1,40 +1,64 @@ --- -- name: Install XAI Sentry Node CLI - hosts: all - become: yes +- name: Create program folder + become: true + ansible.builtin.file: + state: directory + owner: root + group: root + mode: '0755' + dest: /opt/xai/{{ xai_version }} - tasks: - # Update the system package list - - name: System update - apt: - update_cache: yes - force_apt_get: yes - cache_valid_time: 3600 - # Install required dependencies - - name: Install dependencies - apt: - name: - - curl - - unzip - state: present +- name: Download binary to tmp + become: true + ansible.builtin.unarchive: + remote_src: true + src: "{{ xai_download_url }}" + dest: /tmp + failed_when: false - # Download XAI Sentry Node CLI - - name: Download XAI Sentry Node CLI - get_url: - url: https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip - dest: /tmp/sentry-node-cli-linux.zip +- name: Find binary + become: true + ansible.builtin.find: + paths: + - /tmp + - "{{ xai_download_path | default('/tmp') }}" + patterns: 'sentry-node-cli-linux$' + 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', xai_checksum | default('')) + }} + _path: "{{ (_files | first | default(_download['files'] | first))['path'] }}" + block: + - name: Copy required files + ansible.builtin.copy: + remote_src: true + src: "{{ _path }}" + dest: /opt/xai/{{ xai_version }} + mode: '0755' - # Extract the CLI package to the destination folder - - name: Extract XAI Sentry Node CLI - unarchive: - src: /tmp/sentry-node-cli-linux.zip - dest: /usr/local/bin/ - remote_src: yes - # Set executable permissions for the CLI binary - - name: Set execute permissions - file: - path: /usr/local/bin/sentry-node-cli-linux + - name: Symlink binary to path + become: true + ansible.builtin.file: + src: /opt/xai/{{ xai_version }}/{{ _path | basename }} + path: /usr/local/bin/xai mode: '0755' - state: file + owner: root + group: root + state: hard + force: true + +- name: Start node + ansible.builtin.include_tasks: + file: commands/start.yml \ No newline at end of file diff --git a/depin/services/roles/xai/tasks/commands/start.yml b/depin/services/roles/xai/tasks/commands/start.yml index 7c470037..68630af0 100644 --- a/depin/services/roles/xai/tasks/commands/start.yml +++ b/depin/services/roles/xai/tasks/commands/start.yml @@ -1,11 +1,6 @@ --- -- name: Start XAI Sentry Node - hosts: all - become: yes - - tasks: - # Run the XAI Sentry Node CLI - - name: Start XAI Sentry Node CLI + # Run the XAI Sentry Node CLI + - name: Start XAI Sentry Node CLI command: /usr/local/bin/sentry-node-cli-linux args: chdir: /usr/local/bin/ diff --git a/depin/services/roles/xai/tasks/commands/stop.yml b/depin/services/roles/xai/tasks/commands/stop.yml index 7254af50..a4b8d07f 100644 --- a/depin/services/roles/xai/tasks/commands/stop.yml +++ b/depin/services/roles/xai/tasks/commands/stop.yml @@ -1,6 +1,2 @@ --- # Stop XAI Sentry Node service -- name: Stop XAI Sentry Node - ansible.builtin.systemd: - name: xai-sentry - state: stopped diff --git a/depin/services/roles/xai/tasks/commands/uninstall.yml b/depin/services/roles/xai/tasks/commands/uninstall.yml index 05fbda35..310fab89 100644 --- a/depin/services/roles/xai/tasks/commands/uninstall.yml +++ b/depin/services/roles/xai/tasks/commands/uninstall.yml @@ -1,14 +1,16 @@ --- # Stop the service before uninstalling -- name: Stop XAI Sentry Node - ansible.builtin.systemd: - name: xai-sentry - state: stopped + # Remove the binary file for XAI Sentry Node - name: Remove XAI Sentry Node binary file: - path: /usr/local/bin/sentry-node-cli-linux + path: /opt/xai + state: absent + +- name: Remove XAI Sentry Node binary + file: + path: /usr/local/bin/xai state: absent # Remove the systemd service for XAI Sentry Node diff --git a/depin/services/roles/xai/tasks/main.yml b/depin/services/roles/xai/tasks/main.yml index 7e103192..d4dcc621 100644 --- a/depin/services/roles/xai/tasks/main.yml +++ b/depin/services/roles/xai/tasks/main.yml @@ -1,5 +1,5 @@ --- -- name: "XAI {{ service_cmd | default(xai_cmd) }}" +- name: "XAI {{ xai_cmd }}" ansible.builtin.include_tasks: - file: "commands/{{ service_cmd | default(xai_cmd) }}.yml" + file: "commands/{{ xai_cmd }}.yml" From 98bb1c19dd2192e248bcbe33a2e839a04f91585b Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Wed, 25 Sep 2024 13:08:19 +0200 Subject: [PATCH 13/18] Updates xai service --- depin/services/roles/xai/defaults/main.yml | 2 +- depin/services/roles/xai/tasks/commands/start.yml | 4 ---- depin/services/roles/xai/tasks/commands/uninstall.yml | 10 +++------- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/depin/services/roles/xai/defaults/main.yml b/depin/services/roles/xai/defaults/main.yml index 267f7bad..be371630 100644 --- a/depin/services/roles/xai/defaults/main.yml +++ b/depin/services/roles/xai/defaults/main.yml @@ -2,7 +2,7 @@ xai_cmd: "{{ depin_cmd | default('install') }}" xai_version: 1.1.14 -xai_metrics_port: 33002 +xai_metrics_port: 33004 xai_download_url: "https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip" xai_install_path: "/opt/xai/{{ xai_version }}" diff --git a/depin/services/roles/xai/tasks/commands/start.yml b/depin/services/roles/xai/tasks/commands/start.yml index 68630af0..f5b797f2 100644 --- a/depin/services/roles/xai/tasks/commands/start.yml +++ b/depin/services/roles/xai/tasks/commands/start.yml @@ -1,6 +1,2 @@ --- # Run the XAI Sentry Node CLI - - name: Start XAI Sentry Node CLI - command: /usr/local/bin/sentry-node-cli-linux - args: - chdir: /usr/local/bin/ diff --git a/depin/services/roles/xai/tasks/commands/uninstall.yml b/depin/services/roles/xai/tasks/commands/uninstall.yml index 310fab89..e5a622e4 100644 --- a/depin/services/roles/xai/tasks/commands/uninstall.yml +++ b/depin/services/roles/xai/tasks/commands/uninstall.yml @@ -1,9 +1,10 @@ --- # Stop the service before uninstalling - +- name: Disable systemd service + ansible.builtin.include_tasks: commands/stop.yml # Remove the binary file for XAI Sentry Node -- name: Remove XAI Sentry Node binary +- name: Remove XAI Sentry Node work directory file: path: /opt/xai state: absent @@ -13,8 +14,3 @@ path: /usr/local/bin/xai state: absent -# Remove the systemd service for XAI Sentry Node -- name: Remove systemd service for XAI Sentry - file: - path: /etc/systemd/system/xai-sentry.service - state: absent From c8a228ebad34e66c7d34a861c464022f6d10adcd Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Fri, 25 Oct 2024 16:03:02 +0200 Subject: [PATCH 14/18] Update xai/main.yml --- depin/services/roles/xai/tasks/main.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/depin/services/roles/xai/tasks/main.yml b/depin/services/roles/xai/tasks/main.yml index d4dcc621..473d9c2f 100644 --- a/depin/services/roles/xai/tasks/main.yml +++ b/depin/services/roles/xai/tasks/main.yml @@ -1,5 +1,6 @@ ---- -- name: "XAI {{ xai_cmd }}" +- name: XAI {{ xai_cmd }} + vars: + cmd_file: "{{ role_path }}/tasks/commands/{{ xai_cmd }}.yml" + when: cmd_file is file ansible.builtin.include_tasks: - file: "commands/{{ xai_cmd }}.yml" - + file: "{{ cmd_file }}" \ No newline at end of file From bd9d2cc1d39bfe1f7ad625137ee010d263e3417a Mon Sep 17 00:00:00 2001 From: Alexey Gladky Date: Fri, 25 Oct 2024 17:06:44 +0200 Subject: [PATCH 15/18] Updates after PR review --- depin/core/roles/metrics/defaults/main.yml | 1 + depin/services/roles/autonomi/vars/main.yml | 9 --- .../hychain/templates/metrics.service.j2 | 18 ------ .../roles/storagechain/tasks/metrics.yml | 62 ------------------- depin/services/roles/xai/defaults/main.yml | 2 +- molecule/default/tasks/vars/hychain.yml | 5 -- skeletons/role/tasks/metrics.yml.j2 | 60 ------------------ 7 files changed, 2 insertions(+), 155 deletions(-) delete mode 100644 depin/services/roles/autonomi/vars/main.yml delete mode 100644 depin/services/roles/hychain/templates/metrics.service.j2 delete mode 100644 depin/services/roles/storagechain/tasks/metrics.yml delete mode 100644 molecule/default/tasks/vars/hychain.yml delete mode 100644 skeletons/role/tasks/metrics.yml.j2 diff --git a/depin/core/roles/metrics/defaults/main.yml b/depin/core/roles/metrics/defaults/main.yml index 48769996..c2b54ef1 100644 --- a/depin/core/roles/metrics/defaults/main.yml +++ b/depin/core/roles/metrics/defaults/main.yml @@ -8,3 +8,4 @@ metrics_port: autonomi: 33002 hychain: 33003 storagechain: 33004 + xai: 33005 diff --git a/depin/services/roles/autonomi/vars/main.yml b/depin/services/roles/autonomi/vars/main.yml deleted file mode 100644 index 544d9bda..00000000 --- a/depin/services/roles/autonomi/vars/main.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -deb_architecture: # noqa var-naming[no-role-prefix] - aarch64: arm64 - x86_64: amd64 - arm: arm - i386: "386" - -system_architecture: |- # noqa var-naming[no-role-prefix] - {{ [ansible_architecture] | map('extract', deb_architecture) | first }} diff --git a/depin/services/roles/hychain/templates/metrics.service.j2 b/depin/services/roles/hychain/templates/metrics.service.j2 deleted file mode 100644 index aaf99814..00000000 --- a/depin/services/roles/hychain/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 diff --git a/depin/services/roles/storagechain/tasks/metrics.yml b/depin/services/roles/storagechain/tasks/metrics.yml deleted file mode 100644 index c8c7927a..00000000 --- a/depin/services/roles/storagechain/tasks/metrics.yml +++ /dev/null @@ -1,62 +0,0 @@ ---- -- name: Install metrics - vars: - _name: "{{ role_name | replace('_', '-') }}" - _metrics_port: storagechain_metrics_port - when: storagechain_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 - - - 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 - vars: - _name: "{{ role_name | replace('_', '-') }}" - when: storagechain_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 diff --git a/depin/services/roles/xai/defaults/main.yml b/depin/services/roles/xai/defaults/main.yml index be371630..0bb493e4 100644 --- a/depin/services/roles/xai/defaults/main.yml +++ b/depin/services/roles/xai/defaults/main.yml @@ -2,7 +2,7 @@ xai_cmd: "{{ depin_cmd | default('install') }}" xai_version: 1.1.14 -xai_metrics_port: 33004 + xai_download_url: "https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip" xai_install_path: "/opt/xai/{{ xai_version }}" diff --git a/molecule/default/tasks/vars/hychain.yml b/molecule/default/tasks/vars/hychain.yml deleted file mode 100644 index 237a2212..00000000 --- a/molecule/default/tasks/vars/hychain.yml +++ /dev/null @@ -1,5 +0,0 @@ -test_role: depin.services.hychain - -# override role variables -# https://explorer.hytopia.com/address/0x488b60121887a638026B5a8dEa70B7200E4D8Eb0 -hychain_private_key: "0x64c077bf4de3b020f874e8a9384cbdd98dafeb49cc9e78f0996771f86c0c48e2" diff --git a/skeletons/role/tasks/metrics.yml.j2 b/skeletons/role/tasks/metrics.yml.j2 deleted file mode 100644 index 64362951..00000000 --- a/skeletons/role/tasks/metrics.yml.j2 +++ /dev/null @@ -1,60 +0,0 @@ ---- -- name: Install metrics - vars: - _name: "{{ "{{ role_name | replace('_', '-') }}" }}" - _metrics_port: {{ role_name | replace('_', '-') }}_metrics_port - when: {{ role_name | replace('_', '-') }}_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 - - - 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: {{ role_name | replace('_', '-') }}_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 From bb922e235fe2ad3da6c8fc91e5e88ce1abc3c8ff Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Wed, 20 Nov 2024 16:56:45 +0400 Subject: [PATCH 16/18] update depin.core configuration --- depin/core/roles/metrics/default.yml | 2 + depin/services/roles/xai/defaults/main.yml | 14 +++--- .../roles/xai/tasks/commands/install.yml | 43 +++++++++---------- 3 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 depin/core/roles/metrics/default.yml diff --git a/depin/core/roles/metrics/default.yml b/depin/core/roles/metrics/default.yml new file mode 100644 index 00000000..f863640a --- /dev/null +++ b/depin/core/roles/metrics/default.yml @@ -0,0 +1,2 @@ +--- +xai_metrics_port: 33004 diff --git a/depin/services/roles/xai/defaults/main.yml b/depin/services/roles/xai/defaults/main.yml index be371630..6b8492eb 100644 --- a/depin/services/roles/xai/defaults/main.yml +++ b/depin/services/roles/xai/defaults/main.yml @@ -1,16 +1,16 @@ --- - xai_cmd: "{{ depin_cmd | default('install') }}" xai_version: 1.1.14 -xai_metrics_port: 33004 xai_download_url: "https://github.com/xai-foundation/sentry/releases/latest/download/sentry-node-cli-linux.zip" - xai_install_path: "/opt/xai/{{ xai_version }}" - xai_node_count: 1 - xai_node_port: 8545 - xai_start_command: "/opt/xai/{{ xai_version }}/sentry-node-cli-linux start" - xai_stop_command: "/opt/xai/{{ xai_version }}/sentry-node-cli-linux stop" + +xai_programs: + - name: sentry-node-cli-linux + url: "{{ xai_download_url }}" + checksum: "" + install_path: "{{ xai_install_path }}" + symlink: /usr/local/bin/xai diff --git a/depin/services/roles/xai/tasks/commands/install.yml b/depin/services/roles/xai/tasks/commands/install.yml index 55db54b5..602f7740 100644 --- a/depin/services/roles/xai/tasks/commands/install.yml +++ b/depin/services/roles/xai/tasks/commands/install.yml @@ -1,4 +1,10 @@ --- +- name: Install programs using core.installer + ansible.builtin.include_role: + name: depin.core.installer + vars: + programs: "{{ xai_programs }}" + - name: Create program folder become: true ansible.builtin.file: @@ -6,15 +12,16 @@ owner: root group: root mode: '0755' - dest: /opt/xai/{{ xai_version }} - + dest: "{{ item.install_path }}" + loop: "{{ xai_programs }}" - name: Download binary to tmp become: true ansible.builtin.unarchive: remote_src: true - src: "{{ xai_download_url }}" + src: "{{ item.url }}" dest: /tmp + loop: "{{ xai_programs }}" failed_when: false - name: Find binary @@ -22,43 +29,35 @@ ansible.builtin.find: paths: - /tmp - - "{{ xai_download_path | default('/tmp') }}" - patterns: 'sentry-node-cli-linux$' + patterns: "{{ item.name }}" use_regex: true get_checksum: true recurse: true - register: _download + loop: "{{ xai_programs }}" + register: _downloads - name: Install node - when: _download['files'] | length > 0 become: true - vars: - _files: | - {{ - _download['files'] | - selectattr('checksum', 'equalto', xai_checksum | default('')) - }} - _path: "{{ (_files | first | default(_download['files'] | first))['path'] }}" block: - name: Copy required files ansible.builtin.copy: remote_src: true - src: "{{ _path }}" - dest: /opt/xai/{{ xai_version }} + src: "{{ (_downloads.results[loop.index0]['files'] | first).path }}" + dest: "{{ item.install_path }}" mode: '0755' - - name: Symlink binary to path become: true ansible.builtin.file: - src: /opt/xai/{{ xai_version }}/{{ _path | basename }} - path: /usr/local/bin/xai + src: "{{ item.install_path }}/{{ item.name }}" + path: "{{ item.symlink }}" mode: '0755' owner: root group: root - state: hard - force: true + state: link + force: true + loop: "{{ xai_programs }}" - name: Start node ansible.builtin.include_tasks: - file: commands/start.yml \ No newline at end of file + file: commands/start.yml From ab576e7fac53696c26c3195a2f7ad2c4d124f8e3 Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Tue, 26 Nov 2024 12:52:36 +0400 Subject: [PATCH 17/18] fix xai port --- depin/core/roles/metrics/default.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/depin/core/roles/metrics/default.yml b/depin/core/roles/metrics/default.yml index f863640a..51478ea9 100644 --- a/depin/core/roles/metrics/default.yml +++ b/depin/core/roles/metrics/default.yml @@ -1,2 +1,2 @@ --- -xai_metrics_port: 33004 +xai_metrics_port: 33005 From 61fb24c4c2a9fc3a74108f9731e9bee50eb862af Mon Sep 17 00:00:00 2001 From: Sergei Kuznetsov Date: Thu, 28 Nov 2024 19:50:51 +0400 Subject: [PATCH 18/18] removed start from main.yml --- depin/services/roles/xai/defaults/main.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/depin/services/roles/xai/defaults/main.yml b/depin/services/roles/xai/defaults/main.yml index 6b8492eb..783fb609 100644 --- a/depin/services/roles/xai/defaults/main.yml +++ b/depin/services/roles/xai/defaults/main.yml @@ -5,8 +5,6 @@ xai_download_url: "https://github.com/xai-foundation/sentry/releases/latest/down xai_install_path: "/opt/xai/{{ xai_version }}" xai_node_count: 1 xai_node_port: 8545 -xai_start_command: "/opt/xai/{{ xai_version }}/sentry-node-cli-linux start" -xai_stop_command: "/opt/xai/{{ xai_version }}/sentry-node-cli-linux stop" xai_programs: - name: sentry-node-cli-linux