Initial commit

This commit is contained in:
Joshua Boniface 2018-05-31 20:26:44 -04:00
commit c42795049f
5 changed files with 202 additions and 0 deletions

112
VMInstance.py Normal file
View File

@ -0,0 +1,112 @@
#!/usr/bin/env python3
import os, time, uuid, libvirt, kazoo.client
class VMInstance:
def __init__(self, domuuid, zk, thishypervisor):
# Passed-in variables on creation
self.domuuid = domuuid
self.zkey = '/domains/%s' % domuuid
self.zk = zk
self.thishypervisor = thishypervisor
# These will all be set later
self.hypervisor = None
self.state = None
self.dom = None
# Watch for changes to the hypervisor field in Zookeeper
@zk.DataWatch(self.zkey + '/hypervisor')
def watch_hypervisor(data, stat):
self.hypervisor = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, data.decode('ascii')))
self.manage_vm_state()
# Watch for changes to the state field in Zookeeper
@zk.DataWatch(self.zkey + '/state')
def watch_state(data, stat):
self.state = data.decode('ascii')
print("Version: %s, data: %s" % (stat.version, self.state))
self.manage_vm_state()
# Start up the VM
def start_vm(self, conn, xmlconfig):
print("Starting VM %s" % self.domuuid)
dom = conn.createXML(xmlconfig, 0)
if dom == None:
print('Failed to create a domain from an XML definition.')
exit(1)
return dom
# Stop the VM forcibly
def stop_vm(self):
print("Forcibly stopping VM %s" % self.domuuid)
self.dom.destroy()
# Shutdown the VM gracefully
def shutdown_vm(self):
print("Stopping VM %s" % self.domuuid)
self.dom.shutdown()
# Migrate the VM to a target host
def migrate_vm(self):
self.zk.set(self.zkey + '/status', b'migrate')
dest_conn = libvirt.open('qemu+ssh://%s/system' % target)
if dest_conn == None:
print('Failed to open connection to qemu+ssh://%s/system' % target)
exit(1)
target_dom = self.dom.migrate(dest_conn, libvirt.VIR_MIGRATE_LIVE, None, None, 0)
if target_dom == None:
print('Could not migrate to the new domain')
exit(1)
print('Migrated successfully')
dest_conn.close()
# Receive the migration from another host (wait until VM is running)
def receive_migrate(self):
while True:
if self.dom.state() != libvirt.VIR_DOMAIN_RUNNING:
continue
else:
self.zk.set(self.zkey + '/status', b'start')
break
#
# Main function to manage a VM (taking only self)
#
def manage_vm_state(self):
# Start up a new Libvirt connection
libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name)
if conn == None:
print('Failed to open local libvirt connection.')
exit(1)
# Check the current state of the VM
try:
self.dom = conn.lookupByUUID(uuid.UUID(self.domuuid).bytes)
running = self.dom.state()
except:
running = False
if running != False and self.state == "stop" and self.hypervisor == self.thishypervisor:
self.stop_vm()
if running != False and self.state == "shutdown" and self.hypervisor == self.thishypervisor:
self.shutdown_vm()
elif running == False and self.state == "migrate" and self.hypervisr == self.thishypervisor:
self.receive_migrate()
elif running != False and self.state == "migrate" and self.hypervisr != self.thishypervisor:
self.migrate_vm()
elif running == False and self.state == "start" and self.hypervisor == self.thishypervisor:
# Grab the domain information from Zookeeper
domxml, domxmlstat = self.zk.get(self.zkey + '/xml')
domxml = str(domxml.decode('ascii'))
self.dom = self.start_vm(conn, domxml)
# The VM should now be running so return the domain and active connection
conn.close

Binary file not shown.

Binary file not shown.

6
pvc.py Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env python3
def help():
print("pvc - Parallel Virtual Cluster command-line utility")
help()

84
pvcd.py Executable file
View File

@ -0,0 +1,84 @@
#!/usr/bin/env python3
from kazoo.client import KazooClient
from kazoo.client import KazooState
import libvirt
import sys
import uuid
import VMInstance
import time
def help():
print("pvcd - Parallel Virtual Cluster management daemon")
# exit(0)
help()
# Connect to zookeeper
zk = KazooClient(hosts='127.0.0.1:2181')
try:
zk.start()
except:
print("Failed to connect to local Zookeeper instance")
exit(1)
def zk_listener(state):
if state == KazooState.LOST:
# Register somewhere that the session was lost
pass
elif state == KazooState.SUSPENDED:
# Handle being disconnected from Zookeeper
pass
else:
# Handle being connected/reconnected to Zookeeper
pass
zk.add_listener(zk_listener)
# Connect to libvirt
libvirt_name = "qemu:///system"
conn = libvirt.open(libvirt_name)
if conn == None:
print('Failed to open connection to %s' % libvirt_name)
exit(1)
# Gather data about hypervisor
hostname = conn.getHostname()
nodeinfo = conn.getInfo()
numnodes = nodeinfo[4]
memlistNUMA = conn.getCellsFreeMemory(0, numnodes)
memlistTOTAL = conn.getFreeMemory()
print("Node hostname: %s" % hostname)
print("Free memory: %s" % memlistTOTAL)
cell = 0
for cellfreemem in memlistNUMA:
print('NUMA Node '+str(cell)+': '+str(cellfreemem)+' bytes free memory')
cell += 1
print('Virtualization type: '+conn.getType())
uri = conn.getURI()
print('Canonical URI: '+uri)
print()
map = conn.getCPUMap()
print("CPUs: " + str(map[0]))
print("Available: " + str(map[1]))
print()
def start_vm(vmname):
print("Starting VM %s" % vmname)
vm = VMInstance.VMInstance('b1dc4e21-544f-47aa-9bb7-8af0bc443b78', zk, hostname);
while True:
# Tick loop
time.sleep(1)
pass
conn.close()
zk.stop()