Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-rapi @ 82d9caef

History | View | Annotate | Download (4.2 kB)

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