From a7af33a45b8bbdcd56f3c7ad375066b8a9df3a18 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 5 Sep 2024 07:27:35 +0000 Subject: [PATCH] Add ansible playbooks to setup and run nitro nodes (#2) Part of [Automate testnet nitro deployments using Ansible](https://www.notion.so/Automate-testnet-nitro-deployments-using-Ansible-0d15579430204b8daba9a8aa31e07568) - Add playbooks to setup and run L1 and L2 nitro nodes Co-authored-by: Adw8 Reviewed-on: https://git.vdb.to/cerc-io/testnet-ops/pulls/2 Co-authored-by: Prathamesh Musale Co-committed-by: Prathamesh Musale --- .gitignore | 2 + README.md | 39 +++++++ nitro-nodes-setup/README.md | 87 +++++++++++++++ nitro-nodes-setup/nitro-vars-example.yml | 12 ++ nitro-nodes-setup/run-nitro-nodes.yml | 105 ++++++++++++++++++ nitro-nodes-setup/setup-vars.yml | 3 + .../templates/configs/l1-nitro-config.env.j2 | 8 ++ .../templates/configs/l2-nitro-config.env.j2 | 10 ++ .../templates/specs/l1-nitro-spec.yml.j2 | 11 ++ .../templates/specs/l2-nitro-spec.yml.j2 | 11 ++ 10 files changed, 288 insertions(+) create mode 100644 .gitignore create mode 100644 nitro-nodes-setup/README.md create mode 100644 nitro-nodes-setup/nitro-vars-example.yml create mode 100644 nitro-nodes-setup/run-nitro-nodes.yml create mode 100644 nitro-nodes-setup/setup-vars.yml create mode 100644 nitro-nodes-setup/templates/configs/l1-nitro-config.env.j2 create mode 100644 nitro-nodes-setup/templates/configs/l2-nitro-config.env.j2 create mode 100644 nitro-nodes-setup/templates/specs/l1-nitro-spec.yml.j2 create mode 100644 nitro-nodes-setup/templates/specs/l2-nitro-spec.yml.j2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bf19fc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +nitro-nodes-setup/out/ +nitro-nodes-setup/nitro-vars.yml diff --git a/README.md b/README.md index e95d1ee..3e13766 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,41 @@ # testnet-ops +## Installation + +- Install [Ansible](https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-and-upgrading-ansible-with-pip) + +- Add location of the directory containing the ansible binary to your `PATH` + +- Set Locale Encoding to `UTF-8` + + Ansible requires the locale encoding to be `UTF-8`. You can either use the `LANG` prefix when running Ansible commands or set the system-wide locale. + + - Option 1: Use `LANG` Prefix in Commands + + If you prefer not to change the system-wide locale, you can use the `LANG` prefix when running Ansible commands: + + ```bash + LANG=en_US.UTF-8 ansible-playbook your_playbook.yml + ``` + + - Option 2: Set System-Wide Locale + + - Edit the `/etc/default/locale` file: + + ```bash + sudo nano /etc/default/locale + ``` + + - Set the `LANG` variable to en_US.UTF-8: + + ```Copy code + LANG="en_US.UTF-8" + ``` + + - Reboot your system or log out and log back in to apply the changes. + + - Reference: + +## Playbooks + +- [nitro-nodes-setup](./nitro-nodes-setup/README.md) diff --git a/nitro-nodes-setup/README.md b/nitro-nodes-setup/README.md new file mode 100644 index 0000000..256487e --- /dev/null +++ b/nitro-nodes-setup/README.md @@ -0,0 +1,87 @@ +# nitro-nodes-setup + +## Setup Ansible + +To get started, follow the [installation](../README.md#installation) guide to setup ansible on your machine + +## Run a nitro node + +The following commands have to be executed in [`nitro-nodes-setup`](./) directory + +- Copy the `nitro-vars-example.yml` vars file + + ```bash + cp nitro-vars-example.yml nitro-vars.yml + ``` + +- Edit [`nitro-vars.yml`](./nitro-vars.yml) and fill in the following values + + ```bash + # L1 WS endpoint + nitro_l1_chain_url: "" + + # L2 WS endpoint + nitro_l2_chain_url: "" + + # Private key for your nitro address + nitro_sc_pk: "" + + # Private key of the account on chain that is used for funding channels in Nitro node + nitro_chain_pk: "" + + # Contract address of NitroAdjudicator + na_address: "" + + # Contract address of VirtualPaymentApp + vpa_address: "" + + # Contract address of ConsensusApp + ca_address: "" + + # Address of the bridge node + bridge_contract_address: "" + + # Multiaddr of the L1 bridge node + nitro_l1_bridge_multiaddr: "" + + # Multiaddr of the L2 bridge node + nitro_l2_bridge_multiaddr: "" + + # Multiaddr with publically accessible IP address / DNS for your L1 nitro node + # Example: "/ip4/192.168.x.y/tcp/3009" + # Example: "/dns4/example.com/tcp/3009" + nitro_l1_ext_multiaddr: "" + + # Multiaddr with publically accessible IP address / DNS for your L2 nitro node + nitro_l2_ext_multiaddr: "" + ``` + +- To run a nitro node, execute the `run-nitro-nodes.yml` Ansible playbook by running the following command. + + NOTE: By default, deployments are created in the `nitro-nodes-setup/out` directory. To change this location, update the `nitro_directory` variable in the [setup-vars.yml](./setup-vars.yml) file. + + ```bash + LANG=en_US.utf8 ansible-playbook -i localhost, --connection=local run-nitro-nodes.yml --extra-vars='{ "target_host": "localhost"}' -kK --user $USER + ``` + + - For skipping container build, run with `"skip_container_build" : true` in the `--extra-vars` parameter: + + ```bash + LANG=en_US.utf8 ansible-playbook -i localhost, --connection=local run-nitro-nodes.yml --extra-vars='{ "target_host": "localhost", "skip_container_build": true }' -kK --user $USER + ``` + +## Check Deployment Status + +- Run the following command in the directory where the deployments are created + + - Check L1 nitro node logs: + + ```bash + laconic-so deployment --dir l1-nitro-deployment logs nitro-node -f + ``` + + - Check L2 nitro node logs: + + ```bash + laconic-so deployment --dir l2-nitro-deployment logs nitro-node -f + ``` diff --git a/nitro-nodes-setup/nitro-vars-example.yml b/nitro-nodes-setup/nitro-vars-example.yml new file mode 100644 index 0000000..8b03de2 --- /dev/null +++ b/nitro-nodes-setup/nitro-vars-example.yml @@ -0,0 +1,12 @@ +nitro_l1_chain_url: "" +nitro_l2_chain_url: "" +nitro_sc_pk: "" +nitro_chain_pk: "" +na_address: "" +vpa_address: "" +ca_address: "" +bridge_contract_address: "" +nitro_l1_bridge_multiaddr: "" +nitro_l2_bridge_multiaddr: "" +nitro_l1_ext_multiaddr: "" +nitro_l2_ext_multiaddr: "" diff --git a/nitro-nodes-setup/run-nitro-nodes.yml b/nitro-nodes-setup/run-nitro-nodes.yml new file mode 100644 index 0000000..77245d3 --- /dev/null +++ b/nitro-nodes-setup/run-nitro-nodes.yml @@ -0,0 +1,105 @@ +- name: Setup and run nitro nodes + hosts: "{{ target_host }}" + + vars_files: + - setup-vars.yml + - nitro-vars.yml + + environment: + PATH: "{{ ansible_env.PATH }}:/home/{{ansible_user}}/bin" + + tasks: + - name: Create directory for nitro-stack + file: + path: "{{ nitro_directory }}" + state: directory + + - name: Change owner of nitro-directory + file: + path: "{{ nitro_directory }}" + owner: "{{ansible_user}}" + group: "{{ansible_user}}" + state: directory + recurse: yes + + - name: Clone go-nitro stack repo + command: laconic-so fetch-stack git.vdb.to/cerc-io/nitro-stack --git-ssh --pull + ignore_errors: yes + + - name: Clone repositories required for nitro-stack + command: laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stacks/nitro-node setup-repositories --git-ssh --pull + ignore_errors: yes + + - name: Build containers + command: laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stacks/nitro-node build-containers --force-rebuild + when: not skip_container_build + + - name: Generate spec file for L1 nitro node + template: + src: "./templates/specs/l1-nitro-spec.yml.j2" + dest: "{{ nitro_directory }}/l1-nitro-spec.yml" + + - name: Generate spec file for L2 nitro node + template: + src: "./templates/specs/l2-nitro-spec.yml.j2" + dest: "{{ nitro_directory }}/l2-nitro-spec.yml" + + - name: Check if deployment exists for L1 nitro node + stat: + path: "{{ nitro_directory }}/l1-nitro-deployment" + register: l1_deployment + + - name: Create a deployment for L1 nitro node + command: laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stacks/nitro-node deploy create --spec-file l1-nitro-spec.yml --deployment-dir l1-nitro-deployment + args: + chdir: "{{ nitro_directory }}" + when: not l1_deployment.stat.exists + + - name: Check if deployment exists for L2 nitro node + stat: + path: "{{ nitro_directory }}/l2-nitro-deployment" + register: l2_deployment + + - name: Create a deployment for L2 nitro node + command: laconic-so --stack ~/cerc/nitro-stack/stack-orchestrator/stacks/nitro-node deploy create --spec-file l2-nitro-spec.yml --deployment-dir l2-nitro-deployment + args: + chdir: "{{ nitro_directory }}" + when: not l2_deployment.stat.exists + + - name: Check if the config.env file is empty for L1 deployment + shell: "[ -s {{ nitro_directory }}/l1-nitro-deployment/config.env ] && echo 'File is not empty' || echo 'File is empty'" + register: file_check_result + + - name: Display the result of the file check + debug: + msg: "{{ file_check_result.stdout }}" + + - name: Copy config.env for L1 nitro deployment if it is empty + template: + src: "./templates/configs/l1-nitro-config.env.j2" + dest: "{{ nitro_directory }}/l1-nitro-deployment/config.env" + when: file_check_result.stdout == 'File is empty' + + - name: Check if the config.env file is empty for L2 deployment + shell: "[ -s {{ nitro_directory }}/l2-nitro-deployment/config.env ] && echo 'File is not empty' || echo 'File is empty'" + register: file_check_result + + - name: Display the result of the file check + debug: + msg: "{{ file_check_result.stdout }}" + + - name: Copy config.env for L2 nitro deployment if it is empty + template: + src: "./templates/configs/l2-nitro-config.env.j2" + dest: "{{ nitro_directory }}/l2-nitro-deployment/config.env" + when: file_check_result.stdout == 'File is empty' + + - name: Start deployment for L1 nitro node + command: laconic-so deployment --dir l1-nitro-deployment start + args: + chdir: "{{ nitro_directory }}" + + - name: Start deployment for L2 nitro node + command: laconic-so deployment --dir l2-nitro-deployment start + args: + chdir: "{{ nitro_directory }}" diff --git a/nitro-nodes-setup/setup-vars.yml b/nitro-nodes-setup/setup-vars.yml new file mode 100644 index 0000000..7762444 --- /dev/null +++ b/nitro-nodes-setup/setup-vars.yml @@ -0,0 +1,3 @@ +target_host: "localhost" +nitro_directory: ./out +skip_container_build: false diff --git a/nitro-nodes-setup/templates/configs/l1-nitro-config.env.j2 b/nitro-nodes-setup/templates/configs/l1-nitro-config.env.j2 new file mode 100644 index 0000000..dc92e23 --- /dev/null +++ b/nitro-nodes-setup/templates/configs/l1-nitro-config.env.j2 @@ -0,0 +1,8 @@ +NITRO_CHAIN_URL={{ nitro_l1_chain_url }} +NITRO_SC_PK={{ nitro_sc_pk }} +NITRO_CHAIN_PK={{ nitro_chain_pk }} +NA_ADDRESS={{ na_address }} +VPA_ADDRESS={{ vpa_address }} +CA_ADDRESS={{ ca_address }} +NITRO_BOOTPEERS={{ nitro_l1_bridge_multiaddr }} +NITRO_EXT_MULTIADDR={{ nitro_l1_ext_multiaddr }} diff --git a/nitro-nodes-setup/templates/configs/l2-nitro-config.env.j2 b/nitro-nodes-setup/templates/configs/l2-nitro-config.env.j2 new file mode 100644 index 0000000..be853b6 --- /dev/null +++ b/nitro-nodes-setup/templates/configs/l2-nitro-config.env.j2 @@ -0,0 +1,10 @@ +NITRO_CHAIN_URL={{ nitro_l2_chain_url }} +NITRO_SC_PK={{ nitro_sc_pk }} +NITRO_CHAIN_PK={{ nitro_chain_pk }} +NA_ADDRESS={{ na_address }} +VPA_ADDRESS={{ vpa_address }} +CA_ADDRESS={{ ca_address }} +BRIDGE_ADDRESS={{ bridge_contract_address }} +NITRO_BOOTPEERS={{ nitro_l2_bridge_multiaddr }} +NITRO_EXT_MULTIADDR={{ nitro_l2_ext_multiaddr }} +NITRO_L2=true diff --git a/nitro-nodes-setup/templates/specs/l1-nitro-spec.yml.j2 b/nitro-nodes-setup/templates/specs/l1-nitro-spec.yml.j2 new file mode 100644 index 0000000..73ec889 --- /dev/null +++ b/nitro-nodes-setup/templates/specs/l1-nitro-spec.yml.j2 @@ -0,0 +1,11 @@ +stack: /home/{{ansible_user}}/cerc/nitro-stack/stack-orchestrator/stacks/nitro-node +deploy-to: compose +network: + ports: + nitro-node: + - 3007:3005 + - 4007:4005 +volumes: + nitro_node_data: ./data/nitro_node_data + nitro_node_tls: ./data/nitro_node_tls + nitro_node_caroot: ./data/nitro_node_caroot diff --git a/nitro-nodes-setup/templates/specs/l2-nitro-spec.yml.j2 b/nitro-nodes-setup/templates/specs/l2-nitro-spec.yml.j2 new file mode 100644 index 0000000..4d0a0c6 --- /dev/null +++ b/nitro-nodes-setup/templates/specs/l2-nitro-spec.yml.j2 @@ -0,0 +1,11 @@ +stack: /home/{{ansible_user}}/cerc/nitro-stack/stack-orchestrator/stacks/nitro-node +deploy-to: compose +network: + ports: + nitro-node: + - 3009:3005 + - 4009:4005 +volumes: + nitro_node_data: ./data/nitro_node_data + nitro_node_tls: ./data/nitro_node_tls + nitro_node_caroot: ./data/nitro_node_caroot