bootstrap-bk-set.yaml


Overview

This Ansible playbook automates the process of bootstrapping a BK set in a distributed system. It queries the current BK set sequence number to determine if bootstrapping is necessary, manages the registration of the node's public wallet key, waits for the BK set to be properly updated with the node's information, and prepares the node environment by installing the BK set configuration and starting the necessary services via Docker Compose.

The main purpose of this playbook is to ensure that a node joins the BK set correctly during initial setup or system recovery, handling synchronization and configuration tasks automatically.


Task Breakdown and Functional Details

1. Query BK set sequence number

- name: Query BK set sequence number
  uri:
    url: "http://{{ HOST_PUBLIC_IP }}:{{ BIND_API_PORT }}/v2/bk_set"
    return_content: yes
  register: api_response
  until: api_response.status == 200
  retries: 30
  delay: 3

2. Set bootstrap flag based on sequence number

- name: Set bootstrap flag based on sequence number
  set_fact:
    WANT_BOOTSTRAP: "{{ api_response.json.seq_no == 0 }}"

3. Exit if bootstrap is not needed

- name: Exit if bootstrap is not needed
  meta: end_host
  when: not WANT_BOOTSTRAP

4. Get public wallet key

- name: Get public wallet key
  ansible.builtin.shell:
    chdir: "{{ BK_DIR }}/bk-configs/"
    cmd: "cat {{ NODE_OWNER_KEY | basename }} | jq -r '.public'"
  register: BK_PUBLIC_KEY_OUTPUT

5. Register public wallet key

- name: Register public wallet key
  ansible.builtin.set_fact:
    BK_OWNER_WALLET_PUBKEY: "{{ BK_PUBLIC_KEY_OUTPUT.stdout }}"

6. Wait 5 seconds before checking BK set

- name: Wait 5 seconds before checking BK set
  pause:
    seconds: 5

7. Wait for our public key to appear in BK set update

- name: Wait for our public key to appear in BK set update
  uri:
    url: "{{ BOOTSTRAP_BK_SET_URL }}"
    method: GET
    return_content: true
    status_code: 200
  register: resp
  until: resp is succeeded and (BK_OWNER_WALLET_PUBKEY in resp.content)
  retries: 60
  delay: 5

8. Install collected BK set

- name: Install collected BK set
  copy:
    content: "{{ resp.content }}"
    dest: "{{ BK_DIR }}/bk-configs/bk_set.json"

9. Set HAS_BK_SET_FILE fact

- name: Set HAS_BK_SET_FILE fact
  ansible.builtin.set_fact:
    HAS_BK_SET_FILE: true

10. Node graceful shutdown

- name: Node graceful shutdown
  include_tasks: graceful-shutdown.yaml

11. Production compose

- name: Production compose
  ansible.builtin.template:
    src: templates/docker-compose.j2
    dest: "{{ BK_DIR }}/docker-compose.yaml"
    mode: "0644"

12. Compose up

- name: Compose up
  ansible.builtin.shell:
    chdir: "{{ BK_DIR }}"
    cmd: docker compose up -d --remove-orphans

Implementation Details and Algorithms


Interaction with Other System Components


Visual Diagram: Task Workflow Flowchart

flowchart TD
A[Query BK set seq_no] --> B{seq_no == 0?}
B -- Yes --> C[Extract public wallet key]
C --> D[Register public wallet key fact]
D --> E[Pause 5 seconds]
E --> F[Wait for public key in BK set]
F --> G[Install BK set file]
G --> H[Set HAS_BK_SET_FILE fact]
H --> I[Node graceful shutdown]
I --> J[Render docker-compose.yaml]
J --> K[Run docker compose up]
B -- No --> L[End playbook for host]