retcode.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. '''
  2. A retcode validator
  3. '''
  4. class PepperRetcode(object):
  5. '''
  6. Validation container
  7. '''
  8. def validate(self, options, result):
  9. '''
  10. Validate result dictionary retcode values.
  11. :param options: optparse options
  12. :param result: dictionary from Saltstack master
  13. :return: exit code
  14. '''
  15. if options.fail_any:
  16. return self.validate_fail_any(result)
  17. if options.fail_any_none:
  18. return self.validate_fail_any_none(result)
  19. if options.fail_all:
  20. return self.validate_fail_all(result)
  21. if options.fail_all_none:
  22. return self.validate_fail_all_none(result)
  23. return 0
  24. @staticmethod
  25. def validate_fail_any(result):
  26. '''
  27. Validate result dictionary retcode values.
  28. Returns 0 if no retcode keys.
  29. Returns first non zero retcode if any of recodes is non zero.
  30. :param result: dictionary from Saltstack master
  31. :return: exit code
  32. '''
  33. if isinstance(result, list):
  34. if isinstance(result[0], dict):
  35. minion = result[0]
  36. retcodes = list(minion[name].get('retcode')
  37. for name in minion if isinstance(minion[name], dict) and
  38. minion[name].get('retcode') is not None)
  39. return next((r for r in retcodes if r != 0), 0)
  40. return 0
  41. @staticmethod
  42. def validate_fail_any_none(result):
  43. '''
  44. Validate result dictionary retcode values.
  45. Returns -1 if no retcode keys.
  46. Returns first non zero retcode if any of recodes is non zero.
  47. :param result: dictionary from Saltstack master
  48. :return: exit code
  49. '''
  50. if isinstance(result, list):
  51. if isinstance(result[0], dict):
  52. minion = result[0]
  53. retcodes = list(minion[name].get('retcode')
  54. for name in minion if isinstance(minion[name], dict) and
  55. minion[name].get('retcode') is not None)
  56. if not retcodes:
  57. return -1 # there are no retcodes
  58. return next((r for r in retcodes if r != 0), 0)
  59. return -1
  60. @staticmethod
  61. def validate_fail_all(result):
  62. '''
  63. Validate result dictionary retcode values.
  64. Returns 0 if no retcode keys.
  65. Returns first non zero retcode if all recodes are non zero.
  66. :param result: dictionary from Saltstack master
  67. :return: exit code
  68. '''
  69. if isinstance(result, list):
  70. if isinstance(result[0], dict):
  71. minion = result[0]
  72. retcodes = list(minion[name].get('retcode')
  73. for name in minion if isinstance(minion[name], dict) and
  74. minion[name].get('retcode') is not None)
  75. if all(r != 0 for r in retcodes):
  76. return next((r for r in retcodes if r != 0), 0)
  77. return 0
  78. @staticmethod
  79. def validate_fail_all_none(result):
  80. '''
  81. Validate result dictionary retcode values.
  82. Returns -1 if no retcode keys.
  83. Returns first non zero retcode if all recodes are non zero.
  84. :param result: dictionary from Saltstack master
  85. :return: exit code
  86. '''
  87. if isinstance(result, list):
  88. if isinstance(result[0], dict):
  89. minion = result[0]
  90. retcodes = list(minion[name].get('retcode')
  91. for name in minion if isinstance(minion[name], dict) and
  92. minion[name].get('retcode') is not None)
  93. if not retcodes:
  94. return -1 # there are no retcodes
  95. if all(r != 0 for r in retcodes):
  96. return next((r for r in retcodes if r != 0), 0)
  97. else:
  98. return 0
  99. return -1