From e1423574288c35cf766080babf0a6be9a28d1dcc Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 5 Sep 2024 07:58:00 +0000 Subject: [PATCH] Add ansible playbook to setup and run L2 deployment (#3) Part of [Automate testnet nitro deployments using Ansible](https://www.notion.so/Automate-testnet-nitro-deployments-using-Ansible-0d15579430204b8daba9a8aa31e07568) Co-authored-by: Adw8 Reviewed-on: https://git.vdb.to/cerc-io/testnet-ops/pulls/3 Co-authored-by: Prathamesh Musale Co-committed-by: Prathamesh Musale --- .gitignore | 1 + README.md | 5 +- l2-setup/README.md | 77 ++++++++++++++++++ l2-setup/l2-vars.yml | 9 +++ l2-setup/run-optimism.yml | 89 +++++++++++++++++++++ l2-setup/setup-vars.yml | 4 + l2-setup/templates/configs/l2-config.env.j2 | 9 +++ l2-setup/templates/specs/l2-spec.yml.j2 | 18 +++++ 8 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 l2-setup/README.md create mode 100644 l2-setup/l2-vars.yml create mode 100644 l2-setup/run-optimism.yml create mode 100644 l2-setup/setup-vars.yml create mode 100644 l2-setup/templates/configs/l2-config.env.j2 create mode 100644 l2-setup/templates/specs/l2-spec.yml.j2 diff --git a/.gitignore b/.gitignore index bf19fc6..5a08ef8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ nitro-nodes-setup/out/ nitro-nodes-setup/nitro-vars.yml +l2-setup/out diff --git a/README.md b/README.md index 3e13766..41871c5 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ - Set the `LANG` variable to en_US.UTF-8: - ```Copy code + ``` LANG="en_US.UTF-8" ``` @@ -38,4 +38,5 @@ ## Playbooks -- [nitro-nodes-setup](./nitro-nodes-setup/README.md) +- [l2-setup](./l2-setup/README.md) +- [nitro-node-setup](./nitro-nodes-setup/README.md) diff --git a/l2-setup/README.md b/l2-setup/README.md new file mode 100644 index 0000000..ae31893 --- /dev/null +++ b/l2-setup/README.md @@ -0,0 +1,77 @@ +# l2-setup + +## Setup Ansible + +To get started, follow the [installation](../README.md#installation) guide to setup ansible on your machine + +## Setup and Run Optimism + +The following commands have to be executed in [`l2-setup`](./) directory + +- Edit [`l2-vars.yml`](./l2-vars.yml) with the required values + + ```bash + # L1 chain ID + l1_chain_id: "" + + # L1 RPC endpoint + l1_rpc: "" + + # L1 RPC endpoint host or IP address + l1_host: "" + + # L1 RPC endpoint port number + l1_port: "" + + # L1 Beacon endpoint + l1_beacon: "" + + # Address of the funded account on L1 + # Used for optimism contracts deployment + l1_address: "" + + # Private key of the funded account on L1 + l1_priv_key: "" + ``` + +- To setup and run L2, execute the `run-optimism.yml` Ansible playbook by running the following command. + + NOTE: By default, deployments are created in the `l2-setup/out` directory. To change this location, update the `l2_directory` variable in the [setup-vars.yml](./setup-vars.yml) file. + + ```bash + LANG=en_US.utf8 ansible-playbook -i localhost, --connection=local run-optimism.yml --extra-vars='{ "target_host": "localhost"}' -kK --user $USER + ``` + + - For skipping container build, set `"skip_container_build" : true` in the `--extra-vars` parameter: + + ```bash + LANG=en_US.utf8 ansible-playbook -i localhost, --connection=local run-optimism.yml --extra-vars='{"target_host" : "localhost", "skip_container_build": true}' -kK --user $USER + ``` + +- To run using existing contracts deployment + + - Update `artifact_path` in [`setup-vars.yml`](./setup-vars.yml) file with path to data directory of the existing deployment + + - Run the ansible playbook with `"existing_contracts_deployment": true` in the `--extra-vars` parameter: + + ```bash + LANG=en_US.utf8 ansible-playbook -i localhost, --connection=local run-optimism.yml --extra-vars='{"target_host" : "localhost", "existing_contracts_deployment": true}' -kK --user $USER + ``` + +## Check Deployment Status + +- Run the following command in the directory where the optimism-deployment is created + + - Follow optimism contracts deployment logs: + + ```bash + laconic-so deployment --dir optimism-deployment logs -f fixturenet-optimism-contracts + ``` + + - Check L2 logs: + + ```bash + laconic-so deployment --dir optimism-deployment logs -f op-geth + + # Ensure new blocks are getting created + ``` diff --git a/l2-setup/l2-vars.yml b/l2-setup/l2-vars.yml new file mode 100644 index 0000000..13796c9 --- /dev/null +++ b/l2-setup/l2-vars.yml @@ -0,0 +1,9 @@ +l1_chain_id: "" +l1_rpc: "" +l1_host: "" +l1_port: "" +l1_beacon: "" +l1_address: "" +l1_priv_key: "" +proposer_amount: "0.2ether" +batcher_amount: "0.1ether" diff --git a/l2-setup/run-optimism.yml b/l2-setup/run-optimism.yml new file mode 100644 index 0000000..0555a8b --- /dev/null +++ b/l2-setup/run-optimism.yml @@ -0,0 +1,89 @@ +- name: Setup L2 on host + hosts: "{{ target_host }}" + + vars_files: + - setup-vars.yml + - l2-vars.yml + + environment: + PATH: "{{ ansible_env.PATH }}:/home/{{ansible_user}}/bin" + + tasks: + - name: Create directory for L2 + file: + path: "{{ l2_directory }}" + state: directory + + - name: Change owner of l2-directory + file: + path: "{{ l2_directory }}" + owner: "{{ansible_user}}" + group: "{{ansible_user}}" + state: directory + recurse: yes + become: yes + + - name: Clone fixturenet-optimism-stack + command: laconic-so fetch-stack git.vdb.to/cerc-io/fixturenet-optimism-stack --pull + ignore_errors: yes + + - name: Clone required repositories for fixturenet-optimism + command: laconic-so --stack ~/cerc/fixturenet-optimism-stack/stack/fixturenet-optimism setup-repositories --pull + + - name: Build container images for L2 + command: laconic-so --stack ~/cerc/fixturenet-optimism-stack/stack/fixturenet-optimism build-containers --force-rebuild + when: not skip_container_build + + - name: Generate spec file for L2 deployment + template: + src: "./templates/specs/l2-spec.yml.j2" + dest: "{{ l2_directory }}/optimism-spec.yml" + + - name: Check if the deployment directory exists for L2 + stat: + path: "{{ l2_directory }}/optimism-deployment" + register: l2_deployment_dir + + - name: Create a deployment from the spec file for L2 + command: laconic-so --stack ~/cerc/fixturenet-optimism-stack/stack/fixturenet-optimism deploy create --spec-file optimism-spec.yml --deployment-dir optimism-deployment + args: + chdir: "{{ l2_directory }}" + when: not l2_deployment_dir.stat.exists + + - name: Generate config.env for L2 deployment + template: + src: "./templates/configs/l2-config.env.j2" + dest: "{{ l2_directory }}/optimism-deployment/config.env" + + - name: Copy deployed contract addresses and configuration files + block: + - name: Copy l1 deployment file + copy: + src: "{{ artifact_path }}/l1_deployment/{{ l1_chain_id }}-deploy.json" + dest: "{{ l2_directory }}/optimism-deployment/data/l1_deployment" + remote_src: "{{ target_host != 'localhost' }}" + + - name: Copy l2 configuration file + copy: + src: "{{ artifact_path }}/l2_config/{{ l1_chain_id }}.json" + dest: "{{ l2_directory }}/optimism-deployment/data/l2_config" + remote_src: "{{ target_host != 'localhost' }}" + + - name: Copy allocs-l2 file + copy: + src: "{{ artifact_path }}/l2_config/allocs-l2.json" + dest: "{{ l2_directory }}/optimism-deployment/data/l2_config" + remote_src: "{{ target_host != 'localhost' }}" + + - name: Copy l2 accounts file + copy: + src: "{{ artifact_path }}/l2_accounts/accounts.json" + dest: "{{ l2_directory }}/optimism-deployment/data/l2_accounts" + remote_src: "{{ target_host != 'localhost' }}" + + when: existing_contracts_deployment + + - name: Start L2-deployment + command: laconic-so deployment --dir optimism-deployment start + args: + chdir: "{{ l2_directory }}" diff --git a/l2-setup/setup-vars.yml b/l2-setup/setup-vars.yml new file mode 100644 index 0000000..ea03344 --- /dev/null +++ b/l2-setup/setup-vars.yml @@ -0,0 +1,4 @@ +skip_container_build: false +l2_directory: "./out" +existing_contracts_deployment: false +artifact_path: "" diff --git a/l2-setup/templates/configs/l2-config.env.j2 b/l2-setup/templates/configs/l2-config.env.j2 new file mode 100644 index 0000000..11fdfaf --- /dev/null +++ b/l2-setup/templates/configs/l2-config.env.j2 @@ -0,0 +1,9 @@ +CERC_L1_CHAIN_ID={{ l1_chain_id }} +CERC_L1_RPC={{ l1_rpc }} +CERC_L1_HOST={{ l1_host }} +CERC_L1_PORT={{ l1_port }} +CERC_L1_BEACON={{ l1_beacon }} +CERC_L1_ADDRESS={{ l1_address }} +CERC_L1_PRIV_KEY={{ l1_priv_key }} +CERC_PROPOSER_AMOUNT={{ proposer_amount }} +CERC_BATCHER_AMOUNT={{ batcher_amount }} diff --git a/l2-setup/templates/specs/l2-spec.yml.j2 b/l2-setup/templates/specs/l2-spec.yml.j2 new file mode 100644 index 0000000..456e557 --- /dev/null +++ b/l2-setup/templates/specs/l2-spec.yml.j2 @@ -0,0 +1,18 @@ +stack: /home/{{ansible_user}}/cerc/fixturenet-optimism-stack/stack/fixturenet-optimism +deploy-to: compose +network: + ports: + op-geth: + - '9545:8545' + - '9546:8546' + op-node: + - '8547' + op-batcher: + - '8548' + op-proposer: + - '8560' +volumes: + l1_deployment: ./data/l1_deployment + l2_accounts: ./data/l2_accounts + l2_config: ./data/l2_config + l2_geth_data: ./data/l2_geth_data