Fix epydoc format warnings
[ganeti-local] / daemons / ganeti-rapi
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 """ Ganeti Remote API master script.
22 """
23
24 import glob
25 import logging
26 import optparse
27 import sys
28 import os
29 import signal
30
31 from ganeti import constants
32 from ganeti import errors
33 from ganeti import http
34 from ganeti import daemon
35 from ganeti import ssconf
36 from ganeti import utils
37 from ganeti.rapi import connector
38
39 import ganeti.http.server
40
41
42 class RemoteApiHttpServer(http.server.HttpServer):
43   """REST Request Handler Class.
44
45   """
46   def __init__(self, *args, **kwargs):
47     http.server.HttpServer.__init__(self, *args, **kwargs)
48     self._resmap = connector.Mapper()
49
50   def HandleRequest(self, req):
51     """Handles a request.
52
53     """
54     (HandlerClass, items, args) = self._resmap.getController(req.request_path)
55     handler = HandlerClass(items, args, req)
56
57     method = req.request_method.upper()
58     try:
59       fn = getattr(handler, method)
60     except AttributeError, err:
61       raise http.HttpBadRequest()
62
63     try:
64       result = fn()
65       sn = handler.getSerialNumber()
66       if sn:
67         req.response_headers[http.HTTP_ETAG] = str(sn)
68     except:
69       logging.exception("Error while handling the %s request", method)
70       raise
71
72     return result
73
74
75 def ParseOptions():
76   """Parse the command line options.
77
78   @return: (options, args) as from OptionParser.parse_args()
79
80   """
81   parser = optparse.OptionParser(description="Ganeti Remote API",
82                     usage="%prog [-d] [-p port]",
83                     version="%%prog (ganeti) %s" %
84                                  constants.RAPI_VERSION)
85   parser.add_option("-d", "--debug", dest="debug",
86                     help="Enable some debug messages",
87                     default=False, action="store_true")
88   parser.add_option("-p", "--port", dest="port",
89                     help="Port to run API (%s default)." %
90                                  constants.RAPI_PORT,
91                     default=constants.RAPI_PORT, type="int")
92   parser.add_option("-S", "--https", dest="ssl",
93                     help="Secure HTTP protocol with SSL",
94                     default=False, action="store_true")
95   parser.add_option("-K", "--ssl-key", dest="ssl_key",
96                     help="SSL key",
97                     default=None, type="string")
98   parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
99                     help="SSL certificate",
100                     default=None, type="string")
101   parser.add_option("-f", "--foreground", dest="fork",
102                     help="Don't detach from the current terminal",
103                     default=True, action="store_false")
104
105   options, args = parser.parse_args()
106
107   if len(args) != 0:
108     print >> sys.stderr, "Usage: %s [-d] [-p port]" % sys.argv[0]
109     sys.exit(1)
110
111   if options.ssl and not (options.ssl_cert and options.ssl_key):
112     print >> sys.stderr, ("For secure mode please provide "
113                          "--ssl-key and --ssl-cert arguments")
114     sys.exit(1)
115
116   return options, args
117
118
119 def main():
120   """Main function.
121
122   """
123   options, args = ParseOptions()
124
125   ssconf.CheckMaster(options.debug)
126
127   if options.fork:
128     utils.Daemonize(logfile=constants.LOG_RAPISERVER)
129
130   utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
131                      stderr_logging=not options.fork)
132
133   utils.WritePidFile(constants.RAPI_PID)
134   try:
135     mainloop = daemon.Mainloop()
136     server = RemoteApiHttpServer(mainloop, "", options.port)
137     server.Start()
138     try:
139       mainloop.Run()
140     finally:
141       server.Stop()
142   finally:
143     utils.RemovePidFile(constants.RAPI_PID)
144
145
146 if __name__ == '__main__':
147   main()