def _restart_api_server(self): self.server.terminate() self.server.join() # kill all the running plugins - forked from restapi server holds # the smokerd port - new server cannot bind for proc in psutil.process_iter(): if proc.name.startswith('smokerd plugin'): lg.info("Killing running plugin %s", proc.name) proc.kill() self.server = RestServer(self) self.server.start()
def run(self): """ Run daemon * change effective uid/gid * start thread for each check * start webserver """ lg.info("Starting daemon") # Change effective UID/GID if 'uid' in self.conf and 'gid' in self.conf: if os.geteuid != self.conf['uid'] and os.getegid != self.conf[ 'gid']: try: os.setegid(self.conf['gid']) os.seteuid(self.conf['uid']) except TypeError as e: lg.error( "Config parameters uid/gid have to be integers: %s" % e) except OSError as e: lg.error("Can't switch effective UID/GID to %s/%s: %s" % (self.conf['uid'], self.conf['gid'], e)) lg.exception(e) self._shutdown(exitcode=1) else: lg.info("Not changing effective UID/GID, keeping %s/%s" % (os.geteuid(), os.getegid())) if not isinstance(self.conf['bind_port'], int): lg.error("Config parameter bind_port has to be integer") # Initialize plugin manager config = {} for key in ['plugins', 'templates', 'actions']: try: config[key] = self.conf[key] except KeyError as e: lg.warn("Config section not found: %s" % e) # Check we have some plugins configured if not config['plugins']: lg.error('No configured plugins') self._shutdown(exitcode=1) if 'nr_concurrent_plugins' in self.conf: config['semaphore_count'] = self.conf['nr_concurrent_plugins'] try: self.pluginmgr = PluginManager(**config) except Exception as e: lg.error("Can't initialize PluginManager") lg.exception(e) self._shutdown(exitcode=1) lg.info("Starting webserver on %(bind_host)s:%(bind_port)s" % self.conf) try: self.server = RestServer(self) self.server.start() except Exception as e: lg.error("Can't start HTTP server: %s" % e) lg.exception(e) self._shutdown(exitcode=1) # Catch SIGINT and SIGTERM if supported if hasattr(signal, 'SIGINT'): signal.signal(signal.SIGINT, self._shutdown) if hasattr(signal, 'SIGTERM'): signal.signal(signal.SIGTERM, self._shutdown) if hasattr(signal, 'SIGHUP'): signal.signal(signal.SIGHUP, self._reopen_logfiles) self._watchdog()