Don't click.echo except in main program

Return all the strings we printed directly, so that the caller can print
them normally.
This commit is contained in:
Joshua Boniface 2020-01-05 12:35:00 -05:00
parent 46f3915ab4
commit 08570bd7b9
7 changed files with 95 additions and 123 deletions

View File

@ -159,9 +159,12 @@ def ceph_util(config):
return False, response.json()['message']
def format_raw_output(status_data):
click.echo('{bold}Ceph cluster {stype} (primary node {end}{blue}{primary}{end}{bold}){end}\n'.format(bold=ansiprint.bold(), end=ansiprint.end(), blue=ansiprint.blue(), stype=status_data['type'], primary=status_data['primary_node']))
click.echo(status_data['ceph_data'])
click.echo('')
ainformation = list()
ainformation.append('{bold}Ceph cluster {stype} (primary node {end}{blue}{primary}{end}{bold}){end}\n'.format(bold=ansiprint.bold(), end=ansiprint.end(), blue=ansiprint.blue(), stype=status_data['type'], primary=status_data['primary_node']))
ainformation.append(status_data['ceph_data'])
ainformation.append('')
return '\n'.join(ainformation)
#
# OSD functions
@ -589,7 +592,7 @@ Wr: {osd_wrops: <{osd_wrops_length}} \
)
)
click.echo('\n'.join(sorted(osd_list_output)))
return '\n'.join(sorted(osd_list_output))
#
@ -900,7 +903,7 @@ Wr: {pool_write_ops: <{pool_write_ops_length}} \
)
)
click.echo('\n'.join(sorted(pool_list_output)))
return '\n'.join(sorted(pool_list_output))
#
# Volume functions
@ -1186,7 +1189,7 @@ def format_list_volume(volume_list):
)
)
click.echo('\n'.join(sorted(volume_list_output)))
return '\n'.join(sorted(volume_list_output))
#
@ -1404,4 +1407,4 @@ def format_list_snapshot(snapshot_list):
)
)
click.echo('\n'.join(sorted(snapshot_list_output)))
return '\n'.join(sorted(snapshot_list_output))

View File

@ -156,8 +156,5 @@ def format_info(cluster_information, oformat):
ainformation.append('')
ainformation.append(osds_string)
information = '\n'.join(ainformation)
click.echo(information)
click.echo('')
ainformation.append('')
return '\n'.join(ainformation)

View File

@ -483,8 +483,7 @@ def getOutputColours(network_information):
def format_info(config, network_information, long_output):
if not network_information:
click.echo("No network found")
return
return "No network found"
v6_flag_colour, v4_flag_colour, dhcp6_flag_colour, dhcp4_flag_colour = getOutputColours(network_information)
@ -513,30 +512,36 @@ def format_info(config, network_information, long_output):
ainformation.append('{}DHCPv4 range:{} {} - {}'.format(ansiprint.purple(), ansiprint.end(), network_information['ip4']['dhcp_start'], network_information['ip4']['dhcp_end']))
if long_output:
dhcp4_reservations_list = getNetworkDHCPReservations(zk_conn, vni)
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('')
# Only show static reservations in the detailed information
dhcp4_reservations_string = formatDHCPLeaseList(zk_conn, vni, dhcp4_reservations_list, reservations=True)
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")
firewall_rules = zkhandler.listchildren(zk_conn, '/networks/{}/firewall_rules'.format(vni))
if firewall_rules:
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('')
formatted_firewall_rules = get_list_firewall_rules(zk_conn, vni)
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
click.echo('\n'.join(ainformation))
return '\n'.join(ainformation)
def format_list(config, network_list):
if not network_list:
click.echo("No network found")
return
return "No network found"
# Handle single-element lists
if not isinstance(network_list, list):
@ -653,7 +658,7 @@ def format_list(config, network_list):
)
)
click.echo('\n'.join(sorted(network_list_output)))
return '\n'.join(sorted(network_list_output))
def format_list_dhcp(dhcp_lease_list):
dhcp_lease_list_output = []
@ -665,15 +670,15 @@ def format_list_dhcp(dhcp_lease_list):
lease_timestamp_length = 13
for dhcp_lease_information in dhcp_lease_list:
# hostname column
_lease_hostname_length = len(dhcp_lease_information['hostname']) + 1
_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(dhcp_lease_information['ip4_address']) + 1
_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(dhcp_lease_information['mac_address']) + 1
_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
@ -710,14 +715,14 @@ def format_list_dhcp(dhcp_lease_list):
lease_ip4_address_length=lease_ip4_address_length,
lease_mac_address_length=lease_mac_address_length,
lease_timestamp_length=12,
lease_hostname=dhcp_lease_information['hostname'],
lease_ip4_address=dhcp_lease_information['ip4_address'],
lease_mac_address=dhcp_lease_information['mac_address'],
lease_timestamp=dhcp_lease_information['timestamp']
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'])
)
)
click.echo('\n'.join(sorted(dhcp_lease_list_output)))
return '\n'.join(sorted(dhcp_lease_list_output))
def format_list_acl(acl_list):
# Handle when we get an empty entry
@ -788,4 +793,4 @@ def format_list_acl(acl_list):
)
)
click.echo('\n'.join(sorted(acl_list_output)))
return '\n'.join(sorted(acl_list_output))

View File

@ -208,10 +208,8 @@ def format_info(node_information, long_output):
ainformation.append('{}Allocated RAM (MiB):{} {}'.format(ansiprint.purple(), ansiprint.end(), node_information['memory']['allocated']))
# Join it all together
information = '\n'.join(ainformation)
click.echo(information)
click.echo('')
ainformation.append('')
return '\n'.join(ainformation)
def format_list(node_list):
# Handle single-element lists
@ -354,4 +352,4 @@ Mem (M): {node_mem_total: <{mem_total_length}} {node_mem_used: <{mem_used_length
)
)
click.echo('\n'.join(sorted(node_list_output)))
return '\n'.join(sorted(node_list_output))

View File

@ -673,6 +673,7 @@ def format_list_template(template_data, template_type=None):
"""
template_types = [ 'system', 'network', 'storage' ]
normalized_template_data = dict()
ainformation = list()
if template_type in template_types:
template_types = [ template_type ]
@ -682,23 +683,25 @@ def format_list_template(template_data, template_type=None):
normalized_template_data = template_data
if 'system' in template_types:
click.echo('System templates:')
click.echo()
format_list_template_system(normalized_template_data['system_templates'])
ainformation.append('System templates:')
ainformation.append('')
ainformation.append(format_list_template_system(normalized_template_data['system_templates']))
if len(template_types) > 1:
click.echo()
ainformation.append('')
if 'network' in template_types:
click.echo('Network templates:')
click.echo()
format_list_template_network(normalized_template_data['network_templates'])
ainformation.append('Network templates:')
ainformation.append('')
ainformation.append(format_list_template_network(normalized_template_data['network_templates']))
if len(template_types) > 1:
click.echo()
ainformation.append('')
if 'storage' in template_types:
click.echo('Storage templates:')
click.echo()
format_list_template_storage(normalized_template_data['storage_templates'])
ainformation.append('Storage templates:')
ainformation.append('')
ainformation.append(format_list_template_storage(normalized_template_data['storage_templates']))
return '\n'.join(ainformation)
def format_list_template_system(template_data):
if isinstance(template_data, dict):
@ -836,7 +839,7 @@ Metatemplate: {template_node_limit: <{template_node_limit_length}} \
)
)
click.echo('\n'.join([template_list_output_header] + template_list_output))
return '\n'.join([template_list_output_header] + template_list_output)
return True, ''
@ -912,9 +915,7 @@ def format_list_template_network(template_template):
)
)
click.echo('\n'.join([template_list_output_header] + template_list_output))
return True, ''
return '\n'.join([template_list_output_header] + template_list_output)
def format_list_template_storage(template_template):
if isinstance(template_template, dict):
@ -1044,9 +1045,7 @@ def format_list_template_storage(template_template):
)
)
click.echo('\n'.join([template_list_output_header] + template_list_output))
return True, ''
return '\n'.join([template_list_output_header] + template_list_output)
def format_list_userdata(userdata_data, lines=None):
if isinstance(userdata_data, dict):
@ -1121,9 +1120,7 @@ def format_list_userdata(userdata_data, lines=None):
)
)
click.echo('\n'.join([userdata_list_output_header] + userdata_list_output))
return True, ''
return '\n'.join([userdata_list_output_header] + userdata_list_output)
def format_list_script(script_data, lines=None):
if isinstance(script_data, dict):
@ -1198,9 +1195,7 @@ def format_list_script(script_data, lines=None):
)
)
click.echo('\n'.join([script_list_output_header] + script_list_output))
return True, ''
return '\n'.join([script_list_output_header] + script_list_output)
def format_list_profile(profile_data):
if isinstance(profile_data, dict):
@ -1305,7 +1300,4 @@ Data: {profile_userdata: <{profile_userdata_length}} \
)
)
click.echo('\n'.join([profile_list_output_header] + profile_list_output))
return True, ''
return '\n'.join([profile_list_output_header] + profile_list_output)

View File

@ -361,8 +361,10 @@ def view_console_log(config, vm, lines=100):
pager = subprocess.Popen(['less', '-R'], stdin=subprocess.PIPE)
pager.communicate(input=loglines.encode('utf8'))
except FileNotFoundError:
click.echo("Error: `less` pager not found, dumping log ({} lines) to stdout".format(lines))
return True, loglines
ainformation = list()
ainformation.append("Error: `less` pager not found, dumping log ({} lines) to stdout".format(lines))
ainformation.append(loglines)
return False, '\n'.join(ainformation)
return True, ''
@ -556,10 +558,8 @@ def format_info(config, domain_information, long_output):
ainformation.append(' {0: <3} {1: <14} {2: <8}'.format(domain_information['controllers'].index(controller), controller['type'], controller['model']))
# Join it all together
information = '\n'.join(ainformation)
click.echo(information)
click.echo('')
ainformation.append('')
return '\n'.join(ainformation)
def format_list(config, vm_list, raw):
# Handle single-element lists
@ -582,9 +582,10 @@ def format_list(config, vm_list, raw):
# Handle raw mode since it just lists the names
if raw:
ainformation = list()
for vm in sorted(item['name'] for item in vm_list):
click.echo(vm)
return True, ''
ainformation.append(vm)
return '\n'.join(ainformation)
vm_list_output = []
@ -717,6 +718,4 @@ def format_list(config, vm_list, raw):
)
)
click.echo('\n'.join(sorted(vm_list_output)))
return True, ''
return '\n'.join(sorted(vm_list_output))

View File

@ -405,8 +405,7 @@ def node_info(node, long_output):
retcode, retdata = pvc_node.node_info(config, node)
if retcode:
pvc_node.format_info(retdata, long_output)
retdata = ''
retdata = pvc_node.format_info(retdata, long_output)
cleanup(retcode, retdata)
###############################################################################
@ -423,8 +422,7 @@ def node_list(limit):
retcode, retdata = pvc_node.node_list(config, limit)
if retcode:
pvc_node.format_list(retdata)
retdata = ''
retdata = pvc_node.format_list(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -826,8 +824,7 @@ def vm_info(domain, long_output):
retcode, retdata = pvc_vm.vm_info(config, domain)
if retcode:
pvc_vm.format_info(config, retdata, long_output)
retdata = ''
retdata = pvc_vm.format_info(config, retdata, long_output)
cleanup(retcode, retdata)
###############################################################################
@ -880,8 +877,7 @@ def vm_list(target_node, target_state, limit, raw):
retcode, retdata = pvc_vm.vm_list(config, limit, target_node, target_state)
if retcode:
pvc_vm.format_list(config, retdata, raw)
retdata = ''
retdata = pvc_vm.format_list(config, retdata, raw)
cleanup(retcode, retdata)
###############################################################################
@ -1089,8 +1085,7 @@ def net_info(vni, long_output):
retcode, retdata = pvc_network.net_info(config, vni)
if retcode:
pvc_network.format_info(config, retdata, long_output)
retdata = ''
retdata = pvc_network.format_info(config, retdata, long_output)
cleanup(retcode, retdata)
###############################################################################
@ -1107,8 +1102,7 @@ def net_list(limit):
retcode, retdata = pvc_network.net_list(config, limit)
if retcode:
pvc_network.format_list(config, retdata)
retdata = ''
retdata = pvc_network.format_list(config, retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1186,8 +1180,7 @@ def net_dhcp_list(net, limit, only_static):
retcode, retdata = pvc_network.net_dhcp_list(config, net, limit, only_static)
if retcode:
pvc_network.format_list_dhcp(retdata)
retdata = ''
retdata = pvc_network.format_list_dhcp(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1300,8 +1293,7 @@ def net_acl_list(net, limit, direction):
retcode, retdata = pvc_network.net_acl_list(config, net, limit, direction)
if retcode:
pvc_network.format_list_acl(retdata)
retdata = ''
retdata = pvc_network.format_list_acl(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1346,8 +1338,7 @@ def ceph_status():
retcode, retdata = pvc_ceph.ceph_status(config)
if retcode:
pvc_ceph.format_raw_output(retdata)
retdata = ''
retdata = pvc_ceph.format_raw_output(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1361,8 +1352,7 @@ def ceph_util():
retcode, retdata = pvc_ceph.ceph_util(config)
if retcode:
pvc_ceph.format_raw_output(retdata)
retdata = ''
retdata = pvc_ceph.format_raw_output(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1519,8 +1509,7 @@ def ceph_osd_list(limit):
retcode, retdata = pvc_ceph.ceph_osd_list(config, limit)
if retcode:
pvc_ceph.format_list_osd(retdata)
retdata = ''
retdata = pvc_ceph.format_list_osd(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1604,8 +1593,7 @@ def ceph_pool_list(limit):
retcode, retdata = pvc_ceph.ceph_pool_list(config, limit)
if retcode:
pvc_ceph.format_list_pool(retdata)
retdata = ''
retdata = pvc_ceph.format_list_pool(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1749,8 +1737,7 @@ def ceph_volume_list(limit, pool):
retcode, retdata = pvc_ceph.ceph_volume_list(config, limit, pool)
if retcode:
pvc_ceph.format_list_volume(retdata)
retdata = ''
retdata = pvc_ceph.format_list_volume(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1865,8 +1852,7 @@ def ceph_volume_snapshot_list(pool, volume, limit):
retcode, retdata = pvc_ceph.ceph_snapshot_list(config, limit, volume, pool)
if retcode:
pvc_ceph.format_list_snapshot(retdata)
retdata = ''
retdata = pvc_ceph.format_list_snapshot(retdata)
cleanup(retcode, retdata)
@ -1908,8 +1894,7 @@ def provisioner_template_list(limit):
"""
retcode, retdata = pvc_provisioner.template_list(config, limit)
if retcode:
pvc_provisioner.format_list_template(retdata)
retdata = ''
retdata = pvc_provisioner.format_list_template(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -1937,8 +1922,7 @@ def provisioner_template_system_list(limit):
"""
retcode, retdata = pvc_provisioner.template_list(config, limit, template_type='system')
if retcode:
pvc_provisioner.format_list_template(retdata, template_type='system')
retdata = ''
retdata = pvc_provisioner.format_list_template(retdata, template_type='system')
cleanup(retcode, retdata)
###############################################################################
@ -2051,8 +2035,7 @@ def provisioner_template_network_list(limit):
"""
retcode, retdata = pvc_provisioner.template_list(config, limit, template_type='network')
if retcode:
pvc_provisioner.format_list_template(retdata, template_type='network')
retdata = ''
retdata = pvc_provisioner.format_list_template(retdata, template_type='network')
cleanup(retcode, retdata)
###############################################################################
@ -2194,8 +2177,7 @@ def provisioner_template_storage_list(limit):
"""
retcode, retdata = pvc_provisioner.template_list(config, limit, template_type='storage')
if retcode:
pvc_provisioner.format_list_template(retdata, template_type='storage')
retdata = ''
retdata = pvc_provisioner.format_list_template(retdata, template_type='storage')
cleanup(retcode, retdata)
###############################################################################
@ -2358,8 +2340,7 @@ def provisioner_userdata_list(limit, full):
lines = 4
else:
lines = None
pvc_provisioner.format_list_userdata(retdata, lines)
retdata = ''
retdata = pvc_provisioner.format_list_userdata(retdata, lines)
cleanup(retcode, retdata)
###############################################################################
@ -2520,8 +2501,7 @@ def provisioner_script_list(limit, full):
lines = 4
else:
lines = None
pvc_provisioner.format_list_script(retdata, lines)
retdata = ''
retdata = pvc_provisioner.format_list_script(retdata, lines)
cleanup(retcode, retdata)
###############################################################################
@ -2673,8 +2653,7 @@ def provisioner_profile_list(limit):
"""
retcode, retdata = pvc_provisioner.profile_list(config, limit)
if retcode:
pvc_provisioner.format_list_profile(retdata)
retdata = ''
retdata = pvc_provisioner.format_list_profile(retdata)
cleanup(retcode, retdata)
###############################################################################
@ -2873,8 +2852,7 @@ def status_cluster(oformat):
retcode, retdata = pvc_cluster.get_info(config)
if retcode:
pvc_cluster.format_info(retdata, oformat)
retdata = ''
retdata = pvc_cluster.format_info(retdata, oformat)
cleanup(retcode, retdata)
###############################################################################