2018-09-20 03:25:58 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# common.py - PVC client function library, common fuctions
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2023-12-29 11:16:59 -05:00
|
|
|
# Copyright (C) 2018-2024 Joshua M. Boniface <joshua@boniface.me>
|
2018-09-20 03:25:58 -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.
|
2018-09-20 03:25:58 -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-11-07 13:35:12 -05:00
|
|
|
import time
|
2018-09-20 03:25:58 -04:00
|
|
|
import uuid
|
|
|
|
import lxml
|
2020-02-08 23:43:11 -05:00
|
|
|
import subprocess
|
2021-06-01 12:17:25 -04:00
|
|
|
import signal
|
2020-06-07 00:40:21 -04:00
|
|
|
from json import loads
|
2020-12-01 04:44:33 -05:00
|
|
|
from re import match as re_match
|
2024-08-30 10:41:56 -04:00
|
|
|
from re import search as re_search
|
2021-06-21 18:40:11 -04:00
|
|
|
from re import split as re_split
|
2023-11-09 14:05:15 -05:00
|
|
|
from re import sub as re_sub
|
2024-05-21 13:14:55 -04:00
|
|
|
from difflib import unified_diff
|
2019-12-23 20:43:20 -05:00
|
|
|
from distutils.util import strtobool
|
2021-06-01 12:17:25 -04:00
|
|
|
from threading import Thread
|
|
|
|
from shlex import split as shlex_split
|
2021-07-01 14:00:59 -04:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
2023-12-09 12:36:32 -05:00
|
|
|
###############################################################################
|
|
|
|
# Global Variables
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
# State lists
|
2023-12-09 12:51:41 -05:00
|
|
|
fault_state_combinations = [
|
|
|
|
"new",
|
|
|
|
"ack",
|
|
|
|
]
|
2023-12-09 12:36:32 -05:00
|
|
|
node_state_combinations = [
|
|
|
|
"run,ready",
|
|
|
|
"run,flush",
|
|
|
|
"run,flushed",
|
|
|
|
"run,unflush",
|
|
|
|
"init,ready",
|
|
|
|
"init,flush",
|
|
|
|
"init,flushed",
|
|
|
|
"init,unflush",
|
2023-12-27 13:32:44 -05:00
|
|
|
"shutdown,ready",
|
|
|
|
"shutdown,flush",
|
|
|
|
"shutdown,flushed",
|
|
|
|
"shutdown,unflush",
|
2023-12-09 12:36:32 -05:00
|
|
|
"stop,ready",
|
|
|
|
"stop,flush",
|
|
|
|
"stop,flushed",
|
|
|
|
"stop,unflush",
|
|
|
|
"dead,ready",
|
|
|
|
"dead,flush",
|
|
|
|
"dead,fence-flush",
|
|
|
|
"dead,flushed",
|
|
|
|
"dead,unflush",
|
|
|
|
"fenced,ready",
|
|
|
|
"fenced,flush",
|
|
|
|
"fenced,flushed",
|
|
|
|
"fenced,unflush",
|
|
|
|
]
|
|
|
|
vm_state_combinations = [
|
|
|
|
"start",
|
|
|
|
"restart",
|
|
|
|
"shutdown",
|
|
|
|
"stop",
|
|
|
|
"disable",
|
|
|
|
"fail",
|
|
|
|
"migrate",
|
|
|
|
"unmigrate",
|
|
|
|
"provision",
|
2024-08-20 11:51:57 -04:00
|
|
|
"import",
|
|
|
|
"restore",
|
2024-09-11 10:12:29 -04:00
|
|
|
"mirror",
|
2023-12-09 12:36:32 -05:00
|
|
|
]
|
|
|
|
ceph_osd_state_combinations = [
|
|
|
|
"up,in",
|
|
|
|
"up,out",
|
|
|
|
"down,in",
|
|
|
|
"down,out",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-07-01 14:00:59 -04:00
|
|
|
###############################################################################
|
|
|
|
# Performance Profiler decorator
|
|
|
|
###############################################################################
|
|
|
|
|
2023-09-12 16:41:02 -04:00
|
|
|
|
2021-07-01 14:00:59 -04:00
|
|
|
# Get performance statistics on a function or class
|
|
|
|
class Profiler(object):
|
|
|
|
def __init__(self, config):
|
2021-11-06 03:02:43 -04:00
|
|
|
self.is_debug = config["debug"]
|
|
|
|
self.pvc_logdir = "/var/log/pvc"
|
2021-07-01 14:00:59 -04:00
|
|
|
|
|
|
|
def __call__(self, function):
|
|
|
|
if not callable(function):
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self.is_debug:
|
|
|
|
return function
|
|
|
|
|
|
|
|
@wraps(function)
|
|
|
|
def profiler_wrapper(*args, **kwargs):
|
|
|
|
import cProfile
|
|
|
|
import pstats
|
|
|
|
from os import path, makedirs
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
if not path.exists(self.pvc_logdir):
|
2021-11-06 03:02:43 -04:00
|
|
|
print(
|
|
|
|
"Profiler: Requested profiling of {} but no log dir present; printing instead.".format(
|
|
|
|
str(function.__name__)
|
|
|
|
)
|
|
|
|
)
|
2021-07-01 14:00:59 -04:00
|
|
|
log_result = False
|
|
|
|
else:
|
|
|
|
log_result = True
|
2021-11-06 03:02:43 -04:00
|
|
|
profiler_logdir = "{}/profiler".format(self.pvc_logdir)
|
2021-07-01 14:00:59 -04:00
|
|
|
if not path.exists(profiler_logdir):
|
|
|
|
makedirs(profiler_logdir)
|
|
|
|
|
|
|
|
pr = cProfile.Profile()
|
|
|
|
pr.enable()
|
|
|
|
|
|
|
|
ret = function(*args, **kwargs)
|
|
|
|
|
|
|
|
pr.disable()
|
|
|
|
stats = pstats.Stats(pr)
|
|
|
|
stats.sort_stats(pstats.SortKey.TIME)
|
|
|
|
|
|
|
|
if log_result:
|
2021-11-06 03:02:43 -04:00
|
|
|
stats.dump_stats(
|
|
|
|
filename="{}/{}_{}.log".format(
|
|
|
|
profiler_logdir,
|
|
|
|
str(function.__name__),
|
|
|
|
str(datetime.now()).replace(" ", "_"),
|
|
|
|
)
|
|
|
|
)
|
2021-07-01 14:00:59 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
print(
|
|
|
|
"Profiler stats for function {} at {}:".format(
|
|
|
|
str(function.__name__), str(datetime.now())
|
|
|
|
)
|
|
|
|
)
|
2021-07-01 14:00:59 -04:00
|
|
|
stats.print_stats()
|
|
|
|
|
|
|
|
return ret
|
2021-11-06 03:02:43 -04:00
|
|
|
|
2021-07-01 14:00:59 -04:00
|
|
|
return profiler_wrapper
|
2021-06-01 12:17:25 -04:00
|
|
|
|
2019-12-23 20:43:20 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
###############################################################################
|
|
|
|
# Supplemental functions
|
|
|
|
###############################################################################
|
|
|
|
|
2023-09-12 16:41:02 -04:00
|
|
|
|
2020-02-08 23:31:07 -05:00
|
|
|
#
|
2021-06-01 12:17:25 -04:00
|
|
|
# Run a local OS daemon in the background
|
2020-02-08 23:31:07 -05:00
|
|
|
#
|
2021-06-01 12:17:25 -04:00
|
|
|
class OSDaemon(object):
|
|
|
|
def __init__(self, command_string, environment, logfile):
|
|
|
|
command = shlex_split(command_string)
|
|
|
|
# Set stdout to be a logfile if set
|
|
|
|
if logfile:
|
2021-11-06 03:02:43 -04:00
|
|
|
stdout = open(logfile, "a")
|
2021-06-01 12:17:25 -04:00
|
|
|
else:
|
|
|
|
stdout = subprocess.PIPE
|
|
|
|
|
|
|
|
# Invoke the process
|
|
|
|
self.proc = subprocess.Popen(
|
2020-02-08 23:31:07 -05:00
|
|
|
command,
|
|
|
|
env=environment,
|
2021-06-01 12:17:25 -04:00
|
|
|
stdout=stdout,
|
|
|
|
stderr=stdout,
|
2020-02-08 23:31:07 -05:00
|
|
|
)
|
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
# Signal the process
|
|
|
|
def signal(self, sent_signal):
|
|
|
|
signal_map = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"hup": signal.SIGHUP,
|
|
|
|
"int": signal.SIGINT,
|
|
|
|
"term": signal.SIGTERM,
|
|
|
|
"kill": signal.SIGKILL,
|
2021-06-01 12:17:25 -04:00
|
|
|
}
|
|
|
|
self.proc.send_signal(signal_map[sent_signal])
|
|
|
|
|
|
|
|
|
|
|
|
def run_os_daemon(command_string, environment=None, logfile=None):
|
|
|
|
daemon = OSDaemon(command_string, environment, logfile)
|
|
|
|
return daemon
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Run a local OS command via shell
|
|
|
|
#
|
|
|
|
def run_os_command(command_string, background=False, environment=None, timeout=None):
|
2023-10-23 09:50:58 -04:00
|
|
|
if not isinstance(command_string, list):
|
|
|
|
command = shlex_split(command_string)
|
|
|
|
else:
|
|
|
|
command = command_string
|
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
if background:
|
2021-11-06 03:02:43 -04:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
def runcmd():
|
|
|
|
try:
|
|
|
|
subprocess.run(
|
|
|
|
command,
|
|
|
|
env=environment,
|
|
|
|
timeout=timeout,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
pass
|
2021-11-06 03:02:43 -04:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
thread = Thread(target=runcmd, args=())
|
|
|
|
thread.start()
|
|
|
|
return 0, None, None
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
command_output = subprocess.run(
|
|
|
|
command,
|
|
|
|
env=environment,
|
|
|
|
timeout=timeout,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
retcode = command_output.returncode
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
retcode = 128
|
|
|
|
except Exception:
|
|
|
|
retcode = 255
|
|
|
|
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
stdout = command_output.stdout.decode("ascii")
|
2021-06-01 12:17:25 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
stdout = ""
|
2021-06-01 12:17:25 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
stderr = command_output.stderr.decode("ascii")
|
2021-06-01 12:17:25 -04:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
stderr = ""
|
2021-06-01 12:17:25 -04:00
|
|
|
return retcode, stdout, stderr
|
2020-02-08 23:31:07 -05:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Validate a UUID
|
|
|
|
#
|
|
|
|
def validateUUID(dom_uuid):
|
|
|
|
try:
|
|
|
|
uuid.UUID(dom_uuid)
|
|
|
|
return True
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Parse a Domain XML object
|
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def getDomainXML(zkhandler, dom_uuid):
|
2018-09-20 03:25:58 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
xml = zkhandler.read(("domain.xml", dom_uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2018-09-20 03:25:58 -04:00
|
|
|
return None
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# Parse XML using lxml.objectify
|
|
|
|
parsed_xml = lxml.objectify.fromstring(xml)
|
|
|
|
return parsed_xml
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Get the main details for a VM object from XML
|
|
|
|
#
|
|
|
|
def getDomainMainDetails(parsed_xml):
|
|
|
|
# Get the information we want from it
|
|
|
|
duuid = str(parsed_xml.uuid)
|
|
|
|
try:
|
|
|
|
ddescription = str(parsed_xml.description)
|
|
|
|
except AttributeError:
|
|
|
|
ddescription = "N/A"
|
|
|
|
dname = str(parsed_xml.name)
|
|
|
|
dmemory = str(parsed_xml.memory)
|
2021-11-06 03:02:43 -04:00
|
|
|
dmemory_unit = str(parsed_xml.memory.attrib.get("unit"))
|
|
|
|
if dmemory_unit == "KiB":
|
2018-10-14 02:01:35 -04:00
|
|
|
dmemory = int(int(dmemory) / 1024)
|
2021-11-06 03:02:43 -04:00
|
|
|
elif dmemory_unit == "GiB":
|
2018-10-14 02:01:35 -04:00
|
|
|
dmemory = int(int(dmemory) * 1024)
|
2018-09-20 03:25:58 -04:00
|
|
|
dvcpu = str(parsed_xml.vcpu)
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
dvcputopo = "{}/{}/{}".format(
|
|
|
|
parsed_xml.cpu.topology.attrib.get("sockets"),
|
|
|
|
parsed_xml.cpu.topology.attrib.get("cores"),
|
|
|
|
parsed_xml.cpu.topology.attrib.get("threads"),
|
|
|
|
)
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
dvcputopo = "N/A"
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
return duuid, dname, ddescription, dmemory, dvcpu, dvcputopo
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Get long-format details
|
|
|
|
#
|
|
|
|
def getDomainExtraDetails(parsed_xml):
|
2019-07-04 12:52:53 -04:00
|
|
|
dtype = str(parsed_xml.os.type)
|
2021-11-06 03:02:43 -04:00
|
|
|
darch = str(parsed_xml.os.type.attrib["arch"])
|
|
|
|
dmachine = str(parsed_xml.os.type.attrib["machine"])
|
|
|
|
dconsole = str(parsed_xml.devices.console.attrib["type"])
|
2019-07-04 12:52:53 -04:00
|
|
|
demulator = str(parsed_xml.devices.emulator)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
return dtype, darch, dmachine, dconsole, demulator
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Get CPU features
|
|
|
|
#
|
|
|
|
def getDomainCPUFeatures(parsed_xml):
|
|
|
|
dfeatures = []
|
2020-07-08 12:32:42 -04:00
|
|
|
try:
|
|
|
|
for feature in parsed_xml.features.getchildren():
|
|
|
|
dfeatures.append(feature.tag)
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-07-08 12:32:42 -04:00
|
|
|
pass
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
return dfeatures
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Get disk devices
|
|
|
|
#
|
2020-06-07 00:40:21 -04:00
|
|
|
def getDomainDisks(parsed_xml, stats_data):
|
2018-09-20 03:25:58 -04:00
|
|
|
ddisks = []
|
|
|
|
for device in parsed_xml.devices.getchildren():
|
2021-11-06 03:02:43 -04:00
|
|
|
if device.tag == "disk":
|
2018-09-20 03:25:58 -04:00
|
|
|
disk_attrib = device.source.attrib
|
|
|
|
disk_target = device.target.attrib
|
2021-11-06 03:02:43 -04:00
|
|
|
disk_type = device.attrib.get("type")
|
|
|
|
disk_stats_list = [
|
|
|
|
x
|
|
|
|
for x in stats_data.get("disk_stats", [])
|
|
|
|
if x.get("name") == disk_attrib.get("name")
|
|
|
|
]
|
2020-06-07 00:40:21 -04:00
|
|
|
try:
|
|
|
|
disk_stats = disk_stats_list[0]
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-06-07 00:40:21 -04:00
|
|
|
disk_stats = {}
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if disk_type == "network":
|
2020-06-07 00:40:21 -04:00
|
|
|
disk_obj = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"type": disk_attrib.get("protocol"),
|
|
|
|
"name": disk_attrib.get("name"),
|
|
|
|
"dev": disk_target.get("dev"),
|
|
|
|
"bus": disk_target.get("bus"),
|
|
|
|
"rd_req": disk_stats.get("rd_req", 0),
|
|
|
|
"rd_bytes": disk_stats.get("rd_bytes", 0),
|
|
|
|
"wr_req": disk_stats.get("wr_req", 0),
|
|
|
|
"wr_bytes": disk_stats.get("wr_bytes", 0),
|
2020-06-07 00:40:21 -04:00
|
|
|
}
|
2021-11-06 03:02:43 -04:00
|
|
|
elif disk_type == "file":
|
2020-06-07 00:40:21 -04:00
|
|
|
disk_obj = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"type": "file",
|
|
|
|
"name": disk_attrib.get("file"),
|
|
|
|
"dev": disk_target.get("dev"),
|
|
|
|
"bus": disk_target.get("bus"),
|
|
|
|
"rd_req": disk_stats.get("rd_req", 0),
|
|
|
|
"rd_bytes": disk_stats.get("rd_bytes", 0),
|
|
|
|
"wr_req": disk_stats.get("wr_req", 0),
|
|
|
|
"wr_bytes": disk_stats.get("wr_bytes", 0),
|
2020-06-07 00:40:21 -04:00
|
|
|
}
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
|
|
|
disk_obj = {}
|
|
|
|
ddisks.append(disk_obj)
|
|
|
|
|
|
|
|
return ddisks
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
#
|
|
|
|
# Get a list of disk devices
|
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def getDomainDiskList(zkhandler, dom_uuid):
|
|
|
|
domain_information = getInformationFromXML(zkhandler, dom_uuid)
|
2019-07-09 09:29:47 -04:00
|
|
|
disk_list = []
|
2021-11-06 03:02:43 -04:00
|
|
|
for disk in domain_information["disks"]:
|
|
|
|
disk_list.append(disk["name"])
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
return disk_list
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-07-13 01:46:50 -04:00
|
|
|
#
|
|
|
|
# Get a list of domain tags
|
|
|
|
#
|
|
|
|
def getDomainTags(zkhandler, dom_uuid):
|
2021-07-13 19:04:56 -04:00
|
|
|
"""
|
|
|
|
Get a list of tags for domain dom_uuid
|
|
|
|
|
|
|
|
The UUID must be validated before calling this function!
|
|
|
|
"""
|
|
|
|
tags = list()
|
|
|
|
|
2023-12-11 03:34:36 -05:00
|
|
|
all_tags = zkhandler.children(("domain.meta.tags", dom_uuid))
|
|
|
|
|
|
|
|
tag_reads = list()
|
|
|
|
for tag in all_tags:
|
|
|
|
tag_reads += [
|
|
|
|
("domain.meta.tags", dom_uuid, "tag.type", tag),
|
|
|
|
("domain.meta.tags", dom_uuid, "tag.protected", tag),
|
|
|
|
]
|
|
|
|
all_tag_data = zkhandler.read_many(tag_reads)
|
|
|
|
|
|
|
|
for tidx, tag in enumerate(all_tags):
|
|
|
|
# Split the large list of return values by the IDX of this tag
|
|
|
|
# Each tag result is 2 fields long
|
|
|
|
pos_start = tidx * 2
|
|
|
|
pos_end = tidx * 2 + 2
|
|
|
|
tag_type, protected = tuple(all_tag_data[pos_start:pos_end])
|
|
|
|
protected = bool(strtobool(protected))
|
2021-11-06 03:02:43 -04:00
|
|
|
tags.append({"name": tag, "type": tag_type, "protected": protected})
|
2021-07-13 19:04:56 -04:00
|
|
|
|
2021-07-13 01:46:50 -04:00
|
|
|
return tags
|
|
|
|
|
|
|
|
|
2024-05-21 13:14:55 -04:00
|
|
|
#
|
|
|
|
# Get a list of domain snapshots
|
|
|
|
#
|
|
|
|
def getDomainSnapshots(zkhandler, dom_uuid):
|
|
|
|
"""
|
|
|
|
Get a list of snapshots for domain dom_uuid
|
|
|
|
|
|
|
|
The UUID must be validated before calling this function!
|
|
|
|
"""
|
|
|
|
snapshots = list()
|
|
|
|
|
|
|
|
all_snapshots = zkhandler.children(("domain.snapshots", dom_uuid))
|
|
|
|
|
2024-05-21 15:13:29 -04:00
|
|
|
current_timestamp = time.time()
|
2024-05-21 13:14:55 -04:00
|
|
|
current_dom_xml = zkhandler.read(("domain.xml", dom_uuid))
|
|
|
|
|
|
|
|
snapshots = list()
|
|
|
|
for snapshot in all_snapshots:
|
|
|
|
(
|
|
|
|
snap_name,
|
|
|
|
snap_timestamp,
|
|
|
|
_snap_rbd_snapshots,
|
|
|
|
snap_dom_xml,
|
|
|
|
) = zkhandler.read_many(
|
|
|
|
[
|
|
|
|
("domain.snapshots", dom_uuid, "domain_snapshot.name", snapshot),
|
|
|
|
("domain.snapshots", dom_uuid, "domain_snapshot.timestamp", snapshot),
|
|
|
|
(
|
|
|
|
"domain.snapshots",
|
|
|
|
dom_uuid,
|
|
|
|
"domain_snapshot.rbd_snapshots",
|
|
|
|
snapshot,
|
|
|
|
),
|
|
|
|
("domain.snapshots", dom_uuid, "domain_snapshot.xml", snapshot),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
snap_rbd_snapshots = _snap_rbd_snapshots.split(",")
|
|
|
|
|
|
|
|
snap_dom_xml_diff = list(
|
|
|
|
unified_diff(
|
|
|
|
current_dom_xml.split("\n"),
|
|
|
|
snap_dom_xml.split("\n"),
|
|
|
|
fromfile="current",
|
|
|
|
tofile="snapshot",
|
|
|
|
fromfiledate="",
|
|
|
|
tofiledate="",
|
|
|
|
n=1,
|
|
|
|
lineterm="",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-05-21 15:13:29 -04:00
|
|
|
_snap_timestamp = float(snap_timestamp)
|
|
|
|
snap_age_secs = int(current_timestamp) - int(_snap_timestamp)
|
2024-05-21 13:14:55 -04:00
|
|
|
snapshots.append(
|
|
|
|
{
|
|
|
|
"name": snap_name,
|
|
|
|
"timestamp": snap_timestamp,
|
2024-11-16 13:31:56 -05:00
|
|
|
"age": snap_age_secs,
|
2024-05-21 13:14:55 -04:00
|
|
|
"xml_diff_lines": snap_dom_xml_diff,
|
|
|
|
"rbd_snapshots": snap_rbd_snapshots,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return sorted(snapshots, key=lambda s: s["timestamp"], reverse=True)
|
|
|
|
|
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
#
|
2021-07-13 02:08:54 -04:00
|
|
|
# Get a set of domain metadata
|
2019-07-09 09:29:47 -04:00
|
|
|
#
|
2021-07-13 02:08:54 -04:00
|
|
|
def getDomainMetadata(zkhandler, dom_uuid):
|
2021-07-13 19:04:56 -04:00
|
|
|
"""
|
|
|
|
Get the domain metadata for domain dom_uuid
|
|
|
|
|
|
|
|
The UUID must be validated before calling this function!
|
|
|
|
"""
|
2023-12-11 03:01:44 -05:00
|
|
|
(
|
|
|
|
domain_node_limit,
|
|
|
|
domain_node_selector,
|
|
|
|
domain_node_autostart,
|
|
|
|
domain_migration_method,
|
2024-01-10 16:13:31 -05:00
|
|
|
domain_migration_max_downtime,
|
2023-12-11 03:01:44 -05:00
|
|
|
) = zkhandler.read_many(
|
|
|
|
[
|
|
|
|
("domain.meta.node_limit", dom_uuid),
|
|
|
|
("domain.meta.node_selector", dom_uuid),
|
|
|
|
("domain.meta.autostart", dom_uuid),
|
|
|
|
("domain.meta.migrate_method", dom_uuid),
|
2024-01-10 16:13:31 -05:00
|
|
|
("domain.meta.migrate_max_downtime", dom_uuid),
|
2023-12-11 03:01:44 -05:00
|
|
|
]
|
|
|
|
)
|
2019-10-12 01:36:50 -04:00
|
|
|
|
|
|
|
if not domain_node_limit:
|
2019-12-19 13:29:15 -05:00
|
|
|
domain_node_limit = None
|
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
domain_node_limit = domain_node_limit.split(",")
|
2019-12-19 13:29:15 -05:00
|
|
|
|
2023-12-11 03:01:44 -05:00
|
|
|
if not domain_node_selector or domain_node_selector == "none":
|
|
|
|
domain_node_selector = None
|
|
|
|
|
2019-10-17 10:31:19 -04:00
|
|
|
if not domain_node_autostart:
|
2019-12-19 13:29:15 -05:00
|
|
|
domain_node_autostart = None
|
2019-10-12 01:17:39 -04:00
|
|
|
|
2023-12-11 03:01:44 -05:00
|
|
|
if not domain_migration_method or domain_migration_method == "none":
|
|
|
|
domain_migration_method = None
|
|
|
|
|
2024-01-10 16:13:31 -05:00
|
|
|
if not domain_migration_max_downtime or domain_migration_max_downtime == "none":
|
|
|
|
domain_migration_max_downtime = 300
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
return (
|
|
|
|
domain_node_limit,
|
|
|
|
domain_node_selector,
|
|
|
|
domain_node_autostart,
|
|
|
|
domain_migration_method,
|
2024-01-10 16:13:31 -05:00
|
|
|
domain_migration_max_downtime,
|
2021-11-06 03:02:43 -04:00
|
|
|
)
|
2021-07-13 02:08:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Get domain information from XML
|
|
|
|
#
|
|
|
|
def getInformationFromXML(zkhandler, uuid):
|
|
|
|
"""
|
|
|
|
Gather information about a VM from the Libvirt XML configuration in the Zookeper database
|
|
|
|
and return a dict() containing it.
|
|
|
|
"""
|
2023-12-11 03:34:36 -05:00
|
|
|
(
|
|
|
|
domain_state,
|
|
|
|
domain_node,
|
|
|
|
domain_lastnode,
|
|
|
|
domain_failedreason,
|
|
|
|
domain_profile,
|
|
|
|
domain_vnc,
|
|
|
|
stats_data,
|
|
|
|
) = zkhandler.read_many(
|
|
|
|
[
|
|
|
|
("domain.state", uuid),
|
|
|
|
("domain.node", uuid),
|
|
|
|
("domain.last_node", uuid),
|
|
|
|
("domain.failed_reason", uuid),
|
|
|
|
("domain.profile", uuid),
|
|
|
|
("domain.console.vnc", uuid),
|
|
|
|
("domain.stats", uuid),
|
|
|
|
]
|
|
|
|
)
|
2021-11-06 03:02:43 -04:00
|
|
|
|
|
|
|
(
|
|
|
|
domain_node_limit,
|
|
|
|
domain_node_selector,
|
|
|
|
domain_node_autostart,
|
|
|
|
domain_migration_method,
|
2024-01-10 16:13:31 -05:00
|
|
|
domain_migration_max_downtime,
|
2021-11-06 03:02:43 -04:00
|
|
|
) = getDomainMetadata(zkhandler, uuid)
|
2023-12-11 03:34:36 -05:00
|
|
|
|
2021-07-13 19:04:56 -04:00
|
|
|
domain_tags = getDomainTags(zkhandler, uuid)
|
2024-05-21 13:14:55 -04:00
|
|
|
domain_snapshots = getDomainSnapshots(zkhandler, uuid)
|
2019-12-11 16:50:38 -05:00
|
|
|
|
2021-07-02 11:53:48 -04:00
|
|
|
if domain_vnc:
|
2021-11-06 03:02:43 -04:00
|
|
|
domain_vnc_listen, domain_vnc_port = domain_vnc.split(":")
|
2021-07-02 11:53:48 -04:00
|
|
|
else:
|
2023-12-11 03:34:36 -05:00
|
|
|
domain_vnc_listen = None
|
|
|
|
domain_vnc_port = None
|
2020-12-20 16:00:55 -05:00
|
|
|
|
2021-05-29 20:35:28 -04:00
|
|
|
parsed_xml = getDomainXML(zkhandler, uuid)
|
2019-07-09 09:29:47 -04:00
|
|
|
|
2021-07-09 13:13:54 -04:00
|
|
|
if stats_data is not None:
|
|
|
|
try:
|
|
|
|
stats_data = loads(stats_data)
|
|
|
|
except Exception:
|
|
|
|
stats_data = {}
|
|
|
|
else:
|
2020-06-07 00:40:21 -04:00
|
|
|
stats_data = {}
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
(
|
|
|
|
domain_uuid,
|
|
|
|
domain_name,
|
|
|
|
domain_description,
|
|
|
|
domain_memory,
|
|
|
|
domain_vcpu,
|
|
|
|
domain_vcputopo,
|
|
|
|
) = getDomainMainDetails(parsed_xml)
|
2023-12-11 03:34:36 -05:00
|
|
|
|
2020-06-07 00:40:21 -04:00
|
|
|
domain_networks = getDomainNetworks(parsed_xml, stats_data)
|
2019-07-09 09:29:47 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
(
|
|
|
|
domain_type,
|
|
|
|
domain_arch,
|
|
|
|
domain_machine,
|
|
|
|
domain_console,
|
|
|
|
domain_emulator,
|
|
|
|
) = getDomainExtraDetails(parsed_xml)
|
2019-07-09 09:29:47 -04:00
|
|
|
|
|
|
|
domain_features = getDomainCPUFeatures(parsed_xml)
|
2020-06-07 00:40:21 -04:00
|
|
|
domain_disks = getDomainDisks(parsed_xml, stats_data)
|
2019-07-09 09:29:47 -04:00
|
|
|
domain_controllers = getDomainControllers(parsed_xml)
|
2020-11-06 19:05:48 -05:00
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
if domain_lastnode:
|
2021-11-06 03:02:43 -04:00
|
|
|
domain_migrated = "from {}".format(domain_lastnode)
|
2019-07-09 09:29:47 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
domain_migrated = "no"
|
2019-07-09 09:29:47 -04:00
|
|
|
|
|
|
|
domain_information = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"name": domain_name,
|
|
|
|
"uuid": domain_uuid,
|
|
|
|
"state": domain_state,
|
|
|
|
"node": domain_node,
|
|
|
|
"last_node": domain_lastnode,
|
|
|
|
"migrated": domain_migrated,
|
|
|
|
"failed_reason": domain_failedreason,
|
|
|
|
"node_limit": domain_node_limit,
|
|
|
|
"node_selector": domain_node_selector,
|
|
|
|
"node_autostart": bool(strtobool(domain_node_autostart)),
|
|
|
|
"migration_method": domain_migration_method,
|
2024-01-10 16:13:31 -05:00
|
|
|
"migration_max_downtime": int(domain_migration_max_downtime),
|
2021-11-06 03:02:43 -04:00
|
|
|
"tags": domain_tags,
|
2024-05-21 13:14:55 -04:00
|
|
|
"snapshots": domain_snapshots,
|
2021-11-06 03:02:43 -04:00
|
|
|
"description": domain_description,
|
|
|
|
"profile": domain_profile,
|
|
|
|
"memory": int(domain_memory),
|
|
|
|
"memory_stats": stats_data.get("mem_stats", {}),
|
|
|
|
"vcpu": int(domain_vcpu),
|
|
|
|
"vcpu_topology": domain_vcputopo,
|
|
|
|
"vcpu_stats": stats_data.get("cpu_stats", {}),
|
|
|
|
"networks": domain_networks,
|
|
|
|
"type": domain_type,
|
|
|
|
"arch": domain_arch,
|
|
|
|
"machine": domain_machine,
|
|
|
|
"console": domain_console,
|
|
|
|
"vnc": {"listen": domain_vnc_listen, "port": domain_vnc_port},
|
|
|
|
"emulator": domain_emulator,
|
|
|
|
"features": domain_features,
|
|
|
|
"disks": domain_disks,
|
|
|
|
"controllers": domain_controllers,
|
|
|
|
"xml": lxml.etree.tostring(parsed_xml, encoding="ascii", method="xml")
|
|
|
|
.decode()
|
|
|
|
.replace('"', "'"),
|
2019-07-09 09:29:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return domain_information
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Get network devices
|
|
|
|
#
|
2020-06-07 00:40:21 -04:00
|
|
|
def getDomainNetworks(parsed_xml, stats_data):
|
2018-09-20 03:25:58 -04:00
|
|
|
dnets = []
|
|
|
|
for device in parsed_xml.devices.getchildren():
|
2021-11-06 03:02:43 -04:00
|
|
|
if device.tag == "interface":
|
2020-06-07 00:40:21 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
net_type = device.attrib.get("type")
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-08-21 10:27:45 -04:00
|
|
|
net_type = None
|
2021-06-21 22:21:54 -04:00
|
|
|
|
2020-08-21 10:27:45 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
net_mac = device.mac.attrib.get("address")
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-08-21 10:27:45 -04:00
|
|
|
net_mac = None
|
2021-06-21 22:21:54 -04:00
|
|
|
|
2020-08-21 10:27:45 -04:00
|
|
|
try:
|
|
|
|
net_bridge = device.source.attrib.get(net_type)
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-08-21 10:27:45 -04:00
|
|
|
net_bridge = None
|
2021-06-21 22:21:54 -04:00
|
|
|
|
2020-08-21 10:27:45 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
net_model = device.model.attrib.get("type")
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-08-21 10:27:45 -04:00
|
|
|
net_model = None
|
2021-06-21 22:21:54 -04:00
|
|
|
|
2020-08-21 10:27:45 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
net_stats_list = [
|
|
|
|
x
|
|
|
|
for x in stats_data.get("net_stats", [])
|
|
|
|
if x.get("bridge") == net_bridge
|
|
|
|
]
|
2020-06-07 00:40:21 -04:00
|
|
|
net_stats = net_stats_list[0]
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-06-07 00:40:21 -04:00
|
|
|
net_stats = {}
|
2021-06-21 22:21:54 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
net_rd_bytes = net_stats.get("rd_bytes", 0)
|
|
|
|
net_rd_packets = net_stats.get("rd_packets", 0)
|
|
|
|
net_rd_errors = net_stats.get("rd_errors", 0)
|
|
|
|
net_rd_drops = net_stats.get("rd_drops", 0)
|
|
|
|
net_wr_bytes = net_stats.get("wr_bytes", 0)
|
|
|
|
net_wr_packets = net_stats.get("wr_packets", 0)
|
|
|
|
net_wr_errors = net_stats.get("wr_errors", 0)
|
|
|
|
net_wr_drops = net_stats.get("wr_drops", 0)
|
|
|
|
|
|
|
|
if net_type == "direct":
|
|
|
|
net_vni = "macvtap:" + device.source.attrib.get("dev")
|
|
|
|
net_bridge = device.source.attrib.get("dev")
|
|
|
|
elif net_type == "hostdev":
|
|
|
|
net_vni = "hostdev:" + str(device.sriov_device)
|
2021-06-21 22:21:54 -04:00
|
|
|
net_bridge = str(device.sriov_device)
|
2021-06-15 00:27:01 -04:00
|
|
|
else:
|
2021-11-06 03:02:43 -04:00
|
|
|
net_vni = re_match(r"[vm]*br([0-9a-z]+)", net_bridge).group(1)
|
2021-06-21 22:21:54 -04:00
|
|
|
|
2020-06-07 00:40:21 -04:00
|
|
|
net_obj = {
|
2021-11-06 03:02:43 -04:00
|
|
|
"type": net_type,
|
|
|
|
"vni": net_vni,
|
|
|
|
"mac": net_mac,
|
|
|
|
"source": net_bridge,
|
|
|
|
"model": net_model,
|
|
|
|
"rd_bytes": net_rd_bytes,
|
|
|
|
"rd_packets": net_rd_packets,
|
|
|
|
"rd_errors": net_rd_errors,
|
|
|
|
"rd_drops": net_rd_drops,
|
|
|
|
"wr_bytes": net_wr_bytes,
|
|
|
|
"wr_packets": net_wr_packets,
|
|
|
|
"wr_errors": net_wr_errors,
|
|
|
|
"wr_drops": net_wr_drops,
|
2020-06-07 00:40:21 -04:00
|
|
|
}
|
2018-09-20 03:25:58 -04:00
|
|
|
dnets.append(net_obj)
|
|
|
|
|
|
|
|
return dnets
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Get controller devices
|
|
|
|
#
|
|
|
|
def getDomainControllers(parsed_xml):
|
|
|
|
dcontrollers = []
|
|
|
|
for device in parsed_xml.devices.getchildren():
|
2021-11-06 03:02:43 -04:00
|
|
|
if device.tag == "controller":
|
|
|
|
controller_type = device.attrib.get("type")
|
2018-09-20 03:25:58 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
controller_model = device.attrib.get("model")
|
2018-09-20 03:25:58 -04:00
|
|
|
except KeyError:
|
2021-11-06 03:02:43 -04:00
|
|
|
controller_model = "none"
|
|
|
|
controller_obj = {"type": controller_type, "model": controller_model}
|
2018-09-20 03:25:58 -04:00
|
|
|
dcontrollers.append(controller_obj)
|
|
|
|
|
|
|
|
return dcontrollers
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Verify node is valid in cluster
|
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def verifyNode(zkhandler, node):
|
2021-11-06 03:02:43 -04:00
|
|
|
return zkhandler.exists(("node", node))
|
2018-10-27 15:24:42 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-24 15:10:12 -04:00
|
|
|
#
|
2018-10-27 15:24:42 -04:00
|
|
|
# Get the primary coordinator node
|
2018-09-24 15:10:12 -04:00
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def getPrimaryNode(zkhandler):
|
2018-10-27 15:24:42 -04:00
|
|
|
failcount = 0
|
|
|
|
while True:
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
primary_node = zkhandler.read("base.config.primary_node")
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2021-11-06 03:02:43 -04:00
|
|
|
primary_node == "none"
|
2018-10-27 15:24:42 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if primary_node == "none":
|
2018-10-27 15:24:42 -04:00
|
|
|
raise
|
|
|
|
time.sleep(1)
|
|
|
|
failcount += 1
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
if failcount > 2:
|
|
|
|
return None
|
2018-09-24 15:10:12 -04:00
|
|
|
|
2018-10-27 15:24:42 -04:00
|
|
|
return primary_node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 11:20:23 -04:00
|
|
|
#
|
2019-10-12 01:45:44 -04:00
|
|
|
# Find a migration target
|
2018-09-20 11:20:23 -04:00
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def findTargetNode(zkhandler, dom_uuid):
|
2019-10-12 01:45:44 -04:00
|
|
|
# Determine VM node limits; set config value if read fails
|
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
node_limit = zkhandler.read(("domain.meta.node_limit", dom_uuid)).split(",")
|
2019-10-12 17:58:48 -04:00
|
|
|
if not any(node_limit):
|
|
|
|
node_limit = None
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-10-12 01:45:44 -04:00
|
|
|
node_limit = None
|
2018-09-20 11:20:23 -04:00
|
|
|
|
2019-10-12 01:45:44 -04:00
|
|
|
# Determine VM search field or use default; set config value if read fails
|
2018-09-20 11:20:23 -04:00
|
|
|
try:
|
2021-11-06 03:02:43 -04:00
|
|
|
search_field = zkhandler.read(("domain.meta.node_selector", dom_uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2021-06-01 11:05:15 -04:00
|
|
|
search_field = None
|
|
|
|
|
|
|
|
# If our search field is invalid, use the default
|
2022-11-07 11:59:53 -05:00
|
|
|
if search_field is None or search_field in ["None", "none"]:
|
2021-11-06 03:02:43 -04:00
|
|
|
search_field = zkhandler.read("base.config.migration_target_selector")
|
2019-10-12 01:45:44 -04:00
|
|
|
|
|
|
|
# Execute the search
|
2021-11-06 03:02:43 -04:00
|
|
|
if search_field == "mem":
|
2022-05-18 03:47:16 -04:00
|
|
|
return findTargetNodeMemFree(zkhandler, node_limit, dom_uuid)
|
2022-11-15 15:45:59 -05:00
|
|
|
if search_field == "memprov":
|
|
|
|
return findTargetNodeMemProv(zkhandler, node_limit, dom_uuid)
|
2021-11-06 03:02:43 -04:00
|
|
|
if search_field == "load":
|
2021-05-29 20:35:28 -04:00
|
|
|
return findTargetNodeLoad(zkhandler, node_limit, dom_uuid)
|
2021-11-06 03:02:43 -04:00
|
|
|
if search_field == "vcpus":
|
2021-05-29 20:35:28 -04:00
|
|
|
return findTargetNodeVCPUs(zkhandler, node_limit, dom_uuid)
|
2021-11-06 03:02:43 -04:00
|
|
|
if search_field == "vms":
|
2021-05-29 20:35:28 -04:00
|
|
|
return findTargetNodeVMs(zkhandler, node_limit, dom_uuid)
|
2019-10-12 01:45:44 -04:00
|
|
|
|
|
|
|
# Nothing was found
|
|
|
|
return None
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2019-10-12 01:45:44 -04:00
|
|
|
# Get the list of valid target nodes
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def getNodes(zkhandler, node_limit, dom_uuid):
|
2019-10-12 01:45:44 -04:00
|
|
|
valid_node_list = []
|
2021-11-06 03:02:43 -04:00
|
|
|
full_node_list = zkhandler.children("base.node")
|
|
|
|
current_node = zkhandler.read(("domain.node", dom_uuid))
|
2018-09-20 11:20:23 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in full_node_list:
|
2019-10-12 01:45:44 -04:00
|
|
|
if node_limit and node not in node_limit:
|
|
|
|
continue
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
daemon_state = zkhandler.read(("node.state.daemon", node))
|
|
|
|
domain_state = zkhandler.read(("node.state.domain", node))
|
2018-09-20 11:20:23 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
if node == current_node:
|
2018-09-20 11:20:23 -04:00
|
|
|
continue
|
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
if daemon_state != "run" or domain_state != "ready":
|
2018-09-20 11:20:23 -04:00
|
|
|
continue
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
valid_node_list.append(node)
|
2018-09-20 11:20:23 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
return valid_node_list
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2022-11-15 15:45:59 -05:00
|
|
|
# via free memory
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2022-11-15 15:45:59 -05:00
|
|
|
def findTargetNodeMemFree(zkhandler, node_limit, dom_uuid):
|
|
|
|
most_memfree = 0
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = None
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2021-05-29 20:35:28 -04:00
|
|
|
node_list = getNodes(zkhandler, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2021-11-06 03:02:43 -04:00
|
|
|
memfree = int(zkhandler.read(("node.memory.free", node)))
|
2019-10-12 01:45:44 -04:00
|
|
|
|
2022-11-15 15:45:59 -05:00
|
|
|
if memfree > most_memfree:
|
|
|
|
most_memfree = memfree
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
return target_node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2022-05-18 03:47:16 -04:00
|
|
|
#
|
2022-11-15 15:45:59 -05:00
|
|
|
# via provisioned memory
|
2022-05-18 03:47:16 -04:00
|
|
|
#
|
2022-11-15 15:45:59 -05:00
|
|
|
def findTargetNodeMemProv(zkhandler, node_limit, dom_uuid):
|
|
|
|
most_provfree = 0
|
2022-05-18 03:47:16 -04:00
|
|
|
target_node = None
|
|
|
|
|
|
|
|
node_list = getNodes(zkhandler, node_limit, dom_uuid)
|
|
|
|
for node in node_list:
|
2022-11-15 15:45:59 -05:00
|
|
|
memprov = int(zkhandler.read(("node.memory.provisioned", node)))
|
|
|
|
memused = int(zkhandler.read(("node.memory.used", node)))
|
2022-05-18 03:47:16 -04:00
|
|
|
memfree = int(zkhandler.read(("node.memory.free", node)))
|
2022-11-15 15:45:59 -05:00
|
|
|
memtotal = memused + memfree
|
|
|
|
provfree = memtotal - memprov
|
2022-05-18 03:47:16 -04:00
|
|
|
|
2022-11-15 15:45:59 -05:00
|
|
|
if provfree > most_provfree:
|
|
|
|
most_provfree = provfree
|
2022-05-18 03:47:16 -04:00
|
|
|
target_node = node
|
|
|
|
|
|
|
|
return target_node
|
|
|
|
|
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2018-09-20 03:25:58 -04:00
|
|
|
# via load average
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def findTargetNodeLoad(zkhandler, node_limit, dom_uuid):
|
2020-01-29 17:22:29 -05:00
|
|
|
least_load = 9999.0
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = None
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2021-05-29 20:35:28 -04:00
|
|
|
node_list = getNodes(zkhandler, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2021-11-06 03:02:43 -04:00
|
|
|
load = float(zkhandler.read(("node.cpu.load", node)))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
if load < least_load:
|
|
|
|
least_load = load
|
2020-01-29 17:22:29 -05:00
|
|
|
target_node = node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
return target_node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2018-09-20 03:25:58 -04:00
|
|
|
# via total vCPUs
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def findTargetNodeVCPUs(zkhandler, node_limit, dom_uuid):
|
2019-10-12 01:45:44 -04:00
|
|
|
least_vcpus = 9999
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = None
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2021-05-29 20:35:28 -04:00
|
|
|
node_list = getNodes(zkhandler, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2021-11-06 03:02:43 -04:00
|
|
|
vcpus = int(zkhandler.read(("node.vcpu.allocated", node)))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
if vcpus < least_vcpus:
|
|
|
|
least_vcpus = vcpus
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
return target_node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2018-09-20 03:25:58 -04:00
|
|
|
# via total VMs
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
2021-05-29 20:35:28 -04:00
|
|
|
def findTargetNodeVMs(zkhandler, node_limit, dom_uuid):
|
2019-10-12 01:45:44 -04:00
|
|
|
least_vms = 9999
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = None
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2021-05-29 20:35:28 -04:00
|
|
|
node_list = getNodes(zkhandler, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2021-11-06 03:02:43 -04:00
|
|
|
vms = int(zkhandler.read(("node.count.provisioned_domains", node)))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
if vms < least_vms:
|
|
|
|
least_vms = vms
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = node
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
return target_node
|
2018-10-27 17:51:03 -04:00
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2021-06-01 12:17:25 -04:00
|
|
|
#
|
|
|
|
# Connect to the primary node and run a command
|
|
|
|
#
|
2018-10-27 17:51:03 -04:00
|
|
|
def runRemoteCommand(node, command, become=False):
|
2018-10-27 18:11:58 -04:00
|
|
|
import paramiko
|
|
|
|
import hashlib
|
|
|
|
import dns.resolver
|
|
|
|
import dns.flags
|
|
|
|
|
2018-10-27 17:51:03 -04:00
|
|
|
# Support doing SSHFP checks
|
|
|
|
class DnssecPolicy(paramiko.client.MissingHostKeyPolicy):
|
|
|
|
def missing_host_key(self, client, hostname, key):
|
|
|
|
sshfp_expect = hashlib.sha1(key.asbytes()).hexdigest()
|
2021-11-06 03:02:43 -04:00
|
|
|
ans = dns.resolver.query(hostname, "SSHFP")
|
2018-10-27 17:51:03 -04:00
|
|
|
if not ans.response.flags & dns.flags.DO:
|
2021-11-06 03:02:43 -04:00
|
|
|
raise AssertionError("Answer is not DNSSEC signed")
|
2018-10-27 17:51:03 -04:00
|
|
|
for answer in ans.response.answer:
|
|
|
|
for item in answer.items:
|
|
|
|
if sshfp_expect in item.to_text():
|
2021-11-06 03:02:43 -04:00
|
|
|
client._log(
|
|
|
|
paramiko.common.DEBUG,
|
|
|
|
"Found {} in SSHFP for host {}".format(
|
|
|
|
key.get_name(), hostname
|
|
|
|
),
|
|
|
|
)
|
2018-10-27 17:51:03 -04:00
|
|
|
return
|
2021-11-06 03:02:43 -04:00
|
|
|
raise AssertionError("SSHFP not published in DNS")
|
2018-10-27 17:51:03 -04:00
|
|
|
|
|
|
|
if become:
|
2021-11-06 03:02:43 -04:00
|
|
|
command = "sudo " + command
|
2018-10-27 17:51:03 -04:00
|
|
|
|
|
|
|
ssh_client = paramiko.client.SSHClient()
|
|
|
|
ssh_client.load_system_host_keys()
|
|
|
|
ssh_client.set_missing_host_key_policy(DnssecPolicy())
|
|
|
|
ssh_client.connect(node)
|
|
|
|
stdin, stdout, stderr = ssh_client.exec_command(command)
|
2021-11-06 03:02:43 -04:00
|
|
|
return (
|
|
|
|
stdout.read().decode("ascii").rstrip(),
|
|
|
|
stderr.read().decode("ascii").rstrip(),
|
|
|
|
)
|
2021-06-01 12:17:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Reload the firewall rules of the system
|
|
|
|
#
|
|
|
|
def reload_firewall_rules(rules_file, logger=None):
|
|
|
|
if logger is not None:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Reloading firewall configuration", state="o")
|
2021-06-01 12:17:25 -04:00
|
|
|
|
2021-11-06 03:02:43 -04:00
|
|
|
retcode, stdout, stderr = run_os_command("/usr/sbin/nft -f {}".format(rules_file))
|
2021-06-01 12:17:25 -04:00
|
|
|
if retcode != 0 and logger is not None:
|
2021-11-06 03:02:43 -04:00
|
|
|
logger.out("Failed to reload configuration: {}".format(stderr), state="e")
|
2021-06-01 12:17:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create an IP address
|
|
|
|
#
|
|
|
|
def createIPAddress(ipaddr, cidrnetmask, dev):
|
2021-11-06 03:02:43 -04:00
|
|
|
run_os_command("ip address add {}/{} dev {}".format(ipaddr, cidrnetmask, dev))
|
2021-06-01 12:17:25 -04:00
|
|
|
run_os_command(
|
2021-11-06 03:02:43 -04:00
|
|
|
"arping -P -U -W 0.02 -c 2 -i {dev} -S {ip} {ip}".format(dev=dev, ip=ipaddr)
|
2021-06-01 12:17:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Remove an IP address
|
|
|
|
#
|
|
|
|
def removeIPAddress(ipaddr, cidrnetmask, dev):
|
2021-11-06 03:02:43 -04:00
|
|
|
run_os_command("ip address delete {}/{} dev {}".format(ipaddr, cidrnetmask, dev))
|
2021-06-21 18:40:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Sort a set of interface names (e.g. ens1f1v10)
|
|
|
|
#
|
|
|
|
def sortInterfaceNames(interface_names):
|
|
|
|
# We can't handle non-list inputs
|
|
|
|
if not isinstance(interface_names, list):
|
|
|
|
return interface_names
|
|
|
|
|
|
|
|
def atoi(text):
|
|
|
|
return int(text) if text.isdigit() else text
|
|
|
|
|
|
|
|
def natural_keys(text):
|
|
|
|
"""
|
|
|
|
alist.sort(key=natural_keys) sorts in human order
|
|
|
|
http://nedbatchelder.com/blog/200712/human_sorting.html
|
|
|
|
(See Toothy's implementation in the comments)
|
|
|
|
"""
|
2021-11-06 03:02:43 -04:00
|
|
|
return [atoi(c) for c in re_split(r"(\d+)", text)]
|
2021-06-21 18:40:11 -04:00
|
|
|
|
|
|
|
return sorted(interface_names, key=natural_keys)
|
2023-11-09 14:05:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Parse a "detect" device into a real block device name
|
|
|
|
#
|
2024-08-30 10:41:56 -04:00
|
|
|
def get_detect_device_lsscsi(detect_string):
|
2023-11-09 14:05:15 -05:00
|
|
|
"""
|
|
|
|
Parses a "detect:" string into a normalized block device path using lsscsi.
|
|
|
|
|
|
|
|
A detect string is formatted "detect:<NAME>:<SIZE>:<ID>", where
|
|
|
|
NAME is some unique identifier in lsscsi, SIZE is a human-readable
|
|
|
|
size value to within +/- 3% of the real size of the device, and
|
|
|
|
ID is the Nth (0-indexed) matching entry of that NAME and SIZE.
|
|
|
|
"""
|
|
|
|
_, name, size, idd = detect_string.split(":")
|
|
|
|
if _ != "detect":
|
|
|
|
return None
|
|
|
|
|
|
|
|
retcode, stdout, stderr = run_os_command("lsscsi -s")
|
|
|
|
if retcode:
|
|
|
|
print(f"Failed to run lsscsi: {stderr}")
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Get valid lines
|
|
|
|
lsscsi_lines_raw = stdout.split("\n")
|
|
|
|
lsscsi_lines = list()
|
|
|
|
for line in lsscsi_lines_raw:
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
split_line = line.split()
|
|
|
|
if split_line[1] != "disk":
|
|
|
|
continue
|
|
|
|
lsscsi_lines.append(line)
|
|
|
|
|
|
|
|
# Handle size determination (+/- 3%)
|
|
|
|
lsscsi_sizes = set()
|
|
|
|
for line in lsscsi_lines:
|
|
|
|
lsscsi_sizes.add(split_line[-1])
|
|
|
|
for l_size in lsscsi_sizes:
|
|
|
|
b_size = float(re_sub(r"\D.", "", size))
|
|
|
|
t_size = float(re_sub(r"\D.", "", l_size))
|
|
|
|
|
|
|
|
plusthreepct = t_size * 1.03
|
|
|
|
minusthreepct = t_size * 0.97
|
|
|
|
|
|
|
|
if b_size > minusthreepct and b_size < plusthreepct:
|
|
|
|
size = l_size
|
|
|
|
break
|
|
|
|
|
|
|
|
blockdev = None
|
|
|
|
matches = list()
|
|
|
|
for idx, line in enumerate(lsscsi_lines):
|
|
|
|
# Skip non-disk entries
|
|
|
|
if line.split()[1] != "disk":
|
|
|
|
continue
|
|
|
|
# Skip if name is not contained in the line (case-insensitive)
|
|
|
|
if name.lower() not in line.lower():
|
|
|
|
continue
|
|
|
|
# Skip if the size does not match
|
|
|
|
if size != line.split()[-1]:
|
|
|
|
continue
|
|
|
|
# Get our blockdev and append to the list
|
|
|
|
matches.append(line.split()[-2])
|
|
|
|
|
|
|
|
blockdev = None
|
|
|
|
# Find the blockdev at index {idd}
|
|
|
|
for idx, _blockdev in enumerate(matches):
|
|
|
|
if int(idx) == int(idd):
|
|
|
|
blockdev = _blockdev
|
|
|
|
break
|
|
|
|
|
|
|
|
return blockdev
|
2024-08-30 10:41:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_detect_device_nvme(detect_string):
|
|
|
|
"""
|
|
|
|
Parses a "detect:" string into a normalized block device path using nvme.
|
|
|
|
|
|
|
|
A detect string is formatted "detect:<NAME>:<SIZE>:<ID>", where
|
|
|
|
NAME is some unique identifier in lsscsi, SIZE is a human-readable
|
|
|
|
size value to within +/- 3% of the real size of the device, and
|
|
|
|
ID is the Nth (0-indexed) matching entry of that NAME and SIZE.
|
|
|
|
"""
|
|
|
|
|
|
|
|
unit_map = {
|
|
|
|
"kB": 1000,
|
|
|
|
"MB": 1000 * 1000,
|
|
|
|
"GB": 1000 * 1000 * 1000,
|
|
|
|
"TB": 1000 * 1000 * 1000 * 1000,
|
|
|
|
"PB": 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
"EB": 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, name, _size, idd = detect_string.split(":")
|
|
|
|
if _ != "detect":
|
|
|
|
return None
|
|
|
|
|
2024-08-30 10:51:41 -04:00
|
|
|
size_re = re_search(r"([\d.]+)([kKMGTP]B)", _size)
|
2024-08-30 10:41:56 -04:00
|
|
|
size_val = float(size_re.group(1))
|
|
|
|
size_unit = size_re.group(2)
|
|
|
|
size_bytes = int(size_val * unit_map[size_unit])
|
|
|
|
|
|
|
|
retcode, stdout, stderr = run_os_command("nvme list --output-format json")
|
|
|
|
if retcode:
|
|
|
|
print(f"Failed to run nvme: {stderr}")
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Parse the output with json
|
|
|
|
nvme_data = loads(stdout).get("Devices", list())
|
|
|
|
|
|
|
|
# Handle size determination (+/- 3%)
|
|
|
|
size = None
|
|
|
|
nvme_sizes = set()
|
|
|
|
for entry in nvme_data:
|
|
|
|
nvme_sizes.add(entry["PhysicalSize"])
|
|
|
|
for l_size in nvme_sizes:
|
|
|
|
plusthreepct = size_bytes * 1.03
|
|
|
|
minusthreepct = size_bytes * 0.97
|
|
|
|
|
|
|
|
if l_size > minusthreepct and l_size < plusthreepct:
|
|
|
|
size = l_size
|
|
|
|
break
|
|
|
|
if size is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
blockdev = None
|
|
|
|
matches = list()
|
|
|
|
for entry in nvme_data:
|
|
|
|
# Skip if name is not contained in the line (case-insensitive)
|
|
|
|
if name.lower() not in entry["ModelNumber"].lower():
|
|
|
|
continue
|
|
|
|
# Skip if the size does not match
|
|
|
|
if size != entry["PhysicalSize"]:
|
|
|
|
continue
|
|
|
|
# Get our blockdev and append to the list
|
|
|
|
matches.append(entry["DevicePath"])
|
|
|
|
|
|
|
|
blockdev = None
|
|
|
|
# Find the blockdev at index {idd}
|
|
|
|
for idx, _blockdev in enumerate(matches):
|
|
|
|
if int(idx) == int(idd):
|
|
|
|
blockdev = _blockdev
|
|
|
|
break
|
|
|
|
|
|
|
|
return blockdev
|
|
|
|
|
|
|
|
|
|
|
|
def get_detect_device(detect_string):
|
|
|
|
"""
|
|
|
|
Parses a "detect:" string into a normalized block device path.
|
|
|
|
|
|
|
|
First tries to parse using "lsscsi" (get_detect_device_lsscsi). If this returns an invalid
|
|
|
|
block device name, then try to parse using "nvme" (get_detect_device_nvme). This works around
|
|
|
|
issues with more recent devices (e.g. the Dell R6615 series) not properly reporting block
|
|
|
|
device paths for NVMe devices with "lsscsi".
|
|
|
|
"""
|
|
|
|
|
|
|
|
device = get_detect_device_lsscsi(detect_string)
|
|
|
|
if device is None or not re_match(r"^/dev", device):
|
|
|
|
device = get_detect_device_nvme(detect_string)
|
|
|
|
|
|
|
|
if device is not None and re_match(r"^/dev", device):
|
|
|
|
return device
|
|
|
|
else:
|
|
|
|
return None
|