Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions ansible/playbooks/clean-data.yml
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 }}
Copy link
Contributor

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?

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 }}"
15 changes: 12 additions & 3 deletions ansible/playbooks/site.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
# Main site playbook: Complete deployment workflow
# 1. Generate genesis files locally (including .key files)
# 2. Copy genesis files to remote hosts
# 3. Deploy nodes
# 1. Clean data directories (if clean_data=true)
# 2. Generate genesis files locally (including .key files)
# 3. Copy genesis files to remote hosts
# 4. Deploy nodes
#
# Usage:
# ansible-playbook -i inventory/hosts.yml playbooks/site.yml \
Expand All @@ -13,9 +14,17 @@
# network_dir - Path to network directory (genesis_dir derived from this)
# genesis_dir - Path to genesis directory (required)
# node_names - Nodes to deploy: "all" or comma-separated names
# clean_data - Set to true to clean data directories before deployment (default: false)
# skip_genesis - Set to true to skip genesis generation (default: false)
# genesis_offset - Seconds to add to current time for genesis (default: 360 for ansible mode)

- name: Clean Data Directories
import_playbook: clean-data.yml
vars:
genesis_dir: "{{ network_dir }}/genesis"
node_names: "{{ node_names }}"
when: clean_data | default(false) | bool

- name: Generate Genesis Files
import_playbook: generate-genesis.yml
vars:
Expand Down
13 changes: 12 additions & 1 deletion ansible/roles/lantern/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this into a shared task file under roles/common/tasks/clean-node-data.yml and use include_tasks in each role.

- name: Clean node data if requested
  include_tasks: "{{ playbook_dir }}/../roles/common/tasks/clean-node-data.yml"
  when: clean_data | default(false) | bool

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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Expand Down
13 changes: 12 additions & 1 deletion ansible/roles/lighthouse/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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:
- clean_data | default(false) | bool
- node_data_contents.matched | default(0) > 0

- name: Create node data directory
file:
Expand Down
13 changes: 12 additions & 1 deletion ansible/roles/qlean/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,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
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:
- clean_data | default(false) | bool
- node_data_contents.matched | default(0) > 0

- name: Create node data directory
file:
Expand Down
13 changes: 12 additions & 1 deletion ansible/roles/ream/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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:
- clean_data | default(false) | bool
- node_data_contents.matched | default(0) > 0

- name: Create node data directory
file:
Expand Down
13 changes: 12 additions & 1 deletion ansible/roles/zeam/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,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
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:
- clean_data | default(false) | bool
- node_data_contents.matched | default(0) > 0

- name: Create node data directory
file:
Expand Down