Revision 1df6506c daemons/ganeti-noded

b/daemons/ganeti-noded
27 27
import os
28 28
import sys
29 29
import traceback
30
import BaseHTTPServer
31
import simplejson
32 30
import errno
33 31
import logging
34 32

  
......
41 39
from ganeti import objects
42 40
from ganeti import errors
43 41
from ganeti import ssconf
42
from ganeti import http
44 43
from ganeti import utils
45 44

  
46 45
_EXIT_GANETI_NODED = False
47 46

  
48 47

  
49
class ServerObject(BaseHTTPServer.BaseHTTPRequestHandler):
48
class NodeDaemonRequestHandler(http.HTTPRequestHandler):
50 49
  """The server implementation.
51 50

  
52 51
  This class holds all methods exposed over the RPC interface.
53 52

  
54 53
  """
55
  def do_PUT(self):
56
    """Handle a post request.
54
  def HandleRequest(self):
55
    """Handle a request.
57 56

  
58 57
    """
58
    global _EXIT_GANETI_NODED
59

  
60
    if self.command.upper() != "PUT":
61
      raise http.HTTPBadRequest()
62

  
59 63
    path = self.path
60 64
    if path.startswith("/"):
61 65
      path = path[1:]
62
    logging.debug("ServerObject: received call '%s'", path)
63
    mname = "perspective_%s" % path
64
    if not hasattr(self, mname):
65
      self.send_error(404)
66
      return False
67 66

  
68
    method = getattr(self, mname)
69
    try:
70
      body_length = int(self.headers.get('Content-Length', '0'))
71
    except ValueError:
72
      self.send_error(400, 'No Content-Length header or invalid format')
73
      return False
67
    method = getattr(self, "perspective_%s" % path, None)
68
    if method is None:
69
      raise httperror.HTTPNotFound()
74 70

  
75 71
    try:
76
      body = self.rfile.read(body_length)
77
    except socket.error, err:
78
      logger.Error("Socket error while reading: %s" % str(err))
79
      return
80
    try:
81
      params = simplejson.loads(body)
82
      logging.debug("ServerObject: method parameters: %s", params)
83
      result = method(params)
84
      payload = simplejson.dumps(result)
72
      return method(self.post_data)
85 73
    except errors.QuitGanetiException, err:
86
      global _EXIT_GANETI_NODED
87 74
      _EXIT_GANETI_NODED = True
88
      if isinstance(err.args, tuple) and len(err.args) == 2:
89
        if err.args[0]:
90
          self.send_error(500, "Error: %s" % str(err[1]))
91
        else:
92
          payload = simplejson.dumps(err.args[1])
93
      else:
94
        self.log_message('QuitGanetiException Usage Error')
95
        self.send_error(500, "Error: %s" % str(err))
96
    except Exception, err:
97
      self.send_error(500, "Error: %s" % str(err))
98
      return False
99
    self.send_response(200)
100
    self.send_header('Content-Length', str(len(payload)))
101
    self.end_headers()
102
    self.wfile.write(payload)
103
    return True
104

  
105
  def log_message(self, format, *args):
106
    """Log a request to the log.
107

  
108
    This is the same as the parent, we just log somewhere else.
109

  
110
    """
111
    msg = ("%s - - [%s] %s" %
112
           (self.address_string(),
113
            self.log_date_time_string(),
114
            format % args))
115
    logging.debug(msg)
116 75

  
117 76
  # the new block devices  --------------------------
118 77

  
......
573 532
                                        new_file_storage_dir)
574 533

  
575 534

  
535
class NodeDaemonHttpServer(http.HTTPServer):
536
  def __init__(self, server_address):
537
    http.HTTPServer.__init__(self, server_address, NodeDaemonRequestHandler)
538

  
539

  
576 540
def ParseOptions():
577 541
  """Parse the command line options.
578 542

  
......
639 603

  
640 604
  global _EXIT_GANETI_NODED
641 605

  
642
  httpd = BaseHTTPServer.HTTPServer(('', port), ServerObject)
606
  httpd = NodeDaemonHttpServer(('', port))
643 607
  while (not _EXIT_GANETI_NODED):
644 608
    httpd.handle_request()
645 609

  

Also available in: Unified diff