112 lines
5.1 KiB
YAML
112 lines
5.1 KiB
YAML
---
|
|
- name: Inspect native MySQL delivery instances
|
|
hosts: "{{ target_hosts | default('all') }}"
|
|
become: true
|
|
gather_facts: false
|
|
vars:
|
|
mysql_instances: "{{ mysql_instances | default([]) }}"
|
|
|
|
tasks:
|
|
- name: Initialize host inspection result list
|
|
ansible.builtin.set_fact:
|
|
mysql_inspect_results: []
|
|
|
|
- name: Inspect instances assigned to this host
|
|
ansible.builtin.shell:
|
|
cmd: |
|
|
set -euo pipefail
|
|
instance="{{ item.instance_name }}"
|
|
service_name="{{ item.service_name | default('mysql-delivery@' ~ item.instance_name ~ '.service') }}"
|
|
config_file="{{ item.config_file | default('/etc/mysql/mysql-delivery/' ~ item.instance_name ~ '.cnf') }}"
|
|
expected_data_dir="{{ item.data_dir | default('') }}"
|
|
expected_base_dir="{{ item.base_dir | default('') }}"
|
|
install_dir="{{ item.install_dir | default('/opt/mysql-delivery/' ~ item.instance_name) }}"
|
|
run_dir="{{ item.run_dir | default('/run/mysql-delivery-' ~ item.instance_name) }}"
|
|
port="{{ item.port | default(0) }}"
|
|
|
|
service_state="$(systemctl is-active "$service_name" 2>/dev/null || true)"
|
|
config_exists=false
|
|
install_exists=false
|
|
base_exists=false
|
|
data_exists=false
|
|
run_exists=false
|
|
port_listening=false
|
|
actual_data_dir=""
|
|
|
|
[ -e "$config_file" ] && config_exists=true
|
|
[ -e "$install_dir" ] && install_exists=true
|
|
[ -n "$expected_base_dir" ] && [ -e "$expected_base_dir" ] && base_exists=true
|
|
[ -n "$expected_data_dir" ] && [ -e "$expected_data_dir" ] && data_exists=true
|
|
[ -e "$run_dir" ] && run_exists=true
|
|
if [ "$config_exists" = true ]; then
|
|
actual_data_dir="$(awk -F= '/^[[:space:]]*datadir[[:space:]]*=/{gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2); print $2; exit}' "$config_file" || true)"
|
|
fi
|
|
if [ -n "$port" ] && [ "$port" != "0" ] && ss -lntH "sport = :$port" | grep -q .; then
|
|
port_listening=true
|
|
fi
|
|
|
|
status=unknown
|
|
if [ "$service_state" = "active" ] && [ "$port_listening" = true ] && [ "$data_exists" = true ] && [ -e "$expected_data_dir/auto.cnf" ]; then
|
|
status=running
|
|
elif [ "$config_exists" = true ] && [ -n "$actual_data_dir" ] && [ -n "$expected_data_dir" ] && [ "$actual_data_dir" != "$expected_data_dir" ]; then
|
|
status=moved
|
|
elif [ "$service_state" != "active" ] && [ "$config_exists" = false ] && [ "$install_exists" = false ] && [ "$base_exists" = false ] && [ "$run_exists" = false ]; then
|
|
status=deleted
|
|
elif [ "$config_exists" = true ] || [ "$install_exists" = true ] || [ "$base_exists" = true ] || [ "$data_exists" = true ]; then
|
|
status=stopped
|
|
fi
|
|
|
|
export X_INSTANCE="$instance"
|
|
export X_STATUS="$status"
|
|
export X_SERVICE_STATE="$service_state"
|
|
export X_PORT_LISTENING="$port_listening"
|
|
export X_CONFIG_EXISTS="$config_exists"
|
|
export X_INSTALL_EXISTS="$install_exists"
|
|
export X_BASE_EXISTS="$base_exists"
|
|
export X_DATA_EXISTS="$data_exists"
|
|
export X_RUN_EXISTS="$run_exists"
|
|
export X_EXPECTED_DATA_DIR="$expected_data_dir"
|
|
export X_ACTUAL_DATA_DIR="$actual_data_dir"
|
|
python3 - <<PY
|
|
import os
|
|
import json
|
|
def truthy(name):
|
|
return os.environ.get(name) == "true"
|
|
print(json.dumps({
|
|
"id": {{ item.id | to_json }},
|
|
"task_id": {{ item.task_id | to_json }},
|
|
"instance_name": os.environ.get("X_INSTANCE", ""),
|
|
"host": {{ inventory_hostname | to_json }},
|
|
"status": os.environ.get("X_STATUS", "unknown"),
|
|
"service_state": os.environ.get("X_SERVICE_STATE", ""),
|
|
"port_listening": truthy("X_PORT_LISTENING"),
|
|
"config_exists": truthy("X_CONFIG_EXISTS"),
|
|
"install_exists": truthy("X_INSTALL_EXISTS"),
|
|
"base_exists": truthy("X_BASE_EXISTS"),
|
|
"data_exists": truthy("X_DATA_EXISTS"),
|
|
"run_exists": truthy("X_RUN_EXISTS"),
|
|
"expected_data_dir": os.environ.get("X_EXPECTED_DATA_DIR", ""),
|
|
"actual_data_dir": os.environ.get("X_ACTUAL_DATA_DIR", ""),
|
|
}, ensure_ascii=False))
|
|
PY
|
|
executable: /bin/bash
|
|
loop: "{{ mysql_instances }}"
|
|
when: >
|
|
(item.target_host | default('')) == inventory_hostname or
|
|
(item.host | default('')) == (ansible_host | default(''))
|
|
register: mysql_inspect_shell
|
|
changed_when: false
|
|
|
|
- name: Merge host inspection results
|
|
ansible.builtin.set_fact:
|
|
mysql_inspect_results: "{{ mysql_inspect_results + [item.stdout | from_json] }}"
|
|
loop: "{{ mysql_inspect_shell.results | default([]) }}"
|
|
when:
|
|
- item is not skipped
|
|
- item.stdout is defined
|
|
- item.stdout | length > 0
|
|
|
|
- name: Emit machine-readable inspection results
|
|
ansible.builtin.debug:
|
|
msg: "XINFRA_MYSQL_INSPECT_RESULT_B64={{ mysql_inspect_results | to_json | b64encode }}"
|