Skip to content

Add a list hosts CSV output with security/bugfix broken out. #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions hosts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
37 changes: 28 additions & 9 deletions sbin/patchman
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down