Clean up flush/unflush to work like the client migration script

This commit is contained in:
Joshua Boniface 2018-06-06 20:36:03 -04:00
parent fd98ae58bf
commit c3280557ec
1 changed files with 34 additions and 34 deletions

View File

@ -88,54 +88,54 @@ class NodeInstance():
# Flush all VMs on the host # Flush all VMs on the host
def flush(self): def flush(self):
for domain in self.domain_list: print('>>> Flushing node {} of running VMs.'.format(self.name))
# Determine the best target hypervisor for dom_uuid in self.domain_list:
least_mem = 2**64 most_memfree = 0
least_host = None hypervisor_list = zk.get_children('/nodes')
for node_name in self.active_node_list: current_hypervisor = zk.get('/dom_uuids/{}/hypervisor'.format(dom_uuid))[0].decode('ascii')
# It should never include itself, but just in case for hypervisor in hypervisor_list:
if node_name == self.name: state = zk.get('/nodes/{}/state'.format(hypervisor))[0].decode('ascii')
if state != 'start' or hypervisor == current_hypervisor:
continue continue
# Get our node object and free memory memfree = int(zk.get('/nodes/{}/memfree'.format(hypervisor))[0].decode('ascii'))
node = self.t_node[node_name] if memfree > most_memfree:
node_freemem = int(node.getfreemem()) most_memfree = memfree
target_hypervisor = hypervisor
# Calculate who has the most free memory
if node_freemem < least_mem:
least_mem = node_freemem
least_host = node_name
if least_host == None: if least_host == None:
print(">>> Failed to find valid migration target for %s" % domain) print('>>> Failed to find valid migration target for %s; shutting down.'.format(dom_uuid)))
transaction = self.zk.transaction() transaction = self.zk.transaction()
transaction.set_data('/domains/' + domain + '/state', 'shutdown'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'shutdown'.encode('ascii'))
transaction.commit() transaction.commit()
else: else:
print(">>> Setting migration to %s for %s" % (least_host, domain)) print('>>> Moving VM "{}" to hypervisor "{}".'.format(dom_uuid, target_hypervisor))
transaction = self.zk.transaction() transaction = self.zk.transaction()
transaction.set_data('/domains/' + domain + '/state', 'migrate'.encode('ascii')) transaction.set_data('/domains/{}/state'.format(dom_uuid), 'start'.encode('ascii'))
transaction.set_data('/domains/' + domain + '/hypervisor', least_host.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'))
result = transaction.commit() result = transaction.commit()
# Wait 1s between migrations # Wait 1s between migrations
time.sleep(1) time.sleep(1)
def unflush(self): def unflush(self):
print('>>> Restoring node %s to active service' % self.name) print('>>> Restoring node {} to active service.'.format(self.name))
for domain in self.s_domain: self.zk.set('/nodes/{}/state'.format(self.name), 'start'.encode('ascii'))
last_hypervisor = self.zk.get("/domains/" + domain + '/lasthypervisor')[0].decode('ascii') for dom_uuid in self.s_domain:
if last_hypervisor == self.name: last_hypervisor = self.zk.get('/domains/{}/lasthypervisor'.format(dom_uuid))[0].decode('ascii')
print(">>> Setting unmigration for %s" % domain) if last_hypervisor != self.name:
transaction = self.zk.transaction() continue
transaction.set_data('/domains/' + domain + '/state', 'migrate'.encode('ascii'))
transaction.set_data('/domains/' + domain + '/hypervisor', self.name.encode('ascii'))
result = transaction.commit()
# Wait 1s between migrations print('>>> Setting unmigration for VM "{}".'.format(domain))
time.sleep(1) transaction = self.zk.transaction()
transaction.set_data('/domains/{}/state'.format(dom_uuid), 'migrate'.encode('ascii'))
transaction.set_data('/domains/{}/hypervisor'.format(dom_uuid), self.name.encode('ascii'))
transaction.set_data('/domains/{}/lasthypervisor'.format(dom_uuid), ''.encode('ascii'))
result = transaction.commit()
self.zk.set("/nodes/" + self.name + "/state", 'start'.encode('ascii')) # Wait 1s between migrations
time.sleep(1)
def update_zookeeper(self): def update_zookeeper(self):
# Connect to libvirt # Connect to libvirt