2013-10-30 21:58:17 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2015-01-14 00:05:26 +00:00
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
2013-10-30 21:58:17 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-12-06 04:39:27 +00:00
|
|
|
"""
|
2014-03-11 21:45:04 +00:00
|
|
|
Set up and run the server.
|
2013-12-06 04:39:27 +00:00
|
|
|
"""
|
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
import os
|
2015-01-14 00:05:26 +00:00
|
|
|
import sys
|
2013-12-07 00:52:16 +00:00
|
|
|
import signal
|
2015-01-14 00:05:26 +00:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import functools
|
|
|
|
import types
|
|
|
|
import time
|
2014-04-11 01:42:26 +00:00
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
from .web.route import Route
|
2014-03-11 21:45:04 +00:00
|
|
|
from .config import Config
|
2014-03-16 03:41:04 +00:00
|
|
|
from .modules import MODULES
|
2015-01-15 15:59:01 +00:00
|
|
|
from .modules.port_manager import PortManager
|
2013-10-30 21:58:17 +00:00
|
|
|
|
2015-01-20 12:24:00 +00:00
|
|
|
# TODO: get rid of * have something generic to automatically import handlers so the routes can be found
|
2015-01-14 10:43:23 +00:00
|
|
|
from gns3server.handlers import *
|
2015-01-14 00:05:26 +00:00
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2013-10-30 21:58:17 +00:00
|
|
|
|
2014-09-29 21:56:01 +00:00
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
class Server:
|
2013-10-30 21:58:17 +00:00
|
|
|
|
2015-01-23 20:01:23 +00:00
|
|
|
def __init__(self, host, port):
|
2013-10-30 21:58:17 +00:00
|
|
|
|
2013-12-05 07:21:06 +00:00
|
|
|
self._host = host
|
|
|
|
self._port = port
|
2015-01-14 00:05:26 +00:00
|
|
|
self._loop = None
|
|
|
|
self._start_time = time.time()
|
2015-01-23 20:01:23 +00:00
|
|
|
self._port_manager = PortManager(host)
|
2014-11-10 06:01:13 +00:00
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-24 19:11:51 +00:00
|
|
|
def _run_application(self, app, ssl_context=None):
|
2015-01-14 00:05:26 +00:00
|
|
|
|
2015-01-24 19:11:51 +00:00
|
|
|
server = yield from self._loop.create_server(app.make_handler(), self._host, self._port, ssl=ssl_context)
|
2015-01-14 00:05:26 +00:00
|
|
|
return server
|
|
|
|
|
|
|
|
def _stop_application(self):
|
2013-12-06 04:39:27 +00:00
|
|
|
"""
|
2015-01-14 00:05:26 +00:00
|
|
|
Cleanup the modules (shutdown running emulators etc.)
|
2013-10-30 21:58:17 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-22 10:49:22 +00:00
|
|
|
for module in MODULES:
|
|
|
|
log.debug("Unloading module {}".format(module.__name__))
|
|
|
|
m = module.instance()
|
2015-01-23 20:01:23 +00:00
|
|
|
asyncio.async(m.unload())
|
2015-01-14 00:05:26 +00:00
|
|
|
self._loop.stop()
|
2014-03-16 03:41:04 +00:00
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
def _signal_handling(self):
|
2014-05-08 01:31:53 +00:00
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
def signal_handler(signame):
|
2015-01-20 13:59:19 +00:00
|
|
|
log.warning("Server has got signal {}, exiting...".format(signame))
|
2015-01-14 00:05:26 +00:00
|
|
|
self._stop_application()
|
|
|
|
|
|
|
|
signals = ["SIGTERM", "SIGINT"]
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
signals.extend(["SIGBREAK"])
|
|
|
|
else:
|
|
|
|
signals.extend(["SIGHUP", "SIGQUIT"])
|
2014-03-16 03:41:04 +00:00
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
for signal_name in signals:
|
|
|
|
callback = functools.partial(signal_handler, signal_name)
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
# add_signal_handler() is not yet supported on Windows
|
|
|
|
signal.signal(getattr(signal, signal_name), callback)
|
|
|
|
else:
|
|
|
|
self._loop.add_signal_handler(getattr(signal, signal_name), callback)
|
|
|
|
|
|
|
|
def _reload_hook(self):
|
|
|
|
|
|
|
|
def reload():
|
|
|
|
|
2015-01-20 13:59:19 +00:00
|
|
|
log.info("Reloading")
|
2015-01-14 00:05:26 +00:00
|
|
|
self._stop_application()
|
|
|
|
os.execv(sys.executable, [sys.executable] + sys.argv)
|
|
|
|
|
|
|
|
# code extracted from tornado
|
|
|
|
for module in sys.modules.values():
|
|
|
|
# Some modules play games with sys.modules (e.g. email/__init__.py
|
|
|
|
# in the standard library), and occasionally this can cause strange
|
|
|
|
# failures in getattr. Just ignore anything that's not an ordinary
|
|
|
|
# module.
|
|
|
|
if not isinstance(module, types.ModuleType):
|
|
|
|
continue
|
|
|
|
path = getattr(module, "__file__", None)
|
|
|
|
if not path:
|
|
|
|
continue
|
|
|
|
if path.endswith(".pyc") or path.endswith(".pyo"):
|
|
|
|
path = path[:-1]
|
|
|
|
modified = os.stat(path).st_mtime
|
|
|
|
if modified > self._start_time:
|
2015-01-20 13:59:19 +00:00
|
|
|
log.debug("File {} has been modified".format(path))
|
2015-01-14 00:05:26 +00:00
|
|
|
reload()
|
|
|
|
self._loop.call_later(1, self._reload_hook)
|
2013-10-30 21:58:17 +00:00
|
|
|
|
2015-01-24 19:11:51 +00:00
|
|
|
def _create_ssl_context(self, server_config):
|
|
|
|
|
|
|
|
import ssl
|
|
|
|
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
|
|
|
|
certfile = server_config["certfile"]
|
|
|
|
certkey = server_config["certkey"]
|
|
|
|
try:
|
|
|
|
ssl_context.load_cert_chain(certfile, certkey)
|
|
|
|
except FileNotFoundError:
|
|
|
|
log.critical("Could not find the SSL certfile or certkey")
|
|
|
|
raise SystemExit
|
|
|
|
except ssl.SSLError as e:
|
|
|
|
log.critical("SSL error: {}".format(e))
|
|
|
|
raise SystemExit
|
|
|
|
return ssl_context
|
|
|
|
|
2013-10-30 21:58:17 +00:00
|
|
|
def run(self):
|
2013-12-05 07:21:06 +00:00
|
|
|
"""
|
2015-01-14 00:05:26 +00:00
|
|
|
Starts the server.
|
2013-10-30 21:58:17 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-20 22:28:40 +00:00
|
|
|
logger = logging.getLogger("asyncio")
|
|
|
|
logger.setLevel(logging.WARNING)
|
|
|
|
|
2015-01-24 19:11:51 +00:00
|
|
|
server_config = Config.instance().get_section_config("Server")
|
2015-01-23 04:11:57 +00:00
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
# use the Proactor event loop on Windows
|
|
|
|
asyncio.set_event_loop(asyncio.ProactorEventLoop())
|
|
|
|
|
2015-01-24 19:11:51 +00:00
|
|
|
ssl_context = None
|
|
|
|
if server_config.getboolean("ssl"):
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
log.critical("SSL mode is not supported on Windows")
|
|
|
|
raise SystemExit
|
|
|
|
ssl_context = self._create_ssl_context(server_config)
|
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
self._loop = asyncio.get_event_loop()
|
|
|
|
app = aiohttp.web.Application()
|
|
|
|
for method, route, handler in Route.get_routes():
|
2015-01-20 13:59:19 +00:00
|
|
|
log.debug("Adding route: {} {}".format(method, route))
|
2015-01-14 00:05:26 +00:00
|
|
|
app.router.add_route(method, route, handler)
|
|
|
|
for module in MODULES:
|
2015-01-20 13:59:19 +00:00
|
|
|
log.debug("Loading module {}".format(module.__name__))
|
2015-01-15 15:59:01 +00:00
|
|
|
m = module.instance()
|
|
|
|
m.port_manager = self._port_manager
|
2013-12-07 00:52:16 +00:00
|
|
|
|
2015-01-20 13:59:19 +00:00
|
|
|
log.info("Starting server on {}:{}".format(self._host, self._port))
|
2015-01-24 19:11:51 +00:00
|
|
|
self._loop.run_until_complete(self._run_application(app, ssl_context))
|
2015-01-14 00:05:26 +00:00
|
|
|
self._signal_handling()
|
2013-12-07 00:52:16 +00:00
|
|
|
|
2015-01-20 12:24:00 +00:00
|
|
|
# FIXME: remove it in production or in tests
|
2015-01-14 00:05:26 +00:00
|
|
|
self._loop.call_later(1, self._reload_hook)
|
2015-01-22 10:49:22 +00:00
|
|
|
self._loop.run_forever()
|