def info(self, name, output='plain', fields=None, values=False): if fields is not None: fields = fields.split(',') nova = self.nova try: vm = nova.servers.find(name=name) except: common.pprint("VM %s not found" % name, color='red') return {'result': 'failure', 'reason': "VM %s not found" % name} if self.debug: print(vars(vm)) yamlinfo = { 'name': vm.name, 'status': vm.status, 'template': self.glance.images.get(vm.image['id']).name } flavor = nova.flavors.get(vm.flavor['id']) yamlinfo['memory'] = flavor.ram yamlinfo['cpus'] = flavor.vcpus yamlinfo['nets'] = [] index = 0 for key in list(vm.addresses): entry1 = vm.addresses[key] for entry2 in entry1: mac = entry2['OS-EXT-IPS-MAC:mac_addr'] if entry2['OS-EXT-IPS:type'] == 'floating': yamlinfo['ip'] = entry2['addr'] else: net = { 'device': 'eth%s' % index, 'mac': mac, 'net': key, 'type': entry2['addr'] } yamlinfo['nets'].append(net) index += 1 metadata = vm.metadata if metadata is not None: if 'plan' in metadata: yamlinfo['plan'] = metadata['plan'] if 'profile' in metadata: yamlinfo['profile'] = metadata['profile'] common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}
def vmstable(): """ retrieves all vms in table """ config = Kconfig() k = config.k vms = [] for vm in k.list(): vm['info'] = print_info(vm, output='plain', pretty=True) vms.append(vm) return render_template('vmstable.html', vms=vms)
def vmstable(): """ retrieves all vms in table """ config = Kconfig() k = config.k reportdir = config.reportdir vms = [] for vm in k.list(): name = vm['name'] if os.path.exists('%s/%s.txt' % (reportdir, name)): if os.path.exists('%s/%s.running' % (reportdir, name)): vm['report'] = 'Running' else: vm['report'] = 'OK' vm['info'] = print_info(vm, output='plain', pretty=True) vms.append(vm) return render_template('vmstable.html', vms=vms)
def info(self, name, output='plain', fields=None, values=False): """ :param name: :param output: :param fields: :param values: :return: """ if fields is not None: fields = fields.split(',') nova = self.nova cinder = self.cinder try: vm = nova.servers.find(name=name) except: common.pprint("VM %s not found" % name, color='red') return {'result': 'failure', 'reason': "VM %s not found" % name} if self.debug: print(vars(vm)) yamlinfo = {'name': vm.name, 'status': vm.status} source = self.glance.images.get( vm.image['id']).name if 'id' in vm.image else '' yamlinfo['template'] = source flavor = nova.flavors.get(vm.flavor['id']) yamlinfo['flavor'] = flavor.name yamlinfo['memory'] = flavor.ram yamlinfo['cpus'] = flavor.vcpus yamlinfo['nets'] = [] index = 0 for key in list(vm.addresses): entry1 = vm.addresses[key] for entry2 in entry1: mac = entry2['OS-EXT-IPS-MAC:mac_addr'] if entry2['OS-EXT-IPS:type'] == 'floating': yamlinfo['ip'] = entry2['addr'] else: net = { 'device': 'eth%s' % index, 'mac': mac, 'net': key, 'type': entry2['addr'] } yamlinfo['nets'].append(net) index += 1 disks = [] for disk in vm._info['os-extended-volumes:volumes_attached']: diskid = disk['id'] volume = cinder.volumes.get(diskid) disksize = volume.size devname = volume.name disks.append({ 'device': devname, 'size': disksize, 'format': '', 'type': '', 'path': diskid }) if disks: yamlinfo['disks'] = disks metadata = vm.metadata if metadata is not None: if 'plan' in metadata: yamlinfo['plan'] = metadata['plan'] if 'profile' in metadata: yamlinfo['profile'] = metadata['profile'] common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}
def info(self, name, output='plain', fields=None, values=False): cpus = random.choice([1, 2, 4, 8]) memory = random.choice([512, 1024, 2048, 4096, 8192]) state = self.status(name) if state == 'up': ip = random_ip() else: ip = None template = random.choice(self.templates + ['']) plan = get_random_name() profile = 'kvirt' yamlinfo = { 'name': name, 'template': template, 'plan': plan, 'profile': profile, 'status': state, 'cpus': cpus, 'memory': memory } if ip is not None: yamlinfo['ip'] = ip disks, nets = [], [] numnets = random.randint(1, 2) numdisks = random.randint(1, 3) for net in range(numnets): device = "eth%s" % net network = random.choice(right) network_type = 'routed' macs = [] for i in range(6): element = random.choice('0123456789abcdef') + random.choice( '0123456789abcdef') macs.append(element) mac = ':'.join(macs) nets.append({ 'device': device, 'mac': mac, 'net': network, 'type': network_type }) for disk in range(numdisks): letter = chr(disk + ord('a')) device = 'vd%s' % letter disksize = random.choice([10, 20, 30, 40, 50]) diskformat = 'file' drivertype = 'qcow2' path = '/var/lib/libvirt/images/%s_%s.img' % (name, disk) disks.append({ 'device': device, 'size': disksize, 'format': diskformat, 'type': drivertype, 'path': path }) yamlinfo['nets'] = nets yamlinfo['disks'] = disks common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}
def info(self, name, output='plain', fields=None, values=False): """ :param name: :param output: :param fields: :param values: :return: """ conn = self.conn vmsearch = self.vms_service.list(search='name=%s' % name) if not vmsearch: common.pprint("VM %s not found" % name, color='red') return {'result': 'failure', 'reason': "VM %s not found" % name} vm = vmsearch[0] if self.debug: print(vars(vm)) vm_service = self.vms_service.vm_service(vm.id) yamlinfo = { 'name': vm.name, 'disks': [], 'nets': [], 'status': vm.status } # yamlinfo['autostart'] = '' if vm.status == 'up': host = conn.follow_link(vm.host) yamlinfo['host'] = host.name tags_service = vm_service.tags_service() for tag in tags_service.list(): if tag.name.startswith('plan_'): yamlinfo['plan'] = tag.name.split('_')[1] if tag.name.startswith('profile_'): yamlinfo['profile'] = tag.name.split('_')[1] template = conn.follow_link(vm.template) source = template.name yamlinfo['template'] = source yamlinfo['memory'] = int(vm._memory / 1024 / 1024) cores = vm.cpu.topology.cores # sockets = vm.cpu.topology.sockets yamlinfo['cpus'] = cores yamlinfo['creationdate'] = vm._creation_time.strftime("%d-%m-%Y %H:%M") devices = self.vms_service.vm_service( vm.id).reported_devices_service().list() ips = [] for device in devices: if device.ips: for ip in device.ips: if str(ip.version) == 'v4' and ip.address not in [ '172.17.0.1', '127.0.0.1' ]: ips.append(ip.address) nics = self.vms_service.vm_service(vm.id).nics_service().list() profiles_service = self.conn.system_service().vnic_profiles_service() netprofiles = {} if ips: yamlinfo['ip'] = ips[-1] for profile in profiles_service.list(): netprofiles[profile.id] = profile.name for nic in nics: device = nic.name mac = nic.mac.address network = netprofiles[nic.vnic_profile.id] network_type = nic.interface yamlinfo['nets'].append({ 'device': device, 'mac': mac, 'net': network, 'type': network_type }) attachments = self.vms_service.vm_service( vm.id).disk_attachments_service().list() for attachment in attachments: disk = conn.follow_link(attachment.disk) device = disk.name disksize = int(disk.provisioned_size / 2**30) diskformat = disk.format drivertype = disk.content_type path = disk.id yamlinfo['disks'].append({ 'device': device, 'size': disksize, 'format': diskformat, 'type': drivertype, 'path': path }) common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}
def info(self, name, output='plain', fields=None, values=False): if fields is not None: fields = fields.split(',') yamlinfo = {} core = self.core crds = self.crds namespace = self.namespace crds = self.crds try: vm = crds.get_namespaced_custom_object(DOMAIN, VERSION, namespace, 'virtualmachines', name) except: common.pprint("VM %s not found" % name, color='red') return {'result': 'failure', 'reason': "VM %s not found" % name} if self.debug: pretty_print(vm) metadata = vm.get("metadata") annotations = metadata.get("annotations") spec = vm.get("spec") volumes = spec["volumes"] name = metadata["name"] # creationdate = metadata["creationTimestamp"].strftime("%d-%m-%Y %H:%M") creationdate = metadata["creationTimestamp"] status = vm['status'] memory = vm['spec']['domain']['resources']['requests']['memory'] state = 'N/A' if 'phase' in status: state = status['phase'] # source = 'N/A' profile = annotations.get('kcli/profile') plan = annotations.get('kcli/plan') template = annotations.get('kcli/template') # report = 'N/A' ip = None host = status['nodeName'] if 'nodeName' in status else None if 'interfaces' in status: interfaces = vm['status']['interfaces'] for interface in interfaces: if 'ipAddress' in interface: ip = interface['ipAddress'] break yamlinfo = { 'name': name, 'nets': [], 'disks': [], 'state': state, 'memory': memory, 'creationdate': creationdate, 'host': host, 'status': state } if template is not None: yamlinfo['template'] = template if ip is not None: yamlinfo['ip'] = ip if plan is not None: yamlinfo['plan'] = plan if profile is not None: yamlinfo['profile'] = profile plaindisks = spec['domain']['devices']['disks'] disks = [] for d in plaindisks: if 'disk' not in d: bus = 'N/A' else: bus = d['disk']['bus'] volumename = d['volumeName'] volumeinfo = [ volume for volume in volumes if volume['name'] == volumename ][0] size = '0' if 'persistentVolumeClaim' in volumeinfo: pvcname = volumeinfo['persistentVolumeClaim']['claimName'] _type = 'pvc' pvc = core.read_namespaced_persistent_volume_claim( pvcname, namespace) size = pvc.spec.resources.requests['storage'].replace('Gi', '') elif 'cloudInitNoCloud' in volumeinfo: _type = 'cloudinit' elif 'registryDisk' in volumeinfo: _type = 'registrydisk' else: _type = 'other' disk = { 'device': d['name'], 'size': size, 'format': bus, 'type': _type, 'path': volumename } disks.append(disk) yamlinfo['disks'] = disks common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}
def info(self, name, output='plain', fields=None, values=False): yamlinfo = {} conn = self.conn resource = self.resource try: vm = conn.describe_instances(InstanceIds=[name])['Reservations'][0]['Instances'][0] except: return {'result': 'failure', 'reason': "VM %s not found" % name} if self.debug: print(vm) name = vm['InstanceId'] state = vm['State']['Name'] ip = vm['PublicIpAddress'] if 'PublicIpAddress' in vm else '' amid = vm['ImageId'] image = resource.Image(amid) source = os.path.basename(image.image_location) plan = '' profile = '' hostname = '' if 'Tags' in vm: for tag in vm['Tags']: if tag['Key'] == 'plan': plan = tag['Value'] if tag['Key'] == 'profile': profile = tag['Value'] if tag['Key'] == 'hostname': hostname = tag['Value'] yamlinfo['name'] = name yamlinfo['status'] = state yamlinfo['ip'] = ip machinetype = vm['InstanceType'] if machinetype in flavors: yamlinfo['cpus'], yamlinfo['memory'] = flavors[machinetype]['cpus'], flavors[machinetype]['memory'] # yamlinfo['autostart'] = vm['scheduling']['automaticRestart'] yamlinfo['template'] = source # yamlinfo['creationdate'] = dateparser.parse(vm['creationTimestamp']).strftime("%d-%m-%Y %H:%M") yamlinfo['plan'] = plan yamlinfo['profile'] = profile yamlinfo['hostname'] = hostname nets = [] for interface in vm['NetworkInterfaces']: network = interface['VpcId'] device = interface['NetworkInterfaceId'] mac = interface['MacAddress'] network_type = interface['PrivateIpAddresses'][0]['PrivateIpAddress'] nets.append({'device': device, 'mac': mac, 'net': network, 'type': network_type}) if nets: yamlinfo['nets'] = nets disks = [] for index, disk in enumerate(vm['BlockDeviceMappings']): devname = disk['DeviceName'] volname = disk['Ebs']['VolumeId'] volume = conn.describe_volumes(VolumeIds=[volname])['Volumes'][0] disksize = volume['Size'] diskformat = '' drivertype = volume['VolumeType'] path = volume['AvailabilityZone'] disks.append({'device': devname, 'size': disksize, 'format': diskformat, 'type': drivertype, 'path': path}) if disks: yamlinfo['disks'] = disks common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}
def info(self, name, output='plain', fields=None, values=False): yamlinfo = {} conn = self.conn project = self.project zone = self.zone try: vm = conn.instances().get(zone=zone, project=project, instance=name).execute() except: common.pprint("VM %s not found" % name, color='red') return {'result': 'failure', 'reason': "VM %s not found" % name} if self.debug: print(vm) yamlinfo['name'] = vm['name'] yamlinfo['status'] = vm['status'] machinetype = os.path.basename(vm['machineType']) if 'custom' in machinetype: yamlinfo['cpus'], yamlinfo['memory'] = machinetype.split('-')[1:] yamlinfo['autostart'] = vm['scheduling']['automaticRestart'] if 'natIP' in vm['networkInterfaces'][0]['accessConfigs'][0]: yamlinfo['ip'] = vm['networkInterfaces'][0]['accessConfigs'][0][ 'natIP'] source = os.path.basename(vm['disks'][0]['source']) source = conn.disks().get(zone=zone, project=self.project, disk=source).execute() if self.project in source['sourceImage']: yamlinfo['template'] = os.path.basename(source['sourceImage']) elif 'licenses' in vm['disks'][0]: yamlinfo['template'] = os.path.basename( vm['disks'][0]['licenses'][-1]) yamlinfo['creationdate'] = dateparser.parse( vm['creationTimestamp']).strftime("%d-%m-%Y %H:%M") nets = [] for interface in vm['networkInterfaces']: network = os.path.basename(interface['network']) device = interface['name'] mac = interface['networkIP'] network_type = '' nets.append({ 'device': device, 'mac': mac, 'net': network, 'type': network_type }) if nets: yamlinfo['nets'] = nets disks = [] for index, disk in enumerate(vm['disks']): devname = disk['deviceName'] diskname = os.path.basename(disk['source']) diskformat = disk['interface'] drivertype = disk['type'] path = os.path.basename(disk['source']) diskinfo = conn.disks().get(zone=zone, project=project, disk=diskname).execute() disksize = diskinfo['sizeGb'] disks.append({ 'device': devname, 'size': disksize, 'format': diskformat, 'type': drivertype, 'path': path }) if disks: yamlinfo['disks'] = disks if 'items' in vm['metadata']: for data in vm['metadata']['items']: if data['key'] == 'plan': yamlinfo['plan'] = data['value'] if data['key'] == 'profile': yamlinfo['profile'] = data['value'] if 'tags' in vm and 'items' in vm['tags']: yamlinfo['tags'] = ','.join(vm['tags']['items']) common.print_info(yamlinfo, output=output, fields=fields, values=values) return {'result': 'success'}