def pprint_floating_ip(ip, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "Floating IP %s" % ip.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = ip.userid vtype_info = OrderedDict([ ("id", ip.id), ("address", ip.address), ("network", ip.network_id), ("port", ip.nic_id), ("server", ip.nic.machine_id), ("userid", userid), ("username", ucache.get_name(userid) if (display_mails and userid is not None) else None), ("project", ip.project), ("shared_to_project", ip.shared_to_project), ("deleted", ip.deleted), ("created", ip.created), ("updated", ip.updated), ]) pprint_table(stdout, vtype_info.items(), separator=" | ", title=title)
def pprint_network(network, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of Network %s in DB" % network.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = network.userid db_network = OrderedDict([ ("name", network.name), ("backend-name", network.backend_id), ("state", network.state), ("userid", userid), ("username", ucache.get_name(userid) if display_mails else None), ("public", network.public), ("floating_ip_pool", network.floating_ip_pool), ("external_router", network.external_router), ("drained", network.drained), ("MAC prefix", network.mac_prefix), ("flavor", network.flavor), ("link", network.link), ("mode", network.mode), ("deleted", network.deleted), ("tags", "), ".join(network.backend_tag)), ("action", network.action) ]) pprint_table(stdout, db_network.items(), None, separator=" | ", title=title)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a server ID") server = get_vm(args[0]) flavor = '%s (%s)' % (server.flavor.id, server.flavor.name) userid = server.userid imageid = server.imageid try: image_name = get_image(imageid, userid).get('name') except: image_name = "None" image = '%s (%s)' % (imageid, image_name) usercache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN) kv = { 'id': server.id, 'name': server.name, 'owner_uuid': userid, 'owner_name': usercache.get_name(userid), 'created': utils.format_date(server.created), 'updated': utils.format_date(server.updated), 'image': image, 'host id': server.hostid, 'flavor': flavor, 'deleted': utils.format_bool(server.deleted), 'suspended': utils.format_bool(server.suspended), 'state': format_vm_state(server), } utils.pprint_table(self.stdout, [kv.values()], kv.keys(), options["output_format"], vertical=True)
def pprint_volume(volume, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of volume %s in DB" % volume.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = volume.userid volume_type = volume.volume_type volume_dict = OrderedDict([ ("id", volume.id), ("size", volume.size), ("disk_template", volume_type.template), ("disk_provider", volume_type.provider), ("server_id", volume.machine_id), ("userid", volume.userid), ("project", volume.project), ("username", ucache.get_name(userid) if display_mails else None), ("index", volume.index), ("name", volume.name), ("state", volume.status), ("delete_on_termination", volume.delete_on_termination), ("deleted", volume.deleted), ("backendjobid", volume.backendjobid), ]) pprint_table(stdout, volume_dict.items(), None, separator=" | ", title=title)
def pprint_network(network, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of Network %s in DB" % network.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = network.userid db_network = OrderedDict([ ("name", network.name), ("backend-name", network.backend_id), ("state", network.state), ("userid", userid), ("username", ucache.get_name(userid) if display_mails else None), ("public", network.public), ("floating_ip_pool", network.floating_ip_pool), ("external_router", network.external_router), ("drained", network.drained), ("MAC prefix", network.mac_prefix), ("flavor", network.flavor), ("link", network.link), ("mode", network.mode), ("deleted", network.deleted), ("tags", "), ".join(network.backend_tag)), ("action", network.action)]) pprint_table(stdout, db_network.items(), None, separator=" | ", title=title)
def pprint_server(server, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of Server %s in DB" % server.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = server.userid try: image = get_image(server.imageid, server.userid)['name'] except: image = server.imageid server_dict = OrderedDict([ ("id", server.id), ("name", server.name), ("userid", server.userid), ("username", ucache.get_name(userid) if display_mails else None), ("project", server.project), ("shared_to_project", server.shared_to_project), ("flavor_id", server.flavor_id), ("flavor_name", server.flavor.name), ("imageid", server.imageid), ("image_name", image), ("created", server.created), ("state", server.operstate), ("backend", server.backend), ("deleted", server.deleted), ("action", server.action), ("task", server.task), ("rescue", server.rescue), ("rescue_image", server.rescue_image), ("task_job_id", server.task_job_id), ("backendjobid", server.backendjobid), ("backendopcode", server.backendopcode), ("backendjobstatus", server.backendjobstatus), ("backend_time", server.backendtime), ]) pprint_table(stdout, server_dict.items(), None, separator=" | ", title=title)
def pprint_port(port, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of Port %s in DB" % port.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = port.userid port = OrderedDict([("id", port.id), ("name", port.name), ("userid", port.userid), ("username", ucache.get_name(userid) if display_mails else None), ("server", port.machine_id), ("network", port.network_id), ("device_owner", port.device_owner), ("mac", port.mac), ("state", port.state)]) pprint_table(stdout, port.items(), None, separator=" | ", title=title)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a server ID") server = get_resource("server", args[0]) flavor = '%s (%s)' % (server.flavor.id, server.flavor.name) userid = server.userid imageid = server.imageid try: image_name = get_image(imageid, userid).get('name') except: image_name = "None" image = '%s (%s)' % (imageid, image_name) usercache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) kv = { 'id': server.id, 'name': server.name, 'owner_uuid': userid, 'owner_name': usercache.get_name(userid), 'project': server.project, 'shared_to_project': server.shared_to_project, 'created': utils.format_date(server.created), 'rescue': server.rescue, 'rescue_image': (server.rescue_image.id if server.rescue_image is not None else None), 'updated': utils.format_date(server.updated), 'image': image, 'host id': server.hostid, 'flavor': flavor, 'deleted': utils.format_bool(server.deleted), 'suspended': utils.format_bool(server.suspended), 'state': format_vm_state(server), 'task': server.task, 'task_job_id': server.task_job_id, } self.pprint_table([kv.values()], kv.keys(), options["output_format"], vertical=True)
def pprint_server(server, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of Server %s in DB" % server.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = server.userid try: image = get_image(server.imageid, server.userid)['name'] except: image = server.imageid server_dict = OrderedDict([ ("id", server.id), ("name", server.name), ("userid", server.userid), ("username", ucache.get_name(userid) if display_mails else None), ("project", server.project), ("shared_to_project", server.shared_to_project), ("flavor_id", server.flavor_id), ("flavor_name", server.flavor.name), ("imageid", server.imageid), ("image_name", image), ("created", server.created), ("state", server.operstate), ("backend", server.backend), ("deleted", server.deleted), ("action", server.action), ("task", server.task), ("task_job_id", server.task_job_id), ("backendjobid", server.backendjobid), ("backendopcode", server.backendopcode), ("backendjobstatus", server.backendjobstatus), ("backend_time", server.backendtime), ]) pprint_table(stdout, server_dict.items(), None, separator=" | ", title=title)
def pprint_port(port, display_mails=False, stdout=None, title=None): if stdout is None: stdout = sys.stdout if title is None: title = "State of Port %s in DB" % port.id ucache = UserCache(ASTAKOS_AUTH_URL, ASTAKOS_TOKEN) userid = port.userid port = OrderedDict([ ("id", port.id), ("name", port.name), ("userid", port.userid), ("username", ucache.get_name(userid) if display_mails else None), ("server", port.machine_id), ("network", port.network_id), ("device_owner", port.device_owner), ("mac", port.mac), ("state", port.state)]) pprint_table(stdout, port.items(), None, separator=" | ", title=title)
def __init__(self): self.images = {} usercache = UserCache(settings.ASTAKOS_AUTH_URL, settings.CYCLADES_SERVICE_TOKEN) self.system_user_uuid = \ usercache.get_uuid(settings.SYSTEM_IMAGES_OWNER)
def handle(self, *args, **options): if len(args) > 0: raise CommandError("List commands do not accept any argument") assert (self.object_class), "object_class variable must be declared" # If an user field is declared, include the USER_EMAIL_FIELD in the # available fields if self.user_uuid_field is not None: self.FIELDS[USER_EMAIL_FIELD] =\ ("_user_email", "The email of the owner") if options["list_fields"]: self.display_fields() return if options["list_filters"]: self.display_filters() return # --output option if options["fields"]: fields = options["fields"] fields = fields.split(",") self.validate_fields(fields) self.fields = options["fields"].split(",") # --display-mails option if options.get("display_mails"): self.fields.append(USER_EMAIL_FIELD) # --filter-by option if options["filter_by"]: filters, excludes = \ utils.parse_queryset_filters(options["filter_by"]) else: filters, excludes = ({}, {}) self.filters.update(filters) self.excludes.update(excludes) # --user option user = options.get("user") if user: if "@" in user: ucache = UserCache(self.astakos_auth_url, self.astakos_token) user = ucache.get_uuid(user) self.filters[self.user_uuid_field] = user # --deleted option if self.deleted_field: deleted = options.get("deleted") if deleted: self.filters[self.deleted_field] = True else: self.filters[self.deleted_field] = False # Special handling of arguments self.handle_args(self, *args, **options) select_related = getattr(self, "select_related", []) prefetch_related = getattr(self, "prefetch_related", []) objects = self.object_class.objects try: for sr in select_related: objects = objects.select_related(sr) for pr in prefetch_related: objects = objects.prefetch_related(pr) objects = objects.filter(**self.filters) objects = objects.exclude(**self.excludes) except FieldError as e: raise CommandError(e) except Exception as e: raise CommandError("Can not filter results: %s" % e) order_key = self.order_by if self.order_by is not None else 'pk' objects = objects.order_by(order_key) if USER_EMAIL_FIELD in self.fields: if '_user_email' in self.object_class._meta.get_all_field_names(): raise RuntimeError("%s has already a 'user_mail' attribute") uuids = [getattr(obj, self.user_uuid_field) for obj in objects] ucache = UserCache(self.astakos_auth_url, self.astakos_token) ucache.fetch_names(list(set(uuids))) for obj in objects: uuid = getattr(obj, self.user_uuid_field) obj._user_email = ucache.get_name(uuid) # Special handling of DB results objects = list(objects) self.handle_db_objects(objects, **options) headers = self.fields columns = [self.FIELDS[key][0] for key in headers] table = [] for obj in objects: row = [] for attr in columns: if callable(attr): row.append(attr(obj)) else: item = obj attrs = attr.split(".") for attr in attrs: item = getattr(item, attr) row.append(item) table.append(row) # Special handle of output self.handle_output(table, headers) # Print output output_format = options["output_format"] if output_format != "json" and not options["headers"]: headers = None utils.pprint_table(self.stdout, table, headers, output_format)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("Please provide a server ID") vm = common.get_vm(args[0]) displayname = options['displayname'] ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN) try: image = common.get_image(vm.imageid, vm.userid)['name'] except: image = vm.imageid sep = '-' * 80 + '\n' labels = filter(lambda x: x is not Omit, ['name', 'owner_uuid', 'owner_name' if displayname else Omit, 'flavor', 'image', 'state', 'backend', 'deleted', 'action', 'backendjobid', 'backendopcode', 'backendjobstatus', 'backend_time']) uuid = vm.userid if displayname: dname = ucache.get_name(uuid) fields = filter(lambda x: x is not Omit, [vm.name, uuid, dname if displayname else Omit, vm.flavor.name, image, common.format_vm_state(vm), str(vm.backend), str(vm.deleted), str(vm.action), str(vm.backendjobid), str(vm.backendopcode), str(vm.backendjobstatus), str(vm.backendtime)]) self.stdout.write(sep) self.stdout.write('State of Server in DB\n') self.stdout.write(sep) for l, f in zip(labels, fields): self.stdout.write(l.ljust(18) + ': ' + f.ljust(20) + '\n') self.stdout.write('\n') for nic in vm.nics.all(): params = (nic.index, nic.ipv4, nic.mac, nic.ipv6, nic.network) self.stdout.write("nic/%d: IPv4: %s, MAC: %s, IPv6:%s," " Network: %s\n" % params) client = vm.get_client() try: g_vm = client.GetInstance(vm.backend_vm_id) self.stdout.write('\n') self.stdout.write(sep) self.stdout.write('State of Server in Ganeti\n') self.stdout.write(sep) for i in GANETI_INSTANCE_FIELDS: try: value = g_vm[i] if i.find('time') != -1: value = datetime.fromtimestamp(value) self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n') except KeyError: pass except GanetiApiError as e: if e.code == 404: self.stdout.write('Server does not exist in backend %s\n' % vm.backend.clustername) else: raise e if not options['jobs']: return self.stdout.write('\n') self.stdout.write(sep) self.stdout.write('Non-archived jobs concerning Server in Ganeti\n') self.stdout.write(sep) jobs = client.GetJobs() for j in jobs: info = client.GetJobStatus(j) summary = ' '.join(info['summary']) job_is_relevant = summary.startswith("INSTANCE") and\ (summary.find(vm.backend_vm_id) != -1) if job_is_relevant: for i in GANETI_JOB_FIELDS: value = info[i] if i.find('_ts') != -1: value = merge_time(value) try: self.stdout.write(i.ljust(14) + ': ' + str(value) + '\n') except KeyError: pass self.stdout.write('\n' + sep) # Return the RAPI client to pool vm.put_client(client)
def handle(self, *args, **options): if len(args) > 0: raise CommandError("List commands do not accept any argument") assert(self.object_class), "object_class variable must be declared" # If an user field is declared, include the USER_EMAIL_FIELD in the # available fields if self.user_uuid_field is not None: self.FIELDS[USER_EMAIL_FIELD] =\ ("_user_email", "The email of the owner") if options["list_fields"]: self.display_fields() return if options["list_filters"]: self.display_filters() return # --output option if options["fields"]: fields = options["fields"] fields = fields.split(",") self.validate_fields(fields) self.fields = options["fields"].split(",") # --display-mails option if options.get("display_mails"): self.fields.append(USER_EMAIL_FIELD) # --filter-by option if options["filter_by"]: try: filters, excludes = \ utils.parse_queryset_filters(options["filter_by"]) except ValueError as e: raise CommandError(e) else: filters, excludes = ({}, {}) self.filters.update(filters) self.excludes.update(excludes) # --user option user = options.get("user") if user: if "@" in user: ucache = UserCache(self.astakos_auth_url, self.astakos_token) user = ucache.get_uuid(user) self.filters[self.user_uuid_field] = user # --deleted option if self.deleted_field: deleted = options.get("deleted") if deleted: self.filters[self.deleted_field] = True else: self.filters[self.deleted_field] = False # Special handling of arguments self.handle_args(self, *args, **options) select_related = getattr(self, "select_related", []) prefetch_related = getattr(self, "prefetch_related", []) objects = self.object_class.objects try: if select_related: objects = objects.select_related(*select_related) if prefetch_related: objects = objects.prefetch_related(*prefetch_related) objects = objects.filter(**self.filters) for key, value in self.excludes.iteritems(): objects = objects.exclude(**{key: value}) except FieldError as e: raise CommandError(e) except Exception as e: raise CommandError("Can not filter results: %s" % e) order_key = self.order_by if self.order_by is not None else 'pk' objects = objects.order_by(order_key) if USER_EMAIL_FIELD in self.fields: if '_user_email' in self.object_class._meta.get_all_field_names(): raise RuntimeError("%s has already a 'user_mail' attribute") uuids = [getattr(obj, self.user_uuid_field) for obj in objects] ucache = UserCache(self.astakos_auth_url, self.astakos_token) ucache.fetch_names(list(set(uuids))) for obj in objects: uuid = getattr(obj, self.user_uuid_field) obj._user_email = ucache.get_name(uuid) # Special handling of DB results objects = list(objects) self.handle_db_objects(objects, **options) headers = self.fields columns = [self.FIELDS[key][0] for key in headers] table = [] for obj in objects: row = [] for attr in columns: if callable(attr): row.append(attr(obj)) else: item = obj attrs = attr.split(".") for attr in attrs: item = getattr(item, attr) row.append(item) table.append(row) # Special handle of output self.handle_output(table, headers) # Print output output_format = options["output_format"] if output_format != "json" and not options["headers"]: headers = None utils.pprint_table(self.stdout, table, headers, output_format)
def handle(self, *args, **options): write = self.stdout.write if len(args) != 1: raise CommandError("Please provide a network ID.") net = get_network(args[0]) ucache = UserCache(ASTAKOS_BASE_URL, ASTAKOS_TOKEN) displayname = options['displayname'] sep = '-' * 80 + '\n' labels = filter(lambda x: x is not Omit, ['name', 'backend-name', 'state', 'owner uuid', 'owner_name' if displayname else Omit, 'subnet', 'gateway', 'mac_prefix', 'link', 'public', 'dhcp', 'flavor', 'deleted', 'action', 'pool']) uuid = net.userid if displayname: dname = ucache.get_name(uuid) fields = filter(lambda x: x is not Omit, [net.name, net.backend_id, net.state, uuid or '-', dname or '-' if displayname else Omit, str(net.subnet), str(net.gateway), str(net.mac_prefix), str(net.link), str(net.public), str(net.dhcp), str(net.flavor), str(net.deleted), str(net.action), str(splitPoolMap(net.get_pool().to_map(), 64))]) write(sep) write('State of Network in DB\n') write(sep) for l, f in zip(labels, fields): write(l.ljust(20) + ': ' + f.ljust(20) + '\n') labels = ('Backend', 'State', 'Deleted', 'JobID', 'OpCode', 'JobStatus') for back_net in BackendNetwork.objects.filter(network=net): write('\n') fields = (back_net.backend.clustername, back_net.operstate, str(back_net.deleted), str(back_net.backendjobid), str(back_net.backendopcode), str(back_net.backendjobstatus)) for l, f in zip(labels, fields): write(l.ljust(20) + ': ' + f.ljust(20) + '\n') write('\n') write(sep) write('State of Network in Ganeti\n') write(sep) for backend in Backend.objects.exclude(offline=True): with pooled_rapi_client(backend) as client: try: g_net = client.GetNetwork(net.backend_id) write("Backend: %s\n" % backend.clustername) print json.dumps(g_net, indent=2) write(sep) except GanetiApiError as e: if e.code == 404: write('Network does not exist in backend %s\n' % backend.clustername) else: raise e