new global setting PLC_VSYS_DEFAULTS
[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         config_tags=api.config.PLC_VSYS_DEFAULTS
32         values= [ value for value in config_tags.split(',') if value ]
33     # let's go
34     for value in values:
35         slice_tags=GetSliceTags({'tagname':'vsys','value':value})
36         names_with_tag = [ st['name'] for st in slice_tags ]
37         counter=0
38         for slice in slices:
39             if slice['name'] not in names_with_tag:
40                 add_value (slice,value,options)
41                 counter+=1
42         if options.verbose:
43             print "Found %d slices for which %s is missing"%(counter,value)
44
45 def main ():
46     usage="""Usage: %prog
47   Checks that a set of slices has a set of vsys tags set
48 Example:
49   %prog -- -p 'inria*' -t promisc -t fd_tuntap -vn
50 """
51     parser = OptionParser(usage=usage)
52     parser.add_option("-t","--tag",action='append',default=[],dest='tags',
53                       help="ignore PLC config and provide tags on the command line - can be repeated")
54     parser.add_option("-a","--all",action='store_true',default=False,
55                       dest='all',help="Apply on foreign slices as well (default is local only)")
56     parser.add_option("-p","--pattern",action='store',default=None,
57                       dest='pattern',help="Apply on slices whose name matches pattern")
58     parser.add_option("-v", "--verbose", action = "store_true", default = False, 
59                       dest='verbose', help="be verbose")
60     parser.add_option("-n", "--dry-run", action = "store_true", default = False, 
61                       dest='dry_run', help="don't actually do it")
62     (options, args) = parser.parse_args()
63
64     if args: 
65         parser.print_help()
66         sys.exit(1)
67
68     check (options)
69         
70 if __name__ == "__main__":
71     main()