check.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. # -*- coding: utf-8 -*-
  2. """
  3. The check Thorium state is used to create gateways to commands, the checks
  4. make it easy to make states that watch registers for changes and then just
  5. succeed or fail based on the state of the register, this creates the pattern
  6. of having a command execution get gated by a check state via a requisite.
  7. """
  8. # import python libs
  9. from __future__ import absolute_import, print_function, unicode_literals
  10. import logging
  11. import salt.utils.stringutils
  12. log = logging.getLogger(__file__)
  13. def gt(name, value):
  14. """
  15. Only succeed if the value in the given register location is greater than
  16. the given value
  17. USAGE:
  18. .. code-block:: yaml
  19. foo:
  20. check.gt:
  21. - value: 42
  22. run_remote_ex:
  23. local.cmd:
  24. - tgt: '*'
  25. - func: test.ping
  26. - require:
  27. - check: foo
  28. """
  29. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  30. if name not in __reg__:
  31. ret["result"] = False
  32. ret["comment"] = "Value {0} not in register".format(name)
  33. return ret
  34. if __reg__[name]["val"] > value:
  35. ret["result"] = True
  36. return ret
  37. def gte(name, value):
  38. """
  39. Only succeed if the value in the given register location is greater or equal
  40. than the given value
  41. USAGE:
  42. .. code-block:: yaml
  43. foo:
  44. check.gte:
  45. - value: 42
  46. run_remote_ex:
  47. local.cmd:
  48. - tgt: '*'
  49. - func: test.ping
  50. - require:
  51. - check: foo
  52. """
  53. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  54. if name not in __reg__:
  55. ret["result"] = False
  56. ret["comment"] = "Value {0} not in register".format(name)
  57. return ret
  58. if __reg__[name]["val"] >= value:
  59. ret["result"] = True
  60. return ret
  61. def lt(name, value):
  62. """
  63. Only succeed if the value in the given register location is less than
  64. the given value
  65. USAGE:
  66. .. code-block:: yaml
  67. foo:
  68. check.lt:
  69. - value: 42
  70. run_remote_ex:
  71. local.cmd:
  72. - tgt: '*'
  73. - func: test.ping
  74. - require:
  75. - check: foo
  76. """
  77. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  78. if name not in __reg__:
  79. ret["result"] = False
  80. ret["comment"] = "Value {0} not in register".format(name)
  81. return ret
  82. if __reg__[name]["val"] < value:
  83. ret["result"] = True
  84. return ret
  85. def lte(name, value):
  86. """
  87. Only succeed if the value in the given register location is less than
  88. or equal the given value
  89. USAGE:
  90. .. code-block:: yaml
  91. foo:
  92. check.lte:
  93. - value: 42
  94. run_remote_ex:
  95. local.cmd:
  96. - tgt: '*'
  97. - func: test.ping
  98. - require:
  99. - check: foo
  100. """
  101. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  102. if name not in __reg__:
  103. ret["result"] = False
  104. ret["comment"] = "Value {0} not in register".format(name)
  105. return ret
  106. if __reg__[name]["val"] <= value:
  107. ret["result"] = True
  108. return ret
  109. def eq(name, value):
  110. """
  111. Only succeed if the value in the given register location is equal to
  112. the given value
  113. USAGE:
  114. .. code-block:: yaml
  115. foo:
  116. check.eq:
  117. - value: 42
  118. run_remote_ex:
  119. local.cmd:
  120. - tgt: '*'
  121. - func: test.ping
  122. - require:
  123. - check: foo
  124. """
  125. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  126. if name not in __reg__:
  127. ret["result"] = False
  128. ret["comment"] = "Value {0} not in register".format(name)
  129. return ret
  130. if __reg__[name]["val"] == value:
  131. ret["result"] = True
  132. return ret
  133. def ne(name, value):
  134. """
  135. Only succeed if the value in the given register location is not equal to
  136. the given value
  137. USAGE:
  138. .. code-block:: yaml
  139. foo:
  140. check.ne:
  141. - value: 42
  142. run_remote_ex:
  143. local.cmd:
  144. - tgt: '*'
  145. - func: test.ping
  146. - require:
  147. - check: foo
  148. """
  149. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  150. if name not in __reg__:
  151. ret["result"] = False
  152. ret["comment"] = "Value {0} not in register".format(name)
  153. return ret
  154. if __reg__[name]["val"] != value:
  155. ret["result"] = True
  156. return ret
  157. def contains(
  158. name,
  159. value,
  160. count_lt=None,
  161. count_lte=None,
  162. count_eq=None,
  163. count_gte=None,
  164. count_gt=None,
  165. count_ne=None,
  166. ):
  167. """
  168. Only succeed if the value in the given register location contains
  169. the given value
  170. USAGE:
  171. .. code-block:: yaml
  172. foo:
  173. check.contains:
  174. - value: itni
  175. run_remote_ex:
  176. local.cmd:
  177. - tgt: '*'
  178. - func: test.ping
  179. - require:
  180. - check: foo
  181. """
  182. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  183. if name not in __reg__:
  184. ret["result"] = False
  185. ret["comment"] = "Value {0} not in register".format(name)
  186. return ret
  187. try:
  188. count_compare = (
  189. count_lt or count_lte or count_eq or count_gte or count_gt or count_ne
  190. )
  191. if count_compare:
  192. occurrences = __reg__[name]["val"].count(value)
  193. log.debug("%s appears %s times", value, occurrences)
  194. ret["result"] = True
  195. if count_lt:
  196. ret["result"] &= occurrences < count_lt
  197. if count_lte:
  198. ret["result"] &= occurrences <= count_lte
  199. if count_eq:
  200. ret["result"] &= occurrences == count_eq
  201. if count_gte:
  202. ret["result"] &= occurrences >= count_gte
  203. if count_gt:
  204. ret["result"] &= occurrences > count_gt
  205. if count_ne:
  206. ret["result"] &= occurrences != count_ne
  207. else:
  208. if value in __reg__[name]["val"]:
  209. ret["result"] = True
  210. except TypeError:
  211. pass
  212. return ret
  213. def event(name):
  214. """
  215. Chekcs for a specific event match and returns result True if the match
  216. happens
  217. USAGE:
  218. .. code-block:: yaml
  219. salt/foo/*/bar:
  220. check.event
  221. run_remote_ex:
  222. local.cmd:
  223. - tgt: '*'
  224. - func: test.ping
  225. - require:
  226. - check: salt/foo/*/bar
  227. """
  228. ret = {"name": name, "changes": {}, "comment": "", "result": False}
  229. for event in __events__:
  230. if salt.utils.stringutils.expr_match(event["tag"], name):
  231. ret["result"] = True
  232. return ret
  233. def len_gt(name, value):
  234. """
  235. Only succeed if length of the given register location is greater than
  236. the given value.
  237. USAGE:
  238. .. code-block:: yaml
  239. foo:
  240. check.len_gt:
  241. - value: 42
  242. run_remote_ex:
  243. local.cmd:
  244. - tgt: '*'
  245. - func: test.ping
  246. - require:
  247. - check: foo
  248. """
  249. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  250. if name not in __reg__:
  251. ret["result"] = False
  252. ret["comment"] = "Value {0} not in register".format(name)
  253. return ret
  254. if len(__reg__[name]["val"]) > value:
  255. ret["result"] = True
  256. return ret
  257. def len_gte(name, value):
  258. """
  259. Only succeed if the length of the given register location is greater or equal
  260. than the given value
  261. USAGE:
  262. .. code-block:: yaml
  263. foo:
  264. check.len_gte:
  265. - value: 42
  266. run_remote_ex:
  267. local.cmd:
  268. - tgt: '*'
  269. - func: test.ping
  270. - require:
  271. - check: foo
  272. """
  273. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  274. if name not in __reg__:
  275. ret["result"] = False
  276. ret["comment"] = "Value {0} not in register".format(name)
  277. return ret
  278. if len(__reg__[name]["val"]) >= value:
  279. ret["result"] = True
  280. return ret
  281. def len_lt(name, value):
  282. """
  283. Only succeed if the length of the given register location is less than
  284. the given value.
  285. USAGE:
  286. .. code-block:: yaml
  287. foo:
  288. check.len_lt:
  289. - value: 42
  290. run_remote_ex:
  291. local.cmd:
  292. - tgt: '*'
  293. - func: test.ping
  294. - require:
  295. - check: foo
  296. """
  297. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  298. if name not in __reg__:
  299. ret["result"] = False
  300. ret["comment"] = "Value {0} not in register".format(name)
  301. return ret
  302. if len(__reg__[name]["val"]) < value:
  303. ret["result"] = True
  304. return ret
  305. def len_lte(name, value):
  306. """
  307. Only succeed if the length of the given register location is less than
  308. or equal the given value
  309. USAGE:
  310. .. code-block:: yaml
  311. foo:
  312. check.len_lte:
  313. - value: 42
  314. run_remote_ex:
  315. local.cmd:
  316. - tgt: '*'
  317. - func: test.ping
  318. - require:
  319. - check: foo
  320. """
  321. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  322. if name not in __reg__:
  323. ret["result"] = False
  324. ret["comment"] = "Value {0} not in register".format(name)
  325. return ret
  326. if len(__reg__[name]["val"]) <= value:
  327. ret["result"] = True
  328. return ret
  329. def len_eq(name, value):
  330. """
  331. Only succeed if the length of the given register location is equal to
  332. the given value.
  333. USAGE:
  334. .. code-block:: yaml
  335. foo:
  336. check.len_eq:
  337. - value: 42
  338. run_remote_ex:
  339. local.cmd:
  340. - tgt: '*'
  341. - func: test.ping
  342. - require:
  343. - check: foo
  344. """
  345. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  346. if name not in __reg__:
  347. ret["result"] = False
  348. ret["comment"] = "Value {0} not in register".format(name)
  349. return ret
  350. if __reg__[name]["val"] == value:
  351. ret["result"] = True
  352. return ret
  353. def len_ne(name, value):
  354. """
  355. Only succeed if the length of the given register location is not equal to
  356. the given value.
  357. USAGE:
  358. .. code-block:: yaml
  359. foo:
  360. check.len_ne:
  361. - value: 42
  362. run_remote_ex:
  363. local.cmd:
  364. - tgt: '*'
  365. - func: test.ping
  366. - require:
  367. - check: foo
  368. """
  369. ret = {"name": name, "result": False, "comment": "", "changes": {}}
  370. if name not in __reg__:
  371. ret["result"] = False
  372. ret["comment"] = "Value {0} not in register".format(name)
  373. return ret
  374. if len(__reg__[name]["val"]) != value:
  375. ret["result"] = True
  376. return ret