Skip to content

Commit bf016dc

Browse files
committed
updated cli
1 parent cb2fc28 commit bf016dc

File tree

4 files changed

+167
-21
lines changed

4 files changed

+167
-21
lines changed

debian/control

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Depends: ${misc:Depends}, python3 (>= 3.6), python3-django (>= 2.2),
1818
python3-djangorestframework, python3-django-filters, python3-debian,
1919
python3-rpm, python3-progressbar, python3-lxml, python3-defusedxml,
2020
python3-requests, python3-colorama, python3-magic, python3-humanize,
21-
python3-pip, python3-memcache, memcached, libapache2-mod-wsgi-py3, apache2
21+
python3-pip, python3-memcache, memcached, libapache2-mod-wsgi-py3, apache2,
22+
python3-click
2223
Suggests: python3-django-celery, python3-mysqldb, python3-psycopg2
2324
Description: Django-based patch status monitoring tool for linux systems.
2425
.

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ humanize==3.13.1
1515
version-utils==0.3.0
1616
python-magic==0.4.25
1717
python-memcached==1.59
18+
python-click==8.0.3

sbin/patchman

+163-20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1818

1919

20+
import click
2021
import os
2122
import sys
2223
import argparse
@@ -122,7 +123,7 @@ def refresh_repos(repo=None, force=False):
122123
info_message.send(sender=None, text='')
123124

124125

125-
def list_repos(repos=None):
126+
def show_repos(repos=None):
126127
""" Print info about a list of repositories
127128
Defaults to all repos
128129
"""
@@ -132,8 +133,17 @@ def list_repos(repos=None):
132133

133134

134135
def list_hosts(hosts=None):
135-
""" Print info about a list of hosts
136-
Defaults to all hosts
136+
""" Print a list of hosts
137+
Defaults to all hosts but hosts can be specified
138+
"""
139+
matching_hosts = get_hosts(hosts, 'Printing information')
140+
for host in matching_hosts:
141+
text = f'Host ID:{host.id!s} | hostname:{host!s}'
142+
info_message.send(sender=None, text=text)
143+
144+
145+
def show_hosts(hosts):
146+
""" Print detailed info about a list of hosts
137147
"""
138148
matching_hosts = get_hosts(hosts, 'Printing information')
139149
for host in matching_hosts:
@@ -224,6 +234,21 @@ def clean_repos():
224234
repo.delete()
225235
update_pbar(i + 1)
226236

237+
def list_reports(s_host=None, processed=False):
238+
""" List reports for all hosts, specify host for a single host.
239+
"""
240+
hosts = get_hosts(s_host, 'Listing Reports')
241+
242+
for host in hosts:
243+
info_message.send(sender=None, text=str(host))
244+
reports = Report.objects.filter(host=host, processed=processed)
245+
246+
if s_host is None:
247+
reports = Report.objects.filter(processed=processed)
248+
249+
for i, report in enumerate(reports):
250+
print(report)
251+
227252

228253
def clean_reports(s_host=None):
229254
""" Delete old reports for all hosts, specify host for a single host.
@@ -270,8 +295,9 @@ def clean_tags():
270295
update_pbar(i + 1)
271296

272297

273-
def host_updates_alt(host=None):
298+
def find_host_updates_bulk(host=None):
274299
""" Find updates for all hosts, specify host for a single host
300+
This algo works faster for updating multiple similar hosts
275301
"""
276302
updated_hosts = []
277303
hosts = get_hosts(host, 'Finding updates')
@@ -322,7 +348,7 @@ def host_updates_alt(host=None):
322348
info_message.send(sender=None, text=text)
323349

324350

325-
def host_updates(host=None):
351+
def find_host_updates(host=None):
326352
""" Find updates for all hosts, specify host for a single host
327353
"""
328354
hosts = get_hosts(host, 'Finding updates')
@@ -436,7 +462,7 @@ def toggle_host_check_dns(hosts=None, check_dns=True):
436462
host.save()
437463

438464

439-
def dns_checks(host=None):
465+
def check_host_dns(host=None):
440466
""" Check all hosts for reverse DNS mismatches, specify host for a single
441467
host
442468
"""
@@ -476,7 +502,7 @@ def clean_updates():
476502
""" Removes PackageUpdate objects that are no longer
477503
linked to any hosts
478504
"""
479-
package_updates = list(PackageUpdate.objects.all())
505+
package_updates = list(PackageUpdate.objects.all().distinct())
480506

481507
for update in package_updates:
482508
if update.host_set.count() == 0:
@@ -497,7 +523,7 @@ def clean_updates():
497523
duplicate.delete()
498524

499525

500-
def dbcheck():
526+
def clean_db():
501527
""" Runs all clean_* functions to check database consistency
502528
"""
503529
clean_updates()
@@ -509,7 +535,7 @@ def dbcheck():
509535
clean_tags()
510536

511537

512-
def collect_args():
538+
def collect_args1():
513539
""" Collect argparse arguments
514540
"""
515541
parser = argparse.ArgumentParser(description='Patchman CLI tool')
@@ -588,7 +614,7 @@ def collect_args():
588614
return parser
589615

590616

591-
def process_args(args):
617+
def process_args1(args):
592618
""" Process command line arguments
593619
"""
594620

@@ -672,15 +698,132 @@ def process_args(args):
672698
return showhelp
673699

674700

675-
def main():
676-
677-
parser = collect_args()
678-
args = parser.parse_args()
679-
set_verbosity(not args.quiet)
680-
showhelp = process_args(args)
681-
if showhelp:
682-
parser.print_help()
683-
701+
@click.group()
702+
@click.option('-q', '--quiet', is_flag=True, default=False)
703+
@click.option('-f', '--force', is_flag=True, default=False)
704+
@click.pass_context
705+
def cli(ctx, quiet, force):
706+
set_verbosity(not quiet)
707+
ctx.ensure_object(dict)
708+
ctx.obj['force'] = force
709+
710+
@cli.group('host')
711+
def host():
712+
pass
713+
714+
@host.command()
715+
@click.option('-H', '--host')
716+
def list(host):
717+
list_hosts(host)
718+
719+
@host.command()
720+
@click.option('-H', '--host', required=True)
721+
def show(host):
722+
show_hosts(host)
723+
724+
@host.command()
725+
@click.option('-H', '--host')
726+
def find_updates(host):
727+
find_host_updates(host)
728+
729+
@host.command()
730+
@click.option('-H', '--host')
731+
def find_updates_bulk(host):
732+
find_host_updates_bulk(host)
733+
734+
@host.command()
735+
@click.option('-A', required=True)
736+
@click.option('-B', required=True)
737+
def diff(A, B):
738+
diff_hosts(A, B)
739+
740+
@host.command()
741+
@click.option('-H', '--host')
742+
def check_dns(host):
743+
check_host_dns(host)
744+
745+
@host.group('set')
746+
@click.option('-H', '--host')
747+
@click.option('--use-host-repos-only', 'repos_to_use', flag_value='host_repos')
748+
@click.option('--use-osgroup-repos-only', 'repos_to_use', flag_value='osgroup_repos')
749+
@click.option('--check-dns/--no-check-dns', is_flag=True)
750+
def host_set(check_dns, repos_to_use):
751+
if not check_dns or not repos_to_use:
752+
text = 'Nothing to set, please pass an option'
753+
info_message.send(sender=None, text=text)
754+
sys.exit(1)
755+
if check_dns:
756+
toggle_host_check_dns(host, True)
757+
else:
758+
toggle_host_check_dns(args.host, False)
759+
#FIXME
760+
761+
@host_set.command()
762+
@click.pass_context
763+
@click.option('--true/--false', is_flag=True, default=True)
764+
def check_dns(ctx, true):
765+
click.echo('Settings host DNS')
766+
#FIXME
767+
768+
@cli.group('repo')
769+
def repo():
770+
pass
771+
772+
@repo.command()
773+
@click.option('-R', '--repo')
774+
@click.pass_context
775+
def refresh(ctx, repo):
776+
refresh_repos(repo, ctx.obj['force'])
777+
778+
@repo.command()
779+
@click.option('-R', '--repo')
780+
def show(repo):
781+
show_repos(repo)
782+
783+
@cli.group('report')
784+
@click.pass_context
785+
@click.option('-H', '--host')
786+
def report(ctx, host=None):
787+
pass
788+
789+
@report.command()
790+
@click.option('-H', '--host')
791+
@click.option('-a', '--all-reports', is_flag=True, default=False, help='include processed reports')
792+
def list(host, all_reports):
793+
list_reports(host, not all_reports)
794+
#FIXME
795+
796+
@report.command()
797+
@click.option('-H', '--host')
798+
@click.pass_context
799+
def process(ctx, host):
800+
process_reports(host, ctx.obj['force'])
801+
802+
@report.command()
803+
@click.option('-H', '--host')
804+
def clean(host):
805+
clean_reports(host)
806+
807+
@cli.group('database')
808+
def database():
809+
pass
810+
811+
@database.command()
812+
def clean():
813+
clean_db()
814+
815+
@cli.group('errata')
816+
def errata():
817+
pass
818+
819+
@errata.command()
820+
@click.pass_context
821+
def download(ctx):
822+
update_errata(ctx.obj['force'])
823+
824+
@errata.command()
825+
def apply():
826+
mark_errata_security_updates()
684827

685828
if __name__ == '__main__':
686-
main()
829+
cli()

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ requires = /usr/bin/python3
2424
python3-importlib-metadata
2525
policycoreutils-python-utils
2626
httpd
27+
python3-click
2728

2829
[install]
2930
optimize=1

0 commit comments

Comments
 (0)