Statistics
| Branch: | Tag: | Revision:

root / daemons / ganeti-rapi @ 03d1dba2

History | View | Annotate | Download (4.2 kB)

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

    
40
class RemoteApiHttpServer(http.HttpServer):
41
  """REST Request Handler Class.
42

    
43
  """
44
  def __init__(self, *args, **kwargs):
45
    http.HttpServer.__init__(self, *args, **kwargs)
46
    self._resmap = connector.Mapper()
47

    
48
  def HandleRequest(self, req):
49
    """Handles a request.
50

    
51
    """
52
    (HandlerClass, items, args) = self._resmap.getController(req.request_path)
53
    handler = HandlerClass(items, args, req)
54

    
55
    method = req.request_method.upper()
56
    try:
57
      fn = getattr(handler, method)
58
    except AttributeError, err:
59
      raise http.HTTPBadRequest()
60

    
61
    try:
62
      result = fn()
63
      sn = handler.getSerialNumber()
64
      if sn:
65
        req.response_headers[http.HTTP_ETAG] = str(sn)
66
    except:
67
      logging.exception("Error while handling the %s request", method)
68
      raise
69

    
70
    return result
71

    
72

    
73
def ParseOptions():
74
  """Parse the command line options.
75

    
76
  Returns:
77
    (options, args) as from OptionParser.parse_args()
78

    
79
  """
80
  parser = optparse.OptionParser(description="Ganeti Remote API",
81
                    usage="%prog [-d] [-p port]",
82
                    version="%%prog (ganeti) %s" %
83
                                 constants.RAPI_VERSION)
84
  parser.add_option("-d", "--debug", dest="debug",
85
                    help="Enable some debug messages",
86
                    default=False, action="store_true")
87
  parser.add_option("-p", "--port", dest="port",
88
                    help="Port to run API (%s default)." %
89
                                 constants.RAPI_PORT,
90
                    default=constants.RAPI_PORT, type="int")
91
  parser.add_option("-S", "--https", dest="ssl",
92
                    help="Secure HTTP protocol with SSL",
93
                    default=False, action="store_true")
94
  parser.add_option("-K", "--ssl-key", dest="ssl_key",
95
                    help="SSL key",
96
                    default=None, type="string")
97
  parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
98
                    help="SSL certificate",
99
                    default=None, type="string")
100
  parser.add_option("-f", "--foreground", dest="fork",
101
                    help="Don't detach from the current terminal",
102
                    default=True, action="store_false")
103

    
104
  options, args = parser.parse_args()
105

    
106
  if len(args) != 0:
107
    print >> sys.stderr, "Usage: %s [-d] [-p port]" % sys.argv[0]
108
    sys.exit(1)
109

    
110
  if options.ssl and not (options.ssl_cert and options.ssl_key):
111
    print >> sys.stderr, ("For secure mode please provide "
112
                         "--ssl-key and --ssl-cert arguments")
113
    sys.exit(1)
114

    
115
  return options, args
116

    
117

    
118
def main():
119
  """Main function.
120

    
121
  """
122
  options, args = ParseOptions()
123

    
124
  ssconf.CheckMaster(options.debug)
125

    
126
  if options.fork:
127
    utils.Daemonize(logfile=constants.LOG_RAPISERVER)
128

    
129
  utils.SetupLogging(constants.LOG_RAPISERVER, debug=options.debug,
130
                     stderr_logging=not options.fork)
131

    
132
  utils.WritePidFile(constants.RAPI_PID)
133
  try:
134
    mainloop = daemon.Mainloop()
135
    server = RemoteApiHttpServer(mainloop, "", options.port)
136
    server.Start()
137
    try:
138
      mainloop.Run()
139
    finally:
140
      server.Stop()
141
  finally:
142
    utils.RemovePidFile(constants.RAPI_PID)
143

    
144

    
145
if __name__ == '__main__':
146
  main()