New Saltstack Salt formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Import python libs
  2. import logging
  3. import glob
  4. import os
  5. import yaml
  6. # Import salt modules
  7. import salt.client
  8. # Import third party libs
  9. from jsonschema import validate
  10. from jsonschema.validators import validator_for
  11. from jsonschema.exceptions import SchemaError
  12. __virtualname__ = 'modelschema'
  13. LOG = logging.getLogger(__name__)
  14. BASE_DIR = '/usr/share/salt-formulas/env/_formulas'
  15. def _get_schemas():
  16. '''
  17. Method will return all known schemas.
  18. '''
  19. output = {}
  20. schemas = glob.glob('{}/*/schemas/*.yaml'.format(BASE_DIR))
  21. for schema in schemas:
  22. if os.path.exists(schema):
  23. filename = schema.split('/')[-1].replace('.yaml', '')
  24. service_name, role_name = filename.split('-')
  25. if service_name not in output:
  26. output[service_name] = {}
  27. with open(schema, 'r') as stream:
  28. try:
  29. data = yaml.load(stream)
  30. except yaml.YAMLError as exc:
  31. data = None
  32. LOG.error(exc)
  33. output[service_name][role_name] = data
  34. return output
  35. def validate_node_model(target, service, role):
  36. '''
  37. Validates pillar by schema for given given minion, service and role.
  38. If no service and role is specified, method will validate all
  39. defined services on minion.
  40. CLI Example:
  41. .. code-block:: bash
  42. salt-run modelschema.validate_node_model
  43. '''
  44. client = salt.client.LocalClient(__opts__['conf_file'])
  45. schema = _get_schemas()[service][role]
  46. result = {}
  47. validator = validator_for(schema)
  48. try:
  49. validator.check_schema(schema)
  50. except SchemaError as exception:
  51. LOG.error(exception)
  52. return result
  53. minions = client.cmd(target, 'pillar.data', timeout=1)
  54. for minion, pillar in minions.items():
  55. model = pillar[service][role]
  56. validation_result = validator(schema).validate(model)
  57. result[minion] = validation_result
  58. return result