Last active
May 25, 2016 07:06
-
-
Save boltronics/67fc3e97d8abe400edbc1853dc9960b7 to your computer and use it in GitHub Desktop.
Prototype code for tracking virtual machines and their dependencies (eg. NFS client VMs depend on a NFS server VM)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
class VirtualMachines(object): | |
def __init__(self): | |
self.virtual_machines = {} | |
def add_vm(self, name, vm_dom, dependencies=[]): | |
"""Add a virtual machine domain entry | |
Arguments: | |
name -- should match libvirt.virDomain.name | |
vm_dom -- libvirt.virDomain instance | |
dependencies -- list of vm_dom names | |
""" | |
print "ADDING %s" % name | |
self.virtual_machines.update( | |
{ | |
name: { | |
'vm_dom': vm_dom, | |
'dependencies': [] | |
} | |
} | |
) | |
for dependency in dependencies: | |
self.add_dependency_by_name(name, dependency) | |
def rem_vm(self, name): | |
"""Delete a virtual machine domain entry""" | |
print "REMOVING %s" % name | |
del(self.virtual_machines[name]) | |
def get_vm_dom(self, name): | |
"""Return a virtual machine domain if it exists""" | |
result = None | |
if name in self.virtual_machines: | |
result = self.virtual_machines[name][vm_dom] | |
return result | |
def add_dependency_by_name(self, name, dependency): | |
"""Add virtual machine domain dependencies referencedd by name""" | |
if name not in self.virtual_machines: | |
raise ValueError( | |
"Cannot add dependency to unknown virtual machine " + \ | |
"'{}'\n".format(name) | |
) | |
elif dependency not in self.virtual_machines: | |
raise ValueError( | |
"Cannot add unknown dependency for '{}'\n".format(name) | |
) | |
elif name == dependency: | |
raise ValueError( | |
"'{}' cannot depend on itself\n".format(name) | |
) | |
else: | |
self.virtual_machines[name]['dependencies'].append( | |
dependency | |
) | |
def get_dependency_names(self, name): | |
"""Returns a list of dependency virtual machine names for name""" | |
return self.virtual_machines[name]['dependencies'] | |
vm = VirtualMachines() | |
vm.add_vm('vm0', None) | |
vm.add_vm('vm1', None) | |
vm.rem_vm('vm1') | |
print "Currently {} record(s).".format(len(vm.virtual_machines)) | |
vm.add_vm('vm2', None) | |
vm.add_vm('vm3', None) | |
vm.add_dependency_by_name('vm2', 'vm0') | |
vm.add_dependency_by_name('vm2', 'vm3') | |
print "vm2 dependencies: %r" % vm.get_dependency_names('vm2') | |
# Explode: | |
vm.add_dependency_by_name('vm2', 'vm1') | |
print "vm2 dependencies: %r" % vm.get_dependency_names('vm2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment