1717# along with Patchman. If not, see <http://www.gnu.org/licenses/>
1818
1919
20+ import click
2021import os
2122import sys
2223import 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 """
@@ -131,7 +132,7 @@ def list_repos(repos=None):
131132 repo .show ()
132133
133134
134- def list_hosts (hosts = None ):
135+ def show_hosts (hosts = None ):
135136 """ Print info about a list of hosts
136137 Defaults to all hosts
137138 """
@@ -224,6 +225,21 @@ def clean_repos():
224225 repo .delete ()
225226 update_pbar (i + 1 )
226227
228+ def list_reports (s_host = None , processed = False ):
229+ """ List reports for all hosts, specify host for a single host.
230+ """
231+ hosts = get_hosts (s_host , 'Listing Reports' )
232+
233+ for host in hosts :
234+ info_message .send (sender = None , text = str (host ))
235+ reports = Report .objects .filter (host = host , processed = processed )
236+
237+ if s_host is None :
238+ reports = Report .objects .filter (processed = processed )
239+
240+ for i , report in enumerate (reports ):
241+ print (report )
242+
227243
228244def clean_reports (s_host = None ):
229245 """ Delete old reports for all hosts, specify host for a single host.
@@ -270,8 +286,9 @@ def clean_tags():
270286 update_pbar (i + 1 )
271287
272288
273- def host_updates_alt (host = None ):
289+ def find_host_updates_bulk (host = None ):
274290 """ Find updates for all hosts, specify host for a single host
291+ This algo works faster for updating multiple similar hosts
275292 """
276293 updated_hosts = []
277294 hosts = get_hosts (host , 'Finding updates' )
@@ -322,7 +339,7 @@ def host_updates_alt(host=None):
322339 info_message .send (sender = None , text = text )
323340
324341
325- def host_updates (host = None ):
342+ def find_host_updates (host = None ):
326343 """ Find updates for all hosts, specify host for a single host
327344 """
328345 hosts = get_hosts (host , 'Finding updates' )
@@ -436,7 +453,7 @@ def toggle_host_check_dns(hosts=None, check_dns=True):
436453 host .save ()
437454
438455
439- def dns_checks (host = None ):
456+ def check_host_dns (host = None ):
440457 """ Check all hosts for reverse DNS mismatches, specify host for a single
441458 host
442459 """
@@ -476,7 +493,7 @@ def clean_updates():
476493 """ Removes PackageUpdate objects that are no longer
477494 linked to any hosts
478495 """
479- package_updates = list (PackageUpdate .objects .all ())
496+ package_updates = list (PackageUpdate .objects .all (). distinct () )
480497
481498 for update in package_updates :
482499 if update .host_set .count () == 0 :
@@ -497,7 +514,7 @@ def clean_updates():
497514 duplicate .delete ()
498515
499516
500- def dbcheck ():
517+ def clean_db ():
501518 """ Runs all clean_* functions to check database consistency
502519 """
503520 clean_updates ()
@@ -509,7 +526,7 @@ def dbcheck():
509526 clean_tags ()
510527
511528
512- def collect_args ():
529+ def collect_args1 ():
513530 """ Collect argparse arguments
514531 """
515532 parser = argparse .ArgumentParser (description = 'Patchman CLI tool' )
@@ -588,7 +605,7 @@ def collect_args():
588605 return parser
589606
590607
591- def process_args (args ):
608+ def process_args1 (args ):
592609 """ Process command line arguments
593610 """
594611
@@ -672,15 +689,114 @@ def process_args(args):
672689 return showhelp
673690
674691
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-
692+ @click .group ()
693+ @click .option ('-q' , '--quiet' , is_flag = True , default = False )
694+ @click .option ('-f' , '--force' , is_flag = True , default = False )
695+ @click .pass_context
696+ def cli (ctx , quiet , force ):
697+ set_verbosity (not quiet )
698+ ctx .ensure_object (dict )
699+ ctx .obj ['force' ] = force
700+
701+ @cli .group ('host' )
702+ def host ():
703+ pass
704+
705+ @host .command ()
706+ @click .option ('-H' , '--host' )
707+ def show (host ):
708+ show_hosts (host )
709+
710+ @host .command ()
711+ @click .option ('-H' , '--host' )
712+ def find_updates (host ):
713+ find_host_updates (host )
714+
715+ @host .command ()
716+ @click .option ('-H' , '--host' )
717+ def find_updates_bulk (host ):
718+ find_host_updates_bulk (host )
719+
720+ @host .command ()
721+ @click .option ('-A' , required = True )
722+ @click .option ('-B' , required = True )
723+ def diff (A , B ):
724+ diff_hosts (A , B )
725+
726+ @host .command ()
727+ @click .option ('-H' , '--host' )
728+ def check_dns (host ):
729+ check_host_dns (host )
730+
731+ @host .group ('set' )
732+ def host_set ():
733+ pass
734+
735+ @cli .group ('repo' )
736+ def repo ():
737+ pass
738+
739+ @repo .command ()
740+ @click .option ('-R' , '--repo' )
741+ @click .pass_context
742+ def refresh (ctx , repo ):
743+ refresh_repos (repo , ctx .obj ['force' ])
744+
745+ @repo .command ()
746+ @click .option ('-R' , '--repo' )
747+ def show (repo ):
748+ show_repos (repo )
749+
750+ @host_set .command ()
751+ @click .pass_context
752+ def dns (ctx ):
753+ click .echo ('Settings host DNS' )
754+
755+
756+ @cli .group ('report' )
757+ @click .pass_context
758+ @click .option ('-H' , '--host' )
759+ def report (ctx , host = None ):
760+ pass
761+
762+ @report .command ()
763+ @click .option ('-H' , '--host' )
764+ @click .option ('-a' , '--all-reports' , is_flag = True , default = False , help = 'include processed reports' )
765+ def list (host , all_reports ):
766+ list_reports (host , not all_reports )
767+ #FIXME
768+
769+ @report .command ()
770+ @click .option ('-H' , '--host' )
771+ @click .pass_context
772+ def process (ctx , host ):
773+ process_reports (host , ctx .obj ['force' ])
774+
775+ @report .command ()
776+ @click .option ('-H' , '--host' )
777+ def clean (host ):
778+ clean_reports (host )
779+
780+ @cli .group ('database' )
781+ def database ():
782+ pass
783+
784+ @database .command ()
785+ def clean ():
786+ clean_db ()
787+
788+ @cli .group ('errata' )
789+ def errata ():
790+ pass
791+
792+ @errata .command ()
793+ @click .pass_context
794+ def download (ctx ):
795+ update_errata (ctx .obj ['force' ])
796+
797+ @errata .command ()
798+ def apply ():
799+ mark_errata_security_updates ()
684800
685801if __name__ == '__main__' :
686- main ()
802+ cli ()
0 commit comments