def test_os_disk_share_with_wrong_wwn(self): # wwn for mpath2 is invalid ssh = MockSSH([("multipath -l", """\ mpath2 (350002ac000) dm-11 3PARdata,VV size=80G features='1 queue_if_no_path' hwhandler='0' wp=rw `-+- policy='round-robin 0' prio=-1 status=active |- 9:0:0:50 sdc 8:32 active undef running |- 9:0:1:50 sdf 8:80 active undef running |- 8:0:0:50 sdi 8:128 active undef running `- 8:0:1:50 sdl 8:176 active undef running mpath3 (350002ac000660910) dm-2 3PARdata,VV size=80G features='1 queue_if_no_path' hwhandler='0' wp=rw `-+- policy='round-robin 0' prio=-1 status=active |- 9:0:0:100 sdd 8:48 active undef running |- 9:0:1:100 sdg 8:96 active undef running |- 8:0:0:100 sdj 8:144 active undef running `- 8:0:1:100 sdm 8:192 active undef running"""), ("pvs --noheadings --units M --separator '|'", "\ /dev/mapper/mpath3|VolGroup00|lvm2|a-|146632.87M|0M"), ("lvs --noheadings --units M", """\ LogVol00 VolGroup00 -wi-ao 144552.49M LogVol01 VolGroup00 -wi-ao 2080.37M"""), ]) storage = hardware.get_disk_shares(ssh) self.assertEqual(storage, { 'VolGroup00': (u'50002AC000660910', 146632) })
def _get_virtual_machines( ssh, master_ip_address, hypervisor_ip_address, proxmox_version=1 ): detected_machines = [] storages = get_disk_shares(ssh, include_logical_volumes=True) stdin, stdout, stderr = ssh.exec_command("sudo /usr/sbin/qm list") for line in stdout.readlines(): line = line.strip() if line.startswith('VMID'): continue vmid, name, status, mem, bootdisk, pid = ( v.strip() for v in line.split() ) if status != 'running': continue vmid = int(vmid) try: device_info = _get_virtual_machine_info( ssh, vmid, master_ip_address, storages, hypervisor_ip_address, proxmox_version=1, ) except NoLanError as e: logger.warning(unicode(e)) else: detected_machines.append(device_info) return detected_machines
def _add_virtual_machines(ssh, parent, master, hypervisor_ip): storages = get_disk_shares(ssh) stdin, stdout, stderr = ssh.exec_command("qm list") dev_ids = [] for line in stdout: line = line.strip() if line.startswith('VMID'): continue vmid, name, status, mem, bootdisk, pid = (v.strip() for v in line.split()) if status != 'running': continue vmid = int(vmid) dev = _add_virtual_machine( ssh, vmid, parent, master, storages, hypervisor_ip, ) if dev is None: continue dev_ids.append(dev.id) for child in parent.child_set.exclude(id__in=dev_ids): logger.info( 'Deleting virtual server %r that no longer exists.' % child) child.deleted = True child.save()
def _get_virtual_machines(ssh, master_ip_address, hypervisor_ip_address): detected_machines = [] storages = get_disk_shares(ssh) stdin, stdout, stderr = ssh.exec_command("qm list") for line in stdout: line = line.strip() if line.startswith('VMID'): continue vmid, name, status, mem, bootdisk, pid = (v.strip() for v in line.split()) if status != 'running': continue vmid = int(vmid) try: device_info = _get_virtual_machine_info( ssh, vmid, master_ip_address, storages, hypervisor_ip_address, ) except NoLanError as e: logger.warning(unicode(e)) else: detected_machines.append(device_info) return detected_machines
def _add_virtual_machines(ssh, parent, master, hypervisor_ip): storages = get_disk_shares(ssh) stdin, stdout, stderr = ssh.exec_command("qm list") dev_ids = [] for line in stdout: line = line.strip() if line.startswith('VMID'): continue vmid, name, status, mem, bootdisk, pid = (v.strip() for v in line.split()) if status != 'running': continue vmid = int(vmid) dev = _add_virtual_machine( ssh, vmid, parent, master, storages, hypervisor_ip, ) if dev is None: continue dev_ids.append(dev.id) for child in parent.child_set.exclude(id__in=dev_ids): logger.info('Deleting virtual server %r that no longer exists.' % child) child.deleted = True child.save()
def _get_disk_shares(ssh): return [ { 'serial_number': wwn, 'size': size, 'volume': lv, } for lv, (wwn, size) in get_disk_shares(ssh).iteritems() ]
def run_ssh_xen(ipaddr): ssh = _connect_ssh(ipaddr) try: vms = get_running_vms(ssh) macs = get_macs(ssh) disks = get_disks(ssh) shares = hardware.get_disk_shares(ssh) finally: ssh.close() dev = {'subdevices': []} for vm_name, vm_uuid, vm_cores, vm_memory in vms: vm_device = {} vm_device['mac_addresses'] = [ mac for i, mac in enumerate(macs.get(vm_name, [])) ] vm_device['serial_number'] = vm_uuid vm_device['hostname'] = vm_name vm_device['processors'] = [ { 'family': 'XEN Virtual', 'name': 'XEN Virtual CPU', 'label': 'CPU %d' % i, 'model_name': 1, # This would be set up in Compontent.create() } for i in xrange(vm_cores) ] vm_device['memory'] = [ { 'family': 'Virtual', 'size': vm_memory, 'label': 'XEN Virtual', }, ] vm_disks = disks.get(vm_name, []) for uuid, sr_uuid, size, device in vm_disks: wwn, mount_size = shares.get('VHD-%s' % sr_uuid, (None, None)) if wwn: share = { 'serial_number': wwn, 'is_virtual': True, 'size': mount_size, 'volume': device, } if not 'disk_shares' in vm_device: vm_device['disk_shares'] = [] vm_device['disk_shares'].append(share) else: storage = {} storage['size'] = size storage['label'] = device if not 'disks' in vm_device: vm_device['disks'] = [] vm_device['disks'].append(storage) dev['subdevices'].append(vm_device) return dev
def update_shares(ssh, dev): """Update remote disk shares""" wwns = [] for lv, (wwn, size) in get_disk_shares(ssh).iteritems(): share = DiskShare.objects.get(wwn=wwn) wwns.append(wwn) mount, created = DiskShareMount.concurrent_get_or_create(share=share, device=dev) mount.size = size if not mount.volume: mount.volume = lv mount.save() for ds in dev.disksharemount_set.filter(is_virtual=False).exclude(share__wwn__in=wwns): ds.delete()
def update_shares(ssh, dev): """Update remote disk shares""" wwns = [] for lv, (wwn, size) in get_disk_shares(ssh).iteritems(): share = DiskShare.objects.get(wwn=wwn) wwns.append(wwn) mount, created = DiskShareMount.concurrent_get_or_create(share=share, device=dev) mount.size = size if not mount.volume: mount.volume = lv mount.save() for ds in dev.disksharemount_set.filter(is_virtual=False).exclude( share__wwn__in=wwns): ds.delete()
def _proxmox_2_3(ip_address, user, password): base_url = 'https://{}:{}/api2/json'.format(ip_address, 8006) try: session = _get_session(base_url, user, password) except (ConnectionError, AuthError): session = None ssh = _connect_ssh(ip_address, user, password) try: node_name = _get_node_name(ssh) node_sn = _get_node_sn(ssh) node_mac_address = _get_node_mac_address(ssh) disk_shares = get_disk_shares(ssh) # in a restricted environment a http session won't be available if not session: device_info = _get_device_info(node_name, session, ssh, base_url) vms = _get_virtual_machines(node_name, session, ssh, base_url) finally: ssh.close() if session: device_info = _get_device_info(node_name, session, ssh, base_url) vms = _get_virtual_machines(node_name, session, ssh, base_url) device_info.update({'system_ip_addresses': [ip_address]}) if node_sn: device_info.update({'serial_number': node_sn}) if node_mac_address: device_info.update({'mac_address': node_mac_address}) if disk_shares: _ = [] for lv, (wwn, size) in disk_shares.iteritems(): _.append({ 'serial_number': wwn, 'is_virtual': False, 'size': size, 'volume': lv, }) device_info.update({'disk_shares': _}) if vms: for vm in vms: vm['management'] = ip_address device_info['subdevices'] = vms return device_info
def _get_disk_shares(ssh): return [{ 'serial_number': wwn, 'size': size, 'volume': lv, } for lv, (wwn, size) in get_disk_shares(ssh).iteritems()]
def test_xen_shares(self): ssh = MockSSH([ ("multipath -l", """\ 3600144f008bf8a000000500cfff20003 dm-16 OI,COMSTAR [size=200G][features=1 queue_if_no_path][hwhandler=0][rw] \_ round-robin 0 [prio=0][active] \_ 1:0:2:7 sdf 8:80 [active][undef] \_ 2:0:2:7 sdg 8:96 [active][undef] 3600144f008bf8a000000506429600005 dm-18 OI,COMSTAR [size=1000G][features=0][hwhandler=0][rw] \_ round-robin 0 [prio=0][active] \_ 2:0:2:8 sdi 8:128 [active][undef] \_ round-robin 0 [prio=0][enabled] \_ 1:0:2:8 sdh 8:112 [active][undef] 350002ac01add042f dm-4 3PARdata,VV [size=1000G][features=1 queue_if_no_path][hwhandler=0][rw] \_ round-robin 0 [prio=0][active] \_ 1:0:1:0 sdc 8:32 [active][undef] \_ 2:0:0:0 sdd 8:48 [active][undef] \_ 1:0:0:0 sdb 8:16 [active][undef] \_ 2:0:1:0 sde 8:64 [active][undef] """), ("pvs --noheadings --units M --separator '|'", """\ /dev/dm-16|VG_XenStorage-e7596296-2426-4a30-66d9-d12b5ba66d52|lvm2|a-|199.99M|604.00M /dev/dm-18|VG_XenStorage-d78067e7-5fc3-f684-b512-c4c5a585dce3|lvm2|a-|999.99M|959.90M /dev/dm-3|XSLocalEXT-060d5424-b9f1-ad56-629b-10c0c4e2cfa5|lvm2|a-|128.68M|0 /dev/dm-4|VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052|lvm2|a-|999.99M|108.94M """), ("lvs --noheadings --units M", """\ MGT VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi-a- 4.00M VHD-0623a1f2-24d8-42e3-bbaa-1d4c8ce24e6a VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 40.09M VHD-3db885b1-0d35-47d1-b6e8-a4fe067c0cd4 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 40.09M VHD-54b78a6b-d44c-4a6c-9fb4-d7d8d37dde19 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 50.11M VHD-5ad6d8d2-0e40-4bb7-8358-1cf5e9a971f3 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 24.05M VHD-662ce76e-294d-4a36-b658-8f2d6e7bbd03 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 10.03M VHD-776f8031-fce3-4f69-b89e-a00970e249df VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 24.05M VHD-8ada0d25-8d72-41a3-94ef-d6a68d829a9f VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi-ao 66.39M VHD-9557f2c2-7c00-40da-bfa4-5edc7ac4850b VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi-ao 29.37M VHD-99fe0180-4880-4101-b84d-b38592024128 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 48.94M VHD-a91d7e03-bcda-42e1-8d7a-8e9e90557f1b VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi-ao 50.11M VHD-a9bfce35-9529-43dc-b3b4-0b733f9b2969 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 40.09M VHD-c86ad50f-6be7-48ce-afb3-d656ce5a2b7f VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi-ao 48.94M VHD-cfa5d5e0-7f01-46ee-b3e5-5d78bd58305d VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 45.02M VHD-e257aeea-74cb-484d-a454-c4651434b3be VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 39.15M VHD-e9d94ff8-8c0e-416c-9c1a-2c94111466f2 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 44.04M VHD-fabc14ba-e004-4481-a80c-efc11eb8cc00 VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi--- 250.50M VHD-fd45752c-342f-4de5-b11d-5da78716ba1a VG_XenStorage-486926cd-e064-61a7-0f8a-9e26c5c50052 -wi-ao 40.09M MGT VG_XenStorage-d78067e7-5fc3-f684-b512-c4c5a585dce3 -wi-a- 4.00M VHD-03da1032-f25b-4886-8038-5ba774432e37 VG_XenStorage-d78067e7-5fc3-f684-b512-c4c5a585dce3 -wi--- 40.09M MGT VG_XenStorage-e7596296-2426-4a30-66d9-d12b5ba66d52 -wi-a- 4.00M VHD-e4d621b5-a74d-4344-8079-c4d71235648d VG_XenStorage-e7596296-2426-4a30-66d9-d12b5ba66d52 -wi--- 199.39M 060d5424-b9f1-ad56-629b-10c0c4e2cfa5 XSLocalEXT-060d5424-b9f1-ad56-629b-10c0c4e2cfa5 -wi-ao 128.68M """), ]) shares = hardware.get_disk_shares(ssh, include_logical_volumes=True) self.maxDiff = None self.assertEqual(shares, { 'MGT': ('600144F008BF8A000000500CFFF20003', 4), 'VHD-03da1032-f25b-4886-8038-5ba774432e37': ( '600144F008BF8A000000506429600005', 40), 'VHD-0623a1f2-24d8-42e3-bbaa-1d4c8ce24e6a': ('50002AC01ADD042F', 40), 'VHD-3db885b1-0d35-47d1-b6e8-a4fe067c0cd4': ('50002AC01ADD042F', 40), 'VHD-54b78a6b-d44c-4a6c-9fb4-d7d8d37dde19': ('50002AC01ADD042F', 50), 'VHD-5ad6d8d2-0e40-4bb7-8358-1cf5e9a971f3': ('50002AC01ADD042F', 24), 'VHD-662ce76e-294d-4a36-b658-8f2d6e7bbd03': ('50002AC01ADD042F', 10), 'VHD-776f8031-fce3-4f69-b89e-a00970e249df': ('50002AC01ADD042F', 24), 'VHD-8ada0d25-8d72-41a3-94ef-d6a68d829a9f': ('50002AC01ADD042F', 66), 'VHD-9557f2c2-7c00-40da-bfa4-5edc7ac4850b': ('50002AC01ADD042F', 29), 'VHD-99fe0180-4880-4101-b84d-b38592024128': ('50002AC01ADD042F', 48), 'VHD-a91d7e03-bcda-42e1-8d7a-8e9e90557f1b': ('50002AC01ADD042F', 50), 'VHD-a9bfce35-9529-43dc-b3b4-0b733f9b2969': ('50002AC01ADD042F', 40), 'VHD-c86ad50f-6be7-48ce-afb3-d656ce5a2b7f': ('50002AC01ADD042F', 48), 'VHD-cfa5d5e0-7f01-46ee-b3e5-5d78bd58305d': ('50002AC01ADD042F', 45), 'VHD-e257aeea-74cb-484d-a454-c4651434b3be': ('50002AC01ADD042F', 39), 'VHD-e4d621b5-a74d-4344-8079-c4d71235648d': ( '600144F008BF8A000000500CFFF20003', 199), 'VHD-e9d94ff8-8c0e-416c-9c1a-2c94111466f2': ('50002AC01ADD042F', 44), 'VHD-fabc14ba-e004-4481-a80c-efc11eb8cc00': ('50002AC01ADD042F', 250), 'VHD-fd45752c-342f-4de5-b11d-5da78716ba1a': ('50002AC01ADD042F', 40) })
def _ssh_xen(ssh, ip_address): sudo_mode = _enable_sudo_mode(ssh) uuid = _get_current_host_uuid(ssh, ip_address, sudo_mode) if not uuid: raise Error('Could not find this host UUID.') vms = _get_running_vms(ssh, uuid, sudo_mode) macs = _get_macs(ssh, sudo_mode) disks = _get_disks(ssh, sudo_mode) shares = get_disk_shares(ssh, include_logical_volumes=True) device_info = { 'subdevices': [], 'type': DeviceType.unknown.raw, 'system_ip_addresses': [ip_address], } for vm_name, vm_uuid, vm_cores, vm_memory in vms: vm_device = { 'model_name': 'XEN Virtual Server', } vm_device['mac_addresses'] = [ MACAddressField.normalize(mac) for i, mac in enumerate(macs.get(vm_name, [])) ] vm_device['serial_number'] = vm_uuid vm_device['hostname'] = vm_name vm_device['processors'] = [{ 'family': 'XEN Virtual', 'name': 'XEN Virtual CPU', 'label': 'CPU %d' % i, 'model_name': 'XEN Virtual', 'cores': 1, 'index': i, } for i in xrange(vm_cores)] vm_device['memory'] = [ { 'family': 'Virtual', 'size': vm_memory, 'label': 'XEN Virtual', }, ] vm_disks = disks.get(vm_name, []) for uuid, sr_uuid, size, device in vm_disks: wwn, mount_size = shares.get('VHD-%s' % uuid, (None, None)) if wwn: share = { 'serial_number': wwn, 'is_virtual': True, 'size': mount_size, 'volume': device, } if 'disk_shares' not in vm_device: vm_device['disk_shares'] = [] vm_device['disk_shares'].append(share) else: storage = { 'size': size, 'label': device, 'family': 'XEN Virtual Disk', } if 'disks' not in vm_device: vm_device['disks'] = [] vm_device['disks'].append(storage) device_info['subdevices'].append(vm_device) return device_info
def run_ssh_xen(ipaddr, parent): ssh = _connect_ssh(ipaddr.address) try: vms = get_running_vms(ssh) macs = get_macs(ssh) disks = get_disks(ssh) shares = hardware.get_disk_shares(ssh) finally: ssh.close() for dev in parent.child_set.exclude( sn__in=[vm_uuid for (vm_name, vm_uuid, vm_cores, vm_memory) in vms] ): dev.deleted = True dev.save() for vm_name, vm_uuid, vm_cores, vm_memory in vms: ethernets = [Eth('vif %d' % i, mac, 0) for i, mac in enumerate(macs.get(vm_name, []))] dev = Device.create(ethernets=ethernets, parent=parent, sn=vm_uuid, model_type=DeviceType.virtual_server, model_name='XEN Virtual Server', priority=SAVE_PRIORITY) dev.name = vm_name dev.save(priority=SAVE_PRIORITY) cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create( speed=0, type=ComponentType.processor.id, family='XEN Virtual', cores=0) if cpu_model_created: cpu_model.name = 'XEN Virtual CPU' cpu_model.save() for i in xrange(vm_cores): cpu, created = Processor.concurrent_get_or_create(device=dev, index=i + 1) if created: cpu.label = 'CPU %d' % i cpu.model = cpu_model cpu.family = 'XEN Virtual' cpu.save() for cpu in dev.processor_set.filter(index__gt=vm_cores+1): cpu.delete() mem_model, mem_model_created = ComponentModel.concurrent_get_or_create( speed=0, type=ComponentType.memory.id, family='XEN Virtual', cores=0, size=0) if mem_model_created: mem_model.name = 'XEN Virtual Memory' mem_model.save() memory, created = Memory.concurrent_get_or_create(device=dev, index=1) memory.size = vm_memory memory.model = mem_model memory.label = 'XEN Memory' memory.save() for mem in dev.memory_set.filter(index__gt=1): mem.delete() disk_model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.disk.id, family='XEN virtual disk') if created: disk_model.name = 'XEN virtual disk' disk_model.save() vm_disks = disks.get(vm_name, []) wwns = [] for uuid, sr_uuid, size, device in vm_disks: wwn, mount_size = shares.get('VHD-%s' % sr_uuid, (None, None)) if wwn: try: share = DiskShare.objects.get(wwn=wwn) wwns.append(wwn) except DiskShare.DoesNotExist: logger.warning('A share with serial %r does not exist.' % wwn) DiscoveryWarning( message="A share with serial %r does not exist." % wwn, plugin=__name__, device=dev, ip=ipaddr, ).save() continue mount, created = DiskShareMount.concurrent_get_or_create( share=share, device=dev) mount.is_virtual = True mount.server = parent mount.size = mount_size mount.volume = device mount.save() else: storage, created = Storage.concurrent_get_or_create( device=dev, mount_point=device, sn=uuid) storage.size = size storage.model = disk_model storage.label = device storage.save() for disk in dev.storage_set.exclude(sn__in={ uuid for uuid, x , y , z in vm_disks }): disk.delete() for ds in dev.disksharemount_set.filter( server=parent).exclude(share__wwn__in=wwns): ds.delete() for ds in dev.disksharemount_set.filter( is_virtual=True).exclude(share__wwn__in=wwns): ds.delete() return ', '.join(vm_name for (vm_name, vm_uuid, vm_cores, vm_memory) in vms)
def _ssh_xen(ssh, ip_address): sudo_mode = _enable_sudo_mode(ssh) uuid = _get_current_host_uuid(ssh, ip_address, sudo_mode) if not uuid: raise Error('Could not find this host UUID.') vms = _get_running_vms(ssh, uuid, sudo_mode) macs = _get_macs(ssh, sudo_mode) disks = _get_disks(ssh, sudo_mode) shares = get_disk_shares(ssh, include_logical_volumes=True) device_info = { 'subdevices': [], 'type': DeviceType.unknown.raw, 'system_ip_addresses': [ip_address], } for vm_name, vm_uuid, vm_cores, vm_memory in vms: vm_device = { 'model_name': 'XEN Virtual Server', } vm_device['mac_addresses'] = [ MACAddressField.normalize( mac ) for i, mac in enumerate(macs.get(vm_name, [])) ] vm_device['serial_number'] = vm_uuid vm_device['hostname'] = vm_name vm_device['processors'] = [ { 'family': 'XEN Virtual', 'name': 'XEN Virtual CPU', 'label': 'CPU %d' % i, 'model_name': 'XEN Virtual', 'cores': 1, 'index': i, } for i in xrange(vm_cores) ] vm_device['memory'] = [ { 'family': 'Virtual', 'size': vm_memory, 'label': 'XEN Virtual', }, ] vm_disks = disks.get(vm_name, []) for uuid, sr_uuid, size, device in vm_disks: wwn, mount_size = shares.get('VHD-%s' % uuid, (None, None)) if wwn: share = { 'serial_number': wwn, 'is_virtual': True, 'size': mount_size, 'volume': device, } if 'disk_shares' not in vm_device: vm_device['disk_shares'] = [] vm_device['disk_shares'].append(share) else: storage = { 'size': size, 'label': device, 'family': 'XEN Virtual Disk', } if 'disks' not in vm_device: vm_device['disks'] = [] vm_device['disks'].append(storage) device_info['subdevices'].append(vm_device) return device_info
def run_ssh_xen(ipaddr, parent): ssh = _connect_ssh(ipaddr.address) try: vms = get_running_vms(ssh) macs = get_macs(ssh) disks = get_disks(ssh) shares = hardware.get_disk_shares(ssh) finally: ssh.close() for dev in parent.child_set.exclude(sn__in=[ vm_uuid for (vm_name, vm_uuid, vm_cores, vm_memory) in vms ]): dev.deleted = True dev.save() for vm_name, vm_uuid, vm_cores, vm_memory in vms: ethernets = [ Eth('vif %d' % i, mac, 0) for i, mac in enumerate(macs.get(vm_name, [])) ] dev = Device.create(ethernets=ethernets, parent=parent, sn=vm_uuid, model_type=DeviceType.virtual_server, model_name='XEN Virtual Server', priority=SAVE_PRIORITY) dev.name = vm_name dev.save(priority=SAVE_PRIORITY) cpu_model, cpu_model_created = ComponentModel.concurrent_get_or_create( speed=0, type=ComponentType.processor.id, family='XEN Virtual', cores=0) if cpu_model_created: cpu_model.name = 'XEN Virtual CPU' cpu_model.save() for i in xrange(vm_cores): cpu, created = Processor.concurrent_get_or_create(device=dev, index=i + 1) if created: cpu.label = 'CPU %d' % i cpu.model = cpu_model cpu.family = 'XEN Virtual' cpu.save() for cpu in dev.processor_set.filter(index__gt=vm_cores + 1): cpu.delete() mem_model, mem_model_created = ComponentModel.concurrent_get_or_create( speed=0, type=ComponentType.memory.id, family='XEN Virtual', cores=0, size=0) if mem_model_created: mem_model.name = 'XEN Virtual Memory' mem_model.save() memory, created = Memory.concurrent_get_or_create(device=dev, index=1) memory.size = vm_memory memory.model = mem_model memory.label = 'XEN Memory' memory.save() for mem in dev.memory_set.filter(index__gt=1): mem.delete() disk_model, created = ComponentModel.concurrent_get_or_create( type=ComponentType.disk.id, family='XEN virtual disk') if created: disk_model.name = 'XEN virtual disk' disk_model.save() vm_disks = disks.get(vm_name, []) wwns = [] for uuid, sr_uuid, size, device in vm_disks: wwn, mount_size = shares.get('VHD-%s' % sr_uuid, (None, None)) if wwn: try: share = DiskShare.objects.get(wwn=wwn) wwns.append(wwn) except DiskShare.DoesNotExist: logger.warning('A share with serial %r does not exist.' % wwn) DiscoveryWarning( message="A share with serial %r does not exist." % wwn, plugin=__name__, device=dev, ip=ipaddr, ).save() continue mount, created = DiskShareMount.concurrent_get_or_create( share=share, device=dev) mount.is_virtual = True mount.server = parent mount.size = mount_size mount.volume = device mount.save() else: storage, created = Storage.concurrent_get_or_create( device=dev, mount_point=device, sn=uuid) storage.size = size storage.model = disk_model storage.label = device storage.save() for disk in dev.storage_set.exclude( sn__in={uuid for uuid, x, y, z in vm_disks}): disk.delete() for ds in dev.disksharemount_set.filter(server=parent).exclude( share__wwn__in=wwns): ds.delete() for ds in dev.disksharemount_set.filter(is_virtual=True).exclude( share__wwn__in=wwns): ds.delete() return ', '.join(vm_name for (vm_name, vm_uuid, vm_cores, vm_memory) in vms)