consist.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. # pylint: disable=resource-leakage
  3. # Import Python libs
  4. from __future__ import absolute_import, print_function
  5. import hashlib
  6. import optparse
  7. import pprint
  8. import subprocess
  9. # Import Salt libs
  10. import salt.utils.color
  11. import salt.utils.files
  12. import salt.utils.yaml
  13. # Import 3rd-party libs
  14. from salt.ext import six
  15. colors = salt.utils.color.get_colors()
  16. def parse():
  17. """
  18. Parse command line options
  19. """
  20. parser = optparse.OptionParser()
  21. parser.add_option(
  22. "-r",
  23. "--runs",
  24. dest="runs",
  25. default=10,
  26. type=int,
  27. help="Specify the number of times to run the consistency check",
  28. )
  29. parser.add_option(
  30. "-c",
  31. "--command",
  32. dest="command",
  33. default="state.show_highstate",
  34. help="The command to execute",
  35. )
  36. options, args = parser.parse_args()
  37. return options.__dict__
  38. def run(command):
  39. """
  40. Execute a single command and check the returns
  41. """
  42. cmd = r"salt \* {0} --yaml-out -t 500 > high".format(command)
  43. subprocess.call(cmd, shell=True)
  44. with salt.utils.files.fopen("high") as fp_:
  45. data = salt.utils.yaml.safe_load(fp_)
  46. hashes = set()
  47. for key, val in six.iteritems(data):
  48. has = hashlib.md5(str(val)).hexdigest()
  49. if has not in hashes:
  50. print("{0}:".format(has))
  51. pprint.pprint(val)
  52. hashes.add(has)
  53. if len(hashes) > 1:
  54. print(
  55. "{0}Command: {1} gave inconsistent returns{2}".format(
  56. colors["LIGHT_RED"], command, colors["ENDC"]
  57. )
  58. )
  59. if __name__ == "__main__":
  60. opts = parse()
  61. for _ in opts["runs"]:
  62. for command in opts["command"].split(","):
  63. print("-" * 30)
  64. print("Running command {0}".format(command))
  65. run(command)