2018-05-31 20:26:44 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-06-06 01:47:53 -04:00
|
|
|
# pvcd.py - PVC client command-line interface
|
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 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/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2018-06-06 13:03:02 -04:00
|
|
|
import kazoo.client, socket, time, click, lxml.objectify, pvcf
|
2018-06-01 12:22:12 -04:00
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
this_host = socket.gethostname()
|
2018-06-06 01:07:59 -04:00
|
|
|
zk_host = ''
|
2018-06-05 01:39:59 -04:00
|
|
|
|
2018-06-06 01:07:59 -04:00
|
|
|
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'], max_content_width=120)
|
2018-05-31 20:26:44 -04:00
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
########################
|
|
|
|
########################
|
|
|
|
## ##
|
|
|
|
## CLICK COMPONENTS ##
|
|
|
|
## ##
|
|
|
|
########################
|
|
|
|
########################
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc node
|
|
|
|
###############################################################################
|
|
|
|
@click.group(name='node', short_help='Manage a PVC hypervisor node', context_settings=CONTEXT_SETTINGS)
|
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'node_name', default=this_host, show_default=True,
|
|
|
|
help='The PVC node to operate on.'
|
|
|
|
)
|
|
|
|
def node():
|
|
|
|
"""
|
|
|
|
Manage the state of a node in the PVC cluster.
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
* The '--name' option defaults to the current host if not set, which is likely not what you want when running this command from a remote host!
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc node flush
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='flush', short_help='Take a node out of service')
|
|
|
|
def flush_host():
|
|
|
|
"""
|
|
|
|
Take a node out of active service and migrate away all VMs.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc node ready
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='ready', short_help='Restore node to service')
|
|
|
|
def ready_host():
|
|
|
|
"""
|
|
|
|
Restore a host to active service and migrate back all VMs.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm
|
|
|
|
###############################################################################
|
|
|
|
@click.group(name='vm', short_help='Manage a PVC virtual machine', context_settings=CONTEXT_SETTINGS)
|
|
|
|
def vm():
|
|
|
|
"""
|
|
|
|
Manage the state of a virtual machine in the PVC cluster.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm define
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='define', short_help='Define a new virtual machine from a Libvirt XML file.')
|
|
|
|
@click.option(
|
|
|
|
'-x', '--xml', 'xml_config_file',
|
|
|
|
help='The XML config file to define the domain from.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-v', '--hypervisor', 'target_hypervisor', default=this_host, show_default=True,
|
|
|
|
help='The home hypervisor for this domain.'
|
|
|
|
)
|
|
|
|
def define_vm(xml_config_file, target_hypervisor):
|
|
|
|
"""
|
|
|
|
Define a new virtual machine from a Libvirt XML configuration file.
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
* The '--hypervisor' option defaults to the current host if not set, which is likely not what you want when running this command from a remote host!
|
|
|
|
"""
|
2018-06-06 12:00:52 -04:00
|
|
|
|
|
|
|
# Open the XML file
|
2018-06-06 13:03:02 -04:00
|
|
|
with open(xml_config_file, 'r') as f_domxmlfile:
|
2018-06-06 12:00:52 -04:00
|
|
|
data = f_domxmlfile.read()
|
|
|
|
f_domxmlfile.close()
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Parse the XML data
|
2018-06-06 12:00:52 -04:00
|
|
|
parsed_xml = lxml.objectify.fromstring(data)
|
|
|
|
dom_uuid = parsed_xml.uuid.text
|
|
|
|
dom_name = parsed_xml.name.text
|
2018-06-06 12:28:15 -04:00
|
|
|
click.echo('Adding new VM with Name "{}" and UUID "{}" to database.'.format(dom_name, dom_uuid))
|
2018-06-06 12:00:52 -04:00
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
|
|
|
# Add the new domain to Zookeeper
|
|
|
|
transaction = zk.transaction()
|
2018-06-06 13:03:02 -04:00
|
|
|
transaction.create('/domains/{}'.format(dom_uuid), dom_name.encode('ascii'))
|
2018-06-06 12:28:15 -04:00
|
|
|
transaction.create('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
|
|
|
|
transaction.create('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
|
|
|
|
transaction.create('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
|
|
|
|
transaction.create('/domains/{}/xml'.format(dom_uuid), data.encode('ascii'))
|
2018-06-06 12:00:52 -04:00
|
|
|
results = transaction.commit()
|
|
|
|
|
|
|
|
# Close the Zookeeper connection
|
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm undefine
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='undefine', short_help='Undefine and stop a virtual machine.')
|
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
|
|
|
def undefine_vm(dom_name, dom_uuid):
|
|
|
|
"""
|
|
|
|
Stop a virtual machine and remove it from the cluster database.
|
|
|
|
"""
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
|
|
|
return
|
|
|
|
|
2018-06-06 12:00:52 -04:00
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# If the --name value was passed, get the UUID
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
|
|
|
|
# Verify we got a result or abort
|
2018-06-06 12:29:19 -04:00
|
|
|
if not pvcf.validateUUID(dom_uuid):
|
2018-06-06 12:28:15 -04:00
|
|
|
if dom_name != None:
|
|
|
|
message_name = dom_name
|
|
|
|
else:
|
|
|
|
message_name = dom_uuid
|
2018-06-06 13:25:32 -04:00
|
|
|
click.echo('Could not find VM "{}" in the cluster!'.format(message_name))
|
2018-06-06 13:03:02 -04:00
|
|
|
return
|
2018-06-06 12:28:15 -04:00
|
|
|
|
|
|
|
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid))
|
2018-06-06 12:00:52 -04:00
|
|
|
# Set the domain into stop mode
|
|
|
|
transaction = zk.transaction()
|
2018-06-06 12:28:15 -04:00
|
|
|
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'stop'.encode('ascii'))
|
2018-06-06 12:00:52 -04:00
|
|
|
results = transaction.commit()
|
|
|
|
|
|
|
|
# Wait for 3 seconds to allow state to flow to all hypervisors
|
2018-06-06 12:28:15 -04:00
|
|
|
click.echo('Waiting for cluster to update.')
|
2018-06-06 12:00:52 -04:00
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
# Delete the configurations
|
2018-06-06 12:28:15 -04:00
|
|
|
click.echo('Undefining VM "{}".'.format(dom_uuid))
|
2018-06-06 12:00:52 -04:00
|
|
|
transaction = zk.transaction()
|
2018-06-06 12:28:15 -04:00
|
|
|
transaction.delete('/domains/{}/state'.format(dom_uuid))
|
|
|
|
transaction.delete('/domains/{}/hypervisor'.format(dom_uuid))
|
|
|
|
transaction.delete('/domains/{}/lasthypervisor'.format(dom_uuid))
|
|
|
|
transaction.delete('/domains/{}/xml'.format(dom_uuid))
|
|
|
|
transaction.delete('/domains/{}'.format(dom_uuid))
|
|
|
|
transaction.commit()
|
2018-06-06 12:00:52 -04:00
|
|
|
|
|
|
|
# Close the Zookeeper connection
|
|
|
|
pvcf.stopZKConnection(zk)
|
2018-06-05 01:39:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm start
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='start', short_help='Start up a defined virtual machine.')
|
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
|
|
|
def start_vm(dom_name, dom_uuid):
|
|
|
|
"""
|
|
|
|
Start up a virtual machine on its configured hypervisor.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
2018-06-05 22:06:08 -04:00
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
2018-06-05 01:39:59 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
|
|
|
# If the --name value was passed, get the UUID
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Verify we got a result or abort
|
2018-06-06 12:29:19 -04:00
|
|
|
if not pvcf.validateUUID(dom_uuid):
|
2018-06-06 12:28:15 -04:00
|
|
|
if dom_name != None:
|
|
|
|
message_name = dom_name
|
|
|
|
else:
|
|
|
|
message_name = dom_uuid
|
2018-06-06 13:25:32 -04:00
|
|
|
click.echo('Could not find VM "{}" in the cluster!'.format(message_name))
|
2018-06-06 13:03:02 -04:00
|
|
|
return
|
2018-06-06 12:28:15 -04:00
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
# Set the VM to start
|
2018-06-06 12:28:15 -04:00
|
|
|
click.echo('Starting VM "{}".'.format(dom_uuid))
|
2018-06-05 01:39:59 -04:00
|
|
|
zk.set('/domains/%s/state' % dom_uuid, 'start'.encode('ascii'))
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
# Close the Zookeeper connection
|
2018-06-05 01:39:59 -04:00
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm shutdown
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='shutdown', short_help='Gracefully shut down a running virtual machine.')
|
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
|
|
|
def shutdown_vm(dom_name, dom_uuid):
|
|
|
|
"""
|
|
|
|
Gracefully shut down a running virtual machine.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
2018-06-05 22:06:08 -04:00
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
2018-06-05 01:39:59 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
|
|
|
# If the --name value was passed, get the UUID
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Verify we got a result or abort
|
2018-06-06 12:29:19 -04:00
|
|
|
if not pvcf.validateUUID(dom_uuid):
|
2018-06-06 12:28:15 -04:00
|
|
|
if dom_name != None:
|
|
|
|
message_name = dom_name
|
|
|
|
else:
|
|
|
|
message_name = dom_uuid
|
2018-06-06 13:25:32 -04:00
|
|
|
click.echo('Could not find VM "{}" in the cluster!'.format(message_name))
|
2018-06-06 13:03:02 -04:00
|
|
|
return
|
2018-06-06 12:28:15 -04:00
|
|
|
|
|
|
|
# Set the VM to shutdown
|
|
|
|
click.echo('Shutting down VM "{}".'.format(dom_uuid))
|
2018-06-05 01:39:59 -04:00
|
|
|
zk.set('/domains/%s/state' % dom_uuid, 'shutdown'.encode('ascii'))
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
# Close the Zookeeper connection
|
2018-06-05 01:39:59 -04:00
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm stop
|
|
|
|
###############################################################################
|
|
|
|
@click.command(name='stop', short_help='Forcibly halt a running virtual machine.')
|
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
|
|
|
def stop_vm(dom_name, dom_uuid):
|
|
|
|
"""
|
|
|
|
Forcibly halt (destroy) a running virtual machine.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
2018-06-05 22:06:08 -04:00
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
2018-06-05 01:39:59 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
|
|
|
# If the --name value was passed, get the UUID
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Verify we got a result or abort
|
2018-06-06 12:29:19 -04:00
|
|
|
if not pvcf.validateUUID(dom_uuid):
|
2018-06-06 12:28:15 -04:00
|
|
|
if dom_name != None:
|
|
|
|
message_name = dom_name
|
|
|
|
else:
|
|
|
|
message_name = dom_uuid
|
2018-06-06 13:25:32 -04:00
|
|
|
click.echo('Could not find VM "{}" in the cluster!'.format(message_name))
|
2018-06-06 13:03:02 -04:00
|
|
|
return
|
2018-06-06 12:28:15 -04:00
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
# Set the VM to start
|
2018-06-06 12:28:15 -04:00
|
|
|
click.echo('Forcibly stopping VM "{}".'.format(dom_uuid))
|
2018-06-05 01:39:59 -04:00
|
|
|
zk.set('/domains/%s/state' % dom_uuid, 'stop'.encode('ascii'))
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
# Close the Zookeeper connection
|
2018-06-05 01:39:59 -04:00
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
###############################################################################
|
|
|
|
# pvc vm migrate
|
|
|
|
###############################################################################
|
2018-06-05 01:39:59 -04:00
|
|
|
@click.command(name='migrate', short_help='Migrate a virtual machine to another node.')
|
2018-06-05 22:06:08 -04:00
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-t', '--target', 'target_hypervisor', default=None,
|
|
|
|
help='The target hypervisor to migrate to.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-f', '--force', 'force_migrate', is_flag=True, default=False,
|
|
|
|
help='Force migrate an already migrated VM.'
|
|
|
|
)
|
|
|
|
def migrate_vm(dom_name, dom_uuid, target_hypervisor, force_migrate):
|
2018-06-05 01:39:59 -04:00
|
|
|
"""
|
|
|
|
Migrate a running virtual machine, via live migration if possible, to another hypervisor node.
|
|
|
|
"""
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
|
|
|
# If the --name value was passed, get the UUID
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Verify we got a result or abort
|
2018-06-06 12:29:19 -04:00
|
|
|
if not pvcf.validateUUID(dom_uuid):
|
2018-06-06 12:28:15 -04:00
|
|
|
if dom_name != None:
|
|
|
|
message_name = dom_name
|
|
|
|
else:
|
|
|
|
message_name = dom_uuid
|
2018-06-06 13:25:32 -04:00
|
|
|
click.echo('Could not find VM "{}" in the cluster!'.format(message_name))
|
2018-06-06 13:03:02 -04:00
|
|
|
return
|
2018-06-06 12:28:15 -04:00
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
current_hypervisor = zk.get('/domains/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
|
|
|
|
last_hypervisor = zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
|
|
|
|
|
|
|
|
if last_hypervisor != '' and force_migrate != True:
|
|
|
|
click.echo('The VM "{}" has been previously migrated.'.format(dom_uuid))
|
|
|
|
click.echo('> Last hypervisor: {}'.format(last_hypervisor))
|
|
|
|
click.echo('> Current hypervisor: {}'.format(current_hypervisor))
|
|
|
|
click.echo('Run `vm unmigrate` to restore the VM to its previous hypervisor, or use `--force` to override this check.')
|
|
|
|
return
|
|
|
|
|
|
|
|
if target_hypervisor == None:
|
|
|
|
# Determine the best hypervisor to migrate the VM to based on active memory usage
|
|
|
|
hypervisor_list = zk.get_children('/nodes')
|
|
|
|
most_memfree = 0
|
|
|
|
for hypervisor in hypervisor_list:
|
|
|
|
state = zk.get('/nodes/{}/state'.format(hypervisor))[0].decode('ascii')
|
|
|
|
if state != 'start' or hypervisor == current_hypervisor:
|
|
|
|
continue
|
|
|
|
|
|
|
|
memfree = int(zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
|
|
|
|
if memfree > most_memfree:
|
|
|
|
most_memfree = memfree
|
|
|
|
target_hypervisor = hypervisor
|
|
|
|
else:
|
|
|
|
if target_hypervisor == current_hypervisor:
|
|
|
|
click.echo('The VM "{}" is already running on hypervisor "{}".'.format(dom_uuid, current_hypervisor))
|
|
|
|
return
|
|
|
|
|
|
|
|
click.echo('Migrating VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
|
|
|
|
transaction = zk.transaction()
|
|
|
|
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
|
|
|
|
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
|
|
|
|
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), current_hypervisor.encode('ascii'))
|
|
|
|
transaction.commit()
|
|
|
|
|
|
|
|
# Close the Zookeeper connection
|
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc vm unmigrate
|
|
|
|
###############################################################################
|
2018-06-05 01:39:59 -04:00
|
|
|
@click.command(name='unmigrate', short_help='Restore a migrated virtual machine to its original node.')
|
2018-06-05 22:06:08 -04:00
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
|
|
|
def unmigrate_vm(dom_name, dom_uuid):
|
2018-06-05 01:39:59 -04:00
|
|
|
"""
|
|
|
|
Restore a previously migrated virtual machine, via live migration if possible, to its original hypervisor node.
|
|
|
|
"""
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
|
|
|
# If the --name value was passed, get the UUID
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
|
2018-06-06 12:28:15 -04:00
|
|
|
# Verify we got a result or abort
|
2018-06-06 12:29:19 -04:00
|
|
|
if not pvcf.validateUUID(dom_uuid):
|
2018-06-06 12:28:15 -04:00
|
|
|
if dom_name != None:
|
|
|
|
message_name = dom_name
|
|
|
|
else:
|
|
|
|
message_name = dom_uuid
|
2018-06-06 13:25:32 -04:00
|
|
|
click.echo('Could not find VM "{}" in the cluster!'.format(message_name))
|
2018-06-06 13:03:02 -04:00
|
|
|
return
|
2018-06-06 12:28:15 -04:00
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
target_hypervisor = zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
|
|
|
|
|
|
|
|
if target_hypervisor == '':
|
|
|
|
click.echo('The VM "{}" has not been previously migrated.'.format(dom_uuid))
|
|
|
|
return
|
|
|
|
|
|
|
|
click.echo('Unmigrating VM "{}" back to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
|
|
|
|
transaction = zk.transaction()
|
|
|
|
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
|
|
|
|
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), target_hypervisor.encode('ascii'))
|
|
|
|
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
|
|
|
|
transaction.commit()
|
|
|
|
|
|
|
|
# Close the Zookeeper connection
|
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# pvc search
|
|
|
|
###############################################################################
|
2018-06-05 01:39:59 -04:00
|
|
|
@click.command(name='search', short_help='Search for a VM object')
|
|
|
|
@click.option(
|
|
|
|
'-n', '--name', 'dom_name',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_uuid', 'argument': '--uuid' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this human-readable name.'
|
|
|
|
)
|
|
|
|
@click.option(
|
|
|
|
'-u', '--uuid', 'dom_uuid',
|
|
|
|
cls=pvcf.MutuallyExclusiveOption,
|
2018-06-05 22:06:08 -04:00
|
|
|
mutually_exclusive=[{ 'function': 'dom_name', 'argument': '--name' }],
|
2018-06-05 01:39:59 -04:00
|
|
|
help='Search for this UUID.'
|
|
|
|
)
|
2018-06-05 18:45:54 -04:00
|
|
|
@click.option(
|
|
|
|
'-l', '--long', 'long_output', is_flag=True, default=False,
|
|
|
|
help='Display more detailed information.'
|
|
|
|
)
|
|
|
|
def search(dom_name, dom_uuid, long_output):
|
2018-06-05 01:39:59 -04:00
|
|
|
"""
|
|
|
|
Search the cluster for a virtual machine's information.
|
|
|
|
"""
|
2018-06-05 22:06:08 -04:00
|
|
|
|
|
|
|
# Ensure at least one search method is set
|
|
|
|
if dom_name == None and dom_uuid == None:
|
|
|
|
click.echo("You must specify either a `--name` or `--uuid` value.")
|
|
|
|
return
|
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
if dom_name != None:
|
|
|
|
dom_uuid = pvcf.searchClusterByName(zk, dom_name)
|
|
|
|
if dom_uuid != None:
|
|
|
|
dom_name = pvcf.searchClusterByUUID(zk, dom_uuid)
|
|
|
|
|
2018-06-05 18:45:54 -04:00
|
|
|
information = pvcf.getInformationFromXML(zk, dom_uuid, long_output)
|
2018-06-06 00:44:43 -04:00
|
|
|
|
|
|
|
if information == None:
|
|
|
|
click.echo('Could not find a domain matching that name or UUID.')
|
|
|
|
return
|
|
|
|
|
2018-06-05 18:45:54 -04:00
|
|
|
click.echo(information)
|
2018-06-05 01:39:59 -04:00
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
|
|
|
|
2018-06-05 22:06:08 -04:00
|
|
|
###############################################################################
|
2018-06-06 01:07:59 -04:00
|
|
|
# pvc init
|
2018-06-05 22:06:08 -04:00
|
|
|
###############################################################################
|
2018-06-06 01:07:59 -04:00
|
|
|
@click.command(name='init', short_help='Initialize a new cluster')
|
2018-06-06 01:20:09 -04:00
|
|
|
@click.option('--yes', is_flag=True,
|
|
|
|
expose_value=False,
|
|
|
|
prompt='DANGER: This command will destroy any existing cluster data. Do you want to continue?')
|
2018-06-06 01:07:59 -04:00
|
|
|
def init_cluster():
|
|
|
|
"""
|
|
|
|
Perform initialization of Zookeeper to act as a PVC cluster
|
|
|
|
"""
|
|
|
|
|
2018-06-06 01:20:09 -04:00
|
|
|
click.echo('Initializing a new cluster with Zookeeper address "{}".'.format(zk_host))
|
2018-06-06 01:07:59 -04:00
|
|
|
|
|
|
|
# Open a Zookeeper connection
|
|
|
|
zk = pvcf.startZKConnection(zk_host)
|
|
|
|
|
2018-06-06 01:20:09 -04:00
|
|
|
# Destroy the existing data
|
|
|
|
try:
|
|
|
|
zk.delete('/domains', recursive=True)
|
|
|
|
zk.delete('nodes', recursive=True)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2018-06-06 01:07:59 -04:00
|
|
|
# Create the root keys
|
|
|
|
transaction = zk.transaction()
|
|
|
|
transaction.create('/domains', ''.encode('ascii'))
|
|
|
|
transaction.create('/nodes', ''.encode('ascii'))
|
|
|
|
transaction.commit()
|
|
|
|
|
|
|
|
# Close the Zookeeper connection
|
|
|
|
pvcf.stopZKConnection(zk)
|
|
|
|
|
2018-06-06 01:29:11 -04:00
|
|
|
click.echo('Successfully initialized new cluster. Any running PVC daemons will need to be restarted.')
|
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
|
2018-06-06 01:07:59 -04:00
|
|
|
###############################################################################
|
|
|
|
# pvc
|
|
|
|
###############################################################################
|
2018-06-05 01:39:59 -04:00
|
|
|
@click.group(context_settings=CONTEXT_SETTINGS)
|
2018-06-06 01:07:59 -04:00
|
|
|
@click.option(
|
|
|
|
'-z', '--zookeeper', '_zk_host', envvar='PVC_ZOOKEEPER', default='{}:2181'.format(this_host), show_default=True,
|
|
|
|
help='Zookeeper connection string.'
|
|
|
|
)
|
|
|
|
def cli(_zk_host):
|
|
|
|
"""
|
|
|
|
Parallel Virtual Cluster CLI management tool
|
|
|
|
"""
|
|
|
|
|
2018-06-06 01:20:09 -04:00
|
|
|
global zk_host
|
2018-06-06 01:07:59 -04:00
|
|
|
zk_host = _zk_host
|
|
|
|
|
2018-06-05 01:39:59 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Click command tree
|
|
|
|
#
|
|
|
|
node.add_command(flush_host)
|
|
|
|
node.add_command(ready_host)
|
|
|
|
|
|
|
|
vm.add_command(define_vm)
|
2018-06-06 12:28:15 -04:00
|
|
|
vm.add_command(undefine_vm)
|
2018-06-05 01:39:59 -04:00
|
|
|
vm.add_command(start_vm)
|
|
|
|
vm.add_command(shutdown_vm)
|
|
|
|
vm.add_command(stop_vm)
|
|
|
|
vm.add_command(migrate_vm)
|
|
|
|
vm.add_command(unmigrate_vm)
|
|
|
|
|
|
|
|
cli.add_command(node)
|
|
|
|
cli.add_command(vm)
|
|
|
|
cli.add_command(search)
|
2018-06-06 01:20:09 -04:00
|
|
|
cli.add_command(init_cluster)
|
2018-06-05 01:39:59 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Main entry point
|
|
|
|
#
|
|
|
|
def main():
|
|
|
|
return cli(obj={})
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|