82
daemon-common/ansiprint.py
Normal file
82
daemon-common/ansiprint.py
Normal file
@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# ansiprint.py - Printing function for formatted messages
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import datetime
|
||||
|
||||
# ANSII colours for output
|
||||
def red():
|
||||
return '\033[91m'
|
||||
def blue():
|
||||
return '\033[94m'
|
||||
def cyan():
|
||||
return '\033[96m'
|
||||
def green():
|
||||
return '\033[92m'
|
||||
def yellow():
|
||||
return '\033[93m'
|
||||
def purple():
|
||||
return '\033[95m'
|
||||
def bold():
|
||||
return '\033[1m'
|
||||
def end():
|
||||
return '\033[0m'
|
||||
|
||||
# Print function
|
||||
def echo(message, prefix, state):
|
||||
# Get the date
|
||||
date = '{} - '.format(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S.%f'))
|
||||
endc = end()
|
||||
|
||||
# Continuation
|
||||
if state == 'c':
|
||||
date = ''
|
||||
colour = ''
|
||||
prompt = ' '
|
||||
# OK
|
||||
elif state == 'o':
|
||||
colour = green()
|
||||
prompt = '>>> '
|
||||
# Error
|
||||
elif state == 'e':
|
||||
colour = red()
|
||||
prompt = '>>> '
|
||||
# Warning
|
||||
elif state == 'w':
|
||||
colour = yellow()
|
||||
prompt = '>>> '
|
||||
# Tick
|
||||
elif state == 't':
|
||||
colour = purple()
|
||||
prompt = '>>> '
|
||||
# Information
|
||||
elif state == 'i':
|
||||
colour = blue()
|
||||
prompt = '>>> '
|
||||
else:
|
||||
colour = bold()
|
||||
prompt = '>>> '
|
||||
|
||||
# Append space to prefix
|
||||
if prefix != '':
|
||||
prefix = prefix + ' '
|
||||
|
||||
print(colour + prompt + endc + date + prefix + message)
|
1461
daemon-common/ceph.py
Normal file
1461
daemon-common/ceph.py
Normal file
File diff suppressed because it is too large
Load Diff
195
daemon-common/cluster.py
Normal file
195
daemon-common/cluster.py
Normal file
@ -0,0 +1,195 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# cluster.py - PVC client function library, cluster management
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import json
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
import daemon_lib.ansiprint as ansiprint
|
||||
import daemon_lib.zkhandler as zkhandler
|
||||
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
|
||||
|
||||
def set_maintenance(zk_conn, maint_state):
|
||||
try:
|
||||
if maint_state == 'true':
|
||||
zkhandler.writedata(zk_conn, {'/maintenance': 'true'})
|
||||
return True, 'Successfully set cluster in maintenance mode'
|
||||
else:
|
||||
zkhandler.writedata(zk_conn, {'/maintenance': 'false'})
|
||||
return True, 'Successfully set cluster in normal mode'
|
||||
except:
|
||||
return False, 'Failed to set cluster maintenance state'
|
||||
|
||||
def getClusterInformation(zk_conn):
|
||||
# Get cluster maintenance state
|
||||
try:
|
||||
maint_state = zkhandler.readdata(zk_conn, '/maintenance')
|
||||
except:
|
||||
maint_state = 'false'
|
||||
|
||||
# Get node information object list
|
||||
retcode, node_list = pvc_node.get_list(zk_conn, None)
|
||||
|
||||
# Get vm information object list
|
||||
retcode, vm_list = pvc_vm.get_list(zk_conn, None, None, None)
|
||||
|
||||
# Get network information object list
|
||||
retcode, network_list = pvc_network.get_list(zk_conn, None, None)
|
||||
|
||||
# Get storage information object list
|
||||
retcode, ceph_osd_list = pvc_ceph.get_list_osd(zk_conn, None)
|
||||
retcode, ceph_pool_list = pvc_ceph.get_list_pool(zk_conn, None)
|
||||
retcode, ceph_volume_list = pvc_ceph.get_list_volume(zk_conn, None, None)
|
||||
retcode, ceph_snapshot_list = pvc_ceph.get_list_snapshot(zk_conn, None, None, None)
|
||||
|
||||
# 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)
|
||||
|
||||
# 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):
|
||||
daemon_state = node['daemon_state']
|
||||
domain_state = node['domain_state']
|
||||
if daemon_state != 'run' and domain_state != 'ready':
|
||||
node_healthy_status[index] = False
|
||||
else:
|
||||
node_healthy_status[index] = True
|
||||
node_report_status[index] = daemon_state + ',' + domain_state
|
||||
|
||||
# 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):
|
||||
vm_state = vm['state']
|
||||
if vm_state not in ['start', 'disable', 'migrate', 'unmigrate', 'provision']:
|
||||
vm_healthy_status[index] = False
|
||||
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):
|
||||
try:
|
||||
ceph_osd_up = ceph_osd['stats']['up']
|
||||
except KeyError:
|
||||
ceph_osd_up = 0
|
||||
|
||||
try:
|
||||
ceph_osd_in = ceph_osd['stats']['in']
|
||||
except KeyError:
|
||||
ceph_osd_in = 0
|
||||
|
||||
if not ceph_osd_up or not ceph_osd_in:
|
||||
ceph_osd_healthy_status[index] = False
|
||||
else:
|
||||
ceph_osd_healthy_status[index] = True
|
||||
up_texts = { 1: 'up', 0: 'down' }
|
||||
in_texts = { 1: 'in', 0: 'out' }
|
||||
ceph_osd_report_status[index] = up_texts[ceph_osd_up] + ',' + in_texts[ceph_osd_in]
|
||||
|
||||
# Find out the overall cluster health; if any element of a healthy_status is false, it's unhealthy
|
||||
if maint_state == 'true':
|
||||
cluster_health = 'Maintenance'
|
||||
elif False in node_healthy_status or False in vm_healthy_status or False in ceph_osd_healthy_status:
|
||||
cluster_health = 'Degraded'
|
||||
else:
|
||||
cluster_health = 'Optimal'
|
||||
|
||||
# State lists
|
||||
node_state_combinations = [
|
||||
'run,ready', 'run,flush', 'run,flushed', 'run,unflush',
|
||||
'init,ready', 'init,flush', 'init,flushed', 'init,unflush',
|
||||
'stop,ready', 'stop,flush', 'stop,flushed', 'stop,unflush'
|
||||
]
|
||||
vm_state_combinations = [
|
||||
'start', 'restart', 'shutdown', 'stop', 'disable', 'fail', 'migrate', 'unmigrate', 'provision'
|
||||
]
|
||||
ceph_osd_state_combinations = [
|
||||
'up,in', 'up,out', 'down,in', 'down,out'
|
||||
]
|
||||
|
||||
# Format the Node states
|
||||
formatted_node_states = {'total': node_count}
|
||||
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
|
||||
formatted_vm_states = {'total': vm_count}
|
||||
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
|
||||
formatted_osd_states = {'total': ceph_osd_count}
|
||||
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 = {
|
||||
'health': cluster_health,
|
||||
'primary_node': common.getPrimaryNode(zk_conn),
|
||||
'upstream_ip': zkhandler.readdata(zk_conn, '/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
|
||||
}
|
||||
|
||||
return cluster_information
|
||||
|
||||
def get_info(zk_conn):
|
||||
# This is a thin wrapper function for naming purposes
|
||||
cluster_information = getClusterInformation(zk_conn)
|
||||
if cluster_information:
|
||||
return True, cluster_information
|
||||
else:
|
||||
return False, 'ERROR: Failed to obtain cluster information!'
|
458
daemon-common/common.py
Normal file
458
daemon-common/common.py
Normal file
@ -0,0 +1,458 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# common.py - PVC client function library, common fuctions
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import uuid
|
||||
import lxml
|
||||
import math
|
||||
import kazoo.client
|
||||
|
||||
from distutils.util import strtobool
|
||||
|
||||
import daemon_lib.zkhandler as zkhandler
|
||||
|
||||
###############################################################################
|
||||
# Supplemental functions
|
||||
###############################################################################
|
||||
|
||||
#
|
||||
# Validate a UUID
|
||||
#
|
||||
def validateUUID(dom_uuid):
|
||||
try:
|
||||
uuid.UUID(dom_uuid)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
#
|
||||
# Connect and disconnect from Zookeeper
|
||||
#
|
||||
def startZKConnection(zk_host):
|
||||
zk_conn = kazoo.client.KazooClient(hosts=zk_host)
|
||||
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)
|
||||
return zk_conn
|
||||
|
||||
def stopZKConnection(zk_conn):
|
||||
zk_conn.stop()
|
||||
zk_conn.close()
|
||||
return 0
|
||||
#
|
||||
# Parse a Domain XML object
|
||||
#
|
||||
def getDomainXML(zk_conn, dom_uuid):
|
||||
try:
|
||||
xml = zkhandler.readdata(zk_conn, '/domains/{}/xml'.format(dom_uuid))
|
||||
except:
|
||||
return None
|
||||
|
||||
# Parse XML using lxml.objectify
|
||||
parsed_xml = lxml.objectify.fromstring(xml)
|
||||
return parsed_xml
|
||||
|
||||
#
|
||||
# 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)
|
||||
dmemory_unit = str(parsed_xml.memory.attrib['unit'])
|
||||
if dmemory_unit == 'KiB':
|
||||
dmemory = int(int(dmemory) / 1024)
|
||||
elif dmemory_unit == 'GiB':
|
||||
dmemory = int(int(dmemory) * 1024)
|
||||
dvcpu = str(parsed_xml.vcpu)
|
||||
try:
|
||||
dvcputopo = '{}/{}/{}'.format(parsed_xml.cpu.topology.attrib['sockets'], parsed_xml.cpu.topology.attrib['cores'], parsed_xml.cpu.topology.attrib['threads'])
|
||||
except:
|
||||
dvcputopo = 'N/A'
|
||||
|
||||
return duuid, dname, ddescription, dmemory, dvcpu, dvcputopo
|
||||
|
||||
#
|
||||
# Get long-format details
|
||||
#
|
||||
def getDomainExtraDetails(parsed_xml):
|
||||
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)
|
||||
|
||||
return dtype, darch, dmachine, dconsole, demulator
|
||||
|
||||
#
|
||||
# Get CPU features
|
||||
#
|
||||
def getDomainCPUFeatures(parsed_xml):
|
||||
dfeatures = []
|
||||
for feature in parsed_xml.features.getchildren():
|
||||
dfeatures.append(feature.tag)
|
||||
|
||||
return dfeatures
|
||||
|
||||
#
|
||||
# Get disk devices
|
||||
#
|
||||
def getDomainDisks(parsed_xml):
|
||||
ddisks = []
|
||||
for device in parsed_xml.devices.getchildren():
|
||||
if device.tag == 'disk':
|
||||
disk_attrib = device.source.attrib
|
||||
disk_target = device.target.attrib
|
||||
disk_type = device.attrib['type']
|
||||
if disk_type == 'network':
|
||||
disk_obj = { 'type': disk_attrib.get('protocol'), 'name': disk_attrib.get('name'), 'dev': disk_target.get('dev'), 'bus': disk_target.get('bus') }
|
||||
elif disk_type == 'file':
|
||||
disk_obj = { 'type': 'file', 'name': disk_attrib.get('file'), 'dev': disk_target.get('dev'), 'bus': disk_target.get('bus') }
|
||||
else:
|
||||
disk_obj = {}
|
||||
ddisks.append(disk_obj)
|
||||
|
||||
return ddisks
|
||||
|
||||
#
|
||||
# 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'])
|
||||
|
||||
return disk_list
|
||||
|
||||
#
|
||||
# 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))
|
||||
|
||||
try:
|
||||
domain_node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(uuid))
|
||||
except:
|
||||
domain_node_limit = None
|
||||
try:
|
||||
domain_node_selector = zkhandler.readdata(zk_conn, '/domains/{}/node_selector'.format(uuid))
|
||||
except:
|
||||
domain_node_selector = None
|
||||
try:
|
||||
domain_node_autostart = zkhandler.readdata(zk_conn, '/domains/{}/node_autostart'.format(uuid))
|
||||
except:
|
||||
domain_node_autostart = None
|
||||
|
||||
if not domain_node_limit:
|
||||
domain_node_limit = None
|
||||
else:
|
||||
domain_node_limit = domain_node_limit.split(',')
|
||||
|
||||
if not domain_node_autostart:
|
||||
domain_node_autostart = None
|
||||
|
||||
try:
|
||||
domain_profile = zkhandler.readdata(zk_conn, '/domains/{}/profile'.format(uuid))
|
||||
except:
|
||||
domain_profile = None
|
||||
|
||||
parsed_xml = getDomainXML(zk_conn, uuid)
|
||||
|
||||
domain_uuid, domain_name, domain_description, domain_memory, domain_vcpu, domain_vcputopo = getDomainMainDetails(parsed_xml)
|
||||
domain_networks = getDomainNetworks(parsed_xml)
|
||||
|
||||
domain_type, domain_arch, domain_machine, domain_console, domain_emulator = getDomainExtraDetails(parsed_xml)
|
||||
|
||||
domain_features = getDomainCPUFeatures(parsed_xml)
|
||||
domain_disks = getDomainDisks(parsed_xml)
|
||||
domain_controllers = getDomainControllers(parsed_xml)
|
||||
|
||||
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,
|
||||
'node_limit': domain_node_limit,
|
||||
'node_selector': domain_node_selector,
|
||||
'node_autostart': bool(strtobool(domain_node_autostart)),
|
||||
'description': domain_description,
|
||||
'profile': domain_profile,
|
||||
'memory': int(domain_memory),
|
||||
'vcpu': int(domain_vcpu),
|
||||
'vcpu_topology': domain_vcputopo,
|
||||
'networks': domain_networks,
|
||||
'type': domain_type,
|
||||
'arch': domain_arch,
|
||||
'machine': domain_machine,
|
||||
'console': domain_console,
|
||||
'emulator': domain_emulator,
|
||||
'features': domain_features,
|
||||
'disks': domain_disks,
|
||||
'controllers': domain_controllers,
|
||||
'xml': lxml.etree.tostring(parsed_xml, encoding='ascii', method='xml').decode().replace('\"', '\'')
|
||||
}
|
||||
|
||||
return domain_information
|
||||
|
||||
#
|
||||
# Get network devices
|
||||
#
|
||||
def getDomainNetworks(parsed_xml):
|
||||
dnets = []
|
||||
for device in parsed_xml.devices.getchildren():
|
||||
if device.tag == 'interface':
|
||||
net_type = device.attrib['type']
|
||||
net_mac = device.mac.attrib['address']
|
||||
net_bridge = device.source.attrib[net_type]
|
||||
net_model = device.model.attrib['type']
|
||||
net_obj = { 'type': net_type, 'mac': net_mac, 'source': net_bridge, 'model': net_model }
|
||||
dnets.append(net_obj)
|
||||
|
||||
return dnets
|
||||
|
||||
#
|
||||
# Get controller devices
|
||||
#
|
||||
def getDomainControllers(parsed_xml):
|
||||
dcontrollers = []
|
||||
for device in parsed_xml.devices.getchildren():
|
||||
if device.tag == 'controller':
|
||||
controller_type = device.attrib['type']
|
||||
try:
|
||||
controller_model = device.attrib['model']
|
||||
except KeyError:
|
||||
controller_model = 'none'
|
||||
controller_obj = { 'type': controller_type, 'model': controller_model }
|
||||
dcontrollers.append(controller_obj)
|
||||
|
||||
return dcontrollers
|
||||
|
||||
#
|
||||
# Verify node is valid in cluster
|
||||
#
|
||||
def verifyNode(zk_conn, node):
|
||||
if zkhandler.exists(zk_conn, '/nodes/{}'.format(node)):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
#
|
||||
# Get the primary coordinator node
|
||||
#
|
||||
def getPrimaryNode(zk_conn):
|
||||
failcount = 0
|
||||
while True:
|
||||
try:
|
||||
primary_node = zkhandler.readdata(zk_conn, '/primary_node')
|
||||
except:
|
||||
primary_node == 'none'
|
||||
|
||||
if primary_node == 'none':
|
||||
raise
|
||||
time.sleep(1)
|
||||
failcount += 1
|
||||
continue
|
||||
else:
|
||||
break
|
||||
|
||||
if failcount > 2:
|
||||
return None
|
||||
|
||||
return primary_node
|
||||
|
||||
#
|
||||
# Find a migration target
|
||||
#
|
||||
def findTargetNode(zk_conn, dom_uuid):
|
||||
# Determine VM node limits; set config value if read fails
|
||||
try:
|
||||
node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(dom_uuid)).split(',')
|
||||
if not any(node_limit):
|
||||
node_limit = None
|
||||
except:
|
||||
node_limit = None
|
||||
|
||||
# Determine VM search field or use default; set config value if read fails
|
||||
try:
|
||||
search_field = zkhandler.readdata(zk_conn, '/domains/{}/node_selector'.format(dom_uuid))
|
||||
except:
|
||||
search_field = 'mem'
|
||||
|
||||
# Execute the search
|
||||
if search_field == 'mem':
|
||||
return findTargetNodeMem(zk_conn, node_limit, dom_uuid)
|
||||
if search_field == 'load':
|
||||
return findTargetNodeLoad(zk_conn, node_limit, dom_uuid)
|
||||
if search_field == 'vcpus':
|
||||
return findTargetNodeVCPUs(zk_conn, node_limit, dom_uuid)
|
||||
if search_field == 'vms':
|
||||
return findTargetNodeVMs(zk_conn, node_limit, dom_uuid)
|
||||
|
||||
# Nothing was found
|
||||
return None
|
||||
|
||||
# Get the list of valid target nodes
|
||||
def getNodes(zk_conn, node_limit, dom_uuid):
|
||||
valid_node_list = []
|
||||
full_node_list = zkhandler.listchildren(zk_conn, '/nodes')
|
||||
try:
|
||||
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
|
||||
except kazoo.exceptions.NoNodeError:
|
||||
current_node = None
|
||||
|
||||
for node in full_node_list:
|
||||
if node_limit and node not in node_limit:
|
||||
continue
|
||||
|
||||
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
|
||||
domain_state = zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node))
|
||||
|
||||
if node == current_node:
|
||||
continue
|
||||
|
||||
if daemon_state != 'run' or domain_state != 'ready':
|
||||
continue
|
||||
|
||||
valid_node_list.append(node)
|
||||
|
||||
return valid_node_list
|
||||
|
||||
# via free memory (relative to allocated memory)
|
||||
def findTargetNodeMem(zk_conn, node_limit, dom_uuid):
|
||||
most_allocfree = 0
|
||||
target_node = None
|
||||
|
||||
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
||||
for node in node_list:
|
||||
memalloc = int(zkhandler.readdata(zk_conn, '/nodes/{}/memalloc'.format(node)))
|
||||
memused = int(zkhandler.readdata(zk_conn, '/nodes/{}/memused'.format(node)))
|
||||
memfree = int(zkhandler.readdata(zk_conn, '/nodes/{}/memfree'.format(node)))
|
||||
memtotal = memused + memfree
|
||||
allocfree = memtotal - memalloc
|
||||
|
||||
if allocfree > most_allocfree:
|
||||
most_allocfree = allocfree
|
||||
target_node = node
|
||||
|
||||
return target_node
|
||||
|
||||
# via load average
|
||||
def findTargetNodeLoad(zk_conn, node_limit, dom_uuid):
|
||||
least_load = 9999.0
|
||||
target_node = None
|
||||
|
||||
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
||||
for node in node_list:
|
||||
load = float(zkhandler.readdata(zk_conn, '/nodes/{}/cpuload'.format(node)))
|
||||
|
||||
if load < least_load:
|
||||
least_load = load
|
||||
target_node = node
|
||||
|
||||
return target_node
|
||||
|
||||
# via total vCPUs
|
||||
def findTargetNodeVCPUs(zk_conn, node_limit, dom_uuid):
|
||||
least_vcpus = 9999
|
||||
target_node = None
|
||||
|
||||
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
||||
for node in node_list:
|
||||
vcpus = int(zkhandler.readdata(zk_conn, '/nodes/{}/vcpualloc'.format(node)))
|
||||
|
||||
if vcpus < least_vcpus:
|
||||
least_vcpus = vcpus
|
||||
target_node = node
|
||||
|
||||
return target_node
|
||||
|
||||
# via total VMs
|
||||
def findTargetNodeVMs(zk_conn, node_limit, dom_uuid):
|
||||
least_vms = 9999
|
||||
target_node = None
|
||||
|
||||
node_list = getNodes(zk_conn, node_limit, dom_uuid)
|
||||
for node in node_list:
|
||||
vms = int(zkhandler.readdata(zk_conn, '/nodes/{}/domainscount'.format(node)))
|
||||
|
||||
if vms < least_vms:
|
||||
least_vms = vms
|
||||
target_node = node
|
||||
|
||||
return target_node
|
||||
|
||||
# Connect to the primary host and run a command
|
||||
def runRemoteCommand(node, command, become=False):
|
||||
import paramiko
|
||||
import hashlib
|
||||
import dns.resolver
|
||||
import dns.flags
|
||||
|
||||
# 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.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh_client.connect(node)
|
||||
stdin, stdout, stderr = ssh_client.exec_command(command)
|
||||
return stdout.read().decode('ascii').rstrip(), stderr.read().decode('ascii').rstrip()
|
948
daemon-common/network.py
Normal file
948
daemon-common/network.py
Normal file
@ -0,0 +1,948 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# network.py - PVC client function library, Network fuctions
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
import uuid
|
||||
import re
|
||||
import tempfile
|
||||
import subprocess
|
||||
import difflib
|
||||
import colorama
|
||||
import click
|
||||
import lxml.objectify
|
||||
import configparser
|
||||
import kazoo.client
|
||||
|
||||
import daemon_lib.ansiprint as ansiprint
|
||||
import daemon_lib.zkhandler as zkhandler
|
||||
import daemon_lib.common as common
|
||||
|
||||
#
|
||||
# Cluster search functions
|
||||
#
|
||||
def getClusterNetworkList(zk_conn):
|
||||
# Get a list of VNIs by listing the children of /networks
|
||||
vni_list = zkhandler.listchildren(zk_conn, '/networks')
|
||||
description_list = []
|
||||
# For each VNI, get the corresponding description from the data
|
||||
for vni in vni_list:
|
||||
description_list.append(zkhandler.readdata(zk_conn, '/networks/{}'.format(vni)))
|
||||
return vni_list, description_list
|
||||
|
||||
def searchClusterByVNI(zk_conn, vni):
|
||||
try:
|
||||
# Get the lists
|
||||
vni_list, description_list = getClusterNetworkList(zk_conn)
|
||||
# We're looking for UUID, so find that element ID
|
||||
index = vni_list.index(vni)
|
||||
# Get the name_list element at that index
|
||||
description = description_list[index]
|
||||
except ValueError:
|
||||
# We didn't find anything
|
||||
return None
|
||||
|
||||
return description
|
||||
|
||||
def searchClusterByDescription(zk_conn, description):
|
||||
try:
|
||||
# Get the lists
|
||||
vni_list, description_list = getClusterNetworkList(zk_conn)
|
||||
# We're looking for name, so find that element ID
|
||||
index = description_list.index(description)
|
||||
# Get the uuid_list element at that index
|
||||
vni = vni_list[index]
|
||||
except ValueError:
|
||||
# We didn't find anything
|
||||
return None
|
||||
|
||||
return vni
|
||||
|
||||
def getNetworkVNI(zk_conn, network):
|
||||
# Validate and obtain alternate passed value
|
||||
if network.isdigit():
|
||||
net_description = searchClusterByVNI(zk_conn, network)
|
||||
net_vni = searchClusterByDescription(zk_conn, net_description)
|
||||
else:
|
||||
net_vni = searchClusterByDescription(zk_conn, network)
|
||||
net_description = searchClusterByVNI(zk_conn, net_vni)
|
||||
|
||||
return net_vni
|
||||
|
||||
def getNetworkDescription(zk_conn, network):
|
||||
# Validate and obtain alternate passed value
|
||||
if network.isdigit():
|
||||
net_description = searchClusterByVNI(zk_conn, network)
|
||||
net_vni = searchClusterByDescription(zk_conn, net_description)
|
||||
else:
|
||||
net_vni = searchClusterByDescription(zk_conn, network)
|
||||
net_description = searchClusterByVNI(zk_conn, net_vni)
|
||||
|
||||
return net_description
|
||||
|
||||
def getNetworkDHCPLeases(zk_conn, vni):
|
||||
# Get a list of DHCP leases by listing the children of /networks/<vni>/dhcp4_leases
|
||||
dhcp4_leases = zkhandler.listchildren(zk_conn, '/networks/{}/dhcp4_leases'.format(vni))
|
||||
return sorted(dhcp4_leases)
|
||||
|
||||
def getNetworkDHCPReservations(zk_conn, vni):
|
||||
# Get a list of DHCP reservations by listing the children of /networks/<vni>/dhcp4_reservations
|
||||
dhcp4_reservations = zkhandler.listchildren(zk_conn, '/networks/{}/dhcp4_reservations'.format(vni))
|
||||
return sorted(dhcp4_reservations)
|
||||
|
||||
def getNetworkACLs(zk_conn, vni, _direction):
|
||||
# Get the (sorted) list of active ACLs
|
||||
if _direction == 'both':
|
||||
directions = ['in', 'out']
|
||||
else:
|
||||
directions = [_direction]
|
||||
|
||||
full_acl_list = []
|
||||
for direction in directions:
|
||||
unordered_acl_list = zkhandler.listchildren(zk_conn, '/networks/{}/firewall_rules/{}'.format(vni, direction))
|
||||
ordered_acls = dict()
|
||||
for acl in unordered_acl_list:
|
||||
order = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/order'.format(vni, direction, acl))
|
||||
ordered_acls[order] = acl
|
||||
|
||||
for order in sorted(ordered_acls.keys()):
|
||||
rule = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/rule'.format(vni, direction, acl))
|
||||
full_acl_list.append({'direction': direction, 'order': int(order), 'description': ordered_acls[order], 'rule': rule})
|
||||
|
||||
return full_acl_list
|
||||
|
||||
def getNetworkInformation(zk_conn, vni):
|
||||
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(vni))
|
||||
nettype = zkhandler.readdata(zk_conn, '/networks/{}/nettype'.format(vni))
|
||||
domain = zkhandler.readdata(zk_conn, '/networks/{}/domain'.format(vni))
|
||||
name_servers = zkhandler.readdata(zk_conn, '/networks/{}/name_servers'.format(vni))
|
||||
ip6_network = zkhandler.readdata(zk_conn, '/networks/{}/ip6_network'.format(vni))
|
||||
ip6_gateway = zkhandler.readdata(zk_conn, '/networks/{}/ip6_gateway'.format(vni))
|
||||
dhcp6_flag = zkhandler.readdata(zk_conn, '/networks/{}/dhcp6_flag'.format(vni))
|
||||
ip4_network = zkhandler.readdata(zk_conn, '/networks/{}/ip4_network'.format(vni))
|
||||
ip4_gateway = zkhandler.readdata(zk_conn, '/networks/{}/ip4_gateway'.format(vni))
|
||||
dhcp4_flag = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_flag'.format(vni))
|
||||
dhcp4_start = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_start'.format(vni))
|
||||
dhcp4_end = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_end'.format(vni))
|
||||
|
||||
# Construct a data structure to represent the data
|
||||
network_information = {
|
||||
'vni': int(vni),
|
||||
'description': description,
|
||||
'type': nettype,
|
||||
'domain': domain,
|
||||
'name_servers': name_servers.split(','),
|
||||
'ip6': {
|
||||
'network': ip6_network,
|
||||
'gateway': ip6_gateway,
|
||||
'dhcp_flag': dhcp6_flag,
|
||||
},
|
||||
'ip4': {
|
||||
'network': ip4_network,
|
||||
'gateway': ip4_gateway,
|
||||
'dhcp_flag': dhcp4_flag,
|
||||
'dhcp_start': dhcp4_start,
|
||||
'dhcp_end': dhcp4_end
|
||||
}
|
||||
}
|
||||
return network_information
|
||||
|
||||
def getDHCPLeaseInformation(zk_conn, vni, mac_address):
|
||||
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/hostname'.format(vni, mac_address))
|
||||
ip4_address = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/ipaddr'.format(vni, mac_address))
|
||||
try:
|
||||
timestamp = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/expiry'.format(vni, mac_address))
|
||||
except:
|
||||
timestamp = 'static'
|
||||
|
||||
# Construct a data structure to represent the data
|
||||
lease_information = {
|
||||
'hostname': hostname,
|
||||
'ip4_address': ip4_address,
|
||||
'mac_address': mac_address,
|
||||
'timestamp': timestamp
|
||||
}
|
||||
return lease_information
|
||||
|
||||
def getACLInformation(zk_conn, vni, direction, description):
|
||||
order = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/order'.format(vni, direction, description))
|
||||
rule = zkhandler.readdata(zk_conn, '/networks/{}/firewall_rules/{}/{}/rule'.format(vni, direction, description))
|
||||
|
||||
# Construct a data structure to represent the data
|
||||
acl_information = {
|
||||
'order': order,
|
||||
'description': description,
|
||||
'rule': rule,
|
||||
'direction': direction
|
||||
}
|
||||
return acl_information
|
||||
|
||||
def isValidMAC(macaddr):
|
||||
allowed = re.compile(r"""
|
||||
(
|
||||
^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$
|
||||
)
|
||||
""",
|
||||
re.VERBOSE|re.IGNORECASE)
|
||||
|
||||
if allowed.match(macaddr):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def isValidIP(ipaddr):
|
||||
ip4_blocks = str(ipaddr).split(".")
|
||||
if len(ip4_blocks) == 4:
|
||||
for block in ip4_blocks:
|
||||
# Check if number is digit, if not checked before calling this function
|
||||
if not block.isdigit():
|
||||
return False
|
||||
tmp = int(block)
|
||||
if 0 > tmp > 255:
|
||||
return False
|
||||
return True
|
||||
return False
|
||||
|
||||
#
|
||||
# Direct functions
|
||||
#
|
||||
def add_network(zk_conn, vni, description, nettype,
|
||||
domain, name_servers, ip4_network, ip4_gateway, ip6_network, ip6_gateway,
|
||||
dhcp4_flag, dhcp4_start, dhcp4_end):
|
||||
# Ensure start and end DHCP ranges are set if the flag is set
|
||||
if dhcp4_flag and ( not dhcp4_start or not dhcp4_end ):
|
||||
return False, 'ERROR: DHCPv4 start and end addresses are required for a DHCPv4-enabled network.'
|
||||
|
||||
# Check if a network with this VNI or description already exists
|
||||
if zkhandler.exists(zk_conn, '/networks/{}'.format(vni)):
|
||||
return False, 'ERROR: A network with VNI "{}" already exists!'.format(vni)
|
||||
for network in zkhandler.listchildren(zk_conn, '/networks'):
|
||||
network_description = zkhandler.readdata(zk_conn, '/networks/{}'.format(network))
|
||||
if network_description == description:
|
||||
return False, 'ERROR: A network with description "{}" already exists!'.format(description)
|
||||
|
||||
# We're generating the default gateway to be ip6_network::1/YY
|
||||
if ip6_network:
|
||||
dhcp6_flag = 'True'
|
||||
if not ip6_gateway:
|
||||
ip6_netpart, ip6_maskpart = ip6_network.split('/')
|
||||
ip6_gateway = '{}1'.format(ip6_netpart)
|
||||
else:
|
||||
dhcp6_flag = 'False'
|
||||
|
||||
if nettype == 'managed' and not domain:
|
||||
domain = '{}.local'.format(description)
|
||||
|
||||
# Add the new network to Zookeeper
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/networks/{}'.format(vni): description,
|
||||
'/networks/{}/nettype'.format(vni): nettype,
|
||||
'/networks/{}/domain'.format(vni): domain,
|
||||
'/networks/{}/name_servers'.format(vni): name_servers,
|
||||
'/networks/{}/ip6_network'.format(vni): ip6_network,
|
||||
'/networks/{}/ip6_gateway'.format(vni): ip6_gateway,
|
||||
'/networks/{}/dhcp6_flag'.format(vni): dhcp6_flag,
|
||||
'/networks/{}/ip4_network'.format(vni): ip4_network,
|
||||
'/networks/{}/ip4_gateway'.format(vni): ip4_gateway,
|
||||
'/networks/{}/dhcp4_flag'.format(vni): dhcp4_flag,
|
||||
'/networks/{}/dhcp4_start'.format(vni): dhcp4_start,
|
||||
'/networks/{}/dhcp4_end'.format(vni): dhcp4_end,
|
||||
'/networks/{}/dhcp4_leases'.format(vni): '',
|
||||
'/networks/{}/dhcp4_reservations'.format(vni): '',
|
||||
'/networks/{}/firewall_rules'.format(vni): '',
|
||||
'/networks/{}/firewall_rules/in'.format(vni): '',
|
||||
'/networks/{}/firewall_rules/out'.format(vni): ''
|
||||
})
|
||||
|
||||
return True, 'Network "{}" added successfully!'.format(description)
|
||||
|
||||
def modify_network(zk_conn, vni, description=None, domain=None, name_servers=None,
|
||||
ip4_network=None, ip4_gateway=None, ip6_network=None, ip6_gateway=None,
|
||||
dhcp4_flag=None, dhcp4_start=None, dhcp4_end=None):
|
||||
# Add the modified parameters to Zookeeper
|
||||
zk_data = dict()
|
||||
if description:
|
||||
zk_data.update({'/networks/{}'.format(vni): description})
|
||||
if domain:
|
||||
zk_data.update({'/networks/{}/domain'.format(vni): domain})
|
||||
if name_servers:
|
||||
zk_data.update({'/networks/{}/name_servers'.format(vni): name_servers})
|
||||
if ip4_network:
|
||||
zk_data.update({'/networks/{}/ip4_network'.format(vni): ip4_network})
|
||||
if ip4_gateway:
|
||||
zk_data.update({'/networks/{}/ip4_gateway'.format(vni): ip4_gateway})
|
||||
if ip6_network:
|
||||
zk_data.update({'/networks/{}/ip6_network'.format(vni): ip6_network})
|
||||
if ip6_network is not None:
|
||||
zk_data.update({'/networks/{}/dhcp6_flag'.format(vni): 'True'})
|
||||
else:
|
||||
zk_data.update({'/networks/{}/dhcp6_flag'.format(vni): 'False'})
|
||||
if ip6_gateway:
|
||||
zk_data.update({'/networks/{}/ip6_gateway'.format(vni): ip6_gateway})
|
||||
else:
|
||||
# If we're changing the network, but don't also specify the gateway,
|
||||
# generate a new one automatically
|
||||
if ip6_network:
|
||||
ip6_netpart, ip6_maskpart = ip6_network.split('/')
|
||||
ip6_gateway = '{}1'.format(ip6_netpart)
|
||||
zk_data.update({'/networks/{}/ip6_gateway'.format(vni): ip6_gateway})
|
||||
if dhcp4_flag:
|
||||
zk_data.update({'/networks/{}/dhcp4_flag'.format(vni): dhcp4_flag})
|
||||
if dhcp4_start:
|
||||
zk_data.update({'/networks/{}/dhcp4_start'.format(vni): dhcp4_start})
|
||||
if dhcp4_end:
|
||||
zk_data.update({'/networks/{}/dhcp4_end'.format(vni): dhcp4_end})
|
||||
|
||||
zkhandler.writedata(zk_conn, zk_data)
|
||||
|
||||
return True, 'Network "{}" modified successfully!'.format(vni)
|
||||
|
||||
def remove_network(zk_conn, network):
|
||||
# Validate and obtain alternate passed value
|
||||
vni = getNetworkVNI(zk_conn, network)
|
||||
description = getNetworkDescription(zk_conn, network)
|
||||
if not vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
# Delete the configuration
|
||||
zkhandler.deletekey(zk_conn, '/networks/{}'.format(vni))
|
||||
|
||||
return True, 'Network "{}" removed successfully!'.format(description)
|
||||
|
||||
|
||||
def add_dhcp_reservation(zk_conn, network, ipaddress, macaddress, hostname):
|
||||
# Validate and obtain standard passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
# Use lowercase MAC format exclusively
|
||||
macaddress = macaddress.lower()
|
||||
|
||||
if not isValidMAC(macaddress):
|
||||
return False, 'ERROR: MAC address "{}" is not valid! Always use ":" as a separator.'.format(macaddress)
|
||||
|
||||
if not isValidIP(ipaddress):
|
||||
return False, 'ERROR: IP address "{}" is not valid!'.format(macaddress)
|
||||
|
||||
if zkhandler.exists(zk_conn, '/networks/{}/dhcp4_reservations/{}'.format(net_vni, macaddress)):
|
||||
return False, 'ERROR: A reservation with MAC "{}" already exists!'.format(macaddress)
|
||||
|
||||
# Add the new static lease to ZK
|
||||
try:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/networks/{}/dhcp4_reservations/{}'.format(net_vni, macaddress): 'static',
|
||||
'/networks/{}/dhcp4_reservations/{}/hostname'.format(net_vni, macaddress): hostname,
|
||||
'/networks/{}/dhcp4_reservations/{}/ipaddr'.format(net_vni, macaddress): ipaddress
|
||||
})
|
||||
except Exception as e:
|
||||
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
||||
|
||||
return True, 'DHCP reservation "{}" added successfully!'.format(macaddress)
|
||||
|
||||
def remove_dhcp_reservation(zk_conn, network, reservation):
|
||||
# Validate and obtain standard passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
match_description = ''
|
||||
|
||||
# Check if the reservation matches a static reservation description, a mac, or an IP address currently in the database
|
||||
dhcp4_reservations_list = getNetworkDHCPReservations(zk_conn, net_vni)
|
||||
for macaddr in dhcp4_reservations_list:
|
||||
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_reservations/{}/hostname'.format(net_vni, macaddr))
|
||||
ipaddress = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_reservations/{}/ipaddr'.format(net_vni, macaddr))
|
||||
if reservation == macaddr or reservation == hostname or reservation == ipaddress:
|
||||
match_description = macaddr
|
||||
lease_type_zk = 'reservations'
|
||||
lease_type_human = 'static reservation'
|
||||
|
||||
# Check if the reservation matches a dynamic reservation description, a mac, or an IP address currently in the database
|
||||
dhcp4_leases_list = getNetworkDHCPLeases(zk_conn, net_vni)
|
||||
for macaddr in dhcp4_leases_list:
|
||||
hostname = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/hostname'.format(net_vni, macaddr))
|
||||
ipaddress = zkhandler.readdata(zk_conn, '/networks/{}/dhcp4_leases/{}/ipaddr'.format(net_vni, macaddr))
|
||||
if reservation == macaddr or reservation == hostname or reservation == ipaddress:
|
||||
match_description = macaddr
|
||||
lease_type_zk = 'leases'
|
||||
lease_type_human = 'dynamic lease'
|
||||
|
||||
if not match_description:
|
||||
return False, 'ERROR: No DHCP reservation or lease exists matching "{}"!'.format(reservation)
|
||||
|
||||
# Remove the entry from zookeeper
|
||||
try:
|
||||
zkhandler.deletekey(zk_conn, '/networks/{}/dhcp4_{}/{}'.format(net_vni, lease_type_zk, match_description))
|
||||
except:
|
||||
return False, 'ERROR: Failed to write to Zookeeper!'
|
||||
|
||||
return True, 'DHCP {} "{}" removed successfully!'.format(lease_type_human, match_description)
|
||||
|
||||
def add_acl(zk_conn, network, direction, description, rule, order):
|
||||
# Validate and obtain standard passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
# Check if the ACL matches a description currently in the database
|
||||
match_description = ''
|
||||
full_acl_list = getNetworkACLs(zk_conn, net_vni, 'both')
|
||||
for acl in full_acl_list:
|
||||
if acl['description'] == description:
|
||||
match_description = acl['description']
|
||||
|
||||
if match_description:
|
||||
return False, 'ERROR: A rule with description "{}" already exists!'.format(description)
|
||||
|
||||
# Change direction to something more usable
|
||||
if direction:
|
||||
direction = "in"
|
||||
else:
|
||||
direction = "out"
|
||||
|
||||
# Handle reordering
|
||||
full_acl_list = getNetworkACLs(zk_conn, net_vni, direction)
|
||||
acl_list_length = len(full_acl_list)
|
||||
# Set order to len
|
||||
if not order or int(order) > acl_list_length:
|
||||
order = acl_list_length
|
||||
# Convert passed-in order to an integer
|
||||
else:
|
||||
order = int(order)
|
||||
|
||||
# Insert into the array at order-1
|
||||
full_acl_list.insert(order, {'direction': direction, 'description': description, 'rule': rule})
|
||||
|
||||
# Update the existing ordering
|
||||
updated_orders = dict()
|
||||
for idx, acl in enumerate(full_acl_list):
|
||||
if acl['description'] == description:
|
||||
continue
|
||||
|
||||
updated_orders[
|
||||
'/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, acl['description'])
|
||||
] = idx
|
||||
|
||||
if updated_orders:
|
||||
try:
|
||||
zkhandler.writedata(zk_conn, updated_orders)
|
||||
except Exception as e:
|
||||
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
||||
|
||||
# Add the new rule
|
||||
try:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/networks/{}/firewall_rules/{}/{}'.format(net_vni, direction, description): '',
|
||||
'/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, direction, description): order,
|
||||
'/networks/{}/firewall_rules/{}/{}/rule'.format(net_vni, direction, description): rule
|
||||
})
|
||||
except Exception as e:
|
||||
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
||||
|
||||
return True, 'Firewall rule "{}" added successfully!'.format(description)
|
||||
|
||||
def remove_acl(zk_conn, network, description):
|
||||
# Validate and obtain standard passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
match_description = ''
|
||||
|
||||
# Check if the ACL matches a description currently in the database
|
||||
acl_list = getNetworkACLs(zk_conn, net_vni, 'both')
|
||||
for acl in acl_list:
|
||||
if acl['description'] == description:
|
||||
match_description = acl['description']
|
||||
match_direction = acl['direction']
|
||||
|
||||
if not match_description:
|
||||
return False, 'ERROR: No firewall rule exists matching description "{}"!'.format(description)
|
||||
|
||||
# Remove the entry from zookeeper
|
||||
try:
|
||||
zkhandler.deletekey(zk_conn, '/networks/{}/firewall_rules/{}/{}'.format(net_vni, match_direction, match_description))
|
||||
except Exception as e:
|
||||
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
||||
|
||||
# Update the existing ordering
|
||||
updated_acl_list = getNetworkACLs(zk_conn, net_vni, match_direction)
|
||||
updated_orders = dict()
|
||||
for idx, acl in enumerate(updated_acl_list):
|
||||
updated_orders[
|
||||
'/networks/{}/firewall_rules/{}/{}/order'.format(net_vni, match_direction, acl['description'])
|
||||
] = idx
|
||||
|
||||
if updated_orders:
|
||||
try:
|
||||
zkhandler.writedata(zk_conn, updated_orders)
|
||||
except Exception as e:
|
||||
return False, 'ERROR: Failed to write to Zookeeper! Exception: "{}".'.format(e)
|
||||
|
||||
return True, 'Firewall rule "{}" removed successfully!'.format(match_description)
|
||||
|
||||
def get_info(zk_conn, network):
|
||||
# Validate and obtain alternate passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
network_information = getNetworkInformation(zk_conn, network)
|
||||
if not network_information:
|
||||
return False, 'ERROR: Could not get information about network "{}"'.format(network)
|
||||
|
||||
return True, network_information
|
||||
|
||||
def get_list(zk_conn, limit, is_fuzzy=True):
|
||||
net_list = []
|
||||
full_net_list = zkhandler.listchildren(zk_conn, '/networks')
|
||||
|
||||
for net in full_net_list:
|
||||
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net))
|
||||
if limit:
|
||||
try:
|
||||
if not is_fuzzy:
|
||||
limit = '^' + limit + '$'
|
||||
|
||||
if re.match(limit, net):
|
||||
net_list.append(getNetworkInformation(zk_conn, net))
|
||||
if re.match(limit, description):
|
||||
net_list.append(getNetworkInformation(zk_conn, net))
|
||||
except Exception as e:
|
||||
return False, 'Regex Error: {}'.format(e)
|
||||
else:
|
||||
net_list.append(getNetworkInformation(zk_conn, net))
|
||||
|
||||
#output_string = formatNetworkList(zk_conn, net_list)
|
||||
return True, net_list
|
||||
|
||||
def get_list_dhcp(zk_conn, network, limit, only_static=False, is_fuzzy=True):
|
||||
# Validate and obtain alternate passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
dhcp_list = []
|
||||
|
||||
if only_static:
|
||||
full_dhcp_list = getNetworkDHCPReservations(zk_conn, net_vni)
|
||||
reservations = True
|
||||
else:
|
||||
full_dhcp_list = getNetworkDHCPLeases(zk_conn, net_vni)
|
||||
reservations = False
|
||||
|
||||
if limit:
|
||||
try:
|
||||
if not is_fuzzy:
|
||||
limit = '^' + limit + '$'
|
||||
|
||||
# Implcitly assume fuzzy limits
|
||||
if not re.match('\^.*', limit):
|
||||
limit = '.*' + limit
|
||||
if not re.match('.*\$', limit):
|
||||
limit = limit + '.*'
|
||||
except Exception as e:
|
||||
return False, 'Regex Error: {}'.format(e)
|
||||
|
||||
|
||||
for lease in full_dhcp_list:
|
||||
valid_lease = False
|
||||
if limit:
|
||||
if re.match(limit, lease):
|
||||
valid_lease = True
|
||||
if re.match(limit, lease):
|
||||
valid_lease = True
|
||||
else:
|
||||
valid_lease = True
|
||||
|
||||
if valid_lease:
|
||||
dhcp_list.append(getDHCPLeaseInformation(zk_conn, net_vni, lease))
|
||||
|
||||
#output_string = formatDHCPLeaseList(zk_conn, net_vni, dhcp_list, reservations=reservations)
|
||||
return True, dhcp_list
|
||||
|
||||
def get_list_acl(zk_conn, network, limit, direction, is_fuzzy=True):
|
||||
# Validate and obtain alternate passed value
|
||||
net_vni = getNetworkVNI(zk_conn, network)
|
||||
if not net_vni:
|
||||
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
|
||||
|
||||
# Change direction to something more usable
|
||||
if direction is None:
|
||||
direction = "both"
|
||||
elif direction is True:
|
||||
direction = "in"
|
||||
elif direction is False:
|
||||
direction = "out"
|
||||
|
||||
acl_list = []
|
||||
full_acl_list = getNetworkACLs(zk_conn, net_vni, direction)
|
||||
|
||||
if limit:
|
||||
try:
|
||||
if not is_fuzzy:
|
||||
limit = '^' + limit + '$'
|
||||
|
||||
# Implcitly assume fuzzy limits
|
||||
if not re.match('\^.*', limit):
|
||||
limit = '.*' + limit
|
||||
if not re.match('.*\$', limit):
|
||||
limit = limit + '.*'
|
||||
except Exception as e:
|
||||
return False, 'Regex Error: {}'.format(e)
|
||||
|
||||
for acl in full_acl_list:
|
||||
valid_acl = False
|
||||
if limit:
|
||||
if re.match(limit, acl['description']):
|
||||
valid_acl = True
|
||||
else:
|
||||
valid_acl = True
|
||||
|
||||
if valid_acl:
|
||||
acl_list.append(acl)
|
||||
|
||||
#output_string = formatACLList(zk_conn, net_vni, direction, acl_list)
|
||||
return True, acl_list
|
||||
|
||||
# CLI-only functions
|
||||
def getOutputColours(network_information):
|
||||
if network_information['ip6']['network'] != "None":
|
||||
v6_flag_colour = ansiprint.green()
|
||||
else:
|
||||
v6_flag_colour = ansiprint.blue()
|
||||
if network_information['ip4']['network'] != "None":
|
||||
v4_flag_colour = ansiprint.green()
|
||||
else:
|
||||
v4_flag_colour = ansiprint.blue()
|
||||
|
||||
if network_information['ip6']['dhcp_flag'] == "True":
|
||||
dhcp6_flag_colour = ansiprint.green()
|
||||
else:
|
||||
dhcp6_flag_colour = ansiprint.blue()
|
||||
if network_information['ip4']['dhcp_flag'] == "True":
|
||||
dhcp4_flag_colour = ansiprint.green()
|
||||
else:
|
||||
dhcp4_flag_colour = ansiprint.blue()
|
||||
|
||||
return v6_flag_colour, v4_flag_colour, dhcp6_flag_colour, dhcp4_flag_colour
|
||||
|
||||
def format_info(network_information, long_output):
|
||||
if not network_information:
|
||||
click.echo("No network found")
|
||||
return
|
||||
|
||||
v6_flag_colour, v4_flag_colour, dhcp6_flag_colour, dhcp4_flag_colour = getOutputColours(network_information)
|
||||
|
||||
# Format a nice output: do this line-by-line then concat the elements at the end
|
||||
ainformation = []
|
||||
ainformation.append('{}Virtual network information:{}'.format(ansiprint.bold(), ansiprint.end()))
|
||||
ainformation.append('')
|
||||
# Basic information
|
||||
ainformation.append('{}VNI:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['vni']))
|
||||
ainformation.append('{}Type:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['type']))
|
||||
ainformation.append('{}Description:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['description']))
|
||||
if network_information['type'] == 'managed':
|
||||
ainformation.append('{}Domain:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['domain']))
|
||||
ainformation.append('{}DNS Servers:{} {}'.format(ansiprint.purple(), ansiprint.end(), ', '.join(network_information['name_servers'])))
|
||||
if network_information['ip6']['network'] != "None":
|
||||
ainformation.append('')
|
||||
ainformation.append('{}IPv6 network:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['ip6']['network']))
|
||||
ainformation.append('{}IPv6 gateway:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['ip6']['gateway']))
|
||||
ainformation.append('{}DHCPv6 enabled:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), dhcp6_flag_colour, network_information['ip6']['dhcp_flag'], ansiprint.end()))
|
||||
if network_information['ip4']['network'] != "None":
|
||||
ainformation.append('')
|
||||
ainformation.append('{}IPv4 network:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['ip4']['network']))
|
||||
ainformation.append('{}IPv4 gateway:{} {}'.format(ansiprint.purple(), ansiprint.end(), network_information['ip4']['gateway']))
|
||||
ainformation.append('{}DHCPv4 enabled:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), dhcp4_flag_colour, network_information['ip4']['dhcp_flag'], ansiprint.end()))
|
||||
if network_information['ip4']['dhcp_flag'] == "True":
|
||||
ainformation.append('{}DHCPv4 range:{} {} - {}'.format(ansiprint.purple(), ansiprint.end(), network_information['ip4']['dhcp_start'], network_information['ip4']['dhcp_end']))
|
||||
|
||||
if long_output:
|
||||
dhcp4_reservations_list = getNetworkDHCPReservations(zk_conn, vni)
|
||||
if dhcp4_reservations_list:
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Client DHCPv4 reservations:{}'.format(ansiprint.bold(), ansiprint.end()))
|
||||
ainformation.append('')
|
||||
# Only show static reservations in the detailed information
|
||||
dhcp4_reservations_string = formatDHCPLeaseList(zk_conn, vni, dhcp4_reservations_list, reservations=True)
|
||||
for line in dhcp4_reservations_string.split('\n'):
|
||||
ainformation.append(line)
|
||||
|
||||
firewall_rules = zkhandler.listchildren(zk_conn, '/networks/{}/firewall_rules'.format(vni))
|
||||
if firewall_rules:
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Network firewall rules:{}'.format(ansiprint.bold(), ansiprint.end()))
|
||||
ainformation.append('')
|
||||
formatted_firewall_rules = get_list_firewall_rules(zk_conn, vni)
|
||||
|
||||
# Join it all together
|
||||
click.echo('\n'.join(ainformation))
|
||||
|
||||
def format_list(network_list):
|
||||
if not network_list:
|
||||
click.echo("No network found")
|
||||
return
|
||||
|
||||
network_list_output = []
|
||||
|
||||
# Determine optimal column widths
|
||||
net_vni_length = 5
|
||||
net_description_length = 12
|
||||
net_nettype_length = 8
|
||||
net_domain_length = 6
|
||||
net_v6_flag_length = 6
|
||||
net_dhcp6_flag_length = 7
|
||||
net_v4_flag_length = 6
|
||||
net_dhcp4_flag_length = 7
|
||||
for network_information in network_list:
|
||||
# vni column
|
||||
_net_vni_length = len(str(network_information['vni'])) + 1
|
||||
if _net_vni_length > net_vni_length:
|
||||
net_vni_length = _net_vni_length
|
||||
# description column
|
||||
_net_description_length = len(network_information['description']) + 1
|
||||
if _net_description_length > net_description_length:
|
||||
net_description_length = _net_description_length
|
||||
# domain column
|
||||
_net_domain_length = len(network_information['domain']) + 1
|
||||
if _net_domain_length > net_domain_length:
|
||||
net_domain_length = _net_domain_length
|
||||
|
||||
# Format the string (header)
|
||||
network_list_output.append('{bold}\
|
||||
{net_vni: <{net_vni_length}} \
|
||||
{net_description: <{net_description_length}} \
|
||||
{net_nettype: <{net_nettype_length}} \
|
||||
{net_domain: <{net_domain_length}} \
|
||||
{net_v6_flag: <{net_v6_flag_length}} \
|
||||
{net_dhcp6_flag: <{net_dhcp6_flag_length}} \
|
||||
{net_v4_flag: <{net_v4_flag_length}} \
|
||||
{net_dhcp4_flag: <{net_dhcp4_flag_length}} \
|
||||
{end_bold}'.format(
|
||||
bold=ansiprint.bold(),
|
||||
end_bold=ansiprint.end(),
|
||||
net_vni_length=net_vni_length,
|
||||
net_description_length=net_description_length,
|
||||
net_nettype_length=net_nettype_length,
|
||||
net_domain_length=net_domain_length,
|
||||
net_v6_flag_length=net_v6_flag_length,
|
||||
net_dhcp6_flag_length=net_dhcp6_flag_length,
|
||||
net_v4_flag_length=net_v4_flag_length,
|
||||
net_dhcp4_flag_length=net_dhcp4_flag_length,
|
||||
net_vni='VNI',
|
||||
net_description='Description',
|
||||
net_nettype='Type',
|
||||
net_domain='Domain',
|
||||
net_v6_flag='IPv6',
|
||||
net_dhcp6_flag='DHCPv6',
|
||||
net_v4_flag='IPv4',
|
||||
net_dhcp4_flag='DHCPv4',
|
||||
)
|
||||
)
|
||||
|
||||
for network_information in network_list:
|
||||
v6_flag_colour, v4_flag_colour, dhcp6_flag_colour, dhcp4_flag_colour = getOutputColours(network_information)
|
||||
if network_information['ip4']['network'] != "None":
|
||||
v4_flag = 'True'
|
||||
else:
|
||||
v4_flag = 'False'
|
||||
|
||||
if network_information['ip6']['network'] != "None":
|
||||
v6_flag = 'True'
|
||||
else:
|
||||
v6_flag = 'False'
|
||||
|
||||
if network_information['ip4']['dhcp_flag'] == "True":
|
||||
dhcp4_range = '{} - {}'.format(network_information['ip4']['dhcp_start'], network_information['ip4']['dhcp_end'])
|
||||
else:
|
||||
dhcp4_range = 'N/A'
|
||||
|
||||
network_list_output.append(
|
||||
'{bold}\
|
||||
{net_vni: <{net_vni_length}} \
|
||||
{net_description: <{net_description_length}} \
|
||||
{net_nettype: <{net_nettype_length}} \
|
||||
{net_domain: <{net_domain_length}} \
|
||||
{v6_flag_colour}{net_v6_flag: <{net_v6_flag_length}}{colour_off} \
|
||||
{dhcp6_flag_colour}{net_dhcp6_flag: <{net_dhcp6_flag_length}}{colour_off} \
|
||||
{v4_flag_colour}{net_v4_flag: <{net_v4_flag_length}}{colour_off} \
|
||||
{dhcp4_flag_colour}{net_dhcp4_flag: <{net_dhcp4_flag_length}}{colour_off} \
|
||||
{end_bold}'.format(
|
||||
bold='',
|
||||
end_bold='',
|
||||
net_vni_length=net_vni_length,
|
||||
net_description_length=net_description_length,
|
||||
net_nettype_length=net_nettype_length,
|
||||
net_domain_length=net_domain_length,
|
||||
net_v6_flag_length=net_v6_flag_length,
|
||||
net_dhcp6_flag_length=net_dhcp6_flag_length,
|
||||
net_v4_flag_length=net_v4_flag_length,
|
||||
net_dhcp4_flag_length=net_dhcp4_flag_length,
|
||||
net_vni=network_information['vni'],
|
||||
net_description=network_information['description'],
|
||||
net_nettype=network_information['type'],
|
||||
net_domain=network_information['domain'],
|
||||
net_v6_flag=v6_flag,
|
||||
v6_flag_colour=v6_flag_colour,
|
||||
net_dhcp6_flag=network_information['ip6']['dhcp_flag'],
|
||||
dhcp6_flag_colour=dhcp6_flag_colour,
|
||||
net_v4_flag=v4_flag,
|
||||
v4_flag_colour=v4_flag_colour,
|
||||
net_dhcp4_flag=network_information['ip4']['dhcp_flag'],
|
||||
dhcp4_flag_colour=dhcp4_flag_colour,
|
||||
colour_off=ansiprint.end()
|
||||
)
|
||||
)
|
||||
|
||||
click.echo('\n'.join(sorted(network_list_output)))
|
||||
|
||||
def format_list_dhcp(dhcp_lease_list):
|
||||
dhcp_lease_list_output = []
|
||||
|
||||
# Determine optimal column widths
|
||||
lease_hostname_length = 9
|
||||
lease_ip4_address_length = 11
|
||||
lease_mac_address_length = 13
|
||||
lease_timestamp_length = 13
|
||||
for dhcp_lease_information in dhcp_lease_list:
|
||||
# hostname column
|
||||
_lease_hostname_length = len(dhcp_lease_information['hostname']) + 1
|
||||
if _lease_hostname_length > lease_hostname_length:
|
||||
lease_hostname_length = _lease_hostname_length
|
||||
# ip4_address column
|
||||
_lease_ip4_address_length = len(dhcp_lease_information['ip4_address']) + 1
|
||||
if _lease_ip4_address_length > lease_ip4_address_length:
|
||||
lease_ip4_address_length = _lease_ip4_address_length
|
||||
# mac_address column
|
||||
_lease_mac_address_length = len(dhcp_lease_information['mac_address']) + 1
|
||||
if _lease_mac_address_length > lease_mac_address_length:
|
||||
lease_mac_address_length = _lease_mac_address_length
|
||||
|
||||
# Format the string (header)
|
||||
dhcp_lease_list_output.append('{bold}\
|
||||
{lease_hostname: <{lease_hostname_length}} \
|
||||
{lease_ip4_address: <{lease_ip4_address_length}} \
|
||||
{lease_mac_address: <{lease_mac_address_length}} \
|
||||
{lease_timestamp: <{lease_timestamp_length}} \
|
||||
{end_bold}'.format(
|
||||
bold=ansiprint.bold(),
|
||||
end_bold=ansiprint.end(),
|
||||
lease_hostname_length=lease_hostname_length,
|
||||
lease_ip4_address_length=lease_ip4_address_length,
|
||||
lease_mac_address_length=lease_mac_address_length,
|
||||
lease_timestamp_length=lease_timestamp_length,
|
||||
lease_hostname='Hostname',
|
||||
lease_ip4_address='IP Address',
|
||||
lease_mac_address='MAC Address',
|
||||
lease_timestamp='Timestamp'
|
||||
)
|
||||
)
|
||||
|
||||
for dhcp_lease_information in dhcp_lease_list:
|
||||
dhcp_lease_list_output.append('{bold}\
|
||||
{lease_hostname: <{lease_hostname_length}} \
|
||||
{lease_ip4_address: <{lease_ip4_address_length}} \
|
||||
{lease_mac_address: <{lease_mac_address_length}} \
|
||||
{lease_timestamp: <{lease_timestamp_length}} \
|
||||
{end_bold}'.format(
|
||||
bold='',
|
||||
end_bold='',
|
||||
lease_hostname_length=lease_hostname_length,
|
||||
lease_ip4_address_length=lease_ip4_address_length,
|
||||
lease_mac_address_length=lease_mac_address_length,
|
||||
lease_timestamp_length=12,
|
||||
lease_hostname=dhcp_lease_information['hostname'],
|
||||
lease_ip4_address=dhcp_lease_information['ip4_address'],
|
||||
lease_mac_address=dhcp_lease_information['mac_address'],
|
||||
lease_timestamp=dhcp_lease_information['timestamp']
|
||||
)
|
||||
)
|
||||
|
||||
click.echo('\n'.join(sorted(dhcp_lease_list_output)))
|
||||
|
||||
def format_list_acl(acl_list):
|
||||
acl_list_output = []
|
||||
|
||||
# Determine optimal column widths
|
||||
acl_direction_length = 10
|
||||
acl_order_length = 6
|
||||
acl_description_length = 12
|
||||
acl_rule_length = 5
|
||||
for acl_information in acl_list:
|
||||
# order column
|
||||
_acl_order_length = len(str(acl_information['order'])) + 1
|
||||
if _acl_order_length > acl_order_length:
|
||||
acl_order_length = _acl_order_length
|
||||
# description column
|
||||
_acl_description_length = len(acl_information['description']) + 1
|
||||
if _acl_description_length > acl_description_length:
|
||||
acl_description_length = _acl_description_length
|
||||
# rule column
|
||||
_acl_rule_length = len(acl_information['rule']) + 1
|
||||
if _acl_rule_length > acl_rule_length:
|
||||
acl_rule_length = _acl_rule_length
|
||||
|
||||
# Format the string (header)
|
||||
acl_list_output.append('{bold}\
|
||||
{acl_direction: <{acl_direction_length}} \
|
||||
{acl_order: <{acl_order_length}} \
|
||||
{acl_description: <{acl_description_length}} \
|
||||
{acl_rule: <{acl_rule_length}} \
|
||||
{end_bold}'.format(
|
||||
bold=ansiprint.bold(),
|
||||
end_bold=ansiprint.end(),
|
||||
acl_direction_length=acl_direction_length,
|
||||
acl_order_length=acl_order_length,
|
||||
acl_description_length=acl_description_length,
|
||||
acl_rule_length=acl_rule_length,
|
||||
acl_direction='Direction',
|
||||
acl_order='Order',
|
||||
acl_description='Description',
|
||||
acl_rule='Rule',
|
||||
)
|
||||
)
|
||||
|
||||
for acl_information in acl_list:
|
||||
acl_list_output.append('{bold}\
|
||||
{acl_direction: <{acl_direction_length}} \
|
||||
{acl_order: <{acl_order_length}} \
|
||||
{acl_description: <{acl_description_length}} \
|
||||
{acl_rule: <{acl_rule_length}} \
|
||||
{end_bold}'.format(
|
||||
bold='',
|
||||
end_bold='',
|
||||
acl_direction_length=acl_direction_length,
|
||||
acl_order_length=acl_order_length,
|
||||
acl_description_length=acl_description_length,
|
||||
acl_rule_length=acl_rule_length,
|
||||
acl_direction=acl_information['direction'],
|
||||
acl_order=acl_information['order'],
|
||||
acl_description=acl_information['description'],
|
||||
acl_rule=acl_information['rule'],
|
||||
)
|
||||
)
|
||||
|
||||
click.echo('\n'.join(sorted(acl_list_output)))
|
||||
|
416
daemon-common/node.py
Normal file
416
daemon-common/node.py
Normal file
@ -0,0 +1,416 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# node.py - PVC client function library, node management
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
import uuid
|
||||
import re
|
||||
import tempfile
|
||||
import subprocess
|
||||
import difflib
|
||||
import colorama
|
||||
import click
|
||||
import lxml.objectify
|
||||
import configparser
|
||||
import kazoo.client
|
||||
|
||||
import daemon_lib.ansiprint as ansiprint
|
||||
import daemon_lib.zkhandler as zkhandler
|
||||
import daemon_lib.common as common
|
||||
import daemon_lib.vm as pvc_vm
|
||||
|
||||
def getNodeInformation(zk_conn, node_name):
|
||||
"""
|
||||
Gather information about a node from the Zookeeper database and return a dict() containing it.
|
||||
"""
|
||||
node_daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node_name))
|
||||
node_coordinator_state = zkhandler.readdata(zk_conn, '/nodes/{}/routerstate'.format(node_name))
|
||||
node_domain_state = zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node_name))
|
||||
node_static_data = zkhandler.readdata(zk_conn, '/nodes/{}/staticdata'.format(node_name)).split()
|
||||
node_cpu_count = int(node_static_data[0])
|
||||
node_kernel = node_static_data[1]
|
||||
node_os = node_static_data[2]
|
||||
node_arch = node_static_data[3]
|
||||
node_vcpu_allocated = int(zkhandler.readdata(zk_conn, 'nodes/{}/vcpualloc'.format(node_name)))
|
||||
node_mem_total = int(zkhandler.readdata(zk_conn, '/nodes/{}/memtotal'.format(node_name)))
|
||||
node_mem_allocated = int(zkhandler.readdata(zk_conn, '/nodes/{}/memalloc'.format(node_name)))
|
||||
node_mem_used = int(zkhandler.readdata(zk_conn, '/nodes/{}/memused'.format(node_name)))
|
||||
node_mem_free = int(zkhandler.readdata(zk_conn, '/nodes/{}/memfree'.format(node_name)))
|
||||
node_load = float(zkhandler.readdata(zk_conn, '/nodes/{}/cpuload'.format(node_name)))
|
||||
node_domains_count = int(zkhandler.readdata(zk_conn, '/nodes/{}/domainscount'.format(node_name)))
|
||||
node_running_domains = zkhandler.readdata(zk_conn, '/nodes/{}/runningdomains'.format(node_name)).split()
|
||||
|
||||
# Construct a data structure to represent the data
|
||||
node_information = {
|
||||
'name': node_name,
|
||||
'daemon_state': node_daemon_state,
|
||||
'coordinator_state': node_coordinator_state,
|
||||
'domain_state': node_domain_state,
|
||||
'cpu_count': node_cpu_count,
|
||||
'kernel': node_kernel,
|
||||
'os': node_os,
|
||||
'arch': node_arch,
|
||||
'load': node_load,
|
||||
'domains_count': node_domains_count,
|
||||
'running_domains': node_running_domains,
|
||||
'vcpu': {
|
||||
'total': node_cpu_count,
|
||||
'allocated': node_vcpu_allocated
|
||||
},
|
||||
'memory': {
|
||||
'total': node_mem_total,
|
||||
'allocated': node_mem_allocated,
|
||||
'used': node_mem_used,
|
||||
'free': node_mem_free
|
||||
}
|
||||
}
|
||||
return node_information
|
||||
|
||||
#
|
||||
# Direct Functions
|
||||
#
|
||||
def secondary_node(zk_conn, node):
|
||||
# Verify node is valid
|
||||
if not common.verifyNode(zk_conn, node):
|
||||
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
|
||||
|
||||
# Ensure node is a coordinator
|
||||
daemon_mode = zkhandler.readdata(zk_conn, '/nodes/{}/daemonmode'.format(node))
|
||||
if daemon_mode == 'hypervisor':
|
||||
return False, 'ERROR: Cannot change router mode on non-coordinator node "{}"'.format(node)
|
||||
|
||||
# Ensure node is in run daemonstate
|
||||
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
|
||||
if daemon_state != 'run':
|
||||
return False, 'ERROR: Node "{}" is not active'.format(node)
|
||||
|
||||
# Get current state
|
||||
current_state = zkhandler.readdata(zk_conn, '/nodes/{}/routerstate'.format(node))
|
||||
if current_state == 'primary':
|
||||
retmsg = 'Setting node {} in secondary router mode.'.format(node)
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/primary_node': 'none'
|
||||
})
|
||||
else:
|
||||
return False, 'Node "{}" is already in secondary router mode.'.format(node)
|
||||
|
||||
return True, retmsg
|
||||
|
||||
def primary_node(zk_conn, node):
|
||||
# Verify node is valid
|
||||
if not common.verifyNode(zk_conn, node):
|
||||
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
|
||||
|
||||
# Ensure node is a coordinator
|
||||
daemon_mode = zkhandler.readdata(zk_conn, '/nodes/{}/daemonmode'.format(node))
|
||||
if daemon_mode == 'hypervisor':
|
||||
return False, 'ERROR: Cannot change router mode on non-coordinator node "{}"'.format(node)
|
||||
|
||||
# Ensure node is in run daemonstate
|
||||
daemon_state = zkhandler.readdata(zk_conn, '/nodes/{}/daemonstate'.format(node))
|
||||
if daemon_state != 'run':
|
||||
return False, 'ERROR: Node "{}" is not active'.format(node)
|
||||
|
||||
# Get current state
|
||||
current_state = zkhandler.readdata(zk_conn, '/nodes/{}/routerstate'.format(node))
|
||||
if current_state == 'secondary':
|
||||
retmsg = 'Setting node {} in primary router mode.'.format(node)
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/primary_node': node
|
||||
})
|
||||
else:
|
||||
return False, 'Node "{}" is already in primary router mode.'.format(node)
|
||||
|
||||
return True, retmsg
|
||||
|
||||
def flush_node(zk_conn, node, wait):
|
||||
# Verify node is valid
|
||||
if not common.verifyNode(zk_conn, node):
|
||||
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
|
||||
|
||||
retmsg = 'Flushing hypervisor {} of running VMs.'.format(node)
|
||||
|
||||
# Add the new domain to Zookeeper
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/nodes/{}/domainstate'.format(node): 'flush'
|
||||
})
|
||||
|
||||
# Wait cannot be triggered from the API
|
||||
if wait:
|
||||
while zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node)) == 'flush':
|
||||
time.sleep(1)
|
||||
retmsg = 'Flushed hypervisor {} of running VMs.'.format(node)
|
||||
|
||||
return True, retmsg
|
||||
|
||||
def ready_node(zk_conn, node, wait):
|
||||
# Verify node is valid
|
||||
if not common.verifyNode(zk_conn, node):
|
||||
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
|
||||
|
||||
retmsg = 'Restoring hypervisor {} to active service.'.format(node)
|
||||
|
||||
# Add the new domain to Zookeeper
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/nodes/{}/domainstate'.format(node): 'unflush'
|
||||
})
|
||||
|
||||
# Wait cannot be triggered from the API
|
||||
if wait:
|
||||
while zkhandler.readdata(zk_conn, '/nodes/{}/domainstate'.format(node)) == 'unflush':
|
||||
time.sleep(1)
|
||||
retmsg = 'Restored hypervisor {} to active service.'.format(node)
|
||||
|
||||
return True, retmsg
|
||||
|
||||
def get_info(zk_conn, node):
|
||||
# Verify node is valid
|
||||
if not common.verifyNode(zk_conn, node):
|
||||
return False, 'ERROR: No node named "{}" is present in the cluster.'.format(node)
|
||||
|
||||
# Get information about node in a pretty format
|
||||
node_information = getNodeInformation(zk_conn, node)
|
||||
if not node_information:
|
||||
return False, 'ERROR: Could not get information about node "{}".'.format(node)
|
||||
|
||||
return True, node_information
|
||||
|
||||
def get_list(zk_conn, limit, is_fuzzy=True):
|
||||
node_list = []
|
||||
full_node_list = zkhandler.listchildren(zk_conn, '/nodes')
|
||||
|
||||
for node in full_node_list:
|
||||
if limit:
|
||||
try:
|
||||
if not is_fuzzy:
|
||||
limit = '^' + limit + '$'
|
||||
|
||||
if re.match(limit, node):
|
||||
node_list.append(getNodeInformation(zk_conn, node))
|
||||
except Exception as e:
|
||||
return False, 'Regex Error: {}'.format(e)
|
||||
else:
|
||||
node_list.append(getNodeInformation(zk_conn, node))
|
||||
|
||||
return True, node_list
|
||||
|
||||
#
|
||||
# CLI-specific functions
|
||||
#
|
||||
def getOutputColours(node_information):
|
||||
if node_information['daemon_state'] == 'run':
|
||||
daemon_state_colour = ansiprint.green()
|
||||
elif node_information['daemon_state'] == 'stop':
|
||||
daemon_state_colour = ansiprint.red()
|
||||
elif node_information['daemon_state'] == 'shutdown':
|
||||
daemon_state_colour = ansiprint.yellow()
|
||||
elif node_information['daemon_state'] == 'init':
|
||||
daemon_state_colour = ansiprint.yellow()
|
||||
elif node_information['daemon_state'] == 'dead':
|
||||
daemon_state_colour = ansiprint.red() + ansiprint.bold()
|
||||
else:
|
||||
daemon_state_colour = ansiprint.blue()
|
||||
|
||||
if node_information['coordinator_state'] == 'primary':
|
||||
coordinator_state_colour = ansiprint.green()
|
||||
elif node_information['coordinator_state'] == 'secondary':
|
||||
coordinator_state_colour = ansiprint.blue()
|
||||
else:
|
||||
coordinator_state_colour = ansiprint.cyan()
|
||||
|
||||
if node_information['domain_state'] == 'ready':
|
||||
domain_state_colour = ansiprint.green()
|
||||
else:
|
||||
domain_state_colour = ansiprint.blue()
|
||||
|
||||
return daemon_state_colour, coordinator_state_colour, domain_state_colour
|
||||
|
||||
def format_info(node_information, long_output):
|
||||
daemon_state_colour, coordinator_state_colour, domain_state_colour = getOutputColours(node_information)
|
||||
|
||||
# Format a nice output; do this line-by-line then concat the elements at the end
|
||||
ainformation = []
|
||||
# Basic information
|
||||
ainformation.append('{}Name:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['name']))
|
||||
ainformation.append('{}Daemon State:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), daemon_state_colour, node_information['daemon_state'], ansiprint.end()))
|
||||
ainformation.append('{}Coordinator State:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), coordinator_state_colour, node_information['coordinator_state'], ansiprint.end()))
|
||||
ainformation.append('{}Domain State:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), domain_state_colour, node_information['domain_state'], ansiprint.end()))
|
||||
ainformation.append('{}Active VM Count:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['domains_count']))
|
||||
if long_output:
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Architecture:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['arch']))
|
||||
ainformation.append('{}Operating System:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['os']))
|
||||
ainformation.append('{}Kernel Version:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['kernel']))
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Host CPUs:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['vcpu']['total']))
|
||||
ainformation.append('{}vCPUs:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['vcpu']['allocated']))
|
||||
ainformation.append('{}Load:{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['load']))
|
||||
ainformation.append('{}Total RAM (MiB):{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['memory']['total']))
|
||||
ainformation.append('{}Used RAM (MiB):{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['memory']['used']))
|
||||
ainformation.append('{}Free RAM (MiB):{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['memory']['free']))
|
||||
ainformation.append('{}Allocated RAM (MiB):{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['memory']['allocated']))
|
||||
|
||||
# Join it all together
|
||||
information = '\n'.join(ainformation)
|
||||
click.echo(information)
|
||||
|
||||
click.echo('')
|
||||
|
||||
def format_list(node_list):
|
||||
node_list_output = []
|
||||
|
||||
# Determine optimal column widths
|
||||
node_name_length = 5
|
||||
daemon_state_length = 7
|
||||
coordinator_state_length = 12
|
||||
domain_state_length = 8
|
||||
domains_count_length = 4
|
||||
cpu_count_length = 6
|
||||
load_length = 5
|
||||
mem_total_length = 6
|
||||
mem_used_length = 5
|
||||
mem_free_length = 5
|
||||
mem_alloc_length = 4
|
||||
for node_information in node_list:
|
||||
# node_name column
|
||||
_node_name_length = len(node_information['name']) + 1
|
||||
if _node_name_length > node_name_length:
|
||||
node_name_length = _node_name_length
|
||||
# daemon_state column
|
||||
_daemon_state_length = len(node_information['daemon_state']) + 1
|
||||
if _daemon_state_length > daemon_state_length:
|
||||
daemon_state_length = _daemon_state_length
|
||||
# coordinator_state column
|
||||
_coordinator_state_length = len(node_information['coordinator_state']) + 1
|
||||
if _coordinator_state_length > coordinator_state_length:
|
||||
coordinator_state_length = _coordinator_state_length
|
||||
# domain_state column
|
||||
_domain_state_length = len(node_information['domain_state']) + 1
|
||||
if _domain_state_length > domain_state_length:
|
||||
domain_state_length = _domain_state_length
|
||||
# domains_count column
|
||||
_domains_count_length = len(str(node_information['domains_count'])) + 1
|
||||
if _domains_count_length > domains_count_length:
|
||||
domains_count_length = _domains_count_length
|
||||
# cpu_count column
|
||||
_cpu_count_length = len(str(node_information['cpu_count'])) + 1
|
||||
if _cpu_count_length > cpu_count_length:
|
||||
cpu_count_length = _cpu_count_length
|
||||
# load column
|
||||
_load_length = len(str(node_information['load'])) + 1
|
||||
if _load_length > load_length:
|
||||
load_length = _load_length
|
||||
# mem_total column
|
||||
_mem_total_length = len(str(node_information['memory']['total'])) + 1
|
||||
if _mem_total_length > mem_total_length:
|
||||
mem_total_length = _mem_total_length
|
||||
# mem_used column
|
||||
_mem_used_length = len(str(node_information['memory']['used'])) + 1
|
||||
if _mem_used_length > mem_used_length:
|
||||
mem_used_length = _mem_used_length
|
||||
# mem_free column
|
||||
_mem_free_length = len(str(node_information['memory']['free'])) + 1
|
||||
if _mem_free_length > mem_free_length:
|
||||
mem_free_length = _mem_free_length
|
||||
# mem_alloc column
|
||||
_mem_alloc_length = len(str(node_information['memory']['allocated'])) + 1
|
||||
if _mem_alloc_length > mem_alloc_length:
|
||||
mem_alloc_length = _mem_alloc_length
|
||||
|
||||
# Format the string (header)
|
||||
node_list_output.append(
|
||||
'{bold}{node_name: <{node_name_length}} \
|
||||
St: {daemon_state_colour}{node_daemon_state: <{daemon_state_length}}{end_colour} {coordinator_state_colour}{node_coordinator_state: <{coordinator_state_length}}{end_colour} {domain_state_colour}{node_domain_state: <{domain_state_length}}{end_colour} \
|
||||
Res: {node_domains_count: <{domains_count_length}} {node_cpu_count: <{cpu_count_length}} {node_load: <{load_length}} \
|
||||
Mem (M): {node_mem_total: <{mem_total_length}} {node_mem_used: <{mem_used_length}} {node_mem_free: <{mem_free_length}} {node_mem_allocated: <{mem_alloc_length}}{end_bold}'.format(
|
||||
node_name_length=node_name_length,
|
||||
daemon_state_length=daemon_state_length,
|
||||
coordinator_state_length=coordinator_state_length,
|
||||
domain_state_length=domain_state_length,
|
||||
domains_count_length=domains_count_length,
|
||||
cpu_count_length=cpu_count_length,
|
||||
load_length=load_length,
|
||||
mem_total_length=mem_total_length,
|
||||
mem_used_length=mem_used_length,
|
||||
mem_free_length=mem_free_length,
|
||||
mem_alloc_length=mem_alloc_length,
|
||||
bold=ansiprint.bold(),
|
||||
end_bold=ansiprint.end(),
|
||||
daemon_state_colour='',
|
||||
coordinator_state_colour='',
|
||||
domain_state_colour='',
|
||||
end_colour='',
|
||||
node_name='Name',
|
||||
node_daemon_state='Daemon',
|
||||
node_coordinator_state='Coordinator',
|
||||
node_domain_state='Domain',
|
||||
node_domains_count='VMs',
|
||||
node_cpu_count='vCPUs',
|
||||
node_load='Load',
|
||||
node_mem_total='Total',
|
||||
node_mem_used='Used',
|
||||
node_mem_free='Free',
|
||||
node_mem_allocated='VMs'
|
||||
)
|
||||
)
|
||||
|
||||
# Format the string (elements)
|
||||
for node_information in node_list:
|
||||
daemon_state_colour, coordinator_state_colour, domain_state_colour = getOutputColours(node_information)
|
||||
node_list_output.append(
|
||||
'{bold}{node_name: <{node_name_length}} \
|
||||
{daemon_state_colour}{node_daemon_state: <{daemon_state_length}}{end_colour} {coordinator_state_colour}{node_coordinator_state: <{coordinator_state_length}}{end_colour} {domain_state_colour}{node_domain_state: <{domain_state_length}}{end_colour} \
|
||||
{node_domains_count: <{domains_count_length}} {node_cpu_count: <{cpu_count_length}} {node_load: <{load_length}} \
|
||||
{node_mem_total: <{mem_total_length}} {node_mem_used: <{mem_used_length}} {node_mem_free: <{mem_free_length}} {node_mem_allocated: <{mem_alloc_length}}{end_bold}'.format(
|
||||
node_name_length=node_name_length,
|
||||
daemon_state_length=daemon_state_length,
|
||||
coordinator_state_length=coordinator_state_length,
|
||||
domain_state_length=domain_state_length,
|
||||
domains_count_length=domains_count_length,
|
||||
cpu_count_length=cpu_count_length,
|
||||
load_length=load_length,
|
||||
mem_total_length=mem_total_length,
|
||||
mem_used_length=mem_used_length,
|
||||
mem_free_length=mem_free_length,
|
||||
mem_alloc_length=mem_alloc_length,
|
||||
bold='',
|
||||
end_bold='',
|
||||
daemon_state_colour=daemon_state_colour,
|
||||
coordinator_state_colour=coordinator_state_colour,
|
||||
domain_state_colour=domain_state_colour,
|
||||
end_colour=ansiprint.end(),
|
||||
node_name=node_information['name'],
|
||||
node_daemon_state=node_information['daemon_state'],
|
||||
node_coordinator_state=node_information['coordinator_state'],
|
||||
node_domain_state=node_information['domain_state'],
|
||||
node_domains_count=node_information['domains_count'],
|
||||
node_cpu_count=node_information['vcpu']['allocated'],
|
||||
node_load=node_information['load'],
|
||||
node_mem_total=node_information['memory']['total'],
|
||||
node_mem_used=node_information['memory']['used'],
|
||||
node_mem_free=node_information['memory']['free'],
|
||||
node_mem_allocated=node_information['memory']['allocated']
|
||||
)
|
||||
)
|
||||
|
||||
click.echo('\n'.join(sorted(node_list_output)))
|
953
daemon-common/vm.py
Normal file
953
daemon-common/vm.py
Normal file
@ -0,0 +1,953 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# vm.py - PVC client function library, VM fuctions
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import os
|
||||
import socket
|
||||
import time
|
||||
import uuid
|
||||
import re
|
||||
import subprocess
|
||||
import difflib
|
||||
import colorama
|
||||
import click
|
||||
import lxml.objectify
|
||||
import configparser
|
||||
import kazoo.client
|
||||
|
||||
from collections import deque
|
||||
|
||||
import daemon_lib.ansiprint as ansiprint
|
||||
import daemon_lib.zkhandler as zkhandler
|
||||
import daemon_lib.common as common
|
||||
|
||||
import daemon_lib.ceph as ceph
|
||||
|
||||
#
|
||||
# Cluster search functions
|
||||
#
|
||||
def getClusterDomainList(zk_conn):
|
||||
# Get a list of UUIDs by listing the children of /domains
|
||||
uuid_list = zkhandler.listchildren(zk_conn, '/domains')
|
||||
name_list = []
|
||||
# For each UUID, get the corresponding name from the data
|
||||
for uuid in uuid_list:
|
||||
name_list.append(zkhandler.readdata(zk_conn, '/domains/%s' % uuid))
|
||||
return uuid_list, name_list
|
||||
|
||||
def searchClusterByUUID(zk_conn, uuid):
|
||||
try:
|
||||
# Get the lists
|
||||
uuid_list, name_list = getClusterDomainList(zk_conn)
|
||||
# We're looking for UUID, so find that element ID
|
||||
index = uuid_list.index(uuid)
|
||||
# Get the name_list element at that index
|
||||
name = name_list[index]
|
||||
except ValueError:
|
||||
# We didn't find anything
|
||||
return None
|
||||
|
||||
return name
|
||||
|
||||
def searchClusterByName(zk_conn, name):
|
||||
try:
|
||||
# Get the lists
|
||||
uuid_list, name_list = getClusterDomainList(zk_conn)
|
||||
# We're looking for name, so find that element ID
|
||||
index = name_list.index(name)
|
||||
# Get the uuid_list element at that index
|
||||
uuid = uuid_list[index]
|
||||
except ValueError:
|
||||
# We didn't find anything
|
||||
return None
|
||||
|
||||
return uuid
|
||||
|
||||
def getDomainUUID(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
if common.validateUUID(domain):
|
||||
dom_name = searchClusterByUUID(zk_conn, domain)
|
||||
dom_uuid = searchClusterByName(zk_conn, dom_name)
|
||||
else:
|
||||
dom_uuid = searchClusterByName(zk_conn, domain)
|
||||
dom_name = searchClusterByUUID(zk_conn, dom_uuid)
|
||||
|
||||
return dom_uuid
|
||||
|
||||
def getDomainName(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
if common.validateUUID(domain):
|
||||
dom_name = searchClusterByUUID(zk_conn, domain)
|
||||
dom_uuid = searchClusterByName(zk_conn, dom_name)
|
||||
else:
|
||||
dom_uuid = searchClusterByName(zk_conn, domain)
|
||||
dom_name = searchClusterByUUID(zk_conn, dom_uuid)
|
||||
|
||||
return dom_name
|
||||
|
||||
#
|
||||
# Direct functions
|
||||
#
|
||||
def is_migrated(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
last_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
||||
if last_node:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def flush_locks(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Verify that the VM is in a stopped state; freeing locks is not safe otherwise
|
||||
state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if state != 'stop':
|
||||
return False, 'ERROR: VM "{}" is not in stopped state; flushing RBD locks on a running VM is dangerous.'.format(domain)
|
||||
|
||||
# Tell the cluster to create a new OSD for the host
|
||||
flush_locks_string = 'flush_locks {}'.format(dom_uuid)
|
||||
zkhandler.writedata(zk_conn, {'/cmd/domains': flush_locks_string})
|
||||
# Wait 1/2 second for the cluster to get the message and start working
|
||||
time.sleep(0.5)
|
||||
# Acquire a read lock, so we get the return exclusively
|
||||
lock = zkhandler.readlock(zk_conn, '/cmd/domains')
|
||||
with lock:
|
||||
try:
|
||||
result = zkhandler.readdata(zk_conn, '/cmd/domains').split()[0]
|
||||
if result == 'success-flush_locks':
|
||||
message = 'Flushed locks on VM "{}"'.format(domain)
|
||||
success = True
|
||||
else:
|
||||
message = 'ERROR: Failed to flush locks on VM "{}"; check node logs for details.'.format(domain)
|
||||
success = False
|
||||
except:
|
||||
message = 'ERROR: Command ignored by node.'
|
||||
success = False
|
||||
|
||||
# Acquire a write lock to ensure things go smoothly
|
||||
lock = zkhandler.writelock(zk_conn, '/cmd/domains')
|
||||
with lock:
|
||||
time.sleep(0.5)
|
||||
zkhandler.writedata(zk_conn, {'/cmd/domains': ''})
|
||||
|
||||
return success, message
|
||||
|
||||
def define_vm(zk_conn, config_data, target_node, node_limit, node_selector, node_autostart, profile=None, initial_state='stop'):
|
||||
# Parse the XML data
|
||||
try:
|
||||
parsed_xml = lxml.objectify.fromstring(config_data)
|
||||
except:
|
||||
return False, 'ERROR: Failed to parse XML data.'
|
||||
dom_uuid = parsed_xml.uuid.text
|
||||
dom_name = parsed_xml.name.text
|
||||
|
||||
# Ensure that the UUID and name are unique
|
||||
if searchClusterByUUID(zk_conn, dom_uuid) or searchClusterByName(zk_conn, dom_name):
|
||||
return False, 'ERROR: Specified VM "{}" or UUID "{}" matches an existing VM on the cluster'.format(dom_name, dom_uuid)
|
||||
|
||||
if not target_node:
|
||||
target_node = common.findTargetNode(zk_conn, dom_uuid)
|
||||
else:
|
||||
# Verify node is valid
|
||||
valid_node = common.verifyNode(zk_conn, target_node)
|
||||
if not valid_node:
|
||||
return False, 'ERROR: Specified node "{}" is invalid.'.format(target_node)
|
||||
|
||||
# Obtain the RBD disk list using the common functions
|
||||
ddisks = common.getDomainDisks(parsed_xml)
|
||||
rbd_list = []
|
||||
for disk in ddisks:
|
||||
if disk['type'] == 'rbd':
|
||||
rbd_list.append(disk['name'])
|
||||
|
||||
# Join the limit
|
||||
if isinstance(node_limit, list) and node_limit:
|
||||
formatted_node_limit = ','.join(node_limit)
|
||||
else:
|
||||
formatted_node_limit = ''
|
||||
|
||||
# Join the RBD list
|
||||
if isinstance(rbd_list, list) and rbd_list:
|
||||
formatted_rbd_list = ','.join(rbd_list)
|
||||
else:
|
||||
formatted_rbd_list = ''
|
||||
|
||||
# Add the new domain to Zookeeper
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}'.format(dom_uuid): dom_name,
|
||||
'/domains/{}/state'.format(dom_uuid): initial_state,
|
||||
'/domains/{}/node'.format(dom_uuid): target_node,
|
||||
'/domains/{}/lastnode'.format(dom_uuid): '',
|
||||
'/domains/{}/node_limit'.format(dom_uuid): formatted_node_limit,
|
||||
'/domains/{}/node_selector'.format(dom_uuid): node_selector,
|
||||
'/domains/{}/node_autostart'.format(dom_uuid): node_autostart,
|
||||
'/domains/{}/failedreason'.format(dom_uuid): '',
|
||||
'/domains/{}/consolelog'.format(dom_uuid): '',
|
||||
'/domains/{}/rbdlist'.format(dom_uuid): formatted_rbd_list,
|
||||
'/domains/{}/profile'.format(dom_uuid): profile,
|
||||
'/domains/{}/xml'.format(dom_uuid): config_data
|
||||
})
|
||||
|
||||
return True, 'Added new VM with Name "{}" and UUID "{}" to database.'.format(dom_name, dom_uuid)
|
||||
|
||||
def modify_vm_metadata(zk_conn, domain, node_limit, node_selector, node_autostart, provisioner_profile):
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
if node_limit is not None:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/node_limit'.format(dom_uuid): node_limit
|
||||
})
|
||||
|
||||
if node_selector is not None:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/node_selector'.format(dom_uuid): node_selector
|
||||
})
|
||||
|
||||
if node_autostart is not None:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/node_autostart'.format(dom_uuid): node_autostart
|
||||
})
|
||||
|
||||
if provisioner_profile is not None:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/profile'.format(dom_uuid): provisioner_profile
|
||||
})
|
||||
|
||||
return True, 'Successfully modified PVC metadata of VM "{}".'.format(domain)
|
||||
|
||||
def modify_vm(zk_conn, domain, restart, new_vm_config):
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
dom_name = getDomainName(zk_conn, domain)
|
||||
|
||||
# Add the modified config to Zookeeper
|
||||
zk_data = {
|
||||
'/domains/{}'.format(dom_uuid): dom_name,
|
||||
'/domains/{}/xml'.format(dom_uuid): new_vm_config
|
||||
}
|
||||
zkhandler.writedata(zk_conn, zk_data)
|
||||
|
||||
if restart:
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'restart'})
|
||||
|
||||
return True, ''
|
||||
|
||||
def dump_vm(zk_conn, domain):
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Gram the domain XML and dump it to stdout
|
||||
vm_xml = zkhandler.readdata(zk_conn, '/domains/{}/xml'.format(dom_uuid))
|
||||
|
||||
return True, vm_xml
|
||||
|
||||
def purge_vm(zk_conn, domain, is_cli=False):
|
||||
"""
|
||||
Helper function for both undefine and remove VM to perform the shutdown, termination,
|
||||
and configuration deletion.
|
||||
"""
|
||||
|
||||
def undefine_vm(zk_conn, domain, is_cli=False):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Shut down the VM
|
||||
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_vm_state != 'stop':
|
||||
if is_cli:
|
||||
click.echo('Forcibly stopping VM "{}".'.format(domain))
|
||||
# Set the domain into stop mode
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
|
||||
|
||||
# Wait for 1 second to allow state to flow to all nodes
|
||||
if is_cli:
|
||||
click.echo('Waiting for cluster to update.')
|
||||
time.sleep(2)
|
||||
|
||||
# Gracefully terminate the class instances
|
||||
if is_cli:
|
||||
click.echo('Deleting VM "{}" from nodes.'.format(domain))
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'delete'})
|
||||
time.sleep(2)
|
||||
|
||||
# Delete the configurations
|
||||
if is_cli:
|
||||
click.echo('Undefining VM "{}".'.format(domain))
|
||||
zkhandler.deletekey(zk_conn, '/domains/{}'.format(dom_uuid))
|
||||
|
||||
return True, 'Undefined VM "{}" from the cluster.'.format(domain)
|
||||
|
||||
def remove_vm(zk_conn, domain, is_cli=False):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
disk_list = common.getDomainDiskList(zk_conn, dom_uuid)
|
||||
|
||||
# Shut down the VM
|
||||
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_vm_state != 'stop':
|
||||
if is_cli:
|
||||
click.echo('Forcibly stopping VM "{}".'.format(domain))
|
||||
# Set the domain into stop mode
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
|
||||
|
||||
# Wait for 1 second to allow state to flow to all nodes
|
||||
if is_cli:
|
||||
click.echo('Waiting for cluster to update.')
|
||||
time.sleep(2)
|
||||
|
||||
# Gracefully terminate the class instances
|
||||
if is_cli:
|
||||
click.echo('Deleting VM "{}" from nodes.'.format(domain))
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'delete'})
|
||||
time.sleep(2)
|
||||
|
||||
# Delete the configurations
|
||||
if is_cli:
|
||||
click.echo('Undefining VM "{}".'.format(domain))
|
||||
zkhandler.deletekey(zk_conn, '/domains/{}'.format(dom_uuid))
|
||||
time.sleep(2)
|
||||
|
||||
# Remove disks
|
||||
for disk in disk_list:
|
||||
# vmpool/vmname_volume
|
||||
try:
|
||||
disk_pool, disk_name = disk.split('/')
|
||||
retcode, message = ceph.remove_volume(zk_conn, disk_pool, disk_name)
|
||||
if is_cli and message:
|
||||
click.echo('{}'.format(message))
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
return True, 'Removed VM "{}" and disks from the cluster.'.format(domain)
|
||||
|
||||
def start_vm(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Set the VM to start
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'start'})
|
||||
|
||||
return True, 'Starting VM "{}".'.format(domain)
|
||||
|
||||
def restart_vm(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get state and verify we're OK to proceed
|
||||
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_state != 'start':
|
||||
return False, 'ERROR: VM "{}" is not in "start" state!'.format(domain)
|
||||
|
||||
# Set the VM to start
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'restart'})
|
||||
|
||||
return True, 'Restarting VM "{}".'.format(domain)
|
||||
|
||||
def shutdown_vm(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get state and verify we're OK to proceed
|
||||
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_state != 'start':
|
||||
return False, 'ERROR: VM "{}" is not in "start" state!'.format(domain)
|
||||
|
||||
# Set the VM to shutdown
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'shutdown'})
|
||||
|
||||
return True, 'Shutting down VM "{}".'.format(domain)
|
||||
|
||||
def stop_vm(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get state and verify we're OK to proceed
|
||||
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
|
||||
# Set the VM to start
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
|
||||
|
||||
return True, 'Forcibly stopping VM "{}".'.format(domain)
|
||||
|
||||
def disable_vm(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get state and verify we're OK to proceed
|
||||
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_state != 'stop':
|
||||
return False, 'ERROR: VM "{}" must be stopped before disabling!'.format(domain)
|
||||
|
||||
# Set the VM to start
|
||||
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'disable'})
|
||||
|
||||
return True, 'Marked VM "{}" as disable.'.format(domain)
|
||||
|
||||
def move_vm(zk_conn, domain, target_node):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
|
||||
|
||||
if not target_node:
|
||||
target_node = common.findTargetNode(zk_conn, dom_uuid)
|
||||
else:
|
||||
# Verify node is valid
|
||||
valid_node = common.verifyNode(zk_conn, target_node)
|
||||
if not valid_node:
|
||||
return False, 'ERROR: Specified node "{}" is invalid.'.format(target_node)
|
||||
|
||||
# Check if node is within the limit
|
||||
node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(dom_uuid))
|
||||
if node_limit and target_node not in node_limit.split(','):
|
||||
return False, 'ERROR: Specified node "{}" is not in the allowed list of nodes for VM "{}".'.format(target_node, domain)
|
||||
|
||||
# Verify if node is current node
|
||||
if target_node == current_node:
|
||||
return False, 'ERROR: VM "{}" is already running on node "{}".'.format(domain, current_node)
|
||||
|
||||
if not target_node:
|
||||
return False, 'ERROR: Could not find a valid migration target for VM "{}".'.format(domain)
|
||||
|
||||
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_vm_state == 'start':
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/state'.format(dom_uuid): 'migrate',
|
||||
'/domains/{}/node'.format(dom_uuid): target_node,
|
||||
'/domains/{}/lastnode'.format(dom_uuid): ''
|
||||
})
|
||||
else:
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/node'.format(dom_uuid): target_node,
|
||||
'/domains/{}/lastnode'.format(dom_uuid): ''
|
||||
})
|
||||
|
||||
return True, 'Permanently migrating VM "{}" to node "{}".'.format(domain, target_node)
|
||||
|
||||
def migrate_vm(zk_conn, domain, target_node, force_migrate, is_cli=False):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get state and verify we're OK to proceed
|
||||
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_state != 'start':
|
||||
target_state = 'start'
|
||||
else:
|
||||
target_state = 'migrate'
|
||||
|
||||
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
|
||||
last_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
||||
|
||||
if last_node and not force_migrate:
|
||||
if is_cli:
|
||||
click.echo('ERROR: VM "{}" has been previously migrated.'.format(domain))
|
||||
click.echo('> Last node: {}'.format(last_node))
|
||||
click.echo('> Current node: {}'.format(current_node))
|
||||
click.echo('Run `vm unmigrate` to restore the VM to its previous node, or use `--force` to override this check.')
|
||||
return False, ''
|
||||
else:
|
||||
return False, 'ERROR: VM "{}" has been previously migrated.'.format(domain)
|
||||
|
||||
if not target_node:
|
||||
target_node = common.findTargetNode(zk_conn, dom_uuid)
|
||||
else:
|
||||
# Verify node is valid
|
||||
valid_node = common.verifyNode(zk_conn, target_node)
|
||||
if not valid_node:
|
||||
return False, 'ERROR: Specified node "{}" is invalid.'.format(target_node)
|
||||
|
||||
# Check if node is within the limit
|
||||
node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(dom_uuid))
|
||||
if node_limit and target_node not in node_limit.split(','):
|
||||
return False, 'ERROR: Specified node "{}" is not in the allowed list of nodes for VM "{}".'.format(target_node, domain)
|
||||
|
||||
# Verify if node is current node
|
||||
if target_node == current_node:
|
||||
return False, 'ERROR: VM "{}" is already running on node "{}".'.format(domain, current_node)
|
||||
|
||||
if not target_node:
|
||||
return False, 'ERROR: Could not find a valid migration target for VM "{}".'.format(domain)
|
||||
|
||||
# Don't overwrite an existing last_node when using force_migrate
|
||||
if last_node and force_migrate:
|
||||
current_node = last_node
|
||||
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/state'.format(dom_uuid): 'migrate',
|
||||
'/domains/{}/node'.format(dom_uuid): target_node,
|
||||
'/domains/{}/lastnode'.format(dom_uuid): current_node
|
||||
})
|
||||
|
||||
return True, 'Migrating VM "{}" to node "{}".'.format(domain, target_node)
|
||||
|
||||
def unmigrate_vm(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get state and verify we're OK to proceed
|
||||
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
||||
if current_state != 'start':
|
||||
# If the current state isn't start, preserve it; we're not doing live migration
|
||||
target_state = current_state
|
||||
else:
|
||||
target_state = 'migrate'
|
||||
|
||||
target_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
||||
|
||||
if target_node == '':
|
||||
return False, 'ERROR: VM "{}" has not been previously migrated.'.format(domain)
|
||||
|
||||
zkhandler.writedata(zk_conn, {
|
||||
'/domains/{}/state'.format(dom_uuid): target_state,
|
||||
'/domains/{}/node'.format(dom_uuid): target_node,
|
||||
'/domains/{}/lastnode'.format(dom_uuid): ''
|
||||
})
|
||||
|
||||
return True, 'Unmigrating VM "{}" back to node "{}".'.format(domain, target_node)
|
||||
|
||||
def get_console_log(zk_conn, domain, lines=1000):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get the data from ZK
|
||||
console_log = zkhandler.readdata(zk_conn, '/domains/{}/consolelog'.format(dom_uuid))
|
||||
|
||||
# Shrink the log buffer to length lines
|
||||
shrunk_log = console_log.split('\n')[-lines:]
|
||||
loglines = '\n'.join(shrunk_log)
|
||||
|
||||
return True, loglines
|
||||
|
||||
def follow_console_log(zk_conn, domain, lines=10):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
||||
|
||||
# Get the initial data from ZK
|
||||
console_log = zkhandler.readdata(zk_conn, '/domains/{}/consolelog'.format(dom_uuid))
|
||||
|
||||
# Shrink the log buffer to length lines
|
||||
shrunk_log = console_log.split('\n')[-lines:]
|
||||
loglines = '\n'.join(shrunk_log)
|
||||
|
||||
# Print the initial data and begin following
|
||||
print(loglines, end='')
|
||||
|
||||
try:
|
||||
while True:
|
||||
# Grab the next line set
|
||||
new_console_log = zkhandler.readdata(zk_conn, '/domains/{}/consolelog'.format(dom_uuid))
|
||||
# Split the new and old log strings into constitutent lines
|
||||
old_console_loglines = console_log.split('\n')
|
||||
new_console_loglines = new_console_log.split('\n')
|
||||
# Set the console log to the new log value for the next iteration
|
||||
console_log = new_console_log
|
||||
# Remove the lines from the old log until we hit the first line of the new log; this
|
||||
# ensures that the old log is a string that we can remove from the new log entirely
|
||||
for index, line in enumerate(old_console_loglines, start=0):
|
||||
if line == new_console_loglines[0]:
|
||||
del old_console_loglines[0:index]
|
||||
break
|
||||
# Rejoin the log lines into strings
|
||||
old_console_log = '\n'.join(old_console_loglines)
|
||||
new_console_log = '\n'.join(new_console_loglines)
|
||||
# Remove the old lines from the new log
|
||||
diff_console_log = new_console_log.replace(old_console_log, "")
|
||||
# If there's a difference, print it out
|
||||
if diff_console_log:
|
||||
print(diff_console_log, end='')
|
||||
# Wait a second
|
||||
time.sleep(1)
|
||||
except kazoo.exceptions.NoNodeError:
|
||||
return False, 'ERROR: VM has gone away.'
|
||||
except:
|
||||
return False, 'ERROR: Lost connection to Zookeeper node.'
|
||||
|
||||
return True, ''
|
||||
|
||||
def get_info(zk_conn, domain):
|
||||
# Validate that VM exists in cluster
|
||||
dom_uuid = getDomainUUID(zk_conn, domain)
|
||||
if not dom_uuid:
|
||||
return False, 'ERROR: No VM named "{}" is present in the cluster.'.format(domain)
|
||||
|
||||
# Gather information from XML config and print it
|
||||
domain_information = common.getInformationFromXML(zk_conn, dom_uuid)
|
||||
if not domain_information:
|
||||
return False, 'ERROR: Could not get information about VM "{}".'.format(domain)
|
||||
|
||||
return True, domain_information
|
||||
|
||||
def get_list(zk_conn, node, state, limit, is_fuzzy=True):
|
||||
if node:
|
||||
# Verify node is valid
|
||||
if not common.verifyNode(zk_conn, node):
|
||||
return False, 'Specified node "{}" is invalid.'.format(node)
|
||||
|
||||
if state:
|
||||
valid_states = [ 'start', 'restart', 'shutdown', 'stop', 'disable', 'fail', 'migrate', 'unmigrate', 'provision' ]
|
||||
if not state in valid_states:
|
||||
return False, 'VM state "{}" is not valid.'.format(state)
|
||||
|
||||
full_vm_list = zkhandler.listchildren(zk_conn, '/domains')
|
||||
vm_list = []
|
||||
|
||||
# Set our limit to a sensible regex
|
||||
if limit and is_fuzzy:
|
||||
try:
|
||||
# Implcitly assume fuzzy limits
|
||||
if not re.match('\^.*', limit):
|
||||
limit = '.*' + limit
|
||||
if not re.match('.*\$', limit):
|
||||
limit = limit + '.*'
|
||||
except Exception as e:
|
||||
return False, 'Regex Error: {}'.format(e)
|
||||
|
||||
# If we're limited, remove other nodes' VMs
|
||||
vm_node = {}
|
||||
vm_state = {}
|
||||
for vm in full_vm_list:
|
||||
# Check we don't match the limit
|
||||
name = zkhandler.readdata(zk_conn, '/domains/{}'.format(vm))
|
||||
vm_node[vm] = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(vm))
|
||||
vm_state[vm] = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(vm))
|
||||
# Handle limiting
|
||||
if limit:
|
||||
try:
|
||||
if re.match(limit, vm):
|
||||
if not node and not state:
|
||||
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
||||
else:
|
||||
if vm_node[vm] == node or vm_state[vm] == state:
|
||||
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
||||
|
||||
if re.match(limit, name):
|
||||
if not node and not state:
|
||||
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
||||
else:
|
||||
if vm_node[vm] == node or vm_state[vm] == state:
|
||||
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
||||
except Exception as e:
|
||||
return False, 'Regex Error: {}'.format(e)
|
||||
else:
|
||||
# Check node to avoid unneeded ZK calls
|
||||
if not node and not state:
|
||||
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
||||
else:
|
||||
if vm_node[vm] == node or vm_state[vm] == state:
|
||||
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
||||
|
||||
return True, vm_list
|
||||
|
||||
#
|
||||
# CLI-specific functions
|
||||
#
|
||||
def format_info(zk_conn, domain_information, long_output):
|
||||
# Format a nice output; do this line-by-line then concat the elements at the end
|
||||
ainformation = []
|
||||
ainformation.append('{}Virtual machine information:{}'.format(ansiprint.bold(), ansiprint.end()))
|
||||
ainformation.append('')
|
||||
# Basic information
|
||||
ainformation.append('{}UUID:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['uuid']))
|
||||
ainformation.append('{}Name:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['name']))
|
||||
ainformation.append('{}Description:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['description']))
|
||||
ainformation.append('{}Profile:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['profile']))
|
||||
ainformation.append('{}Memory (M):{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['memory']))
|
||||
ainformation.append('{}vCPUs:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['vcpu']))
|
||||
ainformation.append('{}Topology (S/C/T):{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['vcpu_topology']))
|
||||
|
||||
if long_output == True:
|
||||
# Virtualization information
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Emulator:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['emulator']))
|
||||
ainformation.append('{}Type:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['type']))
|
||||
ainformation.append('{}Arch:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['arch']))
|
||||
ainformation.append('{}Machine:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['machine']))
|
||||
ainformation.append('{}Features:{} {}'.format(ansiprint.purple(), ansiprint.end(), ' '.join(domain_information['features'])))
|
||||
|
||||
# PVC cluster information
|
||||
ainformation.append('')
|
||||
dstate_colour = {
|
||||
'start': ansiprint.green(),
|
||||
'restart': ansiprint.yellow(),
|
||||
'shutdown': ansiprint.yellow(),
|
||||
'stop': ansiprint.red(),
|
||||
'disable': ansiprint.blue(),
|
||||
'fail': ansiprint.red(),
|
||||
'migrate': ansiprint.blue(),
|
||||
'unmigrate': ansiprint.blue()
|
||||
}
|
||||
ainformation.append('{}State:{} {}{}{}'.format(ansiprint.purple(), ansiprint.end(), dstate_colour[domain_information['state']], domain_information['state'], ansiprint.end()))
|
||||
ainformation.append('{}Current Node:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['node']))
|
||||
if not domain_information['last_node']:
|
||||
domain_information['last_node'] = "N/A"
|
||||
ainformation.append('{}Previous Node:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['last_node']))
|
||||
|
||||
# Get a failure reason if applicable
|
||||
if domain_information['failed_reason']:
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Failure reason:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['failed_reason']))
|
||||
|
||||
if not domain_information['node_selector']:
|
||||
formatted_node_selector = "False"
|
||||
else:
|
||||
formatted_node_selector = domain_information['node_selector']
|
||||
|
||||
if not domain_information['node_limit']:
|
||||
formatted_node_limit = "False"
|
||||
else:
|
||||
formatted_node_limit = ', '.join(domain_information['node_limit'])
|
||||
|
||||
if not domain_information['node_autostart']:
|
||||
formatted_node_autostart = "False"
|
||||
else:
|
||||
formatted_node_autostart = domain_information['node_autostart']
|
||||
|
||||
ainformation.append('{}Migration selector:{} {}'.format(ansiprint.purple(), ansiprint.end(), formatted_node_selector))
|
||||
ainformation.append('{}Node limit:{} {}'.format(ansiprint.purple(), ansiprint.end(), formatted_node_limit))
|
||||
ainformation.append('{}Autostart:{} {}'.format(ansiprint.purple(), ansiprint.end(), formatted_node_autostart))
|
||||
|
||||
# Network list
|
||||
net_list = []
|
||||
for net in domain_information['networks']:
|
||||
# Split out just the numerical (VNI) part of the brXXXX name
|
||||
net_vnis = re.findall(r'\d+', net['source'])
|
||||
if net_vnis:
|
||||
net_vni = net_vnis[0]
|
||||
else:
|
||||
net_vni = re.sub('br', '', net['source'])
|
||||
net_exists = zkhandler.exists(zk_conn, '/networks/{}'.format(net_vni))
|
||||
if not net_exists and net_vni != 'cluster':
|
||||
net_list.append(ansiprint.red() + net_vni + ansiprint.end() + ' [invalid]')
|
||||
else:
|
||||
net_list.append(net_vni)
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Networks:{} {}'.format(ansiprint.purple(), ansiprint.end(), ', '.join(net_list)))
|
||||
|
||||
if long_output == True:
|
||||
# Disk list
|
||||
ainformation.append('')
|
||||
name_length = 0
|
||||
for disk in domain_information['disks']:
|
||||
_name_length = len(disk['name']) + 1
|
||||
if _name_length > name_length:
|
||||
name_length = _name_length
|
||||
ainformation.append('{0}Disks:{1} {2}ID Type {3: <{width}} Dev Bus{4}'.format(ansiprint.purple(), ansiprint.end(), ansiprint.bold(), 'Name', ansiprint.end(), width=name_length))
|
||||
for disk in domain_information['disks']:
|
||||
ainformation.append(' {0: <3} {1: <5} {2: <{width}} {3: <4} {4: <5}'.format(domain_information['disks'].index(disk), disk['type'], disk['name'], disk['dev'], disk['bus'], width=name_length))
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Interfaces:{} {}ID Type Source Model MAC{}'.format(ansiprint.purple(), ansiprint.end(), ansiprint.bold(), ansiprint.end()))
|
||||
for net in domain_information['networks']:
|
||||
ainformation.append(' {0: <3} {1: <8} {2: <10} {3: <8} {4}'.format(domain_information['networks'].index(net), net['type'], net['source'], net['model'], net['mac']))
|
||||
# Controller list
|
||||
ainformation.append('')
|
||||
ainformation.append('{}Controllers:{} {}ID Type Model{}'.format(ansiprint.purple(), ansiprint.end(), ansiprint.bold(), ansiprint.end()))
|
||||
for controller in domain_information['controllers']:
|
||||
ainformation.append(' {0: <3} {1: <14} {2: <8}'.format(domain_information['controllers'].index(controller), controller['type'], controller['model']))
|
||||
|
||||
# Join it all together
|
||||
information = '\n'.join(ainformation)
|
||||
click.echo(information)
|
||||
|
||||
click.echo('')
|
||||
|
||||
def format_list(zk_conn, vm_list, raw):
|
||||
# Function to strip the "br" off of nets and return a nicer list
|
||||
def getNiceNetID(domain_information):
|
||||
# Network list
|
||||
net_list = []
|
||||
for net in domain_information['networks']:
|
||||
# Split out just the numerical (VNI) part of the brXXXX name
|
||||
net_vnis = re.findall(r'\d+', net['source'])
|
||||
if net_vnis:
|
||||
net_vni = net_vnis[0]
|
||||
else:
|
||||
net_vni = re.sub('br', '', net['source'])
|
||||
net_list.append(net_vni)
|
||||
return net_list
|
||||
|
||||
# Handle raw mode since it just lists the names
|
||||
if raw:
|
||||
for vm in sorted(item['name'] for item in vm_list):
|
||||
click.echo(vm)
|
||||
return True, ''
|
||||
|
||||
vm_list_output = []
|
||||
|
||||
# Determine optimal column widths
|
||||
# Dynamic columns: node_name, node, migrated
|
||||
vm_name_length = 5
|
||||
vm_uuid_length = 37
|
||||
vm_state_length = 6
|
||||
vm_nets_length = 9
|
||||
vm_ram_length = 8
|
||||
vm_vcpu_length = 6
|
||||
vm_node_length = 8
|
||||
vm_migrated_length = 10
|
||||
for domain_information in vm_list:
|
||||
net_list = getNiceNetID(domain_information)
|
||||
# vm_name column
|
||||
_vm_name_length = len(domain_information['name']) + 1
|
||||
if _vm_name_length > vm_name_length:
|
||||
vm_name_length = _vm_name_length
|
||||
# vm_state column
|
||||
_vm_state_length = len(domain_information['state']) + 1
|
||||
if _vm_state_length > vm_state_length:
|
||||
vm_state_length = _vm_state_length
|
||||
# vm_nets column
|
||||
_vm_nets_length = len(','.join(net_list)) + 1
|
||||
if _vm_nets_length > vm_nets_length:
|
||||
vm_nets_length = _vm_nets_length
|
||||
# vm_node column
|
||||
_vm_node_length = len(domain_information['node']) + 1
|
||||
if _vm_node_length > vm_node_length:
|
||||
vm_node_length = _vm_node_length
|
||||
# vm_migrated column
|
||||
_vm_migrated_length = len(domain_information['migrated']) + 1
|
||||
if _vm_migrated_length > vm_migrated_length:
|
||||
vm_migrated_length = _vm_migrated_length
|
||||
|
||||
# Format the string (header)
|
||||
vm_list_output.append(
|
||||
'{bold}{vm_name: <{vm_name_length}} {vm_uuid: <{vm_uuid_length}} \
|
||||
{vm_state_colour}{vm_state: <{vm_state_length}}{end_colour} \
|
||||
{vm_networks: <{vm_nets_length}} \
|
||||
{vm_memory: <{vm_ram_length}} {vm_vcpu: <{vm_vcpu_length}} \
|
||||
{vm_node: <{vm_node_length}} \
|
||||
{vm_migrated: <{vm_migrated_length}}{end_bold}'.format(
|
||||
vm_name_length=vm_name_length,
|
||||
vm_uuid_length=vm_uuid_length,
|
||||
vm_state_length=vm_state_length,
|
||||
vm_nets_length=vm_nets_length,
|
||||
vm_ram_length=vm_ram_length,
|
||||
vm_vcpu_length=vm_vcpu_length,
|
||||
vm_node_length=vm_node_length,
|
||||
vm_migrated_length=vm_migrated_length,
|
||||
bold=ansiprint.bold(),
|
||||
end_bold=ansiprint.end(),
|
||||
vm_state_colour='',
|
||||
end_colour='',
|
||||
vm_name='Name',
|
||||
vm_uuid='UUID',
|
||||
vm_state='State',
|
||||
vm_networks='Networks',
|
||||
vm_memory='RAM (M)',
|
||||
vm_vcpu='vCPUs',
|
||||
vm_node='Node',
|
||||
vm_migrated='Migrated'
|
||||
)
|
||||
)
|
||||
|
||||
# Format the string (elements)
|
||||
for domain_information in vm_list:
|
||||
if domain_information['state'] == 'start':
|
||||
vm_state_colour = ansiprint.green()
|
||||
elif domain_information['state'] == 'restart':
|
||||
vm_state_colour = ansiprint.yellow()
|
||||
elif domain_information['state'] == 'shutdown':
|
||||
vm_state_colour = ansiprint.yellow()
|
||||
elif domain_information['state'] == 'stop':
|
||||
vm_state_colour = ansiprint.red()
|
||||
elif domain_information['state'] == 'fail':
|
||||
vm_state_colour = ansiprint.red()
|
||||
else:
|
||||
vm_state_colour = ansiprint.blue()
|
||||
|
||||
# Handle colouring for an invalid network config
|
||||
raw_net_list = getNiceNetID(domain_information)
|
||||
net_list = []
|
||||
vm_net_colour = ''
|
||||
for net_vni in raw_net_list:
|
||||
net_exists = zkhandler.exists(zk_conn, '/networks/{}'.format(net_vni))
|
||||
if not net_exists and net_vni != 'cluster':
|
||||
vm_net_colour = ansiprint.red()
|
||||
net_list.append(net_vni)
|
||||
|
||||
vm_list_output.append(
|
||||
'{bold}{vm_name: <{vm_name_length}} {vm_uuid: <{vm_uuid_length}} \
|
||||
{vm_state_colour}{vm_state: <{vm_state_length}}{end_colour} \
|
||||
{vm_net_colour}{vm_networks: <{vm_nets_length}}{end_colour} \
|
||||
{vm_memory: <{vm_ram_length}} {vm_vcpu: <{vm_vcpu_length}} \
|
||||
{vm_node: <{vm_node_length}} \
|
||||
{vm_migrated: <{vm_migrated_length}}{end_bold}'.format(
|
||||
vm_name_length=vm_name_length,
|
||||
vm_uuid_length=vm_uuid_length,
|
||||
vm_state_length=vm_state_length,
|
||||
vm_nets_length=vm_nets_length,
|
||||
vm_ram_length=vm_ram_length,
|
||||
vm_vcpu_length=vm_vcpu_length,
|
||||
vm_node_length=vm_node_length,
|
||||
vm_migrated_length=vm_migrated_length,
|
||||
bold='',
|
||||
end_bold='',
|
||||
vm_state_colour=vm_state_colour,
|
||||
end_colour=ansiprint.end(),
|
||||
vm_name=domain_information['name'],
|
||||
vm_uuid=domain_information['uuid'],
|
||||
vm_state=domain_information['state'],
|
||||
vm_net_colour=vm_net_colour,
|
||||
vm_networks=','.join(net_list),
|
||||
vm_memory=domain_information['memory'],
|
||||
vm_vcpu=domain_information['vcpu'],
|
||||
vm_node=domain_information['node'],
|
||||
vm_migrated=domain_information['migrated']
|
||||
)
|
||||
)
|
||||
|
||||
click.echo('\n'.join(sorted(vm_list_output)))
|
||||
|
||||
return True, ''
|
||||
|
100
daemon-common/zkhandler.py
Normal file
100
daemon-common/zkhandler.py
Normal file
@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# zkhandler.py - Secure versioned ZooKeeper updates
|
||||
# Part of the Parallel Virtual Cluster (PVC) system
|
||||
#
|
||||
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
###############################################################################
|
||||
|
||||
import kazoo.client
|
||||
import uuid
|
||||
|
||||
import daemon_lib.ansiprint as ansiprint
|
||||
|
||||
# Exists function
|
||||
def exists(zk_conn, key):
|
||||
stat = zk_conn.exists(key)
|
||||
if stat:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
# Child list function
|
||||
def listchildren(zk_conn, key):
|
||||
children = zk_conn.get_children(key)
|
||||
return children
|
||||
|
||||
# Delete key function
|
||||
def deletekey(zk_conn, key, recursive=True):
|
||||
zk_conn.delete(key, recursive=recursive)
|
||||
|
||||
# Data read function
|
||||
def readdata(zk_conn, key):
|
||||
data_raw = zk_conn.get(key)
|
||||
data = data_raw[0].decode('utf8')
|
||||
meta = data_raw[1]
|
||||
return data
|
||||
|
||||
# Data write function
|
||||
def writedata(zk_conn, kv):
|
||||
# Start up a transaction
|
||||
zk_transaction = zk_conn.transaction()
|
||||
|
||||
# Proceed one KV pair at a time
|
||||
for key in sorted(kv):
|
||||
data = kv[key]
|
||||
|
||||
# Check if this key already exists or not
|
||||
if not zk_conn.exists(key):
|
||||
# We're creating a new key
|
||||
zk_transaction.create(key, str(data).encode('utf8'))
|
||||
else:
|
||||
# We're updating a key with version validation
|
||||
orig_data = zk_conn.get(key)
|
||||
version = orig_data[1].version
|
||||
|
||||
# Set what we expect the new version to be
|
||||
new_version = version + 1
|
||||
|
||||
# Update the data
|
||||
zk_transaction.set_data(key, str(data).encode('utf8'))
|
||||
|
||||
# Set up the check
|
||||
try:
|
||||
zk_transaction.check(key, new_version)
|
||||
except TypeError:
|
||||
print('Zookeeper key "{}" does not match expected version'.format(key))
|
||||
return False
|
||||
|
||||
# Commit the transaction
|
||||
try:
|
||||
zk_transaction.commit()
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
# Write lock function
|
||||
def writelock(zk_conn, key):
|
||||
lock_id = str(uuid.uuid1())
|
||||
lock = zk_conn.WriteLock('{}'.format(key), lock_id)
|
||||
return lock
|
||||
|
||||
# Read lock function
|
||||
def readlock(zk_conn, key):
|
||||
lock_id = str(uuid.uuid1())
|
||||
lock = zk_conn.ReadLock('{}'.format(key), lock_id)
|
||||
return lock
|
Reference in New Issue
Block a user