Skip to content

Commit

Permalink
updated cli
Browse files Browse the repository at this point in the history
  • Loading branch information
furlongm committed Oct 25, 2023
1 parent cb2fc28 commit e3ae07c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 20 deletions.
3 changes: 2 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Depends: ${misc:Depends}, python3 (>= 3.6), python3-django (>= 2.2),
python3-djangorestframework, python3-django-filters, python3-debian,
python3-rpm, python3-progressbar, python3-lxml, python3-defusedxml,
python3-requests, python3-colorama, python3-magic, python3-humanize,
python3-pip, python3-memcache, memcached, libapache2-mod-wsgi-py3, apache2
python3-pip, python3-memcache, memcached, libapache2-mod-wsgi-py3, apache2,
python3-click
Suggests: python3-django-celery, python3-mysqldb, python3-psycopg2
Description: Django-based patch status monitoring tool for linux systems.
.
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ humanize==3.13.1
version-utils==0.3.0
python-magic==0.4.25
python-memcached==1.59
python-click==8.0.3
154 changes: 135 additions & 19 deletions sbin/patchman
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# along with Patchman. If not, see <http://www.gnu.org/licenses/>


import click
import os
import sys
import argparse
Expand Down Expand Up @@ -122,7 +123,7 @@ def refresh_repos(repo=None, force=False):
info_message.send(sender=None, text='')


def list_repos(repos=None):
def show_repos(repos=None):
""" Print info about a list of repositories
Defaults to all repos
"""
Expand All @@ -131,7 +132,7 @@ def list_repos(repos=None):
repo.show()


def list_hosts(hosts=None):
def show_hosts(hosts=None):
""" Print info about a list of hosts
Defaults to all hosts
"""
Expand Down Expand Up @@ -224,6 +225,21 @@ def clean_repos():
repo.delete()
update_pbar(i + 1)

def list_reports(s_host=None, processed=False):
""" List reports for all hosts, specify host for a single host.
"""
hosts = get_hosts(s_host, 'Listing Reports')

for host in hosts:
info_message.send(sender=None, text=str(host))
reports = Report.objects.filter(host=host, processed=processed)

if s_host is None:
reports = Report.objects.filter(processed=processed)

for i, report in enumerate(reports):
print(report)


def clean_reports(s_host=None):
""" Delete old reports for all hosts, specify host for a single host.
Expand Down Expand Up @@ -270,8 +286,9 @@ def clean_tags():
update_pbar(i + 1)


def host_updates_alt(host=None):
def find_host_updates_bulk(host=None):
""" Find updates for all hosts, specify host for a single host
This algo works faster for updating multiple similar hosts
"""
updated_hosts = []
hosts = get_hosts(host, 'Finding updates')
Expand Down Expand Up @@ -322,7 +339,7 @@ def host_updates_alt(host=None):
info_message.send(sender=None, text=text)


def host_updates(host=None):
def find_host_updates(host=None):
""" Find updates for all hosts, specify host for a single host
"""
hosts = get_hosts(host, 'Finding updates')
Expand Down Expand Up @@ -436,7 +453,7 @@ def toggle_host_check_dns(hosts=None, check_dns=True):
host.save()


def dns_checks(host=None):
def check_host_dns(host=None):
""" Check all hosts for reverse DNS mismatches, specify host for a single
host
"""
Expand Down Expand Up @@ -476,7 +493,7 @@ def clean_updates():
""" Removes PackageUpdate objects that are no longer
linked to any hosts
"""
package_updates = list(PackageUpdate.objects.all())
package_updates = list(PackageUpdate.objects.all().distinct())

for update in package_updates:
if update.host_set.count() == 0:
Expand All @@ -497,7 +514,7 @@ def clean_updates():
duplicate.delete()


def dbcheck():
def clean_db():
""" Runs all clean_* functions to check database consistency
"""
clean_updates()
Expand All @@ -509,7 +526,7 @@ def dbcheck():
clean_tags()


def collect_args():
def collect_args1():
""" Collect argparse arguments
"""
parser = argparse.ArgumentParser(description='Patchman CLI tool')
Expand Down Expand Up @@ -588,7 +605,7 @@ def collect_args():
return parser


def process_args(args):
def process_args1(args):
""" Process command line arguments
"""

Expand Down Expand Up @@ -672,15 +689,114 @@ def process_args(args):
return showhelp


def main():

parser = collect_args()
args = parser.parse_args()
set_verbosity(not args.quiet)
showhelp = process_args(args)
if showhelp:
parser.print_help()

@click.group()
@click.option('-q', '--quiet', is_flag=True, default=False)
@click.option('-f', '--force', is_flag=True, default=False)
@click.pass_context
def cli(ctx, quiet, force):
set_verbosity(not quiet)
ctx.ensure_object(dict)
ctx.obj['force'] = force

@cli.group('host')
def host():
pass

@host.command()
@click.option('-H', '--host')
def show(host):
show_hosts(host)

@host.command()
@click.option('-H', '--host')
def find_updates(host):
find_host_updates(host)

@host.command()
@click.option('-H', '--host')
def find_updates_bulk(host):
find_host_updates_bulk(host)

@host.command()
@click.option('-A', required=True)
@click.option('-B', required=True)
def diff(A, B):
diff_hosts(A, B)

@host.command()
@click.option('-H', '--host')
def check_dns(host):
check_host_dns(host)

@host.group('set')
def host_set():
pass

@cli.group('repo')
def repo():
pass

@repo.command()
@click.option('-R', '--repo')
@click.pass_context
def refresh(ctx, repo):
refresh_repos(repo, ctx.obj['force'])

@repo.command()
@click.option('-R', '--repo')
def show(repo):
show_repos(repo)

@host_set.command()
@click.pass_context
def dns(ctx):
click.echo('Settings host DNS')


@cli.group('report')
@click.pass_context
@click.option('-H', '--host')
def report(ctx, host=None):
pass

@report.command()
@click.option('-H', '--host')
@click.option('-a', '--all-reports', is_flag=True, default=False, help='include processed reports')
def list(host, all_reports):
list_reports(host, not all_reports)
#FIXME

@report.command()
@click.option('-H', '--host')
@click.pass_context
def process(ctx, host):
process_reports(host, ctx.obj['force'])

@report.command()
@click.option('-H', '--host')
def clean(host):
clean_reports(host)

@cli.group('database')
def database():
pass

@database.command()
def clean():
clean_db()

@cli.group('errata')
def errata():
pass

@errata.command()
@click.pass_context
def download(ctx):
update_errata(ctx.obj['force'])

@errata.command()
def apply():
mark_errata_security_updates()

if __name__ == '__main__':
main()
cli()
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ requires = /usr/bin/python3
python3-importlib-metadata
policycoreutils-python-utils
httpd
python3-click

[install]
optimize=1

0 comments on commit e3ae07c

Please sign in to comment.