2018-09-20 03:25:58 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# vm.py - PVC client function library, VM fuctions
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2020-01-08 19:38:02 -05:00
|
|
|
# Copyright (C) 2018-2020 Joshua M. Boniface <joshua@boniface.me>
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
import time
|
|
|
|
import re
|
|
|
|
import lxml.objectify
|
2019-04-11 19:06:06 -04:00
|
|
|
|
2020-02-08 18:48:59 -05:00
|
|
|
import daemon_lib.ansiprint as ansiprint
|
|
|
|
import daemon_lib.zkhandler as zkhandler
|
|
|
|
import daemon_lib.common as common
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-08 18:48:59 -05:00
|
|
|
import daemon_lib.ceph as ceph
|
2019-06-27 11:19:48 -04:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Cluster search functions
|
|
|
|
#
|
|
|
|
def getClusterDomainList(zk_conn):
|
|
|
|
# Get a list of UUIDs by listing the children of /domains
|
2018-10-27 15:27:08 -04:00
|
|
|
uuid_list = zkhandler.listchildren(zk_conn, '/domains')
|
2018-09-20 03:25:58 -04:00
|
|
|
name_list = []
|
|
|
|
# For each UUID, get the corresponding name from the data
|
|
|
|
for uuid in uuid_list:
|
2018-10-27 15:27:08 -04:00
|
|
|
name_list.append(zkhandler.readdata(zk_conn, '/domains/%s' % uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
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):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
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
|
|
|
|
|
2018-09-25 02:26:37 -04:00
|
|
|
def getDomainName(zk_conn, domain):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-25 02:26:37 -04:00
|
|
|
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
|
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
#
|
|
|
|
# Direct functions
|
|
|
|
#
|
2019-07-25 14:33:50 -04:00
|
|
|
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
|
2019-07-25 15:45:45 -04:00
|
|
|
else:
|
2019-07-25 14:33:50 -04:00
|
|
|
return False
|
|
|
|
|
2019-08-07 13:38:49 -04:00
|
|
|
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
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-08-07 13:38:49 -04:00
|
|
|
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
|
|
|
|
|
2020-10-29 11:31:32 -04:00
|
|
|
def define_vm(zk_conn, config_data, target_node, node_limit, node_selector, node_autostart, migration_method=None, profile=None, initial_state='stop'):
|
2018-09-20 03:25:58 -04:00
|
|
|
# Parse the XML data
|
2019-07-09 10:22:23 -04:00
|
|
|
try:
|
|
|
|
parsed_xml = lxml.objectify.fromstring(config_data)
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2019-07-09 10:22:23 -04:00
|
|
|
return False, 'ERROR: Failed to parse XML data.'
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = parsed_xml.uuid.text
|
|
|
|
dom_name = parsed_xml.name.text
|
|
|
|
|
2019-12-09 09:56:59 -05:00
|
|
|
# Ensure that the UUID and name are unique
|
2019-12-09 11:31:56 -05:00
|
|
|
if searchClusterByUUID(zk_conn, dom_uuid) or searchClusterByName(zk_conn, dom_name):
|
2019-12-09 09:56:59 -05:00
|
|
|
return False, 'ERROR: Specified VM "{}" or UUID "{}" matches an existing VM on the cluster'.format(dom_name, dom_uuid)
|
|
|
|
|
2019-06-24 13:37:56 -04:00
|
|
|
if not target_node:
|
2019-10-12 02:03:23 -04:00
|
|
|
target_node = common.findTargetNode(zk_conn, dom_uuid)
|
2019-06-24 13:25:24 -04:00
|
|
|
else:
|
|
|
|
# Verify node is valid
|
|
|
|
valid_node = common.verifyNode(zk_conn, target_node)
|
|
|
|
if not valid_node:
|
2019-07-09 10:22:23 -04:00
|
|
|
return False, 'ERROR: Specified node "{}" is invalid.'.format(target_node)
|
|
|
|
|
|
|
|
# Obtain the RBD disk list using the common functions
|
2020-06-07 00:40:21 -04:00
|
|
|
ddisks = common.getDomainDisks(parsed_xml, {})
|
2019-07-09 10:22:23 -04:00
|
|
|
rbd_list = []
|
|
|
|
for disk in ddisks:
|
|
|
|
if disk['type'] == 'rbd':
|
|
|
|
rbd_list.append(disk['name'])
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-12-19 12:03:46 -05:00
|
|
|
# Join the limit
|
2019-12-19 13:22:38 -05:00
|
|
|
if isinstance(node_limit, list) and node_limit:
|
2019-12-19 12:03:46 -05:00
|
|
|
formatted_node_limit = ','.join(node_limit)
|
|
|
|
else:
|
2019-12-19 13:22:38 -05:00
|
|
|
formatted_node_limit = ''
|
2019-12-19 12:03:46 -05:00
|
|
|
|
|
|
|
# Join the RBD list
|
2019-12-19 13:22:38 -05:00
|
|
|
if isinstance(rbd_list, list) and rbd_list:
|
2019-12-19 12:03:46 -05:00
|
|
|
formatted_rbd_list = ','.join(rbd_list)
|
|
|
|
else:
|
2019-12-19 13:22:38 -05:00
|
|
|
formatted_rbd_list = ''
|
2019-12-19 12:03:46 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# Add the new domain to Zookeeper
|
2018-10-27 15:27:08 -04:00
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}'.format(dom_uuid): dom_name,
|
2020-01-08 17:46:30 -05:00
|
|
|
'/domains/{}/state'.format(dom_uuid): initial_state,
|
2018-10-27 15:27:08 -04:00
|
|
|
'/domains/{}/node'.format(dom_uuid): target_node,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): '',
|
2019-12-19 12:03:46 -05:00
|
|
|
'/domains/{}/node_limit'.format(dom_uuid): formatted_node_limit,
|
2019-10-12 01:17:39 -04:00
|
|
|
'/domains/{}/node_selector'.format(dom_uuid): node_selector,
|
|
|
|
'/domains/{}/node_autostart'.format(dom_uuid): node_autostart,
|
2020-10-29 11:31:32 -04:00
|
|
|
'/domains/{}/migration_method'.format(dom_uuid): migration_method,
|
2018-10-27 15:27:08 -04:00
|
|
|
'/domains/{}/failedreason'.format(dom_uuid): '',
|
2019-04-11 19:06:06 -04:00
|
|
|
'/domains/{}/consolelog'.format(dom_uuid): '',
|
2019-12-19 12:03:46 -05:00
|
|
|
'/domains/{}/rbdlist'.format(dom_uuid): formatted_rbd_list,
|
2019-12-11 16:49:11 -05:00
|
|
|
'/domains/{}/profile'.format(dom_uuid): profile,
|
2018-10-27 15:27:08 -04:00
|
|
|
'/domains/{}/xml'.format(dom_uuid): config_data
|
|
|
|
})
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-05-20 22:15:28 -04:00
|
|
|
return True, 'Added new VM with Name "{}" and UUID "{}" to database.'.format(dom_name, dom_uuid)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-10-29 11:31:32 -04:00
|
|
|
def modify_vm_metadata(zk_conn, domain, node_limit, node_selector, node_autostart, provisioner_profile, migration_method):
|
2019-10-12 01:17:39 -04:00
|
|
|
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:
|
2019-12-26 19:08:26 -05:00
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/node_limit'.format(dom_uuid): node_limit
|
|
|
|
})
|
2019-10-12 01:17:39 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2020-01-30 11:45:46 -05:00
|
|
|
if provisioner_profile is not None:
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/profile'.format(dom_uuid): provisioner_profile
|
|
|
|
})
|
|
|
|
|
2020-10-29 11:31:32 -04:00
|
|
|
if migration_method is not None:
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/migration_method'.format(dom_uuid): migration_method
|
|
|
|
})
|
|
|
|
|
2019-10-12 01:17:39 -04:00
|
|
|
return True, 'Successfully modified PVC metadata of VM "{}".'.format(domain)
|
|
|
|
|
2018-09-25 02:32:08 -04:00
|
|
|
def modify_vm(zk_conn, domain, restart, new_vm_config):
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
2018-09-25 02:32:08 -04:00
|
|
|
dom_name = getDomainName(zk_conn, domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-05-12 11:07:58 -04:00
|
|
|
# Parse and valiate the XML
|
|
|
|
try:
|
|
|
|
parsed_xml = lxml.objectify.fromstring(new_vm_config)
|
2020-11-06 19:24:10 -05:00
|
|
|
except Exception:
|
2020-05-12 11:07:58 -04:00
|
|
|
return False, 'ERROR: Failed to parse XML data.'
|
|
|
|
|
|
|
|
# Obtain the RBD disk list using the common functions
|
2020-06-07 00:40:21 -04:00
|
|
|
ddisks = common.getDomainDisks(parsed_xml, {})
|
2020-05-12 11:07:58 -04:00
|
|
|
rbd_list = []
|
|
|
|
for disk in ddisks:
|
|
|
|
if disk['type'] == 'rbd':
|
|
|
|
rbd_list.append(disk['name'])
|
|
|
|
|
|
|
|
# Join the RBD list
|
|
|
|
if isinstance(rbd_list, list) and rbd_list:
|
|
|
|
formatted_rbd_list = ','.join(rbd_list)
|
|
|
|
else:
|
|
|
|
formatted_rbd_list = ''
|
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# Add the modified config to Zookeeper
|
2018-10-27 15:27:08 -04:00
|
|
|
zk_data = {
|
|
|
|
'/domains/{}'.format(dom_uuid): dom_name,
|
2020-05-12 11:07:58 -04:00
|
|
|
'/domains/{}/rbdlist'.format(dom_uuid): formatted_rbd_list,
|
2018-10-27 15:27:08 -04:00
|
|
|
'/domains/{}/xml'.format(dom_uuid): new_vm_config
|
|
|
|
}
|
|
|
|
zkhandler.writedata(zk_conn, zk_data)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-01-30 11:15:07 -05:00
|
|
|
if restart:
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'restart'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2020-01-30 11:15:07 -05:00
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
return True, ''
|
|
|
|
|
2019-03-12 21:09:54 -04:00
|
|
|
def dump_vm(zk_conn, domain):
|
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2019-03-12 21:09:54 -04:00
|
|
|
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))
|
|
|
|
|
2019-05-20 22:15:28 -04:00
|
|
|
return True, vm_xml
|
2019-03-12 21:09:54 -04:00
|
|
|
|
2020-02-19 10:18:41 -05:00
|
|
|
def undefine_vm(zk_conn, domain):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Shut down the VM
|
2019-07-06 01:42:55 -04:00
|
|
|
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
2019-06-27 11:19:48 -04:00
|
|
|
if current_vm_state != 'stop':
|
|
|
|
# Set the domain into stop mode
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 10:18:41 -05:00
|
|
|
# Wait for 2 seconds to allow state to flow to all nodes
|
2019-05-20 22:15:28 -04:00
|
|
|
time.sleep(2)
|
2019-06-27 11:19:48 -04:00
|
|
|
|
|
|
|
# Gracefully terminate the class instances
|
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'delete'})
|
|
|
|
time.sleep(2)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
# Delete the configurations
|
2019-06-27 11:19:48 -04:00
|
|
|
zkhandler.deletekey(zk_conn, '/domains/{}'.format(dom_uuid))
|
|
|
|
|
|
|
|
return True, 'Undefined VM "{}" from the cluster.'.format(domain)
|
|
|
|
|
2020-02-19 10:18:41 -05:00
|
|
|
def remove_vm(zk_conn, domain):
|
2019-06-27 11:19:48 -04:00
|
|
|
# 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)
|
|
|
|
|
2019-07-09 09:29:47 -04:00
|
|
|
disk_list = common.getDomainDiskList(zk_conn, dom_uuid)
|
2019-06-27 11:19:48 -04:00
|
|
|
|
|
|
|
# Shut down the VM
|
|
|
|
current_vm_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
if current_vm_state != 'stop':
|
|
|
|
# Set the domain into stop mode
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2019-06-27 11:19:48 -04:00
|
|
|
|
2020-02-19 10:18:41 -05:00
|
|
|
# Wait for 2 seconds to allow state to flow to all nodes
|
2019-06-27 11:19:48 -04:00
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
# Gracefully terminate the class instances
|
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'delete'})
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
# Delete the configurations
|
|
|
|
zkhandler.deletekey(zk_conn, '/domains/{}'.format(dom_uuid))
|
2019-07-10 19:05:36 -04:00
|
|
|
time.sleep(2)
|
2019-06-27 11:19:48 -04:00
|
|
|
|
|
|
|
# Remove disks
|
|
|
|
for disk in disk_list:
|
|
|
|
# vmpool/vmname_volume
|
2019-07-31 23:05:00 -04:00
|
|
|
try:
|
|
|
|
disk_pool, disk_name = disk.split('/')
|
|
|
|
retcode, message = ceph.remove_volume(zk_conn, disk_pool, disk_name)
|
|
|
|
except ValueError:
|
|
|
|
continue
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-06-27 11:19:48 -04:00
|
|
|
return True, 'Removed VM "{}" and disks from the cluster.'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
def start_vm(zk_conn, domain):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Set the VM to start
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'start'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-07-05 16:38:54 -04:00
|
|
|
return True, 'Starting VM "{}".'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
def restart_vm(zk_conn, domain, wait=False):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Get state and verify we're OK to proceed
|
2018-10-27 15:27:08 -04:00
|
|
|
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
if current_state != 'start':
|
2019-07-05 16:38:54 -04:00
|
|
|
return False, 'ERROR: VM "{}" is not in "start" state!'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
retmsg = 'Restarting VM "{}".'.format(domain)
|
|
|
|
|
|
|
|
# Set the VM to restart
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'restart'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
if wait:
|
|
|
|
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == 'restart':
|
|
|
|
time.sleep(1)
|
|
|
|
retmsg = 'Restarted VM "{}"'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
return True, retmsg
|
|
|
|
|
|
|
|
def shutdown_vm(zk_conn, domain, wait=False):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Get state and verify we're OK to proceed
|
2018-10-27 15:27:08 -04:00
|
|
|
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
if current_state != 'start':
|
2019-07-05 16:38:54 -04:00
|
|
|
return False, 'ERROR: VM "{}" is not in "start" state!'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
retmsg = 'Shutting down VM "{}"'.format(domain)
|
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# Set the VM to shutdown
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'shutdown'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2020-02-19 09:45:31 -05:00
|
|
|
|
|
|
|
if wait:
|
|
|
|
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == 'shutdown':
|
|
|
|
time.sleep(1)
|
2020-02-19 10:04:58 -05:00
|
|
|
retmsg = 'Shut down VM "{}"'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
return True, retmsg
|
2018-09-20 03:25:58 -04:00
|
|
|
|
|
|
|
def stop_vm(zk_conn, domain):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Set the VM to start
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'stop'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-07-05 16:38:54 -04:00
|
|
|
return True, 'Forcibly stopping VM "{}".'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-10-23 23:37:42 -04:00
|
|
|
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
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
2020-11-07 12:57:42 -05:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/state'.format(dom_uuid): 'disable'})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2019-10-23 23:37:42 -04:00
|
|
|
|
2019-10-23 23:53:25 -04:00
|
|
|
return True, 'Marked VM "{}" as disable.'.format(domain)
|
2019-10-23 23:37:42 -04:00
|
|
|
|
2020-06-06 11:49:21 -04:00
|
|
|
def move_vm(zk_conn, domain, target_node, wait=False, force_live=False):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
# 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:
|
2020-06-06 11:49:21 -04:00
|
|
|
if force_live:
|
|
|
|
target_state = 'migrate-live'
|
|
|
|
else:
|
|
|
|
target_state = 'migrate'
|
2020-02-19 09:45:31 -05:00
|
|
|
|
2018-10-27 15:27:08 -04:00
|
|
|
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-06-24 13:37:56 -04:00
|
|
|
if not target_node:
|
2019-10-12 01:45:44 -04:00
|
|
|
target_node = common.findTargetNode(zk_conn, dom_uuid)
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
2019-06-24 13:25:24 -04:00
|
|
|
# Verify node is valid
|
|
|
|
valid_node = common.verifyNode(zk_conn, target_node)
|
|
|
|
if not valid_node:
|
2019-10-12 01:50:15 -04:00
|
|
|
return False, 'ERROR: Specified node "{}" is invalid.'.format(target_node)
|
2019-06-24 13:25:24 -04:00
|
|
|
|
2019-10-12 01:45:44 -04:00
|
|
|
# Check if node is within the limit
|
2020-01-05 13:42:23 -05:00
|
|
|
node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(dom_uuid))
|
|
|
|
if node_limit and target_node not in node_limit.split(','):
|
2019-10-12 01:50:15 -04:00
|
|
|
return False, 'ERROR: Specified node "{}" is not in the allowed list of nodes for VM "{}".'.format(target_node, domain)
|
2019-10-12 01:45:44 -04:00
|
|
|
|
2019-06-24 13:25:24 -04:00
|
|
|
# Verify if node is current node
|
2018-10-14 02:01:35 -04:00
|
|
|
if target_node == current_node:
|
2020-06-06 11:21:58 -04:00
|
|
|
last_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
|
|
|
if last_node:
|
2020-08-05 10:34:30 -04:00
|
|
|
zkhandler.writedata(zk_conn, {'/domains/{}/lastnode'.format(dom_uuid): ''})
|
2020-06-06 11:21:58 -04:00
|
|
|
return True, 'Making temporary migration permanent for VM "{}".'.format(domain)
|
|
|
|
|
2019-07-05 16:38:54 -04:00
|
|
|
return False, 'ERROR: VM "{}" is already running on node "{}".'.format(domain, current_node)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-10-17 12:16:21 -04:00
|
|
|
if not target_node:
|
|
|
|
return False, 'ERROR: Could not find a valid migration target for VM "{}".'.format(domain)
|
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
retmsg = 'Permanently migrating VM "{}" to node "{}".'.format(domain, target_node)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
|
|
|
zkhandler.writedata(zk_conn, {
|
2020-10-21 10:40:00 -04:00
|
|
|
'/domains/{}/state'.format(dom_uuid): target_state,
|
|
|
|
'/domains/{}/node'.format(dom_uuid): target_node,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): ''
|
|
|
|
})
|
2020-10-21 11:32:40 -04:00
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
if wait:
|
2020-02-19 10:18:41 -05:00
|
|
|
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == target_state:
|
2020-02-19 09:45:31 -05:00
|
|
|
time.sleep(1)
|
|
|
|
retmsg = 'Permanently migrated VM "{}" to node "{}"'.format(domain, target_node)
|
|
|
|
|
|
|
|
return True, retmsg
|
|
|
|
|
2020-06-06 11:49:21 -04:00
|
|
|
def migrate_vm(zk_conn, domain, target_node, force_migrate, wait=False, force_live=False):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Get state and verify we're OK to proceed
|
2018-10-27 15:27:08 -04:00
|
|
|
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
if current_state != 'start':
|
2020-02-19 09:45:31 -05:00
|
|
|
# If the current state isn't start, preserve it; we're not doing live migration
|
|
|
|
target_state = current_state
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
2020-06-06 11:49:21 -04:00
|
|
|
if force_live:
|
|
|
|
target_state = 'migrate-live'
|
|
|
|
else:
|
|
|
|
target_state = 'migrate'
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-27 15:27:08 -04:00
|
|
|
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
|
|
|
|
last_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-06-24 13:37:56 -04:00
|
|
|
if last_node and not force_migrate:
|
2020-02-19 10:18:41 -05:00
|
|
|
return False, 'ERROR: VM "{}" has been previously migrated.'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-06-24 13:37:56 -04:00
|
|
|
if not target_node:
|
2019-10-12 01:45:44 -04:00
|
|
|
target_node = common.findTargetNode(zk_conn, dom_uuid)
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
2019-06-24 13:25:24 -04:00
|
|
|
# Verify node is valid
|
|
|
|
valid_node = common.verifyNode(zk_conn, target_node)
|
|
|
|
if not valid_node:
|
2019-10-12 01:50:15 -04:00
|
|
|
return False, 'ERROR: Specified node "{}" is invalid.'.format(target_node)
|
2019-06-24 13:25:24 -04:00
|
|
|
|
2019-10-12 01:45:44 -04:00
|
|
|
# Check if node is within the limit
|
2020-01-05 13:42:23 -05:00
|
|
|
node_limit = zkhandler.readdata(zk_conn, '/domains/{}/node_limit'.format(dom_uuid))
|
|
|
|
if node_limit and target_node not in node_limit.split(','):
|
2019-10-12 01:50:15 -04:00
|
|
|
return False, 'ERROR: Specified node "{}" is not in the allowed list of nodes for VM "{}".'.format(target_node, domain)
|
2019-10-12 01:45:44 -04:00
|
|
|
|
2019-06-24 13:25:24 -04:00
|
|
|
# Verify if node is current node
|
2018-10-14 02:01:35 -04:00
|
|
|
if target_node == current_node:
|
2019-07-05 16:38:54 -04:00
|
|
|
return False, 'ERROR: VM "{}" is already running on node "{}".'.format(domain, current_node)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-10-17 12:16:21 -04:00
|
|
|
if not target_node:
|
|
|
|
return False, 'ERROR: Could not find a valid migration target for VM "{}".'.format(domain)
|
|
|
|
|
2019-07-07 15:10:48 -04:00
|
|
|
# Don't overwrite an existing last_node when using force_migrate
|
|
|
|
if last_node and force_migrate:
|
|
|
|
current_node = last_node
|
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
retmsg = 'Migrating VM "{}" to node "{}".'.format(domain, target_node)
|
|
|
|
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): target_state,
|
|
|
|
'/domains/{}/node'.format(dom_uuid): target_node,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): current_node
|
|
|
|
})
|
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
if wait:
|
2020-02-19 10:18:41 -05:00
|
|
|
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == target_state:
|
2020-02-19 09:45:31 -05:00
|
|
|
time.sleep(1)
|
|
|
|
retmsg = 'Migrated VM "{}" to node "{}"'.format(domain, target_node)
|
|
|
|
|
|
|
|
return True, retmsg
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-06-06 11:49:21 -04:00
|
|
|
def unmigrate_vm(zk_conn, domain, wait=False, force_live=False):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2018-09-20 03:25:58 -04:00
|
|
|
dom_uuid = getDomainUUID(zk_conn, domain)
|
2019-04-11 19:06:06 -04:00
|
|
|
if not dom_uuid:
|
2018-09-20 03:25:58 -04:00
|
|
|
return False, 'ERROR: Could not find VM "{}" in the cluster!'.format(domain)
|
|
|
|
|
|
|
|
# Get state and verify we're OK to proceed
|
2018-10-27 15:27:08 -04:00
|
|
|
current_state = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
if current_state != 'start':
|
2019-03-20 10:19:01 -04:00
|
|
|
# If the current state isn't start, preserve it; we're not doing live migration
|
|
|
|
target_state = current_state
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
2020-06-06 11:49:21 -04:00
|
|
|
if force_live:
|
|
|
|
target_state = 'migrate-live'
|
|
|
|
else:
|
|
|
|
target_state = 'migrate'
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-27 15:27:08 -04:00
|
|
|
target_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
if target_node == '':
|
2019-07-05 16:38:54 -04:00
|
|
|
return False, 'ERROR: VM "{}" has not been previously migrated.'.format(domain)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
retmsg = 'Unmigrating VM "{}" back to node "{}".'.format(domain, target_node)
|
|
|
|
|
2020-10-21 11:32:40 -04:00
|
|
|
lock = zkhandler.exclusivelock(zk_conn, '/domains/{}/state'.format(dom_uuid))
|
|
|
|
lock.acquire()
|
|
|
|
zkhandler.writedata(zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): target_state,
|
|
|
|
'/domains/{}/node'.format(dom_uuid): target_node,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): ''
|
|
|
|
})
|
|
|
|
lock.release()
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2020-02-19 09:45:31 -05:00
|
|
|
if wait:
|
2020-02-19 10:18:41 -05:00
|
|
|
while zkhandler.readdata(zk_conn, '/domains/{}/state'.format(dom_uuid)) == target_state:
|
2020-02-19 09:45:31 -05:00
|
|
|
time.sleep(1)
|
|
|
|
retmsg = 'Unmigrated VM "{}" back to node "{}"'.format(domain, target_node)
|
|
|
|
|
|
|
|
return True, retmsg
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-04-11 19:06:06 -04:00
|
|
|
def get_console_log(zk_conn, domain, lines=1000):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2019-04-11 19:06:06 -04:00
|
|
|
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)
|
|
|
|
|
2019-12-25 19:10:12 -05:00
|
|
|
return True, loglines
|
2019-04-11 19:06:06 -04:00
|
|
|
|
2019-05-20 22:15:28 -04:00
|
|
|
def get_info(zk_conn, domain):
|
2019-06-27 11:19:48 -04:00
|
|
|
# Validate that VM exists in cluster
|
2019-05-20 22:15:28 -04:00
|
|
|
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
|
2019-07-09 09:29:47 -04:00
|
|
|
domain_information = common.getInformationFromXML(zk_conn, dom_uuid)
|
2019-06-24 13:37:56 -04:00
|
|
|
if not domain_information:
|
2019-05-20 22:15:28 -04:00
|
|
|
return False, 'ERROR: Could not get information about VM "{}".'.format(domain)
|
|
|
|
|
|
|
|
return True, domain_information
|
|
|
|
|
2019-07-05 16:28:18 -04:00
|
|
|
def get_list(zk_conn, node, state, limit, is_fuzzy=True):
|
2019-06-24 13:37:56 -04:00
|
|
|
if node:
|
2018-09-20 03:25:58 -04:00
|
|
|
# Verify node is valid
|
2019-07-05 14:18:18 -04:00
|
|
|
if not common.verifyNode(zk_conn, node):
|
2019-07-08 22:37:26 -04:00
|
|
|
return False, 'Specified node "{}" is invalid.'.format(node)
|
2018-09-20 03:25:58 -04:00
|
|
|
|
2019-06-24 13:37:56 -04:00
|
|
|
if state:
|
2020-11-07 13:02:54 -05:00
|
|
|
valid_states = ['start', 'restart', 'shutdown', 'stop', 'disable', 'fail', 'migrate', 'unmigrate', 'provision']
|
2020-11-06 20:37:52 -05:00
|
|
|
if state not in valid_states:
|
2019-03-20 11:31:54 -04:00
|
|
|
return False, 'VM state "{}" is not valid.'.format(state)
|
|
|
|
|
2018-10-27 15:27:08 -04:00
|
|
|
full_vm_list = zkhandler.listchildren(zk_conn, '/domains')
|
2018-09-20 03:25:58 -04:00
|
|
|
vm_list = []
|
|
|
|
|
2019-03-12 23:52:59 -04:00
|
|
|
# Set our limit to a sensible regex
|
2019-07-05 16:28:18 -04:00
|
|
|
if limit and is_fuzzy:
|
2019-03-12 23:52:59 -04:00
|
|
|
try:
|
|
|
|
# Implcitly assume fuzzy limits
|
2020-11-06 18:59:30 -05:00
|
|
|
if not re.match('[^].*', limit):
|
2019-03-12 23:52:59 -04:00
|
|
|
limit = '.*' + limit
|
2020-11-06 19:01:22 -05:00
|
|
|
if not re.match('.*[$]', limit):
|
2019-03-12 23:52:59 -04:00
|
|
|
limit = limit + '.*'
|
|
|
|
except Exception as e:
|
|
|
|
return False, 'Regex Error: {}'.format(e)
|
|
|
|
|
2018-09-20 03:25:58 -04:00
|
|
|
# If we're limited, remove other nodes' VMs
|
2019-05-20 22:15:28 -04:00
|
|
|
vm_node = {}
|
|
|
|
vm_state = {}
|
2018-09-25 02:20:32 -04:00
|
|
|
for vm in full_vm_list:
|
2018-09-20 03:25:58 -04:00
|
|
|
# Check we don't match the limit
|
2018-09-25 02:20:32 -04:00
|
|
|
name = zkhandler.readdata(zk_conn, '/domains/{}'.format(vm))
|
2019-03-12 21:39:17 -04:00
|
|
|
vm_node[vm] = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(vm))
|
2019-03-20 11:31:54 -04:00
|
|
|
vm_state[vm] = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(vm))
|
2019-03-12 23:52:59 -04:00
|
|
|
# Handle limiting
|
2019-06-24 13:37:56 -04:00
|
|
|
if limit:
|
2018-09-20 03:25:58 -04:00
|
|
|
try:
|
2019-06-24 13:37:56 -04:00
|
|
|
if re.match(limit, vm):
|
|
|
|
if not node and not state:
|
2019-07-09 09:29:47 -04:00
|
|
|
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
2018-09-25 02:20:32 -04:00
|
|
|
else:
|
2019-03-20 11:31:54 -04:00
|
|
|
if vm_node[vm] == node or vm_state[vm] == state:
|
2019-07-09 09:29:47 -04:00
|
|
|
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
2018-09-25 02:20:32 -04:00
|
|
|
|
2019-06-24 13:37:56 -04:00
|
|
|
if re.match(limit, name):
|
|
|
|
if not node and not state:
|
2019-07-09 09:29:47 -04:00
|
|
|
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
2018-09-25 02:20:32 -04:00
|
|
|
else:
|
2019-03-20 11:31:54 -04:00
|
|
|
if vm_node[vm] == node or vm_state[vm] == state:
|
2019-07-09 09:29:47 -04:00
|
|
|
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
2018-09-20 03:25:58 -04:00
|
|
|
except Exception as e:
|
2018-09-25 02:20:32 -04:00
|
|
|
return False, 'Regex Error: {}'.format(e)
|
2018-09-20 03:25:58 -04:00
|
|
|
else:
|
2018-10-14 02:01:35 -04:00
|
|
|
# Check node to avoid unneeded ZK calls
|
2019-06-24 13:37:56 -04:00
|
|
|
if not node and not state:
|
2019-07-09 09:29:47 -04:00
|
|
|
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
2018-09-25 02:20:32 -04:00
|
|
|
else:
|
2019-03-20 11:31:54 -04:00
|
|
|
if vm_node[vm] == node or vm_state[vm] == state:
|
2019-07-09 09:29:47 -04:00
|
|
|
vm_list.append(common.getInformationFromXML(zk_conn, vm))
|
2019-05-20 22:15:28 -04:00
|
|
|
|
|
|
|
return True, vm_list
|