Advertisement
Guest User

hiera_cached.py

a guest
Jan 23rd, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Adds a hiera key to pillar
  4. '''
  5.  
  6. # Import python libs
  7. import logging
  8. import sys
  9.  
  10. # Import salt libs
  11. import salt.utils
  12. from salt._compat import string_types
  13.  
  14. # Import third party libs
  15. import yaml
  16.  
  17.  
  18. # Set up logging
  19. log = logging.getLogger(__name__)
  20.  
  21.  
  22. def __virtual__():
  23. '''
  24. Only return if hiera_lyft is installed
  25. '''
  26. return 'hiera_cached' if salt.utils.which('hiera') else False
  27.  
  28.  
  29. def ext_pillar(minion_id, pillar, conf_dir, conf_file):
  30. '''
  31. Execute hiera and return the data
  32. '''
  33. data = {}
  34. cmd = ('hiera {0} -c {1}')
  35. for func in __salt__.values():
  36. try:
  37. log.info(func.__module__)
  38. ext_pillars = sys.modules[func.__module__].__ext_pillars__
  39. for pillar in ext_pillars:
  40. cmd = cmd.format(pillar, conf_file)
  41. try:
  42. log.info('Running {0}'.format(cmd))
  43. ret = __salt__['cmd.run'](cmd)
  44. if ret == 'nil':
  45. return {}
  46. else:
  47. ret = ret.replace('=>', ':')
  48. ret = yaml.safe_load(ret)
  49. data[pillar] = ret
  50. except Exception:
  51. msg = 'Hiera YAML data failed to parse from conf {0}'
  52. log.critical(msg.format(conf_file))
  53. except AttributeError:
  54. pass
  55. return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement