コード例 #1
0
def _create_service_s6(base_dir,
                       name,
                       app_run_script,
                       userid='root',
                       downed=False,
                       environ_dir=None,
                       environ=None,
                       environment='prod',
                       monitor_policy=None,
                       trace=None,
                       timeout_finish=None,
                       notification_fd=None,
                       call_before_run=None,
                       call_before_finish=None,
                       run_script='s6.run',
                       log_run_script='s6.logger.run',
                       finish_script='s6.finish',
                       logger_args=None,
                       **kwargs):
    """Initializes service directory.

    Creates run, finish scripts as well as log directory with appropriate
    run script.
    """
    # Disable R0912: Too many branches
    # pylint: disable=R0912
    try:
        user_pw = pwd.getpwnam(userid)

    except KeyError:
        # Check the identity we are going to run as. It needs to exists on the
        # host or we will fail later on as we try to seteuid.
        _LOGGER.exception('Unable to find userid %r in passwd database.',
                          userid)
        raise

    if isinstance(base_dir, sup_impl.ScanDir):
        # We are given a scandir as base, use it.
        svc = base_dir.add_service(name, _service_base.ServiceType.LongRun)
    else:
        svc = LongrunService(base_dir, name)

    # Setup the environ
    if environ is None:
        svc_environ = {}
    else:
        svc_environ = environ.copy()
    svc_environ['HOME'] = user_pw.pw_dir
    svc.environ = svc_environ

    if environment == 'prod':
        ionice_prio = 5
    else:
        ionice_prio = 6

    # Setup the run script
    svc.run_script = utils.generate_template(run_script,
                                             user=userid,
                                             shell=user_pw.pw_shell,
                                             environ_dir=environ_dir,
                                             trace=trace,
                                             call_before_run=call_before_run,
                                             _alias=subproc.get_aliases())

    if monitor_policy is not None or call_before_finish is not None:
        # Setup the finish script
        svc.finish_script = utils.generate_template(
            finish_script,
            monitor_policy=monitor_policy,
            trace=trace,
            call_before_finish=call_before_finish,
            _alias=subproc.get_aliases())

    if log_run_script is not None:
        if logger_args is None:
            logger_args = '-b -p T n20 s1000000'

        # Setup the log run script
        svc.log_run_script = utils.generate_template(
            log_run_script,
            logdir=os.path.relpath(os.path.join(svc.data_dir, 'log'),
                                   svc.logger_dir),
            logger_args=logger_args,
            _alias=subproc.get_aliases())

    svc.default_down = bool(downed)
    svc.notification_fd = notification_fd

    if monitor_policy is not None:
        svc.timeout_finish = 0
        if monitor_policy['limit'] > 0:
            exits_dir = os.path.join(svc.data_dir, EXITS_DIR)
            fs.mkdir_safe(exits_dir)
            fs.rm_children_safe(exits_dir)
    else:
        svc.timeout_finish = timeout_finish

    svc.write()

    # Write the app_start script
    supervisor_utils.script_write(os.path.join(svc.data_dir, 'app_start'),
                                  app_run_script)

    return svc
コード例 #2
0
def _create_service_winss(base_dir,
                          name,
                          app_run_script,
                          downed=False,
                          environ=None,
                          monitor_policy=None,
                          timeout_finish=None,
                          run_script='winss.run',
                          log_run_script='winss.logger.run',
                          finish_script='winss.finish',
                          **kwargs):
    """Initializes service directory.

    Creates run, finish scripts as well as log directory with appropriate
    run script.
    """
    if isinstance(base_dir, sup_impl.ScanDir):
        # We are given a scandir as base, use it.
        svc = base_dir.add_service(name, _service_base.ServiceType.LongRun)
    else:
        svc = LongrunService(base_dir, name)

    # Setup the environ
    if environ is None:
        svc_environ = {}
    else:
        svc_environ = environ.copy()

    svc.environ = svc_environ

    # Setup the run script
    svc.run_script = utils.generate_template(run_script,
                                             app_run_script=app_run_script,
                                             _alias=subproc.get_aliases())

    if monitor_policy is not None:
        # Setup the finish script
        svc.finish_script = utils.generate_template(
            finish_script,
            monitor_policy=monitor_policy,
            _alias=subproc.get_aliases())

    logdir = os.path.join(svc.data_dir, 'log')
    fs.mkdir_safe(logdir)

    if log_run_script is not None:
        # Setup the log run script
        svc.log_run_script = utils.generate_template(
            log_run_script,
            logdir=os.path.relpath(logdir, svc.logger_dir),
            _alias=subproc.get_aliases())

    svc.default_down = bool(downed)
    if monitor_policy is not None:
        svc.timeout_finish = 0
        if monitor_policy['limit'] > 0:
            exits_dir = os.path.join(svc.data_dir, EXITS_DIR)
            fs.mkdir_safe(exits_dir)
            fs.rm_children_safe(exits_dir)
    else:
        svc.timeout_finish = timeout_finish

    svc.write()

    return svc
コード例 #3
0
def _create_service_s6(base_dir,
                       name,
                       app_run_script,
                       userid='root',
                       downed=False,
                       environ_dir=None,
                       environ=None,
                       environment='prod',
                       monitor_policy=None,
                       trace=None,
                       timeout_finish=None,
                       run_script='s6.run',
                       log_run_script='s6.logger.run',
                       finish_script='s6.finish',
                       **kwargs):
    """Initializes service directory.

    Creates run, finish scripts as well as log directory with appropriate
    run script.
    """
    try:
        user_pw = pwd.getpwnam(userid)

    except KeyError:
        # Check the identity we are going to run as. It needs to exists on the
        # host or we will fail later on as we try to seteuid.
        _LOGGER.exception('Unable to find userid %r in passwd database.',
                          userid)
        raise

    if isinstance(base_dir, sup_impl.ScanDir):
        # We are given a scandir as base, use it.
        svc = base_dir.add_service(name, _service_base.ServiceType.LongRun)
    else:
        svc = LongrunService(base_dir, name)

    # Setup the environ
    if environ is None:
        svc_environ = {}
    else:
        svc_environ = environ.copy()
    svc_environ['HOME'] = user_pw.pw_dir
    svc.environ = svc_environ

    if environment == 'prod':
        ionice_prio = 5
    else:
        ionice_prio = 6

    monitored = (monitor_policy is not None)

    # Setup the run script
    svc.run_script = utils.generate_template(run_script,
                                             user=userid,
                                             shell=user_pw.pw_shell,
                                             environ_dir=environ_dir,
                                             monitored=monitored,
                                             ionice_prio=ionice_prio,
                                             _alias=subproc.get_aliases())
    # Setup the finish script
    svc.finish_script = utils.generate_template(finish_script,
                                                _alias=subproc.get_aliases())

    if log_run_script is not None:
        # Setup the log run script
        svc.log_run_script = utils.generate_template(
            log_run_script,
            logdir=os.path.relpath(os.path.join(svc.data_dir, 'log'),
                                   svc.logger_dir),
            _alias=subproc.get_aliases())

    svc.default_down = bool(downed)
    if monitored:
        svc.timeout_finish = 0
    else:
        svc.timeout_finish = timeout_finish

    svc.write()

    # Write the app_start script
    supervisor_utils.script_write(os.path.join(svc.data_dir, 'app_start'),
                                  app_run_script)
    # Optionally write a monitor policy file
    _LOGGER.info('monitor_policy, %r', monitor_policy)
    if monitor_policy is not None:
        exits_dir = os.path.join(svc.data_dir, EXITS_DIR)
        fs.mkdir_safe(exits_dir)
        fs.rm_children_safe(exits_dir)

        supervisor_utils.data_write(os.path.join(svc.data_dir, POLICY_JSON),
                                    json.dumps(monitor_policy))
    # Optionally write trace information file
    if trace is not None:
        supervisor_utils.data_write(os.path.join(svc.data_dir, TRACE_FILE),
                                    json.dumps(trace))

    return svc