+ new XMLRPC_SERVER name to boot.planet-lab.org
[monitor.git] / config.py
1 #!/usr/bin/python
2 import pickle
3 import os
4 import getopt
5 import sys
6 import __main__
7
8 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
9
10 class config:
11         debug = True
12         mail = False
13         bcc  = True
14         email = "soltesz@cs.utk.edu"
15         userlist = None
16         cachert = True
17         cachenodes = True
18         cachesites = True
19         squeeze = False
20         policysavedb = True
21         __file = ".config"
22
23         def __init__(self):
24                  if os.path.exists(self.__file): # file exists, read that.
25                         f = open(self.__file, 'r')
26                         o = pickle.load(f)
27                         self.__dict__.update(o)
28                         f.close()
29
30         def getListFromFile(self, file):
31                 f = open(file, 'r')
32                 list = []
33                 for line in f:
34                         line = line.strip()
35                         list += [line]
36                 return list
37                 
38         def save(self):
39                 f = open(self.__file, 'w')
40                 o = {'debug': self.debug, 
41                          'mail': self.mail, 
42                          'bcc': self.bcc, 
43                          'email':self.email,
44                          'userlist': self.userlist,
45                          'cachert': self.cachert, 
46                          'cachenodes' : self.cachenodes, 
47                          'cachesites': self.cachesites,
48                          'squeeze':self.squeeze,
49                          'policysavedb':self.policysavedb}
50                 pickle.dump(o, f)
51                 f.close()
52
53
54 def usage():
55         config = __main__.config()
56      #   --cachesites=[0|1]      Cache Sites from PLC (current: %s)
57      #   --status                Print memory usage statistics and exit
58         print """
59 Settings:
60         --debug=[0|1]           Set debugging        (current: %s)
61         --mail=[0|1]            Send mail or not     (current: %s)
62         --bcc=[0|1]             Include bcc of user  (current: %s)
63         --email=[email]         Email to use above   (current: %s)
64         --userlist=[filename]   Use a list of nodes  (current: %s)
65         --cachert=[0|1]         Cache the RT db      (current: %s)
66         --cachenodes=[0|1]      Cache Nodes from PLC (current: %s)
67         --squeeze=[0|1]         Squeeze sites or not (current: %s)
68         --policysavedb=[0|1]    Save policy DBs      (current: %s)
69         -h, --help              This message
70 """.lstrip() % (config.debug, 
71                             config.mail, 
72                                 config.bcc, 
73                             config.email, 
74                                 config.userlist, 
75                                 config.cachert, 
76                                 config.cachenodes, 
77                                 config.squeeze, 
78                                 config.policysavedb)
79
80 def main():
81         """ Start threads, do some housekeeping, then daemonize. """
82         # Defaults
83         config = __main__.config()
84
85         try:
86                 longopts = [ "debug=", 
87                                         "mail=", 
88                                         "email=", 
89                                         "bcc=", 
90                                         "userlist=",
91                                         "cachert=", 
92                                         "cachesites=", 
93                                         "cachenodes=", 
94                                         "squeeze=", 
95                                         "policysavedb=", 
96                                         "status", 
97                                         "help"]
98                 (opts, argv) = getopt.getopt(sys.argv[1:], "h", longopts)
99         except getopt.GetoptError, err:
100                 print "Error: " + err.msg
101                 usage()
102                 sys.exit(1)
103
104         for (opt, optval) in opts:
105                 if opt in ["--debug"]:
106                         config.debug = bool(int(optval))
107                         print "Running in DEBUG mode. Copying DB & "
108                         print "caching correspondences. NO SQUEEZING."
109                 elif opt in ["--mail"]:
110                         config.mail = bool(int(optval))
111                         print "NO EMAILS SENT."
112                 elif opt in ["--email"]:
113                         config.email = optval
114                 elif opt in ["--bcc"]:
115                         config.bcc = bool(int(optval))
116                 elif opt in ["--userlist"]:
117                         if len(optval) == 0:
118                                 config.userlist = None
119                         else:
120                                 config.userlist = optval
121                 elif opt in ["--cachert"]:
122                         config.cachert = bool(int(optval))
123                 elif opt in ["--cachesites"]:
124                         config.cachesites = bool(int(optval))
125                 elif opt in ["--cachenodes"]:
126                         config.cachenodes = bool(int(optval))
127                 elif opt in ["--policysavedb"]:
128                         config.policysavedb = bool(int(optval))
129                 elif opt in ["--squeeze"]:
130                         config.squeeze = bool(int(optval))
131                 elif opt in ["--status"]:
132                         #print summary(names)
133                         sys.exit(0)
134                 else:
135                         usage()
136                         sys.exit(0)
137
138         config.save()
139         usage()
140
141
142 if __name__ == '__main__':
143         main()