17
17
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
18
18
19
19
20
+ import click
20
21
import os
21
22
import sys
22
23
import argparse
@@ -122,7 +123,7 @@ def refresh_repos(repo=None, force=False):
122
123
info_message .send (sender = None , text = '' )
123
124
124
125
125
- def list_repos (repos = None ):
126
+ def show_repos (repos = None ):
126
127
""" Print info about a list of repositories
127
128
Defaults to all repos
128
129
"""
@@ -132,8 +133,17 @@ def list_repos(repos=None):
132
133
133
134
134
135
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
137
147
"""
138
148
matching_hosts = get_hosts (hosts , 'Printing information' )
139
149
for host in matching_hosts :
@@ -224,6 +234,21 @@ def clean_repos():
224
234
repo .delete ()
225
235
update_pbar (i + 1 )
226
236
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
+
227
252
228
253
def clean_reports (s_host = None ):
229
254
""" Delete old reports for all hosts, specify host for a single host.
@@ -270,8 +295,9 @@ def clean_tags():
270
295
update_pbar (i + 1 )
271
296
272
297
273
- def host_updates_alt (host = None ):
298
+ def find_host_updates_bulk (host = None ):
274
299
""" Find updates for all hosts, specify host for a single host
300
+ This algo works faster for updating multiple similar hosts
275
301
"""
276
302
updated_hosts = []
277
303
hosts = get_hosts (host , 'Finding updates' )
@@ -322,7 +348,7 @@ def host_updates_alt(host=None):
322
348
info_message .send (sender = None , text = text )
323
349
324
350
325
- def host_updates (host = None ):
351
+ def find_host_updates (host = None ):
326
352
""" Find updates for all hosts, specify host for a single host
327
353
"""
328
354
hosts = get_hosts (host , 'Finding updates' )
@@ -436,7 +462,7 @@ def toggle_host_check_dns(hosts=None, check_dns=True):
436
462
host .save ()
437
463
438
464
439
- def dns_checks (host = None ):
465
+ def check_host_dns (host = None ):
440
466
""" Check all hosts for reverse DNS mismatches, specify host for a single
441
467
host
442
468
"""
@@ -476,7 +502,7 @@ def clean_updates():
476
502
""" Removes PackageUpdate objects that are no longer
477
503
linked to any hosts
478
504
"""
479
- package_updates = list (PackageUpdate .objects .all ())
505
+ package_updates = list (PackageUpdate .objects .all (). distinct () )
480
506
481
507
for update in package_updates :
482
508
if update .host_set .count () == 0 :
@@ -497,7 +523,7 @@ def clean_updates():
497
523
duplicate .delete ()
498
524
499
525
500
- def dbcheck ():
526
+ def clean_db ():
501
527
""" Runs all clean_* functions to check database consistency
502
528
"""
503
529
clean_updates ()
@@ -509,7 +535,7 @@ def dbcheck():
509
535
clean_tags ()
510
536
511
537
512
- def collect_args ():
538
+ def collect_args1 ():
513
539
""" Collect argparse arguments
514
540
"""
515
541
parser = argparse .ArgumentParser (description = 'Patchman CLI tool' )
@@ -588,7 +614,7 @@ def collect_args():
588
614
return parser
589
615
590
616
591
- def process_args (args ):
617
+ def process_args1 (args ):
592
618
""" Process command line arguments
593
619
"""
594
620
@@ -672,15 +698,132 @@ def process_args(args):
672
698
return showhelp
673
699
674
700
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 ()
684
827
685
828
if __name__ == '__main__' :
686
- main ()
829
+ cli ()
0 commit comments