2019-10-22 11:23:12 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# cluster.py - PVC client function library, cluster management
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2022-10-06 11:55:27 -04:00
|
|
|
# Copyright (C) 2018-2022 Joshua M. Boniface <joshua@boniface.me>
|
2019-10-22 11:23:12 -04:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2021-03-25 16:57:17 -04:00
|
|
|
# the Free Software Foundation, version 3.
|
2019-10-22 11:23:12 -04:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2020-08-14 12:27:13 -04:00
|
|
|
import re
|
2019-10-22 11:23:12 -04:00
|
|
|
|
2020-02-08 18:48:59 -05:00
|
|
|
import daemon_lib.common as common
|
|
|
|
import daemon_lib.vm as pvc_vm
|
|
|
|
import daemon_lib.node as pvc_node
|
|
|
|
import daemon_lib.network as pvc_network
|
|
|
|
import daemon_lib.ceph as pvc_ceph
|
2019-10-22 11:23:12 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-05-29 20:32:20 -04:00
|
|
|
def set_maintenance(zkhandler, maint_state):
|
2021-11-06 03:02:43 -04:00
|
|
|
current_maint_state = zkhandler.read("base.config.maintenance")
|
2021-06-05 01:36:40 -04:00
|
|
|
if maint_state == current_maint_state:
|
2021-11-06 03:02:43 -04:00
|
|
|
if maint_state == "true":
|
|
|
|
return True, "Cluster is already in maintenance mode"
|
2020-01-09 10:53:27 -05:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
return True, "Cluster is already in normal mode"
|
2021-06-05 01:36:40 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if maint_state == "true":
|
|
|
|
zkhandler.write([("base.config.maintenance", "true")])
|
|
|
|
return True, "Successfully set cluster in maintenance mode"
|
2021-06-05 01:36:40 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
zkhandler.write([("base.config.maintenance", "false")])
|
|
|
|
return True, "Successfully set cluster in normal mode"
|
2020-01-09 10:53:27 -05:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-05-29 20:32:20 -04:00
|
|
|
def getClusterInformation(zkhandler):
|
2020-01-09 10:53:27 -05:00
|
|
|
# Get cluster maintenance state
|
2021-11-06 03:02:43 -04:00
|
|
|
maint_state = zkhandler.read("base.config.maintenance")
|
2020-01-09 10:53:27 -05:00
|
|
|
|
2020-08-14 12:27:13 -04:00
|
|
|
# List of messages to display to the clients
|
|
|
|
cluster_health_msg = []
|
|
|
|
storage_health_msg = []
|
|
|
|
|
2019-10-22 11:23:12 -04:00
|
|
|
# Get node information object list
|
2021-05-29 20:32:20 -04:00
|
|
|
retcode, node_list = pvc_node.get_list(zkhandler, None)
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Get vm information object list
|
2021-07-14 00:51:48 -04:00
|
|
|
retcode, vm_list = pvc_vm.get_list(zkhandler, None, None, None, None)
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Get network information object list
|
2021-05-29 20:32:20 -04:00
|
|
|
retcode, network_list = pvc_network.get_list(zkhandler, None, None)
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Get storage information object list
|
2021-05-29 20:32:20 -04:00
|
|
|
retcode, ceph_osd_list = pvc_ceph.get_list_osd(zkhandler, None)
|
|
|
|
retcode, ceph_pool_list = pvc_ceph.get_list_pool(zkhandler, None)
|
|
|
|
retcode, ceph_volume_list = pvc_ceph.get_list_volume(zkhandler, None, None)
|
2021-11-06 03:02:43 -04:00
|
|
|
retcode, ceph_snapshot_list = pvc_ceph.get_list_snapshot(
|
|
|
|
zkhandler, None, None, None
|
|
|
|
)
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Determine, for each subsection, the total count
|
|
|
|
node_count = len(node_list)
|
|
|
|
vm_count = len(vm_list)
|
|
|
|
network_count = len(network_list)
|
|
|
|
ceph_osd_count = len(ceph_osd_list)
|
|
|
|
ceph_pool_count = len(ceph_pool_list)
|
|
|
|
ceph_volume_count = len(ceph_volume_list)
|
|
|
|
ceph_snapshot_count = len(ceph_snapshot_list)
|
|
|
|
|
2020-10-18 14:46:32 -04:00
|
|
|
# Determinations for general cluster health
|
|
|
|
cluster_healthy_status = True
|
|
|
|
# Check for (n-1) overprovisioning
|
|
|
|
# Assume X nodes. If the total VM memory allocation (counting only running VMss) is greater than
|
|
|
|
# the total memory of the (n-1) smallest nodes, trigger this warning.
|
|
|
|
n_minus_1_total = 0
|
|
|
|
alloc_total = 0
|
|
|
|
|
|
|
|
node_largest_index = None
|
|
|
|
node_largest_count = 0
|
|
|
|
for index, node in enumerate(node_list):
|
2021-11-06 03:02:43 -04:00
|
|
|
node_mem_total = node["memory"]["total"]
|
|
|
|
node_mem_alloc = node["memory"]["allocated"]
|
2020-10-18 14:46:32 -04:00
|
|
|
alloc_total += node_mem_alloc
|
|
|
|
|
|
|
|
# Determine if this node is the largest seen so far
|
|
|
|
if node_mem_total > node_largest_count:
|
|
|
|
node_largest_index = index
|
|
|
|
node_largest_count = node_mem_total
|
|
|
|
n_minus_1_node_list = list()
|
|
|
|
for index, node in enumerate(node_list):
|
|
|
|
if index == node_largest_index:
|
|
|
|
continue
|
|
|
|
n_minus_1_node_list.append(node)
|
|
|
|
for index, node in enumerate(n_minus_1_node_list):
|
2021-11-06 03:02:43 -04:00
|
|
|
n_minus_1_total += node["memory"]["total"]
|
2020-10-18 14:46:32 -04:00
|
|
|
if alloc_total > n_minus_1_total:
|
|
|
|
cluster_healthy_status = False
|
2021-11-06 03:02:43 -04:00
|
|
|
cluster_health_msg.append(
|
|
|
|
"Total VM memory ({}) is overprovisioned (max {}) for (n-1) failure scenarios".format(
|
|
|
|
alloc_total, n_minus_1_total
|
|
|
|
)
|
|
|
|
)
|
2020-10-18 14:46:32 -04:00
|
|
|
|
2019-10-22 11:23:12 -04:00
|
|
|
# Determinations for node health
|
|
|
|
node_healthy_status = list(range(0, node_count))
|
|
|
|
node_report_status = list(range(0, node_count))
|
|
|
|
for index, node in enumerate(node_list):
|
2021-11-06 03:02:43 -04:00
|
|
|
daemon_state = node["daemon_state"]
|
|
|
|
domain_state = node["domain_state"]
|
|
|
|
if daemon_state != "run" and domain_state != "ready":
|
2019-10-22 11:23:12 -04:00
|
|
|
node_healthy_status[index] = False
|
2021-11-06 03:02:43 -04:00
|
|
|
cluster_health_msg.append(
|
|
|
|
"Node '{}' in {},{} state".format(
|
|
|
|
node["name"], daemon_state, domain_state
|
|
|
|
)
|
|
|
|
)
|
2019-10-22 11:23:12 -04:00
|
|
|
else:
|
|
|
|
node_healthy_status[index] = True
|
2021-11-06 03:02:43 -04:00
|
|
|
node_report_status[index] = daemon_state + "," + domain_state
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Determinations for VM health
|
|
|
|
vm_healthy_status = list(range(0, vm_count))
|
|
|
|
vm_report_status = list(range(0, vm_count))
|
|
|
|
for index, vm in enumerate(vm_list):
|
2021-11-06 03:02:43 -04:00
|
|
|
vm_state = vm["state"]
|
|
|
|
if vm_state not in ["start", "disable", "migrate", "unmigrate", "provision"]:
|
2019-10-22 11:23:12 -04:00
|
|
|
vm_healthy_status[index] = False
|
2021-11-06 03:02:43 -04:00
|
|
|
cluster_health_msg.append(
|
|
|
|
"VM '{}' in {} state".format(vm["name"], vm_state)
|
|
|
|
)
|
2019-10-22 11:23:12 -04:00
|
|
|
else:
|
|
|
|
vm_healthy_status[index] = True
|
|
|
|
vm_report_status[index] = vm_state
|
|
|
|
|
|
|
|
# Determinations for OSD health
|
|
|
|
ceph_osd_healthy_status = list(range(0, ceph_osd_count))
|
|
|
|
ceph_osd_report_status = list(range(0, ceph_osd_count))
|
|
|
|
for index, ceph_osd in enumerate(ceph_osd_list):
|
2019-10-22 13:59:28 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_osd_up = ceph_osd["stats"]["up"]
|
2019-10-22 13:59:28 -04:00
|
|
|
except KeyError:
|
|
|
|
ceph_osd_up = 0
|
|
|
|
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_osd_in = ceph_osd["stats"]["in"]
|
2019-10-22 13:59:28 -04:00
|
|
|
except KeyError:
|
|
|
|
ceph_osd_in = 0
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
up_texts = {1: "up", 0: "down"}
|
|
|
|
in_texts = {1: "in", 0: "out"}
|
2020-08-14 12:27:13 -04:00
|
|
|
|
2019-10-22 11:23:12 -04:00
|
|
|
if not ceph_osd_up or not ceph_osd_in:
|
|
|
|
ceph_osd_healthy_status[index] = False
|
2021-11-06 03:02:43 -04:00
|
|
|
cluster_health_msg.append(
|
|
|
|
"OSD {} in {},{} state".format(
|
|
|
|
ceph_osd["id"], up_texts[ceph_osd_up], in_texts[ceph_osd_in]
|
|
|
|
)
|
|
|
|
)
|
2019-10-22 11:23:12 -04:00
|
|
|
else:
|
|
|
|
ceph_osd_healthy_status[index] = True
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_osd_report_status[index] = (
|
|
|
|
up_texts[ceph_osd_up] + "," + in_texts[ceph_osd_in]
|
|
|
|
)
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Find out the overall cluster health; if any element of a healthy_status is false, it's unhealthy
|
2021-11-06 03:02:43 -04:00
|
|
|
if maint_state == "true":
|
|
|
|
cluster_health = "Maintenance"
|
|
|
|
elif (
|
|
|
|
cluster_healthy_status is False
|
|
|
|
or False in node_healthy_status
|
|
|
|
or False in vm_healthy_status
|
|
|
|
or False in ceph_osd_healthy_status
|
|
|
|
):
|
|
|
|
cluster_health = "Degraded"
|
2019-10-22 11:23:12 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
cluster_health = "Optimal"
|
2019-10-22 11:23:12 -04:00
|
|
|
|
2020-08-13 15:06:19 -04:00
|
|
|
# Find out our storage health from Ceph
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_status = zkhandler.read("base.storage").split("\n")
|
2020-08-14 12:27:13 -04:00
|
|
|
ceph_health = ceph_status[2].split()[-1]
|
|
|
|
|
|
|
|
# Parse the status output to get the health indicators
|
|
|
|
line_record = False
|
|
|
|
for index, line in enumerate(ceph_status):
|
2021-11-06 03:02:43 -04:00
|
|
|
if re.search("services:", line):
|
2020-08-14 12:27:13 -04:00
|
|
|
line_record = False
|
|
|
|
if line_record and len(line.strip()) > 0:
|
|
|
|
storage_health_msg.append(line.strip())
|
2021-11-06 03:02:43 -04:00
|
|
|
if re.search("health:", line):
|
2020-08-14 12:27:13 -04:00
|
|
|
line_record = True
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if maint_state == "true":
|
|
|
|
storage_health = "Maintenance"
|
|
|
|
elif ceph_health != "HEALTH_OK":
|
|
|
|
storage_health = "Degraded"
|
2020-08-13 15:06:19 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
storage_health = "Optimal"
|
2020-08-13 15:06:19 -04:00
|
|
|
|
2019-10-22 11:23:12 -04:00
|
|
|
# State lists
|
|
|
|
node_state_combinations = [
|
2021-11-06 03:02:43 -04:00
|
|
|
"run,ready",
|
|
|
|
"run,flush",
|
|
|
|
"run,flushed",
|
|
|
|
"run,unflush",
|
|
|
|
"init,ready",
|
|
|
|
"init,flush",
|
|
|
|
"init,flushed",
|
|
|
|
"init,unflush",
|
|
|
|
"stop,ready",
|
|
|
|
"stop,flush",
|
|
|
|
"stop,flushed",
|
|
|
|
"stop,unflush",
|
|
|
|
"dead,ready",
|
|
|
|
"dead,flush",
|
|
|
|
"dead,flushed",
|
|
|
|
"dead,unflush",
|
2019-10-22 11:23:12 -04:00
|
|
|
]
|
|
|
|
vm_state_combinations = [
|
2021-11-06 03:02:43 -04:00
|
|
|
"start",
|
|
|
|
"restart",
|
|
|
|
"shutdown",
|
|
|
|
"stop",
|
|
|
|
"disable",
|
|
|
|
"fail",
|
|
|
|
"migrate",
|
|
|
|
"unmigrate",
|
|
|
|
"provision",
|
2019-10-22 11:23:12 -04:00
|
|
|
]
|
2021-11-06 03:02:43 -04:00
|
|
|
ceph_osd_state_combinations = ["up,in", "up,out", "down,in", "down,out"]
|
2019-10-22 11:23:12 -04:00
|
|
|
|
|
|
|
# Format the Node states
|
2021-11-06 03:02:43 -04:00
|
|
|
formatted_node_states = {"total": node_count}
|
2019-10-22 11:23:12 -04:00
|
|
|
for state in node_state_combinations:
|
|
|
|
state_count = 0
|
|
|
|
for node_state in node_report_status:
|
|
|
|
if node_state == state:
|
|
|
|
state_count += 1
|
|
|
|
if state_count > 0:
|
|
|
|
formatted_node_states[state] = state_count
|
|
|
|
|
|
|
|
# Format the VM states
|
2021-11-06 03:02:43 -04:00
|
|
|
formatted_vm_states = {"total": vm_count}
|
2019-10-22 11:23:12 -04:00
|
|
|
for state in vm_state_combinations:
|
|
|
|
state_count = 0
|
|
|
|
for vm_state in vm_report_status:
|
|
|
|
if vm_state == state:
|
|
|
|
state_count += 1
|
|
|
|
if state_count > 0:
|
|
|
|
formatted_vm_states[state] = state_count
|
|
|
|
|
|
|
|
# Format the OSD states
|
2021-11-06 03:02:43 -04:00
|
|
|
formatted_osd_states = {"total": ceph_osd_count}
|
2019-10-22 11:23:12 -04:00
|
|
|
for state in ceph_osd_state_combinations:
|
|
|
|
state_count = 0
|
|
|
|
for ceph_osd_state in ceph_osd_report_status:
|
|
|
|
if ceph_osd_state == state:
|
|
|
|
state_count += 1
|
|
|
|
if state_count > 0:
|
|
|
|
formatted_osd_states[state] = state_count
|
|
|
|
|
|
|
|
# Format the status data
|
|
|
|
cluster_information = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"health": cluster_health,
|
|
|
|
"health_msg": cluster_health_msg,
|
|
|
|
"storage_health": storage_health,
|
|
|
|
"storage_health_msg": storage_health_msg,
|
|
|
|
"primary_node": common.getPrimaryNode(zkhandler),
|
|
|
|
"upstream_ip": zkhandler.read("base.config.upstream_ip"),
|
|
|
|
"nodes": formatted_node_states,
|
|
|
|
"vms": formatted_vm_states,
|
|
|
|
"networks": network_count,
|
|
|
|
"osds": formatted_osd_states,
|
|
|
|
"pools": ceph_pool_count,
|
|
|
|
"volumes": ceph_volume_count,
|
|
|
|
"snapshots": ceph_snapshot_count,
|
2019-10-22 11:23:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return cluster_information
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-05-29 20:32:20 -04:00
|
|
|
def get_info(zkhandler):
|
2019-10-22 11:23:12 -04:00
|
|
|
# This is a thin wrapper function for naming purposes
|
2021-05-29 20:32:20 -04:00
|
|
|
cluster_information = getClusterInformation(zkhandler)
|
2019-10-22 11:23:12 -04:00
|
|
|
if cluster_information:
|
|
|
|
return True, cluster_information
|
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
return False, "ERROR: Failed to obtain cluster information!"
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
def cluster_initialize(zkhandler, overwrite=False):
|
|
|
|
# Abort if we've initialized the cluster before
|
2021-11-06 03:02:43 -04:00
|
|
|
if zkhandler.exists("base.config.primary_node") and not overwrite:
|
|
|
|
return False, "ERROR: Cluster contains data and overwrite not set."
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
if overwrite:
|
2021-06-29 18:41:02 -04:00
|
|
|
# Delete the existing keys
|
2021-11-06 03:02:43 -04:00
|
|
|
for key in zkhandler.schema.keys("base"):
|
|
|
|
if key == "root":
|
2021-06-29 18:41:02 -04:00
|
|
|
# Don't delete the root key
|
|
|
|
continue
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
status = zkhandler.delete("base.{}".format(key), recursive=True)
|
2021-06-29 18:41:02 -04:00
|
|
|
if not status:
|
2021-11-06 03:02:43 -04:00
|
|
|
return (
|
|
|
|
False,
|
|
|
|
"ERROR: Failed to delete data in cluster; running nodes perhaps?",
|
|
|
|
)
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
# Create the root keys
|
|
|
|
zkhandler.schema.apply(zkhandler)
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
return True, "Successfully initialized cluster"
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
def cluster_backup(zkhandler):
|
|
|
|
# Dictionary of values to come
|
|
|
|
cluster_data = dict()
|
|
|
|
|
|
|
|
def get_data(path):
|
|
|
|
data = zkhandler.read(path)
|
|
|
|
children = zkhandler.children(path)
|
|
|
|
|
|
|
|
cluster_data[path] = data
|
|
|
|
|
|
|
|
if children:
|
2021-11-06 03:02:43 -04:00
|
|
|
if path == "/":
|
|
|
|
child_prefix = "/"
|
2021-06-13 14:22:26 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
child_prefix = path + "/"
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
for child in children:
|
2021-11-06 03:02:43 -04:00
|
|
|
if child_prefix + child == "/zookeeper":
|
2021-06-13 14:22:26 -04:00
|
|
|
# We must skip the built-in /zookeeper tree
|
|
|
|
continue
|
2021-11-06 03:02:43 -04:00
|
|
|
if child_prefix + child == "/patroni":
|
2021-06-13 14:22:26 -04:00
|
|
|
# We must skip the /patroni tree
|
|
|
|
continue
|
|
|
|
|
|
|
|
get_data(child_prefix + child)
|
|
|
|
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
get_data("/")
|
2021-06-13 14:22:26 -04:00
|
|
|
except Exception as e:
|
2021-11-06 03:02:43 -04:00
|
|
|
return False, "ERROR: Failed to obtain backup: {}".format(e)
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
return True, cluster_data
|
|
|
|
|
|
|
|
|
|
|
|
def cluster_restore(zkhandler, cluster_data):
|
|
|
|
# Build a key+value list
|
|
|
|
kv = []
|
|
|
|
schema_version = None
|
|
|
|
for key in cluster_data:
|
2021-11-06 03:02:43 -04:00
|
|
|
if key == zkhandler.schema.path("base.schema.version"):
|
2021-06-13 14:22:26 -04:00
|
|
|
schema_version = cluster_data[key]
|
|
|
|
data = cluster_data[key]
|
|
|
|
kv.append((key, data))
|
|
|
|
|
2021-06-13 14:43:37 -04:00
|
|
|
if int(schema_version) != int(zkhandler.schema.version):
|
2021-11-06 03:02:43 -04:00
|
|
|
return (
|
|
|
|
False,
|
|
|
|
"ERROR: Schema version of backup ({}) does not match cluster schema version ({}).".format(
|
|
|
|
schema_version, zkhandler.schema.version
|
|
|
|
),
|
|
|
|
)
|
2021-06-13 14:22:26 -04:00
|
|
|
|
|
|
|
# Close the Zookeeper connection
|
|
|
|
result = zkhandler.write(kv)
|
|
|
|
|
|
|
|
if result:
|
2021-11-06 03:02:43 -04:00
|
|
|
return True, "Restore completed successfully."
|
2021-06-13 14:22:26 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
return False, "Restore failed."
|