|
|
@ -20,54 +20,55 @@
|
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
from __future__ import division, print_function, unicode_literals
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import signal
|
|
|
|
import signal
|
|
|
|
import socket
|
|
|
|
import socket
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
from gevent.pool import Pool
|
|
|
|
from gevent.pool import Pool
|
|
|
|
from gevent import __version__ as geventVersion
|
|
|
|
from gevent import __version__ as _version
|
|
|
|
gevent_present = True
|
|
|
|
VERSION = {'Gevent': 'v' + _version}
|
|
|
|
|
|
|
|
_GEVENT = True
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
|
from tornado.wsgi import WSGIContainer
|
|
|
|
from tornado.wsgi import WSGIContainer
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado.ioloop import IOLoop
|
|
|
|
from tornado import version as tornadoVersion
|
|
|
|
from tornado import version as _version
|
|
|
|
from tornado import log as tornadoLog
|
|
|
|
VERSION = {'Tornado': 'v' + _version}
|
|
|
|
from tornado import options as tornadoOptions
|
|
|
|
_GEVENT = False
|
|
|
|
gevent_present = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from . import logger, config, global_WorkerThread
|
|
|
|
from . import logger, global_WorkerThread
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
log = logger.create()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class server:
|
|
|
|
class WebServer:
|
|
|
|
|
|
|
|
|
|
|
|
wsgiserver = None
|
|
|
|
|
|
|
|
restart = False
|
|
|
|
|
|
|
|
app = None
|
|
|
|
|
|
|
|
access_logger = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
def __init__(self):
|
|
|
|
signal.signal(signal.SIGINT, self.killServer)
|
|
|
|
signal.signal(signal.SIGINT, self._killServer)
|
|
|
|
signal.signal(signal.SIGTERM, self.killServer)
|
|
|
|
signal.signal(signal.SIGTERM, self._killServer)
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(self, application):
|
|
|
|
self.wsgiserver = None
|
|
|
|
self.app = application
|
|
|
|
|
|
|
|
self.port = config.config_port
|
|
|
|
|
|
|
|
self.listening = config.get_config_ipaddress(readable=True) + ":" + str(self.port)
|
|
|
|
|
|
|
|
self.access_logger = None
|
|
|
|
self.access_logger = None
|
|
|
|
|
|
|
|
self.restart = False
|
|
|
|
|
|
|
|
self.app = None
|
|
|
|
|
|
|
|
self.listen_address = None
|
|
|
|
|
|
|
|
self.listen_port = None
|
|
|
|
|
|
|
|
self.unix_socket_file = None
|
|
|
|
|
|
|
|
self.ssl_args = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_app(self, application, config):
|
|
|
|
|
|
|
|
self.app = application
|
|
|
|
|
|
|
|
self.listen_address = config.get_config_ipaddress()
|
|
|
|
|
|
|
|
self.listen_port = config.config_port
|
|
|
|
|
|
|
|
|
|
|
|
if config.config_access_log:
|
|
|
|
if config.config_access_log:
|
|
|
|
if gevent_present:
|
|
|
|
log_name = "gevent.access" if _GEVENT else "tornado.access"
|
|
|
|
logger.setup(config.config_access_logfile, logger.DEFAULT_ACCESS_LEVEL, "access")
|
|
|
|
formatter = logger.ACCESS_FORMATTER_GEVENT if _GEVENT else logger.ACCESS_FORMATTER_TORNADO
|
|
|
|
self.access_logger = logging.getLogger("access")
|
|
|
|
self.access_logger = logger.create_access_log(config.config_access_logfile, log_name, formatter)
|
|
|
|
else:
|
|
|
|
|
|
|
|
logger.setup(config.config_access_logfile, logger.DEFAULT_ACCESS_LEVEL, "tornado.access")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ssl_args = None
|
|
|
|
|
|
|
|
certfile_path = config.get_config_certfile()
|
|
|
|
certfile_path = config.get_config_certfile()
|
|
|
|
keyfile_path = config.get_config_keyfile()
|
|
|
|
keyfile_path = config.get_config_keyfile()
|
|
|
|
if certfile_path and keyfile_path:
|
|
|
|
if certfile_path and keyfile_path:
|
|
|
@ -79,22 +80,46 @@ class server:
|
|
|
|
log.warning('Cert path: %s', certfile_path)
|
|
|
|
log.warning('Cert path: %s', certfile_path)
|
|
|
|
log.warning('Key path: %s', keyfile_path)
|
|
|
|
log.warning('Key path: %s', keyfile_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _make_gevent_unix_socket(self, socket_file):
|
|
|
|
|
|
|
|
# the socket file must not exist prior to bind()
|
|
|
|
|
|
|
|
if os.path.exists(socket_file):
|
|
|
|
|
|
|
|
# avoid nuking regular files and symbolic links (could be a mistype or security issue)
|
|
|
|
|
|
|
|
if os.path.isfile(socket_file) or os.path.islink(socket_file):
|
|
|
|
|
|
|
|
raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), socket_file)
|
|
|
|
|
|
|
|
os.remove(socket_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unix_sock = WSGIServer.get_listener(socket_file, family=socket.AF_UNIX)
|
|
|
|
|
|
|
|
self.unix_socket_file = socket_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ensure current user and group have r/w permissions, no permissions for other users
|
|
|
|
|
|
|
|
# this way the socket can be shared in a semi-secure manner
|
|
|
|
|
|
|
|
# between the user running calibre-web and the user running the fronting webserver
|
|
|
|
|
|
|
|
os.chmod(socket_file, 0o660);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return unix_sock
|
|
|
|
|
|
|
|
|
|
|
|
def _make_gevent_socket(self):
|
|
|
|
def _make_gevent_socket(self):
|
|
|
|
if config.get_config_ipaddress():
|
|
|
|
if os.name != 'nt':
|
|
|
|
return (config.get_config_ipaddress(), self.port)
|
|
|
|
unix_socket_file = os.environ.get("CALIBRE_UNIX_SOCKET")
|
|
|
|
|
|
|
|
if unix_socket_file:
|
|
|
|
|
|
|
|
return self._make_gevent_unix_socket(unix_socket_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.listen_address:
|
|
|
|
|
|
|
|
return (self.listen_address, self.listen_port)
|
|
|
|
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
if os.name == 'nt':
|
|
|
|
return ('0.0.0.0', self.port)
|
|
|
|
return ('0.0.0.0', self.listen_port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
address = ('', self.listen_port)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
s = WSGIServer.get_listener(('', self.port), family=socket.AF_INET6)
|
|
|
|
sock = WSGIServer.get_listener(address, family=socket.AF_INET6)
|
|
|
|
except socket.error as ex:
|
|
|
|
except socket.error as ex:
|
|
|
|
log.error('%s', ex)
|
|
|
|
log.error('%s', ex)
|
|
|
|
log.warning('Unable to listen on \'\', trying on IPv4 only...')
|
|
|
|
log.warning('Unable to listen on \'\', trying on IPv4 only...')
|
|
|
|
s = WSGIServer.get_listener(('', self.port), family=socket.AF_INET)
|
|
|
|
sock = WSGIServer.get_listener(address, family=socket.AF_INET)
|
|
|
|
log.debug("%r %r", s._sock, s._sock.getsockname())
|
|
|
|
return sock
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_gevent(self):
|
|
|
|
def _start_gevent(self):
|
|
|
|
ssl_args = self.ssl_args or {}
|
|
|
|
ssl_args = self.ssl_args or {}
|
|
|
|
log.info('Starting Gevent server')
|
|
|
|
log.info('Starting Gevent server')
|
|
|
|
|
|
|
|
|
|
|
@ -102,78 +127,58 @@ class server:
|
|
|
|
sock = self._make_gevent_socket()
|
|
|
|
sock = self._make_gevent_socket()
|
|
|
|
self.wsgiserver = WSGIServer(sock, self.app, log=self.access_logger, spawn=Pool(), **ssl_args)
|
|
|
|
self.wsgiserver = WSGIServer(sock, self.app, log=self.access_logger, spawn=Pool(), **ssl_args)
|
|
|
|
self.wsgiserver.serve_forever()
|
|
|
|
self.wsgiserver.serve_forever()
|
|
|
|
except socket.error:
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
if self.unix_socket_file:
|
|
|
|
log.info('Unable to listen on "", trying on "0.0.0.0" only...')
|
|
|
|
os.remove(self.unix_socket_file)
|
|
|
|
self.wsgiserver = WSGIServer(('0.0.0.0', config.config_port), self.app, spawn=Pool(), **ssl_args)
|
|
|
|
self.unix_socket_file = None
|
|
|
|
self.wsgiserver.serve_forever()
|
|
|
|
|
|
|
|
except (OSError, socket.error) as e:
|
|
|
|
|
|
|
|
log.info("Error starting server: %s", e.strerror)
|
|
|
|
|
|
|
|
print("Error starting server: %s" % e.strerror)
|
|
|
|
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
|
|
log.exception("Unknown error while starting gevent")
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_tornado(self):
|
|
|
|
def _start_tornado(self):
|
|
|
|
log.info('Starting Tornado server on %s', self.listening)
|
|
|
|
log.info('Starting Tornado server on %s', self.listen_address)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
# Max Buffersize set to 200MB )
|
|
|
|
# Max Buffersize set to 200MB )
|
|
|
|
http_server = HTTPServer(WSGIContainer(self.app),
|
|
|
|
http_server = HTTPServer(WSGIContainer(self.app),
|
|
|
|
max_buffer_size = 209700000,
|
|
|
|
max_buffer_size = 209700000,
|
|
|
|
ssl_options=self.ssl_args)
|
|
|
|
ssl_options=self.ssl_args)
|
|
|
|
address = config.get_config_ipaddress()
|
|
|
|
http_server.listen(self.listen_port, self.listen_address)
|
|
|
|
http_server.listen(self.port, address)
|
|
|
|
|
|
|
|
self.wsgiserver=IOLoop.instance()
|
|
|
|
self.wsgiserver=IOLoop.instance()
|
|
|
|
self.wsgiserver.start()
|
|
|
|
self.wsgiserver.start()
|
|
|
|
# wait for stop signal
|
|
|
|
# wait for stop signal
|
|
|
|
self.wsgiserver.close(True)
|
|
|
|
self.wsgiserver.close(True)
|
|
|
|
except socket.error as err:
|
|
|
|
|
|
|
|
log.exception("Error starting tornado server")
|
|
|
|
|
|
|
|
print("Error starting server: %s" % err.strerror)
|
|
|
|
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def startServer(self):
|
|
|
|
def start(self):
|
|
|
|
if gevent_present:
|
|
|
|
try:
|
|
|
|
|
|
|
|
if _GEVENT:
|
|
|
|
# leave subprocess out to allow forking for fetchers and processors
|
|
|
|
# leave subprocess out to allow forking for fetchers and processors
|
|
|
|
self.start_gevent()
|
|
|
|
self._start_gevent()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self.start_tornado()
|
|
|
|
self._start_tornado()
|
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
|
|
|
log.error("Error starting server: %s", ex)
|
|
|
|
|
|
|
|
print("Error starting server: %s" % ex)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
self.wsgiserver = None
|
|
|
|
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not self.restart:
|
|
|
|
|
|
|
|
log.info("Performing shutdown of Calibre-Web")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
if self.restart is True:
|
|
|
|
|
|
|
|
log.info("Performing restart of Calibre-Web")
|
|
|
|
log.info("Performing restart of Calibre-Web")
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
arguments = list(sys.argv)
|
|
|
|
|
|
|
|
arguments.insert(0, sys.executable)
|
|
|
|
if os.name == 'nt':
|
|
|
|
if os.name == 'nt':
|
|
|
|
arguments = ["\"" + sys.executable + "\""]
|
|
|
|
arguments = ["\"%s\"" % a for a in arguments]
|
|
|
|
for e in sys.argv:
|
|
|
|
|
|
|
|
arguments.append("\"" + e + "\"")
|
|
|
|
|
|
|
|
os.execv(sys.executable, arguments)
|
|
|
|
os.execv(sys.executable, arguments)
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
os.execl(sys.executable, sys.executable, *sys.argv)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
log.info("Performing shutdown of Calibre-Web")
|
|
|
|
|
|
|
|
global_WorkerThread.stop()
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setRestartTyp(self,starttyp):
|
|
|
|
def _killServer(self, signum, frame):
|
|
|
|
self.restart = starttyp
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
|
|
|
|
def killServer(self, signum, frame):
|
|
|
|
def stop(self, restart=False):
|
|
|
|
self.stopServer()
|
|
|
|
self.restart = restart
|
|
|
|
|
|
|
|
|
|
|
|
def stopServer(self):
|
|
|
|
|
|
|
|
if self.wsgiserver:
|
|
|
|
if self.wsgiserver:
|
|
|
|
if gevent_present:
|
|
|
|
if _GEVENT:
|
|
|
|
self.wsgiserver.close()
|
|
|
|
self.wsgiserver.close()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self.wsgiserver.add_callback(self.wsgiserver.stop)
|
|
|
|
self.wsgiserver.add_callback(self.wsgiserver.stop)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def getNameVersion():
|
|
|
|
|
|
|
|
if gevent_present:
|
|
|
|
|
|
|
|
return {'Gevent': 'v' + geventVersion}
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return {'Tornado': 'v' + tornadoVersion}
|
|
|
|
|
|
|
|