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
|
|
|
|
#
|
2020-01-08 19:38:02 -05:00
|
|
|
# Copyright (C) 2018-2020 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
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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 shlex
|
|
|
|
import subprocess
|
2018-09-20 03:25:58 -04:00
|
|
|
import kazoo.client
|
2020-06-07 00:40:21 -04:00
|
|
|
from json import loads
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-12-23 20:43:20 -05:00
|
|
|
from distutils.util import strtobool
|
|
|
|
|
2020-02-08 18:48:59 -05:00
|
|
|
import daemon_lib.zkhandler as zkhandler
|
2018-10-22 00:43:58 -04:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
###############################################################################
|
|
|
|
# Supplemental functions
|
|
|
|
###############################################################################
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2020-02-08 23:31:07 -05:00
|
|
|
#
|
|
|
|
# Run a local OS command via shell
|
|
|
|
#
|
2020-02-09 19:43:07 -05:00
|
|
|
def run_os_command(command_string, background=False, environment=None, timeout=None, shell=False):
|
2020-02-08 23:31:07 -05:00
|
|
|
command = shlex.split(command_string)
|
|
|
|
try:
|
|
|
|
command_output = subprocess.run(
|
|
|
|
command,
|
2020-02-09 19:43:07 -05:00
|
|
|
shell=shell,
|
2020-02-08 23:31:07 -05:00
|
|
|
env=environment,
|
|
|
|
timeout=timeout,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
)
|
|
|
|
retcode = command_output.returncode
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
retcode = 128
|
|
|
|
|
|
|
|
try:
|
|
|
|
stdout = command_output.stdout.decode('ascii')
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-02-08 23:31:07 -05:00
|
|
|
stdout = ''
|
|
|
|
try:
|
|
|
|
stderr = command_output.stderr.decode('ascii')
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-02-08 23:31:07 -05:00
|
|
|
stderr = ''
|
|
|
|
return retcode, stdout, stderr
|
|
|
|
|
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
|
|
|
#
|
|
|
|
# Connect and disconnect from Zookeeper
|
|
|
|
#
|
|
|
|
def startZKConnection(zk_host):
|
|
|
|
zk_conn = kazoo.client.KazooClient(hosts=zk_host)
|
2019-07-12 00:44:25 -04:00
|
|
|
try:
|
|
|
|
zk_conn.start()
|
|
|
|
except kazoo.handlers.threading.KazooTimeoutError:
|
|
|
|
print('Timed out connecting to Zookeeper at "{}".'.format(zk_host))
|
|
|
|
exit(1)
|
|
|
|
except Exception as e:
|
|
|
|
print('Failed to connect to Zookeeper at "{}": {}'.format(zk_host, e))
|
|
|
|
exit(1)
|
2018-09-20 03:25:58 -04:00
|
|
|
return zk_conn
|
|
|
|
|
2020-11-07 14:45:24 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
def stopZKConnection(zk_conn):
|
|
|
|
zk_conn.stop()
|
|
|
|
zk_conn.close()
|
|
|
|
return 0
|
2020-11-07 14:45:24 -05:00
|
|
|
|
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Parse a Domain XML object
|
|
|
|
#
|
|
|
|
def getDomainXML(zk_conn, dom_uuid):
|
|
|
|
try:
|
2018-10-27 15:27:08 -04:00
|
|
|
xml = zkhandler.readdata(zk_conn, '/domains/{}/xml'.format(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)
|
2020-08-21 10:10:13 -04:00
|
|
|
dmemory_unit = str(parsed_xml.memory.attrib.get('unit'))
|
2018-09-20 03:25:58 -04:00
|
|
|
if dmemory_unit == 'KiB':
|
2018-10-14 02:01:35 -04:00
|
|
|
dmemory = int(int(dmemory) / 1024)
|
2018-09-20 03:25:58 -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:
|
2020-08-21 10:10:13 -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:
|
2018-09-20 03:25:58 -04:00
|
|
|
dvcputopo = 'N/A'
|
|
|
|
|
|
|
|
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)
|
|
|
|
darch = str(parsed_xml.os.type.attrib['arch'])
|
|
|
|
dmachine = str(parsed_xml.os.type.attrib['machine'])
|
|
|
|
dconsole = str(parsed_xml.devices.console.attrib['type'])
|
|
|
|
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():
|
|
|
|
if device.tag == 'disk':
|
|
|
|
disk_attrib = device.source.attrib
|
|
|
|
disk_target = device.target.attrib
|
2020-08-21 10:10:13 -04:00
|
|
|
disk_type = device.attrib.get('type')
|
2020-06-07 00:40:21 -04:00
|
|
|
disk_stats_list = [x for x in stats_data.get('disk_stats', []) if x.get('name') == disk_attrib.get('name')]
|
|
|
|
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 = {}
|
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
if disk_type == 'network':
|
2020-06-07 00:40:21 -04:00
|
|
|
disk_obj = {
|
|
|
|
'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)
|
|
|
|
}
|
2018-09-20 03:25:58 -04:00
|
|
|
elif disk_type == 'file':
|
2020-06-07 00:40:21 -04:00
|
|
|
disk_obj = {
|
|
|
|
'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)
|
|
|
|
}
|
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
|
|
|
|
#
|
|
|
|
def getDomainDiskList(zk_conn, dom_uuid):
|
|
|
|
domain_information = getInformationFromXML(zk_conn, dom_uuid)
|
|
|
|
disk_list = []
|
|
|
|
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
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
#
|
|
|
|
# Get domain information from XML
|
|
|
|
#
|
|
|
|
def getInformationFromXML(zk_conn, uuid):
|
|
|
|
"""
|
|
|
|
Gather information about a VM from the Libvirt XML configuration in the Zookeper database
|
|
|
|
and return a dict() containing it.
|
|
|
|
"""
|
|
|
|
domain_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(uuid))
|
|
|
|
domain_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(uuid))
|
|
|
|
domain_lastnode = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(uuid))
|
|
|
|
domain_failedreason = zkhandler.readdata(zk_conn, '/domains/{}/failedreason'.format(uuid))
|
|
|
|
|
2019-10-12 01:36:50 -04:00
|
|
|
try:
|
2019-12-19 13:29:15 -05:00
|
|
|
domain_node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-10-12 01:36:50 -04:00
|
|
|
domain_node_limit = None
|
2019-10-17 12:11:46 -04:00
|
|
|
try:
|
2019-10-17 10:31:19 -04:00
|
|
|
domain_node_selector = zkhandler.readdata(zk_conn, '/domains/{}/node_selector'.format(uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-10-12 01:36:50 -04:00
|
|
|
domain_node_selector = None
|
2019-10-17 10:31:19 -04:00
|
|
|
try:
|
|
|
|
domain_node_autostart = zkhandler.readdata(zk_conn, '/domains/{}/node_autostart'.format(uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-10-12 01:36:50 -04:00
|
|
|
domain_node_autostart = None
|
2020-10-29 11:31:32 -04:00
|
|
|
try:
|
|
|
|
domain_migration_method = zkhandler.readdata(zk_conn, '/domains/{}/migration_method'.format(uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-10-29 11:31:32 -04:00
|
|
|
domain_migration_method = None
|
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:
|
|
|
|
domain_node_limit = domain_node_limit.split(',')
|
|
|
|
|
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
|
|
|
|
2019-12-11 16:50:38 -05:00
|
|
|
try:
|
|
|
|
domain_profile = zkhandler.readdata(zk_conn, '/domains/{}/profile'.format(uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-12-11 16:50:38 -05:00
|
|
|
domain_profile = None
|
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
parsed_xml = getDomainXML(zk_conn, uuid)
|
|
|
|
|
2020-06-07 00:40:21 -04:00
|
|
|
try:
|
|
|
|
stats_data = loads(zkhandler.readdata(zk_conn, '/domains/{}/stats'.format(uuid)))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-06-07 00:40:21 -04:00
|
|
|
stats_data = {}
|
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
domain_uuid, domain_name, domain_description, domain_memory, domain_vcpu, domain_vcputopo = getDomainMainDetails(parsed_xml)
|
2020-06-07 00:40:21 -04:00
|
|
|
domain_networks = getDomainNetworks(parsed_xml, stats_data)
|
2019-07-09 09:29:47 -04:00
|
|
|
|
|
|
|
domain_type, domain_arch, domain_machine, domain_console, domain_emulator = getDomainExtraDetails(parsed_xml)
|
|
|
|
|
|
|
|
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:
|
|
|
|
domain_migrated = 'from {}'.format(domain_lastnode)
|
|
|
|
else:
|
|
|
|
domain_migrated = 'no'
|
|
|
|
|
|
|
|
domain_information = {
|
|
|
|
'name': domain_name,
|
|
|
|
'uuid': domain_uuid,
|
|
|
|
'state': domain_state,
|
|
|
|
'node': domain_node,
|
|
|
|
'last_node': domain_lastnode,
|
|
|
|
'migrated': domain_migrated,
|
|
|
|
'failed_reason': domain_failedreason,
|
2019-10-12 01:17:39 -04:00
|
|
|
'node_limit': domain_node_limit,
|
|
|
|
'node_selector': domain_node_selector,
|
2019-12-23 20:43:20 -05:00
|
|
|
'node_autostart': bool(strtobool(domain_node_autostart)),
|
2020-10-29 11:31:32 -04:00
|
|
|
'migration_method': domain_migration_method,
|
2019-07-09 09:29:47 -04:00
|
|
|
'description': domain_description,
|
2019-12-11 16:50:38 -05:00
|
|
|
'profile': domain_profile,
|
2019-12-23 20:43:20 -05:00
|
|
|
'memory': int(domain_memory),
|
2020-06-07 00:40:21 -04:00
|
|
|
'memory_stats': stats_data.get('mem_stats', {}),
|
2019-12-23 20:43:20 -05:00
|
|
|
'vcpu': int(domain_vcpu),
|
2019-07-09 09:29:47 -04:00
|
|
|
'vcpu_topology': domain_vcputopo,
|
2020-06-07 00:40:21 -04:00
|
|
|
'vcpu_stats': stats_data.get('cpu_stats', {}),
|
2019-07-09 09:29:47 -04:00
|
|
|
'networks': domain_networks,
|
|
|
|
'type': domain_type,
|
|
|
|
'arch': domain_arch,
|
|
|
|
'machine': domain_machine,
|
|
|
|
'console': domain_console,
|
|
|
|
'emulator': domain_emulator,
|
|
|
|
'features': domain_features,
|
|
|
|
'disks': domain_disks,
|
2019-07-25 15:44:29 -04:00
|
|
|
'controllers': domain_controllers,
|
2019-07-25 16:29:29 -04:00
|
|
|
'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():
|
|
|
|
if device.tag == 'interface':
|
2020-06-07 00:40:21 -04:00
|
|
|
try:
|
2020-08-21 10:27:45 -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
|
|
|
|
try:
|
|
|
|
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
|
|
|
|
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
|
|
|
|
try:
|
|
|
|
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
|
|
|
|
try:
|
|
|
|
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 = {}
|
|
|
|
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)
|
|
|
|
net_obj = {
|
|
|
|
'type': net_type,
|
|
|
|
'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
|
|
|
|
}
|
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():
|
|
|
|
if device.tag == 'controller':
|
2020-08-21 10:10:13 -04:00
|
|
|
controller_type = device.attrib.get('type')
|
2018-09-20 03:25:58 -04:00
|
|
|
try:
|
2020-08-21 10:10:13 -04:00
|
|
|
controller_model = device.attrib.get('model')
|
2018-09-20 03:25:58 -04:00
|
|
|
except KeyError:
|
|
|
|
controller_model = 'none'
|
2020-11-07 12:57:42 -05:00
|
|
|
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
|
|
|
|
#
|
|
|
|
def verifyNode(zk_conn, node):
|
2018-10-27 16:19:07 -04:00
|
|
|
if zkhandler.exists(zk_conn, '/nodes/{}'.format(node)):
|
2018-09-23 01:05:54 -04:00
|
|
|
return True
|
2018-10-27 16:19:07 -04:00
|
|
|
else:
|
2018-09-23 01:05:54 -04:00
|
|
|
return False
|
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
|
|
|
#
|
2018-10-27 15:24:42 -04:00
|
|
|
def getPrimaryNode(zk_conn):
|
|
|
|
failcount = 0
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
primary_node = zkhandler.readdata(zk_conn, '/primary_node')
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2018-10-27 15:24:42 -04:00
|
|
|
primary_node == 'none'
|
|
|
|
|
|
|
|
if primary_node == 'none':
|
|
|
|
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
|
|
|
#
|
2019-10-12 02:03:23 -04:00
|
|
|
def findTargetNode(zk_conn, dom_uuid):
|
2019-10-12 01:45:44 -04:00
|
|
|
# Determine VM node limits; set config value if read fails
|
|
|
|
try:
|
2019-10-12 17:58:48 -04:00
|
|
|
node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(dom_uuid)).split(',')
|
|
|
|
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:
|
2019-10-12 17:58:48 -04:00
|
|
|
search_field = zkhandler.readdata(zk_conn, '/domains/{}/node_selector'.format(dom_uuid))
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-10-12 02:03:23 -04:00
|
|
|
search_field = 'mem'
|
2019-10-12 01:45:44 -04:00
|
|
|
|
|
|
|
# Execute the search
|
|
|
|
if search_field == 'mem':
|
2019-10-12 01:59:08 -04:00
|
|
|
return findTargetNodeMem(zk_conn, node_limit, dom_uuid)
|
2019-10-12 01:45:44 -04:00
|
|
|
if search_field == 'load':
|
2019-10-12 01:59:08 -04:00
|
|
|
return findTargetNodeLoad(zk_conn, node_limit, dom_uuid)
|
2019-10-12 01:45:44 -04:00
|
|
|
if search_field == 'vcpus':
|
2019-10-12 01:59:08 -04:00
|
|
|
return findTargetNodeVCPUs(zk_conn, node_limit, dom_uuid)
|
2019-10-12 01:45:44 -04:00
|
|
|
if search_field == 'vms':
|
2019-10-12 01:59:08 -04:00
|
|
|
return findTargetNodeVMs(zk_conn, 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
|
|
|
|
2019-10-12 01:45:44 -04:00
|
|
|
# Get the list of valid target nodes
|
2019-10-12 01:59:08 -04:00
|
|
|
def getNodes(zk_conn, node_limit, dom_uuid):
|
2019-10-12 01:45:44 -04:00
|
|
|
valid_node_list = []
|
|
|
|
full_node_list = zkhandler.listchildren(zk_conn, '/nodes')
|
2019-12-19 11:30:13 -05:00
|
|
|
try:
|
|
|
|
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
|
|
|
|
except kazoo.exceptions.NoNodeError:
|
|
|
|
current_node = None
|
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
|
|
|
|
|
2018-10-27 15:27:08 -04:00
|
|
|
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
|
|
|
|
domain_state = zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(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
|
|
|
|
|
|
|
|
if daemon_state != 'run' or domain_state != 'ready':
|
|
|
|
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
|
|
|
|
2019-10-12 01:45:44 -04:00
|
|
|
# via free memory (relative to allocated memory)
|
2019-10-12 01:59:08 -04:00
|
|
|
def findTargetNodeMem(zk_conn, node_limit, dom_uuid):
|
2020-10-18 14:12:24 -04:00
|
|
|
most_provfree = 0
|
2018-10-14 02:01:35 -04:00
|
|
|
target_node = None
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-10-12 01:59:08 -04:00
|
|
|
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2020-10-18 14:12:24 -04:00
|
|
|
memprov = int(zkhandler.readdata(zk_conn, '/nodes/{}/memprov'.format(node)))
|
2019-10-12 01:45:44 -04:00
|
|
|
memused = int(zkhandler.readdata(zk_conn, '/nodes/{}/memused'.format(node)))
|
|
|
|
memfree = int(zkhandler.readdata(zk_conn, '/nodes/{}/memfree'.format(node)))
|
|
|
|
memtotal = memused + memfree
|
2020-10-18 14:12:24 -04:00
|
|
|
provfree = memtotal - memprov
|
2019-10-12 01:45:44 -04:00
|
|
|
|
2020-10-18 14:12:24 -04:00
|
|
|
if provfree > most_provfree:
|
|
|
|
most_provfree = provfree
|
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
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# via load average
|
2019-10-12 01:59:08 -04:00
|
|
|
def findTargetNodeLoad(zk_conn, 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
|
|
|
|
2019-10-12 01:59:08 -04:00
|
|
|
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2020-01-29 17:22:29 -05:00
|
|
|
load = float(zkhandler.readdata(zk_conn, '/nodes/{}/cpuload'.format(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
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# via total vCPUs
|
2019-10-12 01:59:08 -04:00
|
|
|
def findTargetNodeVCPUs(zk_conn, 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
|
|
|
|
2019-10-12 01:59:08 -04:00
|
|
|
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2019-10-12 01:45:44 -04:00
|
|
|
vcpus = int(zkhandler.readdata(zk_conn, '/nodes/{}/vcpualloc'.format(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
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# via total VMs
|
2019-10-12 01:59:08 -04:00
|
|
|
def findTargetNodeVMs(zk_conn, 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
|
|
|
|
2019-10-12 01:59:08 -04:00
|
|
|
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
2018-10-14 02:01:35 -04:00
|
|
|
for node in node_list:
|
2019-10-12 01:45:44 -04:00
|
|
|
vms = int(zkhandler.readdata(zk_conn, '/nodes/{}/domainscount'.format(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
|
|
|
|
2018-10-27 17:51:03 -04:00
|
|
|
# Connect to the primary host and run a command
|
|
|
|
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()
|
|
|
|
ans = dns.resolver.query(hostname, 'SSHFP')
|
|
|
|
if not ans.response.flags & dns.flags.DO:
|
|
|
|
raise AssertionError('Answer is not DNSSEC signed')
|
|
|
|
for answer in ans.response.answer:
|
|
|
|
for item in answer.items:
|
|
|
|
if sshfp_expect in item.to_text():
|
|
|
|
client._log(paramiko.common.DEBUG, 'Found {} in SSHFP for host {}'.format(key.get_name(), hostname))
|
|
|
|
return
|
|
|
|
raise AssertionError('SSHFP not published in DNS')
|
|
|
|
|
|
|
|
if become:
|
|
|
|
command = 'sudo ' + command
|
|
|
|
|
|
|
|
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)
|
|
|
|
return stdout.read().decode('ascii').rstrip(), stderr.read().decode('ascii').rstrip()
|