def format_info(cluster_information, oformat): if oformat == "json": return json.dumps(cluster_information) if oformat == "json-pretty": return json.dumps(cluster_information, indent=4) # Plain formatting, i.e. human-readable if cluster_information["health"] == "Optimal": health_colour = ansiprint.green() elif cluster_information["health"] == "Maintenance": health_colour = ansiprint.blue() else: health_colour = ansiprint.yellow() if cluster_information["storage_health"] == "Optimal": storage_health_colour = ansiprint.green() elif cluster_information["storage_health"] == "Maintenance": storage_health_colour = ansiprint.blue() else: storage_health_colour = ansiprint.yellow() ainformation = [] if oformat == "short": ainformation.append("{}PVC cluster status:{}".format( ansiprint.bold(), ansiprint.end())) ainformation.append("{}Cluster health:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), health_colour, cluster_information["health"], ansiprint.end(), )) if cluster_information["health_msg"]: for line in cluster_information["health_msg"]: ainformation.append(" > {}".format(line)) ainformation.append("{}Storage health:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), storage_health_colour, cluster_information["storage_health"], ansiprint.end(), )) if cluster_information["storage_health_msg"]: for line in cluster_information["storage_health_msg"]: ainformation.append(" > {}".format(line)) return "\n".join(ainformation) ainformation.append("{}PVC cluster status:{}".format( ansiprint.bold(), ansiprint.end())) ainformation.append("") ainformation.append("{}Cluster health:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), health_colour, cluster_information["health"], ansiprint.end(), )) if cluster_information["health_msg"]: for line in cluster_information["health_msg"]: ainformation.append(" > {}".format(line)) ainformation.append("{}Storage health:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), storage_health_colour, cluster_information["storage_health"], ansiprint.end(), )) if cluster_information["storage_health_msg"]: for line in cluster_information["storage_health_msg"]: ainformation.append(" > {}".format(line)) ainformation.append("") ainformation.append("{}Primary node:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["primary_node"])) ainformation.append("{}Cluster upstream IP:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["upstream_ip"])) ainformation.append("") ainformation.append("{}Total nodes:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["nodes"]["total"])) ainformation.append("{}Total VMs:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["vms"]["total"])) ainformation.append("{}Total networks:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["networks"])) ainformation.append("{}Total OSDs:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["osds"]["total"])) ainformation.append("{}Total pools:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["pools"])) ainformation.append("{}Total volumes:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["volumes"])) ainformation.append("{}Total snapshots:{} {}".format( ansiprint.purple(), ansiprint.end(), cluster_information["snapshots"])) nodes_string = "{}Nodes:{} {}/{} {}ready,run{}".format( ansiprint.purple(), ansiprint.end(), cluster_information["nodes"].get("run,ready", 0), cluster_information["nodes"].get("total", 0), ansiprint.green(), ansiprint.end(), ) for state, count in cluster_information["nodes"].items(): if state == "total" or state == "run,ready": continue nodes_string += " {}/{} {}{}{}".format( count, cluster_information["nodes"]["total"], ansiprint.yellow(), state, ansiprint.end(), ) ainformation.append("") ainformation.append(nodes_string) vms_string = "{}VMs:{} {}/{} {}start{}".format( ansiprint.purple(), ansiprint.end(), cluster_information["vms"].get("start", 0), cluster_information["vms"].get("total", 0), ansiprint.green(), ansiprint.end(), ) for state, count in cluster_information["vms"].items(): if state == "total" or state == "start": continue if state in ["disable", "migrate", "unmigrate", "provision"]: colour = ansiprint.blue() else: colour = ansiprint.yellow() vms_string += " {}/{} {}{}{}".format( count, cluster_information["vms"]["total"], colour, state, ansiprint.end()) ainformation.append("") ainformation.append(vms_string) if cluster_information["osds"]["total"] > 0: osds_string = "{}Ceph OSDs:{} {}/{} {}up,in{}".format( ansiprint.purple(), ansiprint.end(), cluster_information["osds"].get("up,in", 0), cluster_information["osds"].get("total", 0), ansiprint.green(), ansiprint.end(), ) for state, count in cluster_information["osds"].items(): if state == "total" or state == "up,in": continue osds_string += " {}/{} {}{}{}".format( count, cluster_information["osds"]["total"], ansiprint.yellow(), state, ansiprint.end(), ) ainformation.append("") ainformation.append(osds_string) ainformation.append("") return "\n".join(ainformation)
def format_list_dhcp(dhcp_lease_list): dhcp_lease_list_output = [] # Determine optimal column widths lease_hostname_length = 9 lease_ip4_address_length = 11 lease_mac_address_length = 13 lease_timestamp_length = 10 for dhcp_lease_information in dhcp_lease_list: # hostname column _lease_hostname_length = len(str( dhcp_lease_information["hostname"])) + 1 if _lease_hostname_length > lease_hostname_length: lease_hostname_length = _lease_hostname_length # ip4_address column _lease_ip4_address_length = len( str(dhcp_lease_information["ip4_address"])) + 1 if _lease_ip4_address_length > lease_ip4_address_length: lease_ip4_address_length = _lease_ip4_address_length # mac_address column _lease_mac_address_length = len( str(dhcp_lease_information["mac_address"])) + 1 if _lease_mac_address_length > lease_mac_address_length: lease_mac_address_length = _lease_mac_address_length # timestamp column _lease_timestamp_length = len(str( dhcp_lease_information["timestamp"])) + 1 if _lease_timestamp_length > lease_timestamp_length: lease_timestamp_length = _lease_timestamp_length # Format the string (header) dhcp_lease_list_output.append( "{bold}{lease_header: <{lease_header_length}}{end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), lease_header_length=lease_hostname_length + lease_ip4_address_length + lease_mac_address_length + lease_timestamp_length + 3, lease_header="Leases " + "".join([ "-" for _ in range( 7, lease_hostname_length + lease_ip4_address_length + lease_mac_address_length + lease_timestamp_length + 2, ) ]), )) dhcp_lease_list_output.append("{bold}\ {lease_hostname: <{lease_hostname_length}} \ {lease_ip4_address: <{lease_ip4_address_length}} \ {lease_mac_address: <{lease_mac_address_length}} \ {lease_timestamp: <{lease_timestamp_length}} \ {end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), lease_hostname_length=lease_hostname_length, lease_ip4_address_length=lease_ip4_address_length, lease_mac_address_length=lease_mac_address_length, lease_timestamp_length=lease_timestamp_length, lease_hostname="Hostname", lease_ip4_address="IP Address", lease_mac_address="MAC Address", lease_timestamp="Timestamp", )) for dhcp_lease_information in sorted(dhcp_lease_list, key=lambda l: l["hostname"]): dhcp_lease_list_output.append("{bold}\ {lease_hostname: <{lease_hostname_length}} \ {lease_ip4_address: <{lease_ip4_address_length}} \ {lease_mac_address: <{lease_mac_address_length}} \ {lease_timestamp: <{lease_timestamp_length}} \ {end_bold}".format( bold="", end_bold="", lease_hostname_length=lease_hostname_length, lease_ip4_address_length=lease_ip4_address_length, lease_mac_address_length=lease_mac_address_length, lease_timestamp_length=12, lease_hostname=str(dhcp_lease_information["hostname"]), lease_ip4_address=str(dhcp_lease_information["ip4_address"]), lease_mac_address=str(dhcp_lease_information["mac_address"]), lease_timestamp=str(dhcp_lease_information["timestamp"]), )) return "\n".join(dhcp_lease_list_output)
def format_list_acl(acl_list): # Handle when we get an empty entry if not acl_list: acl_list = list() acl_list_output = [] # Determine optimal column widths acl_direction_length = 10 acl_order_length = 6 acl_description_length = 12 acl_rule_length = 5 for acl_information in acl_list: # order column _acl_order_length = len(str(acl_information["order"])) + 1 if _acl_order_length > acl_order_length: acl_order_length = _acl_order_length # description column _acl_description_length = len(acl_information["description"]) + 1 if _acl_description_length > acl_description_length: acl_description_length = _acl_description_length # rule column _acl_rule_length = len(acl_information["rule"]) + 1 if _acl_rule_length > acl_rule_length: acl_rule_length = _acl_rule_length # Format the string (header) acl_list_output.append( "{bold}{acl_header: <{acl_header_length}}{end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), acl_header_length=acl_direction_length + acl_order_length + acl_description_length + acl_rule_length + 3, acl_header="ACLs " + "".join([ "-" for _ in range( 5, acl_direction_length + acl_order_length + acl_description_length + acl_rule_length + 2, ) ]), )) acl_list_output.append("{bold}\ {acl_direction: <{acl_direction_length}} \ {acl_order: <{acl_order_length}} \ {acl_description: <{acl_description_length}} \ {acl_rule: <{acl_rule_length}} \ {end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), acl_direction_length=acl_direction_length, acl_order_length=acl_order_length, acl_description_length=acl_description_length, acl_rule_length=acl_rule_length, acl_direction="Direction", acl_order="Order", acl_description="Description", acl_rule="Rule", )) for acl_information in sorted( acl_list, key=lambda l: l["direction"] + str(l["order"])): acl_list_output.append("{bold}\ {acl_direction: <{acl_direction_length}} \ {acl_order: <{acl_order_length}} \ {acl_description: <{acl_description_length}} \ {acl_rule: <{acl_rule_length}} \ {end_bold}".format( bold="", end_bold="", acl_direction_length=acl_direction_length, acl_order_length=acl_order_length, acl_description_length=acl_description_length, acl_rule_length=acl_rule_length, acl_direction=acl_information["direction"], acl_order=acl_information["order"], acl_description=acl_information["description"], acl_rule=acl_information["rule"], )) return "\n".join(acl_list_output)
def format_info(config, network_information, long_output): if not network_information: return "No network found" ( v6_flag_colour, v4_flag_colour, dhcp6_flag_colour, dhcp4_flag_colour, ) = getOutputColours(network_information) # Format a nice output: do this line-by-line then concat the elements at the end ainformation = [] ainformation.append("{}Virtual network information:{}".format( ansiprint.bold(), ansiprint.end())) ainformation.append("") # Basic information ainformation.append("{}VNI:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["vni"])) ainformation.append("{}Type:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["type"])) ainformation.append("{}MTU:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["mtu"])) ainformation.append("{}Description:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["description"])) if network_information["type"] == "managed": ainformation.append("{}Domain:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["domain"])) ainformation.append("{}DNS Servers:{} {}".format( ansiprint.purple(), ansiprint.end(), ", ".join(network_information["name_servers"]), )) if network_information["ip6"]["network"] != "None": ainformation.append("") ainformation.append("{}IPv6 network:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["ip6"]["network"], )) ainformation.append("{}IPv6 gateway:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["ip6"]["gateway"], )) ainformation.append("{}DHCPv6 enabled:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), dhcp6_flag_colour, network_information["ip6"]["dhcp_flag"], ansiprint.end(), )) if network_information["ip4"]["network"] != "None": ainformation.append("") ainformation.append("{}IPv4 network:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["ip4"]["network"], )) ainformation.append("{}IPv4 gateway:{} {}".format( ansiprint.purple(), ansiprint.end(), network_information["ip4"]["gateway"], )) ainformation.append("{}DHCPv4 enabled:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), dhcp4_flag_colour, network_information["ip4"]["dhcp_flag"], ansiprint.end(), )) if network_information["ip4"]["dhcp_flag"] == "True": ainformation.append("{}DHCPv4 range:{} {} - {}".format( ansiprint.purple(), ansiprint.end(), network_information["ip4"]["dhcp_start"], network_information["ip4"]["dhcp_end"], )) if long_output: retcode, dhcp4_reservations_list = net_dhcp_list( config, network_information["vni"], None) if dhcp4_reservations_list: ainformation.append("") ainformation.append("{}Client DHCPv4 reservations:{}".format( ansiprint.bold(), ansiprint.end())) ainformation.append("") if retcode: dhcp4_reservations_string = format_list_dhcp( dhcp4_reservations_list) for line in dhcp4_reservations_string.split("\n"): ainformation.append(line) else: ainformation.append("No leases found") retcode, firewall_rules_list = net_acl_list( config, network_information["vni"], None, None) if firewall_rules_list: ainformation.append("") ainformation.append("{}Network firewall rules:{}".format( ansiprint.bold(), ansiprint.end())) ainformation.append("") if retcode: firewall_rules_string = format_list_acl( firewall_rules_list) for line in firewall_rules_string.split("\n"): ainformation.append(line) else: ainformation.append("No ACLs found") # Join it all together return "\n".join(ainformation)
def format_list(config, network_list): if not network_list: return "No network found" network_list_output = [] # Determine optimal column widths net_vni_length = 5 net_description_length = 12 net_nettype_length = 8 net_mtu_length = 4 net_domain_length = 6 net_v6_flag_length = 6 net_dhcp6_flag_length = 7 net_v4_flag_length = 6 net_dhcp4_flag_length = 7 for network_information in network_list: # vni column _net_vni_length = len(str(network_information["vni"])) + 1 if _net_vni_length > net_vni_length: net_vni_length = _net_vni_length # description column _net_description_length = len(network_information["description"]) + 1 if _net_description_length > net_description_length: net_description_length = _net_description_length # mtu column _net_mtu_length = len(str(network_information["mtu"])) + 1 if _net_mtu_length > net_mtu_length: net_mtu_length = _net_mtu_length # domain column _net_domain_length = len(network_information["domain"]) + 1 if _net_domain_length > net_domain_length: net_domain_length = _net_domain_length # Format the string (header) network_list_output.append( "{bold}{networks_header: <{networks_header_length}} {config_header: <{config_header_length}}{end_bold}" .format( bold=ansiprint.bold(), end_bold=ansiprint.end(), networks_header_length=net_vni_length + net_description_length + 1, config_header_length=net_nettype_length + net_mtu_length + net_domain_length + net_v6_flag_length + net_dhcp6_flag_length + net_v4_flag_length + net_dhcp4_flag_length + 7, networks_header="Networks " + "".join([ "-" for _ in range(9, net_vni_length + net_description_length) ]), config_header="Config " + "".join([ "-" for _ in range( 7, net_nettype_length + net_mtu_length + net_domain_length + net_v6_flag_length + net_dhcp6_flag_length + net_v4_flag_length + net_dhcp4_flag_length + 6, ) ]), )) network_list_output.append("{bold}\ {net_vni: <{net_vni_length}} \ {net_description: <{net_description_length}} \ {net_nettype: <{net_nettype_length}} \ {net_mtu: <{net_mtu_length}} \ {net_domain: <{net_domain_length}} \ {net_v6_flag: <{net_v6_flag_length}} \ {net_dhcp6_flag: <{net_dhcp6_flag_length}} \ {net_v4_flag: <{net_v4_flag_length}} \ {net_dhcp4_flag: <{net_dhcp4_flag_length}} \ {end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), net_vni_length=net_vni_length, net_description_length=net_description_length, net_nettype_length=net_nettype_length, net_mtu_length=net_mtu_length, net_domain_length=net_domain_length, net_v6_flag_length=net_v6_flag_length, net_dhcp6_flag_length=net_dhcp6_flag_length, net_v4_flag_length=net_v4_flag_length, net_dhcp4_flag_length=net_dhcp4_flag_length, net_vni="VNI", net_description="Description", net_nettype="Type", net_mtu="MTU", net_domain="Domain", net_v6_flag="IPv6", net_dhcp6_flag="DHCPv6", net_v4_flag="IPv4", net_dhcp4_flag="DHCPv4", )) for network_information in sorted(network_list, key=lambda n: int(n["vni"])): ( v6_flag_colour, v4_flag_colour, dhcp6_flag_colour, dhcp4_flag_colour, ) = getOutputColours(network_information) if network_information["ip4"]["network"] != "None": v4_flag = "True" else: v4_flag = "False" if network_information["ip6"]["network"] != "None": v6_flag = "True" else: v6_flag = "False" network_list_output.append("{bold}\ {net_vni: <{net_vni_length}} \ {net_description: <{net_description_length}} \ {net_nettype: <{net_nettype_length}} \ {net_mtu: <{net_mtu_length}} \ {net_domain: <{net_domain_length}} \ {v6_flag_colour}{net_v6_flag: <{net_v6_flag_length}}{colour_off} \ {dhcp6_flag_colour}{net_dhcp6_flag: <{net_dhcp6_flag_length}}{colour_off} \ {v4_flag_colour}{net_v4_flag: <{net_v4_flag_length}}{colour_off} \ {dhcp4_flag_colour}{net_dhcp4_flag: <{net_dhcp4_flag_length}}{colour_off} \ {end_bold}".format( bold="", end_bold="", net_vni_length=net_vni_length, net_description_length=net_description_length, net_nettype_length=net_nettype_length, net_mtu_length=net_mtu_length, net_domain_length=net_domain_length, net_v6_flag_length=net_v6_flag_length, net_dhcp6_flag_length=net_dhcp6_flag_length, net_v4_flag_length=net_v4_flag_length, net_dhcp4_flag_length=net_dhcp4_flag_length, net_vni=network_information["vni"], net_description=network_information["description"], net_nettype=network_information["type"], net_mtu=network_information["mtu"], net_domain=network_information["domain"], net_v6_flag=v6_flag, v6_flag_colour=v6_flag_colour, net_dhcp6_flag=network_information["ip6"]["dhcp_flag"], dhcp6_flag_colour=dhcp6_flag_colour, net_v4_flag=v4_flag, v4_flag_colour=v4_flag_colour, net_dhcp4_flag=network_information["ip4"]["dhcp_flag"], dhcp4_flag_colour=dhcp4_flag_colour, colour_off=ansiprint.end(), )) return "\n".join(network_list_output)
def format_info_sriov_vf(config, vf_information, node): if not vf_information: return "No VF found" # Get information on the using VM if applicable if vf_information["usage"]["used"] == "True" and vf_information["usage"][ "domain"]: vm_information = call_api( config, "get", "/vm/{vm}".format(vm=vf_information["usage"]["domain"])).json() if isinstance(vm_information, list) and len(vm_information) > 0: vm_information = vm_information[0] else: vm_information = None # Format a nice output: do this line-by-line then concat the elements at the end ainformation = [] ainformation.append("{}SR-IOV VF information:{}".format( ansiprint.bold(), ansiprint.end())) ainformation.append("") # Basic information ainformation.append("{}PHY:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["phy"])) ainformation.append("{}PF:{} {} @ {}".format( ansiprint.purple(), ansiprint.end(), vf_information["pf"], node)) ainformation.append("{}MTU:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["mtu"])) ainformation.append("{}MAC Address:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["mac"])) ainformation.append("") # Configuration information ainformation.append("{}vLAN ID:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["config"]["vlan_id"])) ainformation.append("{}vLAN QOS priority:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["config"]["vlan_qos"])) ainformation.append("{}Minimum TX Rate:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["config"]["tx_rate_min"])) ainformation.append("{}Maximum TX Rate:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["config"]["tx_rate_max"])) ainformation.append("{}Link State:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["config"]["link_state"])) ainformation.append("{}Spoof Checking:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), getColour(vf_information["config"]["spoof_check"]), vf_information["config"]["spoof_check"], ansiprint.end(), )) ainformation.append("{}VF User Trust:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), getColour(vf_information["config"]["trust"]), vf_information["config"]["trust"], ansiprint.end(), )) ainformation.append("{}Query RSS Config:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), getColour(vf_information["config"]["query_rss"]), vf_information["config"]["query_rss"], ansiprint.end(), )) ainformation.append("") # PCIe bus information ainformation.append("{}PCIe domain:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["pci"]["domain"])) ainformation.append("{}PCIe bus:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["pci"]["bus"])) ainformation.append("{}PCIe slot:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["pci"]["slot"])) ainformation.append("{}PCIe function:{} {}".format( ansiprint.purple(), ansiprint.end(), vf_information["pci"]["function"])) ainformation.append("") # Usage information ainformation.append("{}VF Used:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), getColour(vf_information["usage"]["used"]), vf_information["usage"]["used"], ansiprint.end(), )) if vf_information["usage"]["used"] == "True" and vm_information is not None: ainformation.append("{}Using Domain:{} {} ({}) ({}{}{})".format( ansiprint.purple(), ansiprint.end(), vf_information["usage"]["domain"], vm_information["name"], getColour(vm_information["state"]), vm_information["state"], ansiprint.end(), )) else: ainformation.append("{}Using Domain:{} N/A".format( ansiprint.purple(), ansiprint.end())) # Join it all together return "\n".join(ainformation)
def format_list_sriov_vf(vf_list): # Handle when we get an empty entry if not vf_list: vf_list = list() vf_list_output = [] # Determine optimal column widths vf_phy_length = 4 vf_pf_length = 3 vf_mtu_length = 4 vf_mac_length = 11 vf_used_length = 5 vf_domain_length = 5 for vf_information in vf_list: # phy column _vf_phy_length = len(str(vf_information["phy"])) + 1 if _vf_phy_length > vf_phy_length: vf_phy_length = _vf_phy_length # pf column _vf_pf_length = len(str(vf_information["pf"])) + 1 if _vf_pf_length > vf_pf_length: vf_pf_length = _vf_pf_length # mtu column _vf_mtu_length = len(str(vf_information["mtu"])) + 1 if _vf_mtu_length > vf_mtu_length: vf_mtu_length = _vf_mtu_length # mac column _vf_mac_length = len(str(vf_information["mac"])) + 1 if _vf_mac_length > vf_mac_length: vf_mac_length = _vf_mac_length # used column _vf_used_length = len(str(vf_information["usage"]["used"])) + 1 if _vf_used_length > vf_used_length: vf_used_length = _vf_used_length # domain column _vf_domain_length = len(str(vf_information["usage"]["domain"])) + 1 if _vf_domain_length > vf_domain_length: vf_domain_length = _vf_domain_length # Format the string (header) vf_list_output.append( "{bold}{vf_header: <{vf_header_length}}{end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), vf_header_length=vf_phy_length + vf_pf_length + vf_mtu_length + vf_mac_length + vf_used_length + vf_domain_length + 5, vf_header="VFs " + "".join([ "-" for _ in range( 4, vf_phy_length + vf_pf_length + vf_mtu_length + vf_mac_length + vf_used_length + vf_domain_length + 4, ) ]), )) vf_list_output.append("{bold}\ {vf_phy: <{vf_phy_length}} \ {vf_pf: <{vf_pf_length}} \ {vf_mtu: <{vf_mtu_length}} \ {vf_mac: <{vf_mac_length}} \ {vf_used: <{vf_used_length}} \ {vf_domain: <{vf_domain_length}} \ {end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), vf_phy_length=vf_phy_length, vf_pf_length=vf_pf_length, vf_mtu_length=vf_mtu_length, vf_mac_length=vf_mac_length, vf_used_length=vf_used_length, vf_domain_length=vf_domain_length, vf_phy="Device", vf_pf="PF", vf_mtu="MTU", vf_mac="MAC Address", vf_used="Used", vf_domain="Domain", )) for vf_information in sorted(vf_list, key=lambda v: v["phy"]): vf_domain = vf_information["usage"]["domain"] if not vf_domain: vf_domain = "N/A" vf_list_output.append("{bold}\ {vf_phy: <{vf_phy_length}} \ {vf_pf: <{vf_pf_length}} \ {vf_mtu: <{vf_mtu_length}} \ {vf_mac: <{vf_mac_length}} \ {vf_used: <{vf_used_length}} \ {vf_domain: <{vf_domain_length}} \ {end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), vf_phy_length=vf_phy_length, vf_pf_length=vf_pf_length, vf_mtu_length=vf_mtu_length, vf_mac_length=vf_mac_length, vf_used_length=vf_used_length, vf_domain_length=vf_domain_length, vf_phy=vf_information["phy"], vf_pf=vf_information["pf"], vf_mtu=vf_information["mtu"], vf_mac=vf_information["mac"], vf_used=vf_information["usage"]["used"], vf_domain=vf_domain, )) return "\n".join(vf_list_output)
def format_list_sriov_pf(pf_list): # The maximum column width of the VFs column max_vfs_length = 70 # Handle when we get an empty entry if not pf_list: pf_list = list() pf_list_output = [] # Determine optimal column widths pf_phy_length = 6 pf_mtu_length = 4 pf_vfs_length = 4 for pf_information in pf_list: # phy column _pf_phy_length = len(str(pf_information["phy"])) + 1 if _pf_phy_length > pf_phy_length: pf_phy_length = _pf_phy_length # mtu column _pf_mtu_length = len(str(pf_information["mtu"])) + 1 if _pf_mtu_length > pf_mtu_length: pf_mtu_length = _pf_mtu_length # vfs column _pf_vfs_length = len(str(", ".join(pf_information["vfs"]))) + 1 if _pf_vfs_length > pf_vfs_length: pf_vfs_length = _pf_vfs_length # We handle columnizing very long lists later if pf_vfs_length > max_vfs_length: pf_vfs_length = max_vfs_length # Format the string (header) pf_list_output.append( "{bold}{pf_header: <{pf_header_length}}{end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), pf_header_length=pf_phy_length + pf_mtu_length + pf_vfs_length + 2, pf_header="PFs " + "".join([ "-" for _ in range( 4, pf_phy_length + pf_mtu_length + pf_vfs_length + 1) ]), )) pf_list_output.append("{bold}\ {pf_phy: <{pf_phy_length}} \ {pf_mtu: <{pf_mtu_length}} \ {pf_vfs: <{pf_vfs_length}} \ {end_bold}".format( bold=ansiprint.bold(), end_bold=ansiprint.end(), pf_phy_length=pf_phy_length, pf_mtu_length=pf_mtu_length, pf_vfs_length=pf_vfs_length, pf_phy="Device", pf_mtu="MTU", pf_vfs="VFs", )) for pf_information in sorted(pf_list, key=lambda p: p["phy"]): # Figure out how to nicely columnize our list nice_vfs_list = [list()] vfs_lines = 0 cur_vfs_length = 0 for vfs in pf_information["vfs"]: vfs_len = len(vfs) cur_vfs_length += vfs_len + 2 # for the comma and space if cur_vfs_length > max_vfs_length: cur_vfs_length = 0 vfs_lines += 1 nice_vfs_list.append(list()) nice_vfs_list[vfs_lines].append(vfs) # Append the lines pf_list_output.append("{bold}\ {pf_phy: <{pf_phy_length}} \ {pf_mtu: <{pf_mtu_length}} \ {pf_vfs: <{pf_vfs_length}} \ {end_bold}".format( bold="", end_bold="", pf_phy_length=pf_phy_length, pf_mtu_length=pf_mtu_length, pf_vfs_length=pf_vfs_length, pf_phy=pf_information["phy"], pf_mtu=pf_information["mtu"], pf_vfs=", ".join(nice_vfs_list[0]), )) if len(nice_vfs_list) > 1: for idx in range(1, len(nice_vfs_list)): pf_list_output.append("{bold}\ {pf_phy: <{pf_phy_length}} \ {pf_mtu: <{pf_mtu_length}} \ {pf_vfs: <{pf_vfs_length}} \ {end_bold}".format( bold="", end_bold="", pf_phy_length=pf_phy_length, pf_mtu_length=pf_mtu_length, pf_vfs_length=pf_vfs_length, pf_phy="", pf_mtu="", pf_vfs=", ".join(nice_vfs_list[idx]), )) return "\n".join(pf_list_output)
def format_list(node_list, raw): if raw: ainformation = list() for node in sorted(item["name"] for item in node_list): ainformation.append(node) return "\n".join(ainformation) node_list_output = [] # Determine optimal column widths node_name_length = 5 pvc_version_length = 8 daemon_state_length = 7 coordinator_state_length = 12 domain_state_length = 7 domains_count_length = 4 cpu_count_length = 6 load_length = 5 mem_total_length = 6 mem_used_length = 5 mem_free_length = 5 mem_alloc_length = 6 mem_prov_length = 5 for node_information in node_list: # node_name column _node_name_length = len(node_information["name"]) + 1 if _node_name_length > node_name_length: node_name_length = _node_name_length # node_pvc_version column _pvc_version_length = len(node_information.get("pvc_version", "N/A")) + 1 if _pvc_version_length > pvc_version_length: pvc_version_length = _pvc_version_length # daemon_state column _daemon_state_length = len(node_information["daemon_state"]) + 1 if _daemon_state_length > daemon_state_length: daemon_state_length = _daemon_state_length # coordinator_state column _coordinator_state_length = len( node_information["coordinator_state"]) + 1 if _coordinator_state_length > coordinator_state_length: coordinator_state_length = _coordinator_state_length # domain_state column _domain_state_length = len(node_information["domain_state"]) + 1 if _domain_state_length > domain_state_length: domain_state_length = _domain_state_length # domains_count column _domains_count_length = len(str(node_information["domains_count"])) + 1 if _domains_count_length > domains_count_length: domains_count_length = _domains_count_length # cpu_count column _cpu_count_length = len(str(node_information["cpu_count"])) + 1 if _cpu_count_length > cpu_count_length: cpu_count_length = _cpu_count_length # load column _load_length = len(str(node_information["load"])) + 1 if _load_length > load_length: load_length = _load_length # mem_total column _mem_total_length = len(str(node_information["memory"]["total"])) + 1 if _mem_total_length > mem_total_length: mem_total_length = _mem_total_length # mem_used column _mem_used_length = len(str(node_information["memory"]["used"])) + 1 if _mem_used_length > mem_used_length: mem_used_length = _mem_used_length # mem_free column _mem_free_length = len(str(node_information["memory"]["free"])) + 1 if _mem_free_length > mem_free_length: mem_free_length = _mem_free_length # mem_alloc column _mem_alloc_length = len(str( node_information["memory"]["allocated"])) + 1 if _mem_alloc_length > mem_alloc_length: mem_alloc_length = _mem_alloc_length # mem_prov column _mem_prov_length = len(str( node_information["memory"]["provisioned"])) + 1 if _mem_prov_length > mem_prov_length: mem_prov_length = _mem_prov_length # Format the string (header) node_list_output.append( "{bold}{node_header: <{node_header_length}} {state_header: <{state_header_length}} {resource_header: <{resource_header_length}} {memory_header: <{memory_header_length}}{end_bold}" .format( node_header_length=node_name_length + pvc_version_length + 1, state_header_length=daemon_state_length + coordinator_state_length + domain_state_length + 2, resource_header_length=domains_count_length + cpu_count_length + load_length + 2, memory_header_length=mem_total_length + mem_used_length + mem_free_length + mem_alloc_length + mem_prov_length + 4, bold=ansiprint.bold(), end_bold=ansiprint.end(), node_header="Nodes " + "".join( ["-" for _ in range(6, node_name_length + pvc_version_length)]), state_header="States " + "".join([ "-" for _ in range( 7, daemon_state_length + coordinator_state_length + domain_state_length + 1, ) ]), resource_header="Resources " + "".join([ "-" for _ in range( 10, domains_count_length + cpu_count_length + load_length + 1) ]), memory_header="Memory (M) " + "".join([ "-" for _ in range( 11, mem_total_length + mem_used_length + mem_free_length + mem_alloc_length + mem_prov_length + 3, ) ]), )) node_list_output.append( "{bold}{node_name: <{node_name_length}} {node_pvc_version: <{pvc_version_length}} \ {daemon_state_colour}{node_daemon_state: <{daemon_state_length}}{end_colour} {coordinator_state_colour}{node_coordinator_state: <{coordinator_state_length}}{end_colour} {domain_state_colour}{node_domain_state: <{domain_state_length}}{end_colour} \ {node_domains_count: <{domains_count_length}} {node_cpu_count: <{cpu_count_length}} {node_load: <{load_length}} \ {node_mem_total: <{mem_total_length}} {node_mem_used: <{mem_used_length}} {node_mem_free: <{mem_free_length}} {node_mem_allocated: <{mem_alloc_length}} {node_mem_provisioned: <{mem_prov_length}}{end_bold}" .format( node_name_length=node_name_length, pvc_version_length=pvc_version_length, daemon_state_length=daemon_state_length, coordinator_state_length=coordinator_state_length, domain_state_length=domain_state_length, domains_count_length=domains_count_length, cpu_count_length=cpu_count_length, load_length=load_length, mem_total_length=mem_total_length, mem_used_length=mem_used_length, mem_free_length=mem_free_length, mem_alloc_length=mem_alloc_length, mem_prov_length=mem_prov_length, bold=ansiprint.bold(), end_bold=ansiprint.end(), daemon_state_colour="", coordinator_state_colour="", domain_state_colour="", end_colour="", node_name="Name", node_pvc_version="Version", node_daemon_state="Daemon", node_coordinator_state="Coordinator", node_domain_state="Domain", node_domains_count="VMs", node_cpu_count="vCPUs", node_load="Load", node_mem_total="Total", node_mem_used="Used", node_mem_free="Free", node_mem_allocated="Alloc", node_mem_provisioned="Prov", )) # Format the string (elements) for node_information in sorted(node_list, key=lambda n: n["name"]): ( daemon_state_colour, coordinator_state_colour, domain_state_colour, mem_allocated_colour, mem_provisioned_colour, ) = getOutputColours(node_information) node_list_output.append( "{bold}{node_name: <{node_name_length}} {node_pvc_version: <{pvc_version_length}} \ {daemon_state_colour}{node_daemon_state: <{daemon_state_length}}{end_colour} {coordinator_state_colour}{node_coordinator_state: <{coordinator_state_length}}{end_colour} {domain_state_colour}{node_domain_state: <{domain_state_length}}{end_colour} \ {node_domains_count: <{domains_count_length}} {node_cpu_count: <{cpu_count_length}} {node_load: <{load_length}} \ {node_mem_total: <{mem_total_length}} {node_mem_used: <{mem_used_length}} {node_mem_free: <{mem_free_length}} {mem_allocated_colour}{node_mem_allocated: <{mem_alloc_length}}{end_colour} {mem_provisioned_colour}{node_mem_provisioned: <{mem_prov_length}}{end_colour}{end_bold}" .format( node_name_length=node_name_length, pvc_version_length=pvc_version_length, daemon_state_length=daemon_state_length, coordinator_state_length=coordinator_state_length, domain_state_length=domain_state_length, domains_count_length=domains_count_length, cpu_count_length=cpu_count_length, load_length=load_length, mem_total_length=mem_total_length, mem_used_length=mem_used_length, mem_free_length=mem_free_length, mem_alloc_length=mem_alloc_length, mem_prov_length=mem_prov_length, bold="", end_bold="", daemon_state_colour=daemon_state_colour, coordinator_state_colour=coordinator_state_colour, domain_state_colour=domain_state_colour, mem_allocated_colour=mem_allocated_colour, mem_provisioned_colour=mem_allocated_colour, end_colour=ansiprint.end(), node_name=node_information["name"], node_pvc_version=node_information.get("pvc_version", "N/A"), node_daemon_state=node_information["daemon_state"], node_coordinator_state=node_information["coordinator_state"], node_domain_state=node_information["domain_state"], node_domains_count=node_information["domains_count"], node_cpu_count=node_information["vcpu"]["allocated"], node_load=node_information["load"], node_mem_total=node_information["memory"]["total"], node_mem_used=node_information["memory"]["used"], node_mem_free=node_information["memory"]["free"], node_mem_allocated=node_information["memory"]["allocated"], node_mem_provisioned=node_information["memory"]["provisioned"], )) return "\n".join(node_list_output)
def format_info(node_information, long_output): ( daemon_state_colour, coordinator_state_colour, domain_state_colour, mem_allocated_colour, mem_provisioned_colour, ) = getOutputColours(node_information) # Format a nice output; do this line-by-line then concat the elements at the end ainformation = [] # Basic information ainformation.append("{}Name:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["name"])) ainformation.append("{}PVC Version:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["pvc_version"])) ainformation.append("{}Daemon State:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), daemon_state_colour, node_information["daemon_state"], ansiprint.end(), )) ainformation.append("{}Coordinator State:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), coordinator_state_colour, node_information["coordinator_state"], ansiprint.end(), )) ainformation.append("{}Domain State:{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), domain_state_colour, node_information["domain_state"], ansiprint.end(), )) ainformation.append("{}Active VM Count:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["domains_count"])) if long_output: ainformation.append("") ainformation.append("{}Architecture:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["arch"])) ainformation.append("{}Operating System:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["os"])) ainformation.append("{}Kernel Version:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["kernel"])) ainformation.append("") ainformation.append("{}Host CPUs:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["vcpu"]["total"])) ainformation.append("{}vCPUs:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["vcpu"]["allocated"])) ainformation.append("{}Load:{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["load"])) ainformation.append("{}Total RAM (MiB):{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["memory"]["total"])) ainformation.append("{}Used RAM (MiB):{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["memory"]["used"])) ainformation.append("{}Free RAM (MiB):{} {}".format( ansiprint.purple(), ansiprint.end(), node_information["memory"]["free"])) ainformation.append("{}Allocated RAM (MiB):{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), mem_allocated_colour, node_information["memory"]["allocated"], ansiprint.end(), )) ainformation.append("{}Provisioned RAM (MiB):{} {}{}{}".format( ansiprint.purple(), ansiprint.end(), mem_provisioned_colour, node_information["memory"]["provisioned"], ansiprint.end(), )) # Join it all together ainformation.append("") return "\n".join(ainformation)