def destroy(self, uuid): debug("destroying %s" % uuid) dom = self.conn.lookupByUUIDString(uuid) try: dom.destroy() except: pass
def check_pid(self): try: self.lock.acquire(timeout=0) except lockfile.AlreadyLocked: debug("Unable to lock pid file.") return False return True
def undefine(self, uuid): debug("undefining %s" % uuid) try: self.destroy(uuid) except libvirt.libvirtError: pass dom = self.conn.lookupByUUIDString(uuid) dom.undefine()
def start(self, vm): debug("starting %s" % vm.key) try: dom = self.conn.lookupByUUIDString(vm.key) except: self.define(vm.xml()) dom = self.conn.lookupByUUIDString(vm.key) dom.create()
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 create(self, base=None): # basepath should be pulled from the base image Volume() object basepath = "%s/%s.img" % (self.conf['base_path'], base) imgpath = "%s/%s.img" % (self.conf['image_path'], self.key) imgcmd = ['qemu-img', 'create', '-b', basepath, '-f', 'qcow2', imgpath] retcode = subprocess.call(imgcmd) if retcode != 0: debug("Unable to create image") return self.data['path'] = imgpath
def run(self): debug("Starting ZMQ Listener") self._setup() while not self.shutdown.is_set(): resp = {} resp['exception'] = False try: msg = json.loads(self.sock.recv(flags=zmq.NOBLOCK)) except zmq.ZMQError: time.sleep(0.001) continue debug("received: " + str(msg)) if not self.handlers.has_key(msg['method']): debug("no handler for request type") resp['return'] = 'unknown method' resp['exception'] = True else: try: resp['return'] = self.handlers[msg['method']](msg['args']) except Exception: debug("unhandled exception while processing request") resp['return'] = traceback.format_exc() resp['exception'] = True resp['method'] = msg['method'] self.sock.send(json.dumps(resp)) self.done.set()
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()
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 __exit__(self, t, v, tb): debug("Exiting daemon Context") self.lock.release() super(Daemon, self).__exit__(t, v, tb)
def __enter__(self): debug("Entering daemon Context") self.files_preserve = open_files() super(Daemon, self).__enter__()
def stop(self, uuid): debug("stopping %s" % uuid) dom = self.conn.lookupByUUIDString(uuid) dom.shutdown()
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 shut(self, a, b): debug("Shutting down") self.shutdown.set()
def add_class(self, cls): debug("Registered API Class: %r" % cls) for m in dir(cls): a = getattr(cls, m) if callable(a): self.handlers[m] = a