def __init__(self): self.config = Config()
def __init__(self, key=None, data=None): super(VM, self).__init__(key, data) self.conf = Config() self.hv = Hypervisor()
class Api(object): def __init__(self): self.config = Config() def ping(self, msg): return msg def vm_list(self, args): ret = [] vms = Vms(args) for _, val in vms.items(): vm = VM(val) vm._load() vm['uuid'] = vm.key if vm['ip'] == 'None': vm['ip'] = 'DHCP' ret.append(vm.data) return ret def vm_get(self, args): vm = VM(key=args['name']) vm._load() return vm.data def vm_edit(self, args): vm = VM(key=args['name']) vm._load() vm[args['key']] = args['value'] vm.save() vm.update_xml() return vm[args['key']] def vm_create(self, args): vm = VM() return vm.create(name=args['name'], ram=args['ram'], cpus=args['cpus'], base=args['base']) def vm_start(self, args): vm = VM(key=args['name']) return vm.start() def vm_stop(self, args): vm = VM(key=args['name']) return vm.stop() def vm_destroy(self, args): vm = VM(key=args['name']) return vm.destroy() def vm_restart(self, args): vm = VM(key=args['name']) return vm.restart() def vm_delete(self, args): vm = VM(key=args['name']) return vm.delete() def config_set(self, args): self.config[args['key']] = args['value'] return self.config[args['key']] def config_get(self, args): key = args['key'] if key: return {key: self.config[key]} self.config._load() return self.config.data def node_list(self, args): return self.ping(args) def node_get(self, args): return self.ping(args) def node_deactivate(self, args): return self.ping(args) def node_activate(self, args): return self.ping(args)
class VM(Storable): ''' 'name': 'image': <uuid> 'base': <uuid> 'cpus': <int> 'ram': <int> 'ip': <str> 'mac': <str> 'vnc': <int> ''' def __init__(self, key=None, data=None): super(VM, self).__init__(key, data) self.conf = Config() self.hv = Hypervisor() def create(self, base=None, cpus=None, ram=None, name=None, ip=None, mac=None, vnc=None): self._store() if not name: debug("Missing name") return if self.store.exists("Vms", "active", name): debug("VM Already Exists") return # set defaults if not base: base = self.conf['vm_default_base'] if not cpus: cpus = self.conf['vm_default_cpus'] if not ram: ram = self.conf['vm_default_ram'] if not vnc: vnc = 5900+self.conf.incr('vnc') if not mac: mac = util.uniq_mac() images = Images('base') self.data['base'] = images[base] self.data['cpus'] = cpus self.data['ram'] = ram self.data['ip'] = ip self.data['mac'] = mac self.data['vnc'] = vnc self.data['state'] = 'off' self.data['name'] = name img = Volume() img.create(self.data['base']) self.data['image'] = img.key debug(self.data) domain = self.xml() debug(domain) self.update_xml() vms = Vms('active') vms[self.data['name']] = self.key self.hv.define(domain) self.save() img.save() vms.save() def update_xml(self): xmld = "%s/%s.xml" % (self.conf['domain_xml_path'], self.key) with open(xmld, 'w') as fp: fp.write(self.xml()) os.chmod(xmld, 0744) debug(self.data) def xml(self): if not self.data: self._load() t = {} t.update(self.data) t['key'] = self.key t['path'] = self.conf['image_path'] debug(t) domain = DOMAIN_TPL % t return domain def start(self): self.hv.start(self) def stop(self): self.hv.stop(self.key) def destroy(self): self.hv.destroy(self.key) self.hv.undefine(self.key) def delete(self): self._load() try: self.hv.undefine(self.key) except: pass try: xmld = "%s/%s.xml" % (self.conf['domain_xml_path'], self.key) os.unlink(xmld) except: pass vms = Vms('active') debug(self.key) debug(self.data) vms.remove(self.data['name']) img = Volume(self.data['image']) img.delete() super(VM, self).delete()