clearer names for actions, and infer actions better
[monitor.git] / monitor / parser.py
1 #!/usr/bin/python
2 import pickle
3 import os
4 import getopt
5 import sys
6 import __main__
7 from optparse import OptionParser
8 import config
9
10 def parse_bool(option, opt_str, value, parser):
11         if opt_str in ["--debug"]:
12                 parser.values.debug = int(int(value))
13         elif opt_str in ["--mail"]:
14                 parser.values.mail = int(int(value))
15         elif opt_str in ["--bcc"]:
16                 parser.values.bcc = int(int(value))
17         elif opt_str in ["--policysavedb"]:
18                 parser.values.policysavedb = int(int(value))
19         elif opt_str in ["--squeeze"]:
20                 parser.values.squeeze = int(int(value))
21         else:
22                 print "blue"
23
24 def parseSetDefaults(parser=None):
25         if parser == None:
26                 parser = OptionParser()
27
28         parser.set_defaults(debug = config.debug,
29                                         mail = config.mail,
30                                         bcc  = config.bcc,
31                                         email = config.from_email,
32                                         run = config.run,
33                                         squeeze = config.squeeze,
34                                         policysavedb = config.policysavedb)
35
36         parser.add_option("", "--debug", dest="debug",
37                           help="Enable debugging", 
38                           type="int",
39                           metavar="[0|1]",
40                           action="callback", 
41                           callback=parse_bool)
42         parser.add_option("", "--mail", dest="mail",
43                           help="Enable sending email",
44                           type="int",
45                           metavar="[0|1]",
46                           action="callback", 
47                           callback=parse_bool)
48         parser.add_option("", "--bcc", dest="bcc",
49                           help="Include BCC to user",
50                           type="int",
51                           metavar="[0|1]",
52                           action="callback", 
53                           callback=parse_bool)
54         parser.add_option("", "--squeeze", dest="squeeze",
55                           help="Squeeze sites or not",
56                           type="int",
57                           metavar="[0|1]",
58                           action="callback", 
59                           callback=parse_bool)
60         parser.add_option("", "--policysavedb", dest="policysavedb",
61                           help="Save the policy event database after a run",
62                           type="int",
63                           metavar="[0|1]",
64                           action="callback", 
65                           callback=parse_bool)
66         parser.add_option("", "--run", dest="run", 
67                           action="store_true",
68                           help="Perform monitor or print configs")
69         parser.add_option("", "--email", dest="email",
70                           help="Specify an email address to use for mail when "+\
71                                         "debug is enabled or for bcc when it is not")
72         return parser
73
74 def parseSetCacheSet(parser=None):
75         if parser == None:
76                 parser = OptionParser()
77
78         parser.set_defaults(cachecalls=True, cachetime=60)
79         parser.add_option("", "--nocache", dest="cachecalls", action="store_false",
80                                         help="When using PLCCache objects, temporarily disable the use of caching. i.e. refresh")
81         parser.add_option("", "--cachetime", dest="cachetime", 
82                                         help="How long to preserve a cached value. Minutes")
83         return parser
84
85 def parseSetNodeSets(parser=None):
86         if parser == None:
87                 parser = OptionParser()
88         
89         parser.set_defaults(node=None, site=None, sitelist=None, nodelist=None, nodeselect=None, nodegroup=None)
90         parser.add_option("", "--node", dest="node", metavar="hostname", 
91                                                 help="Provide a single node to operate on")
92         parser.add_option("", "--site", dest="site", metavar="site name",
93                                                 help="Specify a single site to operate on")
94         parser.add_option("", "--sitelist", dest="sitelist", metavar="site name",
95                                                 help="Specify a list of sites, separated by ','")
96         parser.add_option("", "--nodegroup", dest="nodegroup", metavar="GroupName", 
97                                                 help="Provide the nodegroup for the list of nodes.")
98         parser.add_option("", "--nodelist", dest="nodelist", metavar="FILE", 
99                                                 help="Provide the input file for the list of objects")
100         parser.add_option("", "--nodeselect", dest="nodeselect", metavar="query string", 
101                                                 help="Provide a selection string to return a node list.")
102         return parser
103
104
105 def getParser(parsesets=[], parser=None):
106         if parser == None:
107                 p = OptionParser()
108         else:
109                 p = parser
110
111         if 'cacheset' in parsesets:
112                 p = parseSetCacheSet(p)
113
114         if 'nodesets' in parsesets:
115                 p = parseSetNodeSets(p)
116         if 'defaults' in parsesets:
117                 p = parseSetDefaults(p)
118
119         return p
120
121 def parse_args(parser):
122         class obj: pass
123         (options, args) = parser.parse_args()
124         o = obj()
125         o.__dict__.update(options.__dict__)
126         o.__dict__['args'] = args
127         #config.update(o)
128         config.updatemodule(config, o)
129         return config
130
131 def print_values(parser):
132         exclude = ['parser']
133         for key in parser.__dict__.keys():
134                 if key not in exclude:
135                         print "%20s == %s" % (key, parser.__dict__[key])
136
137 def usage(parser):
138         print_values(parser)
139         parser.print_help()