diff --git a/hosts/models.py b/hosts/models.py index effa34f5b..b78e180b6 100644 --- a/hosts/models.py +++ b/hosts/models.py @@ -88,6 +88,29 @@ def show(self): info_message.send(sender=None, text=text) + def show_csv(self): + """ Show info about this host, in CSV format + """ + text = '{0!s}'.format(self) + text += ',{0!s}'.format(self.ipaddress) + text += ',{0!s}'.format(self.reversedns) + text += ',{0!s}'.format(self.domain) + text += ',{0!s}'.format(self.os) + text += ',{0!s}'.format(self.kernel) + text += ',{0!s}'.format(self.arch) + text += ',{0!s}'.format(self.lastreport) + text += ',{0!s}'.format(self.get_num_packages()) + text += ',{0!s}'.format(self.get_num_repos()) + text += ',{0!s}'.format(self.get_num_updates()) + text += ',{0!s}'.format(self.get_num_security_updates()) + text += ',{0!s}'.format(self.get_num_bugfix_updates()) + text += ',{0!s}'.format(self.tags) + text += ',{0!s}'.format(self.reboot_required) + text += ',{0!s}'.format(self.updated_at) + text += ',{0!s}'.format(self.host_repos_only) + + info_message.send(sender=None, text=text) + def get_absolute_url(self): return reverse('hosts:host_detail', args=[self.hostname]) diff --git a/sbin/patchman b/sbin/patchman index 94fbfab4a..ce2c9bc1a 100755 --- a/sbin/patchman +++ b/sbin/patchman @@ -45,7 +45,7 @@ from patchman.signals import \ progress_info_s, progress_update_s -def get_host(host=None, action='Performing action'): +def get_host(host=None, action='Performing action', quiet=False): """ Helper function to get a single host object """ host_obj = None @@ -63,27 +63,29 @@ def get_host(host=None, action='Performing action'): matches = Host.objects.filter(hostname__startswith=host).count() text = '{0!s} Hosts match hostname "{1!s}"'.format(matches, host) - info_message.send(sender=None, text=text) + if not quiet: + info_message.send(sender=None, text=text) return host_obj -def get_hosts(hosts=None, action='Performing action'): +def get_hosts(hosts=None, action='Performing action', quiet=False): """ Helper function to get a list of hosts """ host_objs = [] if hosts: if isinstance(hosts, str): - host_obj = get_host(hosts, action) + host_obj = get_host(hosts, action, quiet) if host_obj is not None: host_objs.append(host_obj) elif isinstance(hosts, list): for host in hosts: - host_obj = get_host(host, action) + host_obj = get_host(host, action, quiet) if host_obj is not None: host_objs.append(host_obj) else: text = '{0!s} for all Hosts\n'.format(action) - info_message.send(sender=None, text=text) + if not quiet: + info_message.send(sender=None, text=text) host_objs = Host.objects.all() return host_objs @@ -131,13 +133,24 @@ def list_repos(repos=None): repo.show() -def list_hosts(hosts=None): +def list_hosts(hosts=None, csv=False): """ Print info about a list of hosts Defaults to all hosts """ - matching_hosts = get_hosts(hosts, 'Printing information') + if csv: + matching_hosts = get_hosts(hosts, 'Printing information', True) + text = 'Hostname,IP Address,Reverse DNS,Domain,OS,Kernel,Architecture' + text += ',Last report,Packages,Repos,Updates,Security Updates,Bugfix Updates' + text += ',Tags,Needs reboot,Updated at,Host repos' + info_message.send(sender=None, text=text) + else: + matching_hosts = get_hosts(hosts, 'Printing information', False) + for host in matching_hosts: - host.show() + if csv: + host.show_csv() + else: + host.show() def clean_packages(): @@ -491,6 +504,9 @@ def collect_args(): parser.add_argument( '-lh', '--list-hosts', action='store_true', help='List all Hosts') + parser.add_argument( + '-lhc', '--list-hosts-csv', action='store_true', + help='List all Hosts in CSV format') parser.add_argument( '-u', '--host-updates', action='store_true', help='Find Host updates') @@ -547,6 +563,9 @@ def process_args(args): if args.list_hosts: list_hosts(args.host) return False + if args.list_hosts_csv: + list_hosts(args.host, True) + return False if args.diff: diff_hosts(args.diff) return False