def start(self): self.total_bytes_dl = 0 self.total_bytes = 0 self.percent = 0 self.task_id = node.send_command(self.hypervisor, 'installationdisk_download', { 'url': self.url, 'path': self.hypervisor.install_medium_path }) if self.task_id: self.save()
def initalize_hypervisor(hypervisor): conn = hypervisor.get_connection(True) if conn: # get a list of existing storage pools for pool in conn.listStoragePools(): storagepool = conn.storagePoolLookupByName(pool) xml = minidom.parseString(storagepool.XMLDesc(0)) items = xml.getElementsByTagName('name') name = items[0].childNodes[0].data items = xml.getElementsByTagName('path') path = items[0].childNodes[0].data (new_pool, created) = StoragePool.objects.get_or_create( name=name, path=path, hypervisor=hypervisor ) pool = new_pool.get_storagepool() if pool: # get a list of volumes for vol in pool.listVolumes(): if match_vol.search(vol): vol = vol.replace('.qcow2', '') (new_vol, created) = Volume.objects.get_or_create( name=vol, storagepool=new_pool ) if created: new_vol.save() new_vol.update() # get a list of existing installation disks task_id = node.send_command(hypervisor, 'installationdisk_list', {'path': hypervisor.install_medium_path}) now = time.time() while True: time.sleep(2.5) status = node.check_command(hypervisor, task_id) print status if status['state'] == 'SUCCESS': print status['args']['disks'] for disk in status['args']['disks']: print disk (installationdisk, created) = InstallationDisk.objects.get_or_create( name=disk['filename'], hypervisor=hypervisor, url='Unknown', filename=disk['filename'], total_bytes=disk['total_bytes'], user=User.objects.all()[0] # not brilliant.. ) if created: installationdisk.save() break elif status['state'] == 'FAILED' or status['state'] == 'ERROR' or time.time() - now > 60: break
def detach_network(self, address, request=None): dom = self.get_instance() if dom: template = DETACH_NETWORK_TEMPLATE.format(alias=address.ip, device=address.network.device, mac=address.mac) try: dom.detachDeviceFlags(template, 0) address.delete() resp = node.send_command(address.network.hypervisor, "network_remove_ip", {'ip': address.ip}) if resp: print resp except libvirt.libvirtError as e: print e
def get_rx_tx(self): rx = {} tx = {} resp = node.send_command(self.network.hypervisor, "network_check_ip", {'ip': self.ip}) if resp: resp = simplejson.loads(resp) if 'error' not in resp: rx = resp['rx'] tx = resp['tx'] return (rx, tx) rx['bytes'] = 0 rx['packets'] = 0 tx['bytes'] = 0 tx['packets'] = 0 return (rx, tx)
def initalize_hypervisor(hypervisor): conn = hypervisor.get_connection(True) if conn: # get a list of existing storage pools for pool in conn.listDefinedStoragePools() + conn.listStoragePools(): storagepool = conn.storagePoolLookupByName(pool) xml = minidom.parseString(storagepool.XMLDesc(0)) items = xml.getElementsByTagName('name') name = items[0].childNodes[0].data items = xml.getElementsByTagName('path') path = items[0].childNodes[0].data (new_pool, created) = StoragePool.objects.get_or_create( name=name, path=path, hypervisor=hypervisor ) if created: new_pool.save() new_pool.update(True) try: initalize_hypervisor_instances(hypervisor) except Exception as e: print "Initalize hypervisor instances error: %s" % (e) print "HELLO" # get a list of existing installation disks task_id = node.send_command(hypervisor, 'installationdisk_list', {'path': hypervisor.install_medium_path}) print task_id now = time.time() while True: time.sleep(2.5) status = node.check_command(hypervisor, task_id) if status['state'] == 'SUCCESS': for disk in status['args']['disks']: (installationdisk, created) = InstallationDisk.objects.get_or_create( name=disk['filename'], hypervisor=hypervisor, url='Unknown', filename=disk['filename'], total_bytes=disk['total_bytes'], user=User.objects.all()[0] # not brilliant.. ) if created: installationdisk.save() break elif status['state'] == 'FAILED' or status['state'] == 'ERROR' or time.time() - now > 60: break
def delete(self, request=None): task_id = node.send_command(self.hypervisor, 'installationdisk_delete', { 'path': os.path.join(self.hypervisor.install_medium_path, self.filename)}) now = time.time() while True: time.sleep(2.5) status = node.check_command(self.hypervisor, task_id) print status if status['state'] == 'SUCCESS': if request: messages.add_message(request, persistent_messages.SUCCESS, 'Deleted Installation Disk %s on %s' % (self.name, self.hypervisor)) super(InstallationDisk, self).delete() break elif status['state'] == 'FAILED' or status['state'] == 'ERROR': if request: msg = "N/A" if 'args' in status: msg = status['args']['msg'] messages.add_message(request, persistent_messages.ERROR, 'Unable to delete Installation Disk %s on %s: %s' % (self.name, self.hypervisor, msg)) break
def attach_network(self, address, request=None, first=False): dom = self.get_instance() if dom: template = ATTACH_NETWORK_TEMPLATE.format( ip=address.ip, device=address.network.device, alias=address.ip ) try: dom.attachDeviceFlags(template, 0) tree = ElementTree.fromstring(dom.XMLDesc(0)) for i in tree.findall('devices/interface'): for p in i.findall('filterref/parameter'): if p.get('value') == address.ip: mac = i.find('mac').get('address').upper() address.mac = mac address.save() resp = node.send_command(address.network.hypervisor, "network_track_ip", {'ip': address.ip}) if resp: print resp except libvirt.libvirtError as e: print e
def get_node_status_html(self): if send_command(self, 'check', {}): return '<span class="label label-success">Responding</span>' return '<span class="label label-error">Not Responding</span>'