2018-05-31 20:26:44 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-09-12 11:59:23 -04:00
|
|
|
# VMInstance.py - Class implementing a PVC virtual machine and run by pvcvd
|
2018-06-06 01:47:53 -04:00
|
|
|
# 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-09-20 03:42:40 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import uuid
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import libvirt
|
|
|
|
import kazoo.client
|
|
|
|
|
|
|
|
import daemon_lib.ansiiprint as ansiiprint
|
|
|
|
import daemon_lib.zkhandler as zkhandler
|
2018-06-06 22:56:03 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
class VMInstance:
|
2018-06-06 22:59:31 -04:00
|
|
|
# Initialization function
|
2018-06-17 21:55:39 -04:00
|
|
|
def __init__(self, domuuid, zk_conn, config, thishypervisor):
|
2018-05-31 20:26:44 -04:00
|
|
|
# Passed-in variables on creation
|
|
|
|
self.domuuid = domuuid
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn = zk_conn
|
2018-06-08 12:19:48 -04:00
|
|
|
self.config = config
|
2018-05-31 20:26:44 -04:00
|
|
|
self.thishypervisor = thishypervisor
|
|
|
|
|
|
|
|
# These will all be set later
|
|
|
|
self.hypervisor = None
|
|
|
|
self.state = None
|
2018-06-02 18:34:48 -04:00
|
|
|
self.instart = False
|
2018-06-13 12:47:30 -04:00
|
|
|
self.inrestart = False
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inmigrate = False
|
|
|
|
self.inreceive = False
|
2018-06-13 12:47:30 -04:00
|
|
|
self.inshutdown = False
|
|
|
|
self.instop = False
|
2018-05-31 20:26:44 -04:00
|
|
|
|
2018-06-11 20:12:11 -04:00
|
|
|
self.dom = self.lookupByUUID(self.domuuid)
|
2018-07-17 21:42:28 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
# Watch for changes to the state field in Zookeeper
|
2018-06-17 21:55:39 -04:00
|
|
|
@zk_conn.DataWatch('/domains/{}/state'.format(self.domuuid))
|
2018-06-01 12:21:58 -04:00
|
|
|
def watch_state(data, stat, event=""):
|
2018-06-15 01:40:06 -04:00
|
|
|
# If we get a delete state, just terminate outselves
|
2018-06-15 01:50:39 -04:00
|
|
|
if data == None:
|
|
|
|
return
|
2018-06-15 01:40:06 -04:00
|
|
|
# Otherwise perform a management command
|
|
|
|
else:
|
|
|
|
self.manage_vm_state()
|
2018-06-11 20:12:11 -04:00
|
|
|
|
2018-06-02 15:03:44 -04:00
|
|
|
# Get data functions
|
2018-06-02 15:19:29 -04:00
|
|
|
def getstate(self):
|
2018-06-02 15:03:44 -04:00
|
|
|
return self.state
|
|
|
|
|
2018-06-02 15:19:29 -04:00
|
|
|
def gethypervisor(self):
|
|
|
|
return self.hypervisor
|
2018-06-02 15:03:44 -04:00
|
|
|
|
2018-06-12 01:54:01 -04:00
|
|
|
def getdom(self):
|
|
|
|
return self.dom
|
|
|
|
|
2018-07-17 21:51:49 -04:00
|
|
|
def getmemory(self):
|
|
|
|
try:
|
2018-07-17 21:55:38 -04:00
|
|
|
memory = int(self.dom.info()[2] / 1024)
|
2018-07-17 21:51:49 -04:00
|
|
|
except:
|
|
|
|
memory = 0
|
|
|
|
|
|
|
|
return memory
|
2018-07-17 21:40:30 -04:00
|
|
|
|
2018-07-18 12:09:07 -04:00
|
|
|
def getvcpus(self):
|
|
|
|
try:
|
|
|
|
vcpus = int(self.dom.info()[3])
|
|
|
|
except:
|
|
|
|
vcpus = 0
|
|
|
|
|
|
|
|
return vcpus
|
|
|
|
|
2018-06-19 20:01:26 -04:00
|
|
|
# Manage local node domain_list
|
|
|
|
def addDomainToList(self):
|
|
|
|
if not self.domuuid in self.thishypervisor.domain_list:
|
|
|
|
try:
|
2018-06-20 11:53:48 -04:00
|
|
|
# Add the domain to the domain_list array
|
2018-06-19 20:01:26 -04:00
|
|
|
self.thishypervisor.domain_list.append(self.domuuid)
|
2018-06-20 11:53:48 -04:00
|
|
|
# Push the change up to Zookeeper
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/nodes/{}/runningdomains'.format(self.thishypervisor.name): ' '.join(self.thishypervisor.domain_list) })
|
2018-06-19 20:01:26 -04:00
|
|
|
except Exception as e:
|
2018-06-20 11:53:48 -04:00
|
|
|
ansiiprint.echo('Error adding domain to list: {}'.format(e), '', 'c')
|
2018-06-19 20:01:26 -04:00
|
|
|
|
|
|
|
def removeDomainFromList(self):
|
|
|
|
if self.domuuid in self.thishypervisor.domain_list:
|
|
|
|
try:
|
2018-06-20 11:53:48 -04:00
|
|
|
# Remove the domain from the domain_list array
|
2018-06-19 20:01:26 -04:00
|
|
|
self.thishypervisor.domain_list.remove(self.domuuid)
|
2018-06-20 11:53:48 -04:00
|
|
|
# Push the change up to Zookeeper
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/nodes/{}/runningdomains'.format(self.thishypervisor.name): ' '.join(self.thishypervisor.domain_list) })
|
2018-06-19 20:01:26 -04:00
|
|
|
except Exception as e:
|
2018-06-20 11:53:48 -04:00
|
|
|
ansiiprint.echo('Error removing domain from list: {}'.format(e), '', 'c')
|
2018-06-19 20:01:26 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
# Start up the VM
|
2018-06-13 12:31:27 -04:00
|
|
|
def start_vm(self):
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Starting VM', '{}:'.format(self.domuuid), 'i')
|
2018-06-02 18:34:48 -04:00
|
|
|
self.instart = True
|
2018-06-04 01:22:18 -04:00
|
|
|
|
|
|
|
# Start up a new Libvirt connection
|
|
|
|
libvirt_name = "qemu:///system"
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn = libvirt.open(libvirt_name)
|
|
|
|
if lv_conn == None:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
|
2018-06-04 01:22:18 -04:00
|
|
|
self.instart = False
|
|
|
|
return
|
2018-08-23 19:01:57 -04:00
|
|
|
|
|
|
|
# Try to get the current state in case it's already running
|
2018-06-02 00:30:25 -04:00
|
|
|
try:
|
2018-08-23 19:01:57 -04:00
|
|
|
self.dom = self.lookupByUUID(self.domuuid)
|
|
|
|
curstate = self.dom.state()[0]
|
|
|
|
except:
|
|
|
|
curstate = 'notstart'
|
|
|
|
|
|
|
|
if curstate == libvirt.VIR_DOMAIN_RUNNING:
|
|
|
|
# If it is running just update the model
|
2018-06-19 20:01:26 -04:00
|
|
|
self.addDomainToList()
|
2018-07-19 22:46:50 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): '' })
|
2018-08-23 19:01:57 -04:00
|
|
|
else:
|
|
|
|
# Or try to create it
|
|
|
|
try:
|
|
|
|
# Grab the domain information from Zookeeper
|
|
|
|
xmlconfig = zkhandler.readdata(self.zk_conn, '/domains/{}/xml'.format(self.domuuid))
|
|
|
|
dom = lv_conn.createXML(xmlconfig, 0)
|
|
|
|
self.addDomainToList()
|
|
|
|
ansiiprint.echo('Successfully started VM', '{}:'.format(self.domuuid), 'o')
|
|
|
|
self.dom = dom
|
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): '' })
|
|
|
|
except libvirt.libvirtError as e:
|
|
|
|
ansiiprint.echo('Failed to create VM', '{}:'.format(self.domuuid), 'e')
|
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'failed' })
|
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/failedreason'.format(self.domuuid): str(e) })
|
|
|
|
self.dom = None
|
2018-06-02 00:33:54 -04:00
|
|
|
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn.close()
|
2018-06-02 18:34:48 -04:00
|
|
|
self.instart = False
|
2018-06-13 12:47:30 -04:00
|
|
|
|
|
|
|
# Restart the VM
|
|
|
|
def restart_vm(self):
|
|
|
|
ansiiprint.echo('Restarting VM', '{}:'.format(self.domuuid), 'i')
|
|
|
|
self.inrestart = True
|
|
|
|
|
|
|
|
# Start up a new Libvirt connection
|
|
|
|
libvirt_name = "qemu:///system"
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn = libvirt.open(libvirt_name)
|
|
|
|
if lv_conn == None:
|
2018-06-13 12:47:30 -04:00
|
|
|
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
|
|
|
|
self.inrestart = False
|
|
|
|
return
|
|
|
|
|
2018-07-20 01:02:18 -04:00
|
|
|
self.shutdown_vm()
|
|
|
|
self.start_vm()
|
|
|
|
self.addDomainToList()
|
2018-06-13 12:47:30 -04:00
|
|
|
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'start' })
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn.close()
|
2018-06-13 12:47:30 -04:00
|
|
|
self.inrestart = False
|
|
|
|
|
2018-06-06 21:47:06 -04:00
|
|
|
# Stop the VM forcibly without updating state
|
|
|
|
def terminate_vm(self):
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Terminating VM', '{}:'.format(self.domuuid), 'i')
|
2018-06-06 21:47:06 -04:00
|
|
|
self.instop = True
|
|
|
|
try:
|
|
|
|
self.dom.destroy()
|
|
|
|
except AttributeError:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Failed to terminate VM', '{}:'.format(self.domuuid), 'e')
|
2018-06-19 20:01:26 -04:00
|
|
|
self.removeDomainFromList()
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Successfully terminated VM', '{}:'.format(self.domuuid), 'o')
|
2018-06-12 21:44:06 -04:00
|
|
|
self.dom = None
|
2018-06-11 19:14:22 -04:00
|
|
|
self.instop = False
|
2018-06-06 21:47:06 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
# Stop the VM forcibly
|
|
|
|
def stop_vm(self):
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Forcibly stopping VM', '{}:'.format(self.domuuid), 'i')
|
2018-06-02 18:34:48 -04:00
|
|
|
self.instop = True
|
2018-06-06 11:48:28 -04:00
|
|
|
try:
|
|
|
|
self.dom.destroy()
|
|
|
|
except AttributeError:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Failed to stop VM', '{}:'.format(self.domuuid), 'e')
|
2018-06-19 20:01:26 -04:00
|
|
|
self.removeDomainFromList()
|
2018-06-02 15:52:50 -04:00
|
|
|
|
2018-06-13 12:52:40 -04:00
|
|
|
if self.inrestart == False:
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'stop' })
|
2018-06-13 12:52:40 -04:00
|
|
|
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Successfully stopped VM', '{}:'.format(self.domuuid), 'o')
|
2018-06-02 15:52:50 -04:00
|
|
|
self.dom = None
|
2018-06-02 18:34:48 -04:00
|
|
|
self.instop = False
|
2018-05-31 20:26:44 -04:00
|
|
|
|
|
|
|
# Shutdown the VM gracefully
|
|
|
|
def shutdown_vm(self):
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Gracefully stopping VM', '{}:'.format(self.domuuid), 'i')
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inshutdown = True
|
2018-05-31 20:26:44 -04:00
|
|
|
self.dom.shutdown()
|
2018-06-02 16:24:11 -04:00
|
|
|
try:
|
2018-06-02 16:28:18 -04:00
|
|
|
tick = 0
|
|
|
|
while self.dom.state()[0] == libvirt.VIR_DOMAIN_RUNNING and tick < 60:
|
|
|
|
tick += 1
|
2018-06-02 16:24:11 -04:00
|
|
|
time.sleep(0.5)
|
2018-06-02 16:28:18 -04:00
|
|
|
|
|
|
|
if tick >= 60:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Shutdown timeout expired', '{}:'.format(self.domuuid), 'e')
|
2018-06-02 16:28:18 -04:00
|
|
|
self.stop_vm()
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inshutdown = False
|
2018-06-02 16:28:18 -04:00
|
|
|
return
|
2018-06-02 16:24:11 -04:00
|
|
|
except:
|
|
|
|
pass
|
2018-06-02 16:19:51 -04:00
|
|
|
|
2018-06-19 20:01:26 -04:00
|
|
|
self.removeDomainFromList()
|
2018-06-19 19:52:03 -04:00
|
|
|
|
2018-06-13 12:52:40 -04:00
|
|
|
if self.inrestart == False:
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'stop' })
|
2018-06-13 12:52:40 -04:00
|
|
|
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Successfully shutdown VM', '{}:'.format(self.domuuid), 'o')
|
2018-06-02 15:52:50 -04:00
|
|
|
self.dom = None
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inshutdown = False
|
2018-06-02 15:52:50 -04:00
|
|
|
|
2018-06-04 12:15:37 -04:00
|
|
|
def live_migrate_vm(self, dest_hypervisor):
|
2018-06-02 15:39:17 -04:00
|
|
|
try:
|
2018-06-17 21:55:39 -04:00
|
|
|
dest_lv_conn = libvirt.open('qemu+tcp://{}/system'.format(self.hypervisor))
|
|
|
|
if dest_lv_conn == None:
|
2018-06-02 15:39:17 -04:00
|
|
|
raise
|
|
|
|
except:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Failed to open connection to qemu+tcp://{}/system; aborting migration.'.format(self.hypervisor), '{}:'.format(self.domuuid), 'e')
|
2018-06-04 12:15:37 -04:00
|
|
|
return 1
|
2018-05-31 20:26:44 -04:00
|
|
|
|
2018-06-02 15:39:17 -04:00
|
|
|
try:
|
2018-06-17 21:55:39 -04:00
|
|
|
target_dom = self.dom.migrate(dest_lv_conn, libvirt.VIR_MIGRATE_LIVE, None, None, 0)
|
2018-06-02 15:39:17 -04:00
|
|
|
if target_dom == None:
|
|
|
|
raise
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Successfully migrated VM', '{}:'.format(self.domuuid), 'o')
|
2018-06-06 22:59:31 -04:00
|
|
|
|
2018-06-02 15:39:17 -04:00
|
|
|
except:
|
2018-06-17 21:55:39 -04:00
|
|
|
dest_lv_conn.close()
|
2018-06-04 12:15:37 -04:00
|
|
|
return 1
|
|
|
|
|
2018-06-17 21:55:39 -04:00
|
|
|
dest_lv_conn.close()
|
2018-06-04 12:15:37 -04:00
|
|
|
return 0
|
|
|
|
|
|
|
|
# Migrate the VM to a target host
|
|
|
|
def migrate_vm(self):
|
|
|
|
self.inmigrate = True
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Migrating VM to hypervisor "{}"'.format(self.hypervisor), '{}:'.format(self.domuuid), 'i')
|
2018-06-20 11:53:48 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
migrate_ret = self.live_migrate_vm(self.hypervisor)
|
|
|
|
except:
|
|
|
|
migrate_ret = 0
|
|
|
|
|
2018-06-04 12:15:37 -04:00
|
|
|
if migrate_ret != 0:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Could not live migrate VM; shutting down to migrate instead', '{}:'.format(self.domuuid), 'e')
|
2018-06-04 16:30:09 -04:00
|
|
|
self.shutdown_vm()
|
2018-06-06 21:54:38 -04:00
|
|
|
time.sleep(1)
|
2018-06-19 19:52:03 -04:00
|
|
|
else:
|
2018-06-19 20:01:26 -04:00
|
|
|
self.removeDomainFromList()
|
2018-06-19 19:52:03 -04:00
|
|
|
time.sleep(1)
|
2018-06-02 15:58:21 -04:00
|
|
|
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'start' })
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inmigrate = False
|
2018-06-04 11:49:39 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
# Receive the migration from another host (wait until VM is running)
|
2018-06-04 01:22:18 -04:00
|
|
|
def receive_migrate(self):
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inreceive = True
|
2018-06-11 16:53:04 -04:00
|
|
|
ansiiprint.echo('Receiving migration', '{}:'.format(self.domuuid), 'i')
|
2018-06-11 03:01:12 -04:00
|
|
|
while True:
|
2018-06-11 22:47:53 -04:00
|
|
|
time.sleep(0.5)
|
2018-06-26 21:52:22 -04:00
|
|
|
self.state = zkhandler.readdata(self.zk_conn, '/domains/{}/state'.format(self.domuuid))
|
2018-06-07 00:40:32 -04:00
|
|
|
self.dom = self.lookupByUUID(self.domuuid)
|
2018-06-11 13:11:42 -04:00
|
|
|
|
2018-06-11 17:02:36 -04:00
|
|
|
if self.dom == None and self.state == 'migrate':
|
2018-06-02 19:06:59 -04:00
|
|
|
continue
|
2018-06-02 18:38:59 -04:00
|
|
|
|
2018-06-11 17:02:36 -04:00
|
|
|
if self.state != 'migrate':
|
2018-06-02 19:06:59 -04:00
|
|
|
break
|
2018-06-02 18:38:59 -04:00
|
|
|
|
2018-06-11 17:02:36 -04:00
|
|
|
try:
|
|
|
|
if self.dom.state()[0] == libvirt.VIR_DOMAIN_RUNNING:
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2018-06-18 00:51:22 -04:00
|
|
|
try:
|
|
|
|
dom_state = self.dom.state()[0]
|
|
|
|
except AttributeError:
|
|
|
|
dom_state = None
|
|
|
|
|
|
|
|
if dom_state == libvirt.VIR_DOMAIN_RUNNING:
|
2018-06-19 20:01:26 -04:00
|
|
|
self.addDomainToList()
|
2018-06-11 02:08:09 -04:00
|
|
|
ansiiprint.echo('Successfully received migrated VM', '{}:'.format(self.domuuid), 'o')
|
2018-06-11 16:53:04 -04:00
|
|
|
else:
|
2018-06-11 02:03:23 -04:00
|
|
|
ansiiprint.echo('Failed to receive migrated VM', '{}:'.format(self.domuuid), 'e')
|
2018-06-04 03:00:17 -04:00
|
|
|
|
2018-06-02 18:34:48 -04:00
|
|
|
self.inreceive = False
|
2018-05-31 23:28:26 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
#
|
|
|
|
# Main function to manage a VM (taking only self)
|
|
|
|
#
|
|
|
|
def manage_vm_state(self):
|
2018-06-11 21:32:13 -04:00
|
|
|
# Give ourselves a bit of leeway time
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
2018-06-11 21:51:38 -04:00
|
|
|
# Get the current values from zookeeper (don't rely on the watch)
|
2018-06-26 21:52:22 -04:00
|
|
|
self.state = zkhandler.readdata(self.zk_conn, '/domains/{}/state'.format(self.domuuid))
|
|
|
|
self.hypervisor = zkhandler.readdata(self.zk_conn, '/domains/{}/hypervisor'.format(self.domuuid))
|
2018-06-11 18:45:37 -04:00
|
|
|
|
2018-05-31 20:26:44 -04:00
|
|
|
# Check the current state of the VM
|
|
|
|
try:
|
2018-05-31 23:28:26 -04:00
|
|
|
if self.dom != None:
|
2018-06-06 11:42:49 -04:00
|
|
|
running, reason = self.dom.state()
|
2018-05-31 23:28:26 -04:00
|
|
|
else:
|
2018-06-02 15:52:50 -04:00
|
|
|
raise
|
2018-05-31 20:26:44 -04:00
|
|
|
except:
|
2018-06-01 12:21:58 -04:00
|
|
|
running = libvirt.VIR_DOMAIN_NOSTATE
|
2018-05-31 20:26:44 -04:00
|
|
|
|
2018-06-11 19:22:33 -04:00
|
|
|
ansiiprint.echo('VM state change for "{}": {} {}'.format(self.domuuid, self.state, self.hypervisor), '', 'i')
|
|
|
|
|
2018-06-13 12:31:27 -04:00
|
|
|
#######################
|
|
|
|
# Handle state changes
|
|
|
|
#######################
|
|
|
|
# Valid states are:
|
|
|
|
# start
|
|
|
|
# migrate
|
2018-06-13 12:47:30 -04:00
|
|
|
# restart
|
2018-06-13 12:31:27 -04:00
|
|
|
# shutdown
|
|
|
|
# stop
|
|
|
|
|
|
|
|
# Conditional pass one - Are we already performing an action
|
2018-06-13 12:47:30 -04:00
|
|
|
if self.instart == False \
|
|
|
|
and self.inrestart == False \
|
|
|
|
and self.inmigrate == False \
|
|
|
|
and self.inreceive == False \
|
|
|
|
and self.inshutdown == False \
|
|
|
|
and self.instop == False:
|
2018-06-13 12:31:27 -04:00
|
|
|
# Conditional pass two - Is this VM configured to run on this hypervisor
|
|
|
|
if self.hypervisor == self.thishypervisor.name:
|
|
|
|
# Conditional pass three - Is this VM currently running on this hypervisor
|
|
|
|
if running == libvirt.VIR_DOMAIN_RUNNING:
|
|
|
|
# VM is already running and should be
|
|
|
|
if self.state == "start":
|
2018-06-19 20:01:26 -04:00
|
|
|
self.addDomainToList()
|
2018-06-13 12:31:27 -04:00
|
|
|
# VM is already running and should be but stuck in migrate state
|
|
|
|
elif self.state == "migrate":
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'start' })
|
2018-06-19 20:01:26 -04:00
|
|
|
self.addDomainToList()
|
2018-06-13 12:47:30 -04:00
|
|
|
# VM should be restarted
|
|
|
|
elif self.state == "restart":
|
|
|
|
self.restart_vm()
|
2018-06-13 12:31:27 -04:00
|
|
|
# VM should be shut down
|
|
|
|
elif self.state == "shutdown":
|
|
|
|
self.shutdown_vm()
|
|
|
|
# VM should be stopped
|
|
|
|
elif self.state == "stop":
|
|
|
|
self.stop_vm()
|
|
|
|
else:
|
|
|
|
# VM should be started
|
|
|
|
if self.state == "start":
|
|
|
|
self.start_vm()
|
|
|
|
# VM should be migrated to this hypervisor
|
|
|
|
elif self.state == "migrate":
|
|
|
|
self.receive_migrate()
|
2018-06-13 12:47:30 -04:00
|
|
|
# VM should be restarted (i.e. started since it isn't running)
|
|
|
|
if self.state == "restart":
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(self.domuuid): 'start' })
|
2018-06-19 19:52:03 -04:00
|
|
|
# VM should be shut down; ensure it's gone from this node's domain_list
|
2018-06-13 12:31:27 -04:00
|
|
|
elif self.state == "shutdown":
|
2018-06-19 20:01:26 -04:00
|
|
|
self.removeDomainFromList()
|
2018-06-19 19:52:03 -04:00
|
|
|
# VM should be stoped; ensure it's gone from this node's domain_list
|
2018-06-13 12:31:27 -04:00
|
|
|
elif self.state == "stop":
|
2018-06-19 20:01:26 -04:00
|
|
|
self.removeDomainFromList()
|
2018-06-19 19:52:03 -04:00
|
|
|
|
2018-06-13 12:31:27 -04:00
|
|
|
else:
|
|
|
|
# Conditional pass three - Is this VM currently running on this hypervisor
|
|
|
|
if running == libvirt.VIR_DOMAIN_RUNNING:
|
|
|
|
# VM should be migrated away from this hypervisor
|
|
|
|
if self.state == "migrate":
|
|
|
|
self.migrate_vm()
|
|
|
|
# VM should be terminated
|
|
|
|
else:
|
|
|
|
self.terminate_vm()
|
2018-06-06 21:45:03 -04:00
|
|
|
|
2018-06-06 01:47:53 -04:00
|
|
|
|
2018-06-06 22:59:31 -04:00
|
|
|
# This function is a wrapper for libvirt.lookupByUUID which fixes some problems
|
|
|
|
# 1. Takes a text UUID and handles converting it to bytes
|
|
|
|
# 2. Try's it and returns a sensible value if not
|
|
|
|
def lookupByUUID(self, tuuid):
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn = None
|
2018-06-06 22:59:31 -04:00
|
|
|
dom = None
|
|
|
|
libvirt_name = "qemu:///system"
|
2018-06-06 01:47:53 -04:00
|
|
|
|
2018-06-06 22:59:31 -04:00
|
|
|
# Convert the text UUID to bytes
|
|
|
|
buuid = uuid.UUID(tuuid).bytes
|
|
|
|
|
|
|
|
# Try
|
|
|
|
try:
|
|
|
|
# Open a libvirt connection
|
2018-06-17 21:55:39 -04:00
|
|
|
lv_conn = libvirt.open(libvirt_name)
|
|
|
|
if lv_conn == None:
|
2018-06-07 00:29:04 -04:00
|
|
|
ansiiprint.echo('Failed to open local libvirt connection', '{}:'.format(self.domuuid), 'e')
|
2018-06-06 22:59:31 -04:00
|
|
|
return dom
|
|
|
|
|
|
|
|
# Lookup the UUID
|
2018-06-17 21:55:39 -04:00
|
|
|
dom = lv_conn.lookupByUUID(buuid)
|
2018-06-06 22:59:31 -04:00
|
|
|
|
|
|
|
# Fail
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# After everything
|
|
|
|
finally:
|
|
|
|
# Close the libvirt connection
|
2018-06-17 21:55:39 -04:00
|
|
|
if lv_conn != None:
|
|
|
|
lv_conn.close()
|
2018-06-06 22:59:31 -04:00
|
|
|
|
|
|
|
# Return the dom object (or None)
|
|
|
|
return dom
|