123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- # -*- coding: utf-8 -*-
- """
- The check Thorium state is used to create gateways to commands, the checks
- make it easy to make states that watch registers for changes and then just
- succeed or fail based on the state of the register, this creates the pattern
- of having a command execution get gated by a check state via a requisite.
- """
- # import python libs
- from __future__ import absolute_import, print_function, unicode_literals
- import logging
- import salt.utils.stringutils
- log = logging.getLogger(__file__)
- def gt(name, value):
- """
- Only succeed if the value in the given register location is greater than
- the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.gt:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] > value:
- ret["result"] = True
- return ret
- def gte(name, value):
- """
- Only succeed if the value in the given register location is greater or equal
- than the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.gte:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] >= value:
- ret["result"] = True
- return ret
- def lt(name, value):
- """
- Only succeed if the value in the given register location is less than
- the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.lt:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] < value:
- ret["result"] = True
- return ret
- def lte(name, value):
- """
- Only succeed if the value in the given register location is less than
- or equal the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.lte:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] <= value:
- ret["result"] = True
- return ret
- def eq(name, value):
- """
- Only succeed if the value in the given register location is equal to
- the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.eq:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] == value:
- ret["result"] = True
- return ret
- def ne(name, value):
- """
- Only succeed if the value in the given register location is not equal to
- the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.ne:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] != value:
- ret["result"] = True
- return ret
- def contains(
- name,
- value,
- count_lt=None,
- count_lte=None,
- count_eq=None,
- count_gte=None,
- count_gt=None,
- count_ne=None,
- ):
- """
- Only succeed if the value in the given register location contains
- the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.contains:
- - value: itni
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- try:
- count_compare = (
- count_lt or count_lte or count_eq or count_gte or count_gt or count_ne
- )
- if count_compare:
- occurrences = __reg__[name]["val"].count(value)
- log.debug("%s appears %s times", value, occurrences)
- ret["result"] = True
- if count_lt:
- ret["result"] &= occurrences < count_lt
- if count_lte:
- ret["result"] &= occurrences <= count_lte
- if count_eq:
- ret["result"] &= occurrences == count_eq
- if count_gte:
- ret["result"] &= occurrences >= count_gte
- if count_gt:
- ret["result"] &= occurrences > count_gt
- if count_ne:
- ret["result"] &= occurrences != count_ne
- else:
- if value in __reg__[name]["val"]:
- ret["result"] = True
- except TypeError:
- pass
- return ret
- def event(name):
- """
- Chekcs for a specific event match and returns result True if the match
- happens
- USAGE:
- .. code-block:: yaml
- salt/foo/*/bar:
- check.event
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: salt/foo/*/bar
- """
- ret = {"name": name, "changes": {}, "comment": "", "result": False}
- for event in __events__:
- if salt.utils.stringutils.expr_match(event["tag"], name):
- ret["result"] = True
- return ret
- def len_gt(name, value):
- """
- Only succeed if length of the given register location is greater than
- the given value.
- USAGE:
- .. code-block:: yaml
- foo:
- check.len_gt:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if len(__reg__[name]["val"]) > value:
- ret["result"] = True
- return ret
- def len_gte(name, value):
- """
- Only succeed if the length of the given register location is greater or equal
- than the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.len_gte:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if len(__reg__[name]["val"]) >= value:
- ret["result"] = True
- return ret
- def len_lt(name, value):
- """
- Only succeed if the length of the given register location is less than
- the given value.
- USAGE:
- .. code-block:: yaml
- foo:
- check.len_lt:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if len(__reg__[name]["val"]) < value:
- ret["result"] = True
- return ret
- def len_lte(name, value):
- """
- Only succeed if the length of the given register location is less than
- or equal the given value
- USAGE:
- .. code-block:: yaml
- foo:
- check.len_lte:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if len(__reg__[name]["val"]) <= value:
- ret["result"] = True
- return ret
- def len_eq(name, value):
- """
- Only succeed if the length of the given register location is equal to
- the given value.
- USAGE:
- .. code-block:: yaml
- foo:
- check.len_eq:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if __reg__[name]["val"] == value:
- ret["result"] = True
- return ret
- def len_ne(name, value):
- """
- Only succeed if the length of the given register location is not equal to
- the given value.
- USAGE:
- .. code-block:: yaml
- foo:
- check.len_ne:
- - value: 42
- run_remote_ex:
- local.cmd:
- - tgt: '*'
- - func: test.ping
- - require:
- - check: foo
- """
- ret = {"name": name, "result": False, "comment": "", "changes": {}}
- if name not in __reg__:
- ret["result"] = False
- ret["comment"] = "Value {0} not in register".format(name)
- return ret
- if len(__reg__[name]["val"]) != value:
- ret["result"] = True
- return ret
|