-
Notifications
You must be signed in to change notification settings - Fork 16
fix: add clean-data playbook to prevent storage-related deployment failures #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| --- | ||
| # Clean data playbook: Clean node data directories | ||
|
|
||
| - name: Parse and validate node names | ||
| hosts: localhost | ||
| connection: local | ||
| gather_facts: no | ||
| vars: | ||
| validator_config_file: "{{ genesis_dir }}/validator-config.yaml" | ||
| tags: | ||
| - zeam | ||
| - ream | ||
| - qlean | ||
| - lantern | ||
| - lighthouse | ||
| - deploy | ||
|
|
||
| tasks: | ||
| - name: Validate validator-config.yaml exists | ||
| stat: | ||
| path: "{{ validator_config_file }}" | ||
| register: validator_config_stat | ||
|
|
||
| - name: Fail if validator-config.yaml missing | ||
| fail: | ||
| msg: "validator-config.yaml not found at {{ validator_config_file }}" | ||
| when: not validator_config_stat.stat.exists | ||
|
|
||
| - name: Extract all node names | ||
| shell: | | ||
| yq eval '.validators[].name' {{ validator_config_file }} | ||
| register: all_node_names_raw | ||
| changed_when: false | ||
|
|
||
| - name: Set all node names | ||
| set_fact: | ||
| all_node_names: "{{ all_node_names_raw.stdout_lines }}" | ||
|
|
||
| - name: Fail if node_names is not specified | ||
| fail: | ||
| msg: "node_names must be specified. Provide one or more node names (comma or space separated)." | ||
| when: node_names is not defined or node_names == "" | ||
|
|
||
| - name: Handle "all" node names - expand to all nodes | ||
| set_fact: | ||
| clean_nodes: "{{ all_node_names }}" | ||
| when: | ||
| - node_names is defined | ||
| - node_names == "all" | ||
|
|
||
| - name: Parse node names if provided as comma-separated string | ||
| set_fact: | ||
| clean_nodes: "{{ node_names.split(',') | map('trim') | list }}" | ||
| when: | ||
| - node_names is defined | ||
| - node_names is string | ||
| - node_names != "all" | ||
| - '("," in node_names)' | ||
|
|
||
| - name: Parse node names if provided as space-separated string | ||
| set_fact: | ||
| clean_nodes: "{{ node_names.split(' ') | map('trim') | select('length') | list }}" | ||
| when: | ||
| - node_names is defined | ||
| - node_names is string | ||
| - node_names != "all" | ||
| - '("," not in node_names)' | ||
| - '(" " in node_names)' | ||
|
|
||
| - name: Handle single node name | ||
| set_fact: | ||
| clean_nodes: "{{ [node_names] }}" | ||
| when: | ||
| - node_names is defined | ||
| - node_names is string | ||
| - node_names != "all" | ||
| - '"," not in node_names' | ||
| - '" " not in node_names' | ||
|
|
||
| - name: Display nodes to clean | ||
| debug: | ||
| msg: "Cleaning data directories for nodes: {{ clean_nodes | join(', ') }}" | ||
|
|
||
| - name: Add nodes to clean_targets group | ||
| add_host: | ||
| name: "{{ item }}" | ||
| groups: clean_targets | ||
| loop: "{{ clean_nodes }}" | ||
|
|
||
| - name: Clean node data directories on remote hosts | ||
| hosts: clean_targets | ||
| gather_facts: no | ||
| vars: | ||
| # Use remote paths on remote hosts | ||
| data_dir: "{{ remote_data_dir | default('/opt/lean-quickstart/data') }}" | ||
| node_name: "{{ inventory_hostname }}" | ||
| tags: | ||
| - zeam | ||
| - ream | ||
| - qlean | ||
| - lantern | ||
| - lighthouse | ||
| - deploy | ||
|
|
||
| tasks: | ||
| - name: Clean node data directory | ||
| file: | ||
| path: "{{ data_dir }}/{{ node_name }}" | ||
| state: absent | ||
| become: yes | ||
|
|
||
| - name: Display cleaned directory | ||
| debug: | ||
| msg: "Cleaned data directory: {{ data_dir }}/{{ node_name }}" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,11 +60,22 @@ | |
| msg: "Node key file {{ node_name }}.key not found in {{ genesis_dir }}" | ||
| when: not (node_key_stat.stat.exists | default(false)) | ||
|
|
||
| - name: Check if node data directory has contents | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we extract this into a shared task file under This will modularize the code (instead of copying the same content across 5 different roles (which is likely to increase in future). |
||
| find: | ||
| paths: "{{ data_dir }}/{{ node_name }}" | ||
| file_type: any | ||
| hidden: yes | ||
| register: node_data_contents | ||
| failed_when: false | ||
| when: clean_data | default(false) | bool | ||
|
|
||
| - name: Clean node data directory | ||
| file: | ||
| path: "{{ data_dir }}/{{ node_name }}" | ||
| state: absent | ||
| when: clean_data | default(false) | bool | ||
| when: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The find task to check if a directory has contents before deletion is redundant. Ansible's file: state=absent is idempotent. It will succeed whether the directory exists, is empty, or has contents. |
||
| - clean_data | default(false) | bool | ||
| - node_data_contents.matched | default(0) > 0 | ||
|
|
||
| - name: Create node data directory | ||
| file: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nit, but can we please add a check to validate if yq is installed before using it here?