"""
import glob
-import logging
import optparse
import sys
import os
-import ganeti.rapi.RESTHTTPServer
+from ganeti import constants
+from ganeti import utils
+from ganeti.rapi import RESTHTTPServer
RELEASE_VERSION = 0.01
API_PORT = 5080
parser.add_option("-C", "--ssl-cert", dest="ssl_cert",
help="SSL certificate",
default=None, type="string")
+ parser.add_option("-f", "--foreground", dest="fork",
+ help="Don't detach from the current terminal",
+ default=True, action="store_false")
+
options, args = parser.parse_args()
if len(args) != 1 or args[0] not in ("start", "stop"):
Return:
Exit code.
"""
- # do the UNIX double-fork magic
- try:
- pid = os.fork()
- if pid > 0:
- # exit first parent
- sys.exit(0)
- except OSError, e:
- print >> sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
- return 1
-
- # decouple from parent environment
- os.chdir("/")
- os.setsid()
- os.umask(0)
-
- # do second fork
- try:
- pid = os.fork()
- if pid > 0:
- # exit from second parent, print eventual PID before
- print "Ganeti-RAPI PID: %d port: %d" % (pid, options.port)
- return 0
- except OSError, e:
- print >> sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
- return 1
-
- # start the daemon main loop
- ganeti.rapi.RESTHTTPServer.start(options)
+ if options.fork:
+ utils.Daemonize(logfile=constants.LOG_RAPISERVER)
+ RESTHTTPServer.start(options)
def StopAPI(options):
import SocketServer
import BaseHTTPServer
import OpenSSL
-import logging
-import logging.handlers
+from ganeti import constants
+from ganeti import logger
from ganeti.rapi import resources
"""RESTfull HTTPS Server module.
"""
-def OpenLog():
- """Set up logging to the syslog.
-
- """
- log = logging.getLogger('ganeti-rapi')
- slh = logging.handlers.SysLogHandler('/dev/log',
- logging.handlers.SysLogHandler.LOG_DAEMON)
- fmt = logging.Formatter('ganeti-rapi[%(process)d]:%(levelname)s: %(message)s')
- slh.setFormatter(fmt)
- log.addHandler(slh)
- log.setLevel(logging.INFO)
- log.debug("Logging initialized")
-
- return log
-
-
class RESTHTTPServer(BaseHTTPServer.HTTPServer):
"""The class to provide HTTP/HTTPS server.
HandlerClass - HTTPRequestHandler object
options - Command-line options
"""
+ logger.SetupLogging(debug=options.debug, program='ganeti-rapi')
+
BaseHTTPServer.HTTPServer.__init__(self, server_address, HandlerClass)
if options.ssl:
# Set up SSL
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
self.map = resources.Mapper()
- self.log = OpenLog()
- self.log.debug("Request handler setup.")
def handle_one_request(self):
"""Handle a single REST request. """
try:
self.raw_requestline = self.rfile.readline()
except OpenSSL.SSL.Error, ex:
- self.log.exception("Error in SSL: %s" % str(ex))
+ logger.Error("Error in SSL: %s" % str(ex))
if not self.raw_requestline:
self.close_connection = 1
return
every message.
"""
- level = logging.INFO
+ logX = logger.Info
# who is calling?
origin = inspect.stack()[1][0].f_code.co_name
if origin == "log_error":
- level = logging.ERROR
-
- self.log.log(level, "%s - - %s\n" %
+ logX = logger.Error
+ logX("%s - - %s" %
(self.address_string(),
format%args))