bugfix in check-vsys-defaults.py
[myplc.git] / bin / check-vsys-defaults.py
1 #!/usr/bin/env plcsh
2 import sys
3 from optparse import OptionParser
4
5 try:
6     vsys_tag_type=GetSliceTags({'tagname':'vsys'})[0]
7 except:
8     print "Can't find tag vsys - exiting"
9     sys.exit(1)
10
11 def add_value (slice, value, options):
12     (slice_id, slice_name ) = (slice['slice_id'], slice['name'])
13     if options.dry_run:
14         print "Would add vsys=%s to slice %s (%d)"%(value,slice_name,slice_id)
15         return
16     if options.verbose:
17         print "Adding vsys=%s to slice %s (%d)"%(value,slice_name,slice_id)
18     AddSliceTag (slice_id, 'vsys', value)
19
20
21 def check (options):
22     # retrieve applicable slices
23     filter={}
24     if options.pattern: filter['name']=options.pattern
25     if not options.all: filter['peer_id']=None
26     slices=GetSlices(filter)
27     # find list of values 
28     if options.tags:
29         values=options.tags
30     else:
31         values= [ y for y in [ x.strip() for x in api.config.PLC_VSYS_DEFAULTS.split(',') ] if y ]
32     # let's go
33     for value in values:
34         slice_tags=GetSliceTags({'tagname':'vsys','value':value})
35         names_with_tag = [ st['name'] for st in slice_tags ]
36         counter=0
37         for slice in slices:
38             if slice['name'] not in names_with_tag:
39                 add_value (slice,value,options)
40                 counter+=1
41         if options.verbose:
42             print "Found %d slices for which %s is missing"%(counter,value)
43
44 def main ():
45     usage="""Usage: %prog
46   Checks that a set of slices has a set of vsys tags set
47 Example:
48   %prog -- -p 'inria*' -t promisc -t fd_tuntap -vn
49 """
50     parser = OptionParser(usage=usage)
51     parser.add_option("-t","--tag",action='append',default=[],dest='tags',
52                       help="ignore PLC config and provide tags on the command line - can be repeated")
53     parser.add_option("-a","--all",action='store_true',default=False,
54                       dest='all',help="Apply on foreign slices as well (default is local only)")
55     parser.add_option("-p","--pattern",action='store',default=None,
56                       dest='pattern',help="Apply on slices whose name matches pattern")
57     parser.add_option("-v", "--verbose", action = "store_true", default = False, 
58                       dest='verbose', help="be verbose")
59     parser.add_option("-n", "--dry-run", action = "store_true", default = False, 
60                       dest='dry_run', help="don't actually do it")
61     (options, args) = parser.parse_args()
62
63     if args: 
64         parser.print_help()
65         sys.exit(1)
66
67     check (options)
68         
69 if __name__ == "__main__":
70     main()