def win_svc(test_home): """Init Windows service.""" import win32service cenv = os.environ.copy() cenv.update(dict(SWAK_HOME=test_home)) os.environ = cenv home, cfg = select_and_parse(test_home) svc_name = cfg['svc_name'] if get_winsvc_status(svc_name) is not None: logging.critical("Found previously installed service. Remove it" " first.") p = Popen(WSVC_CMD_STOP, env=cenv) assert p.returncode is None p = Popen(WSVC_CMD_REMOVE, env=cenv) time.sleep(3) assert p.returncode is None time.sleep(3) # build # p = Popen(WSVC_CMD_BUILD, env=cenv) # assert p.returncode is None # install p = Popen(WSVC_CMD_INSTALL, env=cenv) assert p.returncode is None time.sleep(SLEEP_TIME) ret = get_winsvc_status(svc_name) assert ret is not None assert win32service.SERVICE_STOPPED == ret[WSVC_CUR_STATE] # start p = Popen(WSVC_CMD_START, env=cenv) assert p.returncode is None time.sleep(SLEEP_TIME) ret = get_winsvc_status(svc_name) # TODO: Service does not start in AppVeyor. assert win32service.SERVICE_RUNNING == ret[WSVC_CUR_STATE] yield None # stop p = Popen(WSVC_CMD_STOP, env=cenv) assert p.returncode is None time.sleep(SLEEP_TIME) ret = get_winsvc_status(svc_name) assert win32service.SERVICE_STOPPED == ret[WSVC_CUR_STATE] # remove p = Popen(WSVC_CMD_REMOVE, env=cenv) time.sleep(SLEEP_TIME) assert p.returncode is None assert None is get_winsvc_status(svc_name)
def daemon(ctx, home): """Daemon CLI entry.""" # Select home and parse its config home, cfg = select_and_parse(home) # Init required directories init_home(home, cfg) # Init logger logconfig.dictConfig(cfg['logger']) ctx.obj['home'] = home # Make pid_path pid_path = get_pid_path(home, cfg['svc_name']) ctx.obj['pid_path'] = pid_path
def test_util_cfg(test_home): """Test config utilities.""" home = select_home(test_home) cpath = get_config_path() assert CFG_FNAME in cpath.replace(home, '').strip(os.sep) # select explicit home assert '/path/to/expcfg' == select_home('/path/to/expcfg', False) # select envvar home os.environ['SWAK_HOME'] = '/path/to/home' home = select_home(None, False) assert '/path/to/home' == home # prefer explicit home to envvar assert '/path/to/home2' == select_home('/path/to/home2', False) # select home as exe dir if no explict / envvar home exists. del os.environ['SWAK_HOME'] path = os.path.join(get_exe_dir(), 'config.yml') with open(path, 'wt') as f: f.write(CFG) assert get_exe_dir() == select_home(None, False) # test select and parse home, cfg = select_and_parse() assert get_exe_dir() == home assert 'swak-test' == cfg['svc_name'] # check default logger assert 'logger' in cfg # check environment variable resolution assert home in cfg['logger']['handlers']['file']['filename'] # select home and parse config home, cfg = select_and_parse(test_home) init_home(home, cfg) os.unlink(path)
def unix_svc(test_home, capfd): """Init Unix service.""" cenv = os.environ.copy() cenv.update(dict(SWAK_HOME=test_home)) home, cfg = select_and_parse(test_home) pid_path = get_pid_path(test_home, cfg['svc_name']) # stop previous daemon if os.path.isfile(pid_path): Popen(USVC_CMD_STOP, env=cenv) p = Popen(USVC_CMD_START, env=cenv) assert p.returncode is None time.sleep(3) assert os.path.isfile(pid_path) yield None p = Popen(USVC_CMD_STOP, env=cenv) assert p.returncode is None time.sleep(3) assert not os.path.isfile(pid_path)
"""This module implements data router.""" import logging from swak.data import MultiDataStream, OneDataStream from swak.match import MatchPattern, OrMatchPattern from swak.plugin import Modifier, Output, is_kind_of_output from swak.config import select_and_parse _, cfg = select_and_parse() DEBUG = cfg['debug'] default_placeholder = None class Pipeline(object): """Pipeline class.""" def __init__(self, tag): """Init. Args: tag (str): Pipeline tag """ self.modifiers = [] self.output = None self.tag = tag def add_modifier(self, modifier): """Add modifier.""" logging.debug("add_modifier {}".format(modifier)) self.modifiers.append(modifier)
sys.argv = [prog_name()] print("================= Service Commands =================") win32serviceutil.HandleCommandLine(None) # Handle help / cli command beforehand (No need to parse config) print(sys.argv) if len(sys.argv) >= 2: cmd = sys.argv[1] if cmd == 'help': show_usage() elif cmd == 'cli': main(obj={}, prog_name="{} cli".format(prog_name()), args=sys.argv[2:]) sys.exit() home, cfg = select_and_parse(None) # Init required directories init_home(home, cfg) # Init logger logconfig.dictConfig(cfg['logger']) class SwakService(win32serviceutil.ServiceFramework): """Swak service class.""" _svc_name_ = cfg['svc_name'] _svc_display_name_ = cfg['svc_dname'] def __init__(self, args): """Init.""" win32serviceutil.ServiceFramework.__init__(self, args)