consist.py 1.8 KB

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