def shutdown(request, response): config = Config.instance() if config.get_section_config("Server").getboolean("local", False) is False: raise HTTPForbidden(text="You can only stop a local server") log.info("Start shutting down the server") # close all the projects first controller = Controller.instance() projects = controller.projects.values() tasks = [] for project in projects: tasks.append(asyncio. async (project.close())) if tasks: done, _ = yield from asyncio.wait(tasks) for future in done: try: future.result() except Exception as e: log.error("Could not close project {}".format(e), exc_info=1) continue # then shutdown the server itself from gns3server.web.web_server import WebServer server = WebServer.instance() asyncio. async (server.shutdown_server()) response.set_status(201)
def shutdown(request, response): config = Config.instance() if config.get_section_config("Server").getboolean("local", False) is False: raise HTTPForbidden(text="You can only stop a local server") log.info("Start shutting down the server") # close all the projects first controller = Controller.instance() projects = controller.projects.values() tasks = [] for project in projects: tasks.append(asyncio.async(project.close())) if tasks: done, _ = yield from asyncio.wait(tasks) for future in done: try: future.result() except Exception as e: log.error("Could not close project {}".format(e), exc_info=1) continue # then shutdown the server itself from gns3server.web.web_server import WebServer server = WebServer.instance() asyncio.async(server.shutdown_server()) response.set_status(201)
async def reload(request, response): from gns3server.web.web_server import WebServer server = WebServer.instance() try: asyncio.ensure_future(server.reload_server()) except asyncio.CancelledError: pass
async def start(self): log.info("Controller is starting") self.load_base_files() server_config = Config.instance().get_section_config("Server") Config.instance().listen_for_config_changes(self._update_config) host = server_config.get("host", "localhost") port = server_config.getint("port", 3080) # clients will use the IP they use to connect to # the controller if console_host is 0.0.0.0 console_host = host if host == "0.0.0.0": host = "127.0.0.1" name = socket.gethostname() if name == "gns3vm": name = "Main server" computes = self._load_controller_settings() from gns3server.web.web_server import WebServer ssl_context = WebServer.instance(host=host, port=port).ssl_context() protocol = server_config.get("protocol", "http") if ssl_context and protocol != "https": log.warning( "Protocol changed to 'https' for local compute because SSL is enabled" .format(port)) protocol = "https" try: self._local_server = await self.add_compute( compute_id="local", name=name, protocol=protocol, host=host, console_host=console_host, port=port, user=server_config.get("user", ""), password=server_config.get("password", ""), force=True, ssl_context=ssl_context) except aiohttp.web.HTTPConflict: log.fatal( "Cannot access to the local server, make sure something else is not running on the TCP port {}" .format(port)) sys.exit(1) for c in computes: try: await self.add_compute(**c) except (aiohttp.web.HTTPError, KeyError): pass # Skip not available servers at loading try: await self.gns3vm.auto_start_vm() except GNS3VMError as e: log.warning(str(e)) await self.load_projects() await self._project_auto_open()
def run(): args = parse_arguments(sys.argv[1:]) if args.daemon and sys.platform.startswith("win"): log.critical("Daemon is not supported on Windows") sys.exit(1) if args.pid: pid_lock(args.pid) kill_ghosts() level = logging.INFO if args.debug: level = logging.DEBUG user_log = init_logger(level, logfile=args.log, quiet=args.quiet) user_log.info("GNS3 server version {}".format(__version__)) current_year = datetime.date.today().year user_log.info("Copyright (c) 2007-{} GNS3 Technologies Inc.".format(current_year)) for config_file in Config.instance().get_config_files(): user_log.info("Config file {} loaded".format(config_file)) set_config(args) server_config = Config.instance().get_section_config("Server") if server_config.getboolean("local"): log.warning("Local mode is enabled. Beware, clients will have full control on your filesystem") # we only support Python 3 version >= 3.5 if sys.version_info < (3, 5, 3): raise SystemExit("Python 3.5.3 or higher is required") user_log.info("Running with Python {major}.{minor}.{micro} and has PID {pid}".format( major=sys.version_info[0], minor=sys.version_info[1], micro=sys.version_info[2], pid=os.getpid())) # check for the correct locale (UNIX/Linux only) locale_check() try: os.getcwd() except FileNotFoundError: log.critical("The current working directory doesn't exist") return CrashReport.instance() host = server_config["host"] port = int(server_config["port"]) server = WebServer.instance(host, port) try: server.run() except OSError as e: # This is to ignore OSError: [WinError 0] The operation completed successfully exception on Windows. if not sys.platform.startswith("win") and not e.winerror == 0: raise except Exception as e: log.critical("Critical error while running the server: {}".format(e), exc_info=1) CrashReport.instance().capture_exception() return if args.pid: log.info("Remove PID file %s", args.pid) try: os.remove(args.pid) except OSError as e: log.critical("Can't remove pid file %s: %s", args.pid, str(e))