Ejemplo n.º 1
0
def mod_watch(name, sfun=None, **kwargs):
    '''
    Call this function via a watch statement

    .. versionadded:: 2014.7.0

    Any parameters in the state return dictionary can be customized by adding
    the keywords ``result``, ``comment``, and ``changes``.

    .. code-block:: yaml

        this_state_will_return_changes:
          test.succeed_with_changes

        this_state_will_NOT_return_changes:
          test.succeed_without_changes

        this_state_is_watching_another_state:
          test.succeed_without_changes:
            - comment: 'This is a custom comment'
            - watch:
              - test: this_state_will_return_changes
              - test: this_state_will_NOT_return_changes

        this_state_is_also_watching_another_state:
          test.succeed_without_changes:
            - watch:
              - test: this_state_will_NOT_return_changes
    '''
    has_changes = []
    if '__reqs__' in __low__:
        for req in __low__['__reqs__']['watch']:
            tag = _gen_tag(req)
            if __running__[tag]['changes']:
                has_changes.append('{state}: {__id__}'.format(**req))

    ret = {
        'name':
        name,
        'result':
        kwargs.get('result', True),
        'comment':
        kwargs.get('comment', 'Watch statement fired.'),
        'changes':
        kwargs.get('changes', {
            'Requisites with changes': has_changes,
        }),
    }
    return ret
Ejemplo n.º 2
0
Archivo: test.py Proyecto: bryson/salt
def mod_watch(name, sfun=None, **kwargs):
    '''
    Call this function via a watch statement

    .. versionadded:: 2014.7.0

    Any parameters in the state return dictionary can be customized by adding
    the keywords ``result``, ``comment``, and ``changes``.

    .. code-block:: yaml

        this_state_will_return_changes:
          test.succeed_with_changes

        this_state_will_NOT_return_changes:
          test.succeed_without_changes

        this_state_is_watching_another_state:
          test.succeed_without_changes:
            - comment: 'This is a custom comment'
            - watch:
              - test: this_state_will_return_changes
              - test: this_state_will_NOT_return_changes

        this_state_is_also_watching_another_state:
          test.succeed_without_changes:
            - watch:
              - test: this_state_will_NOT_return_changes
    '''
    has_changes = []
    if '__reqs__' in __low__:
        for req in __low__['__reqs__']['watch']:
            tag = _gen_tag(req)
            if __running__[tag]['changes']:
                has_changes.append('{state}: {__id__}'.format(**req))

    ret = {
        'name': name,
        'result': kwargs.get('result', True),
        'comment': kwargs.get('comment', 'Watch statement fired.'),
        'changes': kwargs.get('changes', {
            'Requisites with changes': has_changes,
        }),
    }
    return ret
Ejemplo n.º 3
0
def mod_watch(name, sfun=None, **kwargs):
    """
    Call this function via a watch statement

    .. versionadded:: 2014.7.0

    Any parameters in the state return dictionary can be customized by adding
    the keywords ``result``, ``comment``, and ``changes``.

    .. code-block:: yaml

        this_state_will_return_changes:
          test.succeed_with_changes

        this_state_will_NOT_return_changes:
          test.succeed_without_changes

        this_state_is_watching_another_state:
          test.succeed_without_changes:
            - comment: 'This is a custom comment'
            - watch:
              - test: this_state_will_return_changes
              - test: this_state_will_NOT_return_changes

        this_state_is_also_watching_another_state:
          test.succeed_without_changes:
            - watch:
              - test: this_state_will_NOT_return_changes
    """
    has_changes = []
    if "__reqs__" in __low__:
        for req in __low__["__reqs__"]["watch"]:
            tag = _gen_tag(req)
            if __running__[tag]["changes"]:
                has_changes.append("{state}: {__id__}".format(**req))

    ret = {
        "name": name,
        "result": kwargs.get("result", True),
        "comment": kwargs.get("comment", "Watch statement fired."),
        "changes": kwargs.get("changes",
                              {"Requisites with changes": has_changes}),
    }
    return ret
Ejemplo n.º 4
0
Archivo: test.py Proyecto: DaveQB/salt
def mod_watch(name, sfun=None, **kwargs):
    """'
    Call this function via a watch statement

    .. versionadded:: 2014.7.0

    Any parameters in the state return dictionary can be customized by adding
    the keywords ``result``, ``comment``, and ``changes``.

    .. code-block:: yaml

        this_state_will_return_changes:
          test.succeed_with_changes

        this_state_will_NOT_return_changes:
          test.succeed_without_changes

        this_state_is_watching_another_state:
          test.succeed_without_changes:
            - comment: 'This is a custom comment'
            - watch:
              - test: this_state_will_return_changes
              - test: this_state_will_NOT_return_changes

        this_state_is_also_watching_another_state:
          test.succeed_without_changes:
            - watch:
              - test: this_state_will_NOT_return_changes
    """
    has_changes = []
    for req in __low__["__reqs__"]["watch"]:
        tag = _gen_tag(req)
        if __running__[tag]["changes"]:
            has_changes.append("{state}: {__id__}".format(**req))

    ret = {
        "name": name,
        "result": kwargs.get("result", True),
        "comment": kwargs.get("comment", "Watch statement fired."),
        "changes": kwargs.get("changes", {"Requisites with changes": has_changes}),
    }
    return ret
Ejemplo n.º 5
0
def mod_watch(name,
              sfun=None,
              sig=None,
              full_restart=False,
              init_delay=None,
              force=False,
              **kwargs):
    '''
    The service watcher, called to invoke the watch command.
    When called, it will restart or reload the named service.

    .. note::
        This state exists to support special handling of the ``watch``
        :ref:`requisite <requisites>`. It should not be called directly.

        Parameters for this function should be set by the watching service.
        (i.e. ``service.running``)

    name
        The name of the init or rc script used to manage the service

    sfun
        The original function which triggered the mod_watch call
        (`service.running`, for example).

    sig
        The string to search for when looking for the service process with ps

    reload
        If True use reload instead of the default restart. If value is a list of
        requisites; reload only if all watched changes are contained in the reload list.
        Otherwise watch will restart.

    full_restart
        Use service.full_restart instead of restart.
        When set, reload the service instead of restarting it.
        (i.e. ``service nginx reload``)

    full_restart
        Perform a full stop/start of a service by passing ``--full-restart``.
        This option is ignored if ``reload`` is set and is supported by only a few
        :py:func:`service modules <salt.modules.service>`.

    force
        Use service.force_reload instead of reload (needs reload to be set to True)

    init_delay
        Add a sleep command (in seconds) before the service is restarted/reloaded
    '''
    reload_ = kwargs.pop('reload', False)
    ret = {'name': name, 'changes': {}, 'result': True, 'comment': ''}
    past_participle = None

    if sfun == 'dead':
        verb = 'stop'
        past_participle = verb + 'ped'
        if __salt__['service.status'](name, sig):
            func = __salt__['service.stop']
        else:
            ret['result'] = True
            ret['comment'] = 'Service is already {0}'.format(past_participle)
            return ret
    elif sfun == 'running':
        if __salt__['service.status'](name, sig):
            if 'service.reload' in __salt__ and reload_:
                if isinstance(reload_, list):
                    only_reload_needed = True
                    for watch_item in kwargs['__reqs__']['watch']:
                        if __running__[_gen_tag(watch_item)]['changes']:
                            match_found = False
                            for this_reload in reload_:
                                for state, id_ in six.iteritems(this_reload):
                                    if state == watch_item['state'] \
                                            and id_ == watch_item['__id__']:
                                        match_found = True
                            if not match_found:
                                only_reload_needed = False
                    if only_reload_needed:
                        if 'service.force_reload' in __salt__ and force:
                            func = __salt__['service.force_reload']
                            verb = 'forcefully reload'
                        else:
                            func = __salt__['service.reload']
                            verb = 'reload'
                    else:
                        if 'service.full_restart' in __salt__ and full_restart:
                            func = __salt__['service.full_restart']
                            verb = 'fully restart'
                        else:
                            func = __salt__['service.restart']
                            verb = 'restart'
                else:
                    if 'service.force_reload' in __salt__ and force:
                        func = __salt__['service.force_reload']
                        verb = 'forcefully reload'
                    else:
                        func = __salt__['service.reload']
                        verb = 'reload'
            elif 'service.full_restart' in __salt__ and full_restart:
                func = __salt__['service.full_restart']
                verb = 'fully restart'
            else:
                func = __salt__['service.restart']
                verb = 'restart'
        else:
            func = __salt__['service.start']
            verb = 'start'
        if not past_participle:
            past_participle = verb + 'ed'
    else:
        ret['comment'] = 'Unable to trigger watch for service.{0}'.format(sfun)
        ret['result'] = False
        return ret

    if __opts__['test']:
        ret['result'] = None
        ret['comment'] = 'Service is set to be {0}'.format(past_participle)
        return ret

    if verb == 'start' and 'service.stop' in __salt__:
        # stop service before start
        __salt__['service.stop'](name)

    func_kwargs, warnings = _get_systemd_only(func, kwargs)
    if warnings:
        ret.setdefault('warnings', []).extend(warnings)

    try:
        result = func(name, **func_kwargs)
    except CommandExecutionError as exc:
        ret['result'] = False
        ret['comment'] = exc.strerror
        return ret

    if init_delay:
        time.sleep(init_delay)

    ret['changes'] = {name: result}
    ret['result'] = result
    ret['comment'] = 'Service {0}'.format(past_participle) if result else \
                     'Failed to {0} the service'.format(verb)
    return ret