stack_advisor.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env ambari-python-wrap
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. '''
  17. import ambari_simplejson as json
  18. import os
  19. import sys
  20. import traceback
  21. RECOMMEND_COMPONENT_LAYOUT_ACTION = 'recommend-component-layout'
  22. VALIDATE_COMPONENT_LAYOUT_ACTION = 'validate-component-layout'
  23. RECOMMEND_CONFIGURATIONS = 'recommend-configurations'
  24. RECOMMEND_CONFIGURATION_DEPENDENCIES = 'recommend-configuration-dependencies'
  25. VALIDATE_CONFIGURATIONS = 'validate-configurations'
  26. ALL_ACTIONS = [RECOMMEND_COMPONENT_LAYOUT_ACTION,
  27. VALIDATE_COMPONENT_LAYOUT_ACTION,
  28. RECOMMEND_CONFIGURATIONS,
  29. RECOMMEND_CONFIGURATION_DEPENDENCIES,
  30. VALIDATE_CONFIGURATIONS]
  31. USAGE = "Usage: <action> <hosts_file> <services_file>\nPossible actions are: {0}\n".format( str(ALL_ACTIONS) )
  32. SCRIPT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
  33. STACK_ADVISOR_PATH_TEMPLATE = os.path.join(SCRIPT_DIRECTORY, '../stacks/stack_advisor.py')
  34. STACK_ADVISOR_DEFAULT_IMPL_CLASS = 'DefaultStackAdvisor'
  35. STACK_ADVISOR_IMPL_PATH_TEMPLATE = os.path.join(SCRIPT_DIRECTORY, './../stacks/{0}/{1}/services/stack_advisor.py')
  36. STACK_ADVISOR_IMPL_CLASS_TEMPLATE = '{0}{1}StackAdvisor'
  37. class StackAdvisorException(Exception):
  38. pass
  39. def loadJson(path):
  40. try:
  41. with open(path, 'r') as f:
  42. return json.load(f)
  43. except Exception as err:
  44. traceback.print_exc()
  45. raise StackAdvisorException("Error loading file at: {0}".format(path))
  46. def dumpJson(json_object, dump_file):
  47. try:
  48. with open(dump_file, 'w') as out:
  49. json.dump(json_object, out, indent=1)
  50. except Exception as err:
  51. traceback.print_exc()
  52. raise StackAdvisorException("Error writing to file {0} : {1}".format(dump_file, str(err)))
  53. def main(argv=None):
  54. args = argv[1:]
  55. if len(args) < 3:
  56. sys.stderr.write(USAGE)
  57. sys.exit(2)
  58. action = args[0]
  59. if action not in ALL_ACTIONS:
  60. sys.stderr.write(USAGE)
  61. sys.exit(2)
  62. hostsFile = args[1]
  63. servicesFile = args[2]
  64. # Parse hostsFile and servicesFile
  65. hosts = loadJson(hostsFile)
  66. services = loadJson(servicesFile)
  67. # Instantiate StackAdvisor and call action related method
  68. stackName = services["Versions"]["stack_name"]
  69. stackVersion = services["Versions"]["stack_version"]
  70. parentVersions = []
  71. if "stack_hierarchy" in services["Versions"]:
  72. parentVersions = services["Versions"]["stack_hierarchy"]["stack_versions"]
  73. stackAdvisor = instantiateStackAdvisor(stackName, stackVersion, parentVersions)
  74. # Perform action
  75. actionDir = os.path.realpath(os.path.dirname(args[1]))
  76. # filter
  77. hosts = stackAdvisor.filterHostMounts(hosts, services)
  78. if action == RECOMMEND_COMPONENT_LAYOUT_ACTION:
  79. services['context'] = {'call_type': 'recommendComponentLayout'}
  80. result = stackAdvisor.recommendComponentLayout(services, hosts)
  81. result_file = os.path.join(actionDir, "component-layout.json")
  82. elif action == VALIDATE_COMPONENT_LAYOUT_ACTION:
  83. services['context'] = {'call_type': 'validateComponentLayout'}
  84. result = stackAdvisor.validateComponentLayout(services, hosts)
  85. result_file = os.path.join(actionDir, "component-layout-validation.json")
  86. elif action == RECOMMEND_CONFIGURATIONS:
  87. services['context'] = {'call_type': 'recommendConfigurations'}
  88. result = stackAdvisor.recommendConfigurations(services, hosts)
  89. result_file = os.path.join(actionDir, "configurations.json")
  90. elif action == RECOMMEND_CONFIGURATION_DEPENDENCIES:
  91. services['context'] = {'call_type': 'recommendConfigurationDependencies'}
  92. result = stackAdvisor.recommendConfigurationDependencies(services, hosts)
  93. result_file = os.path.join(actionDir, "configurations.json")
  94. else: # action == VALIDATE_CONFIGURATIONS
  95. services['context'] = {'call_type': 'validateConfigurations'}
  96. result = stackAdvisor.validateConfigurations(services, hosts)
  97. result_file = os.path.join(actionDir, "configurations-validation.json")
  98. dumpJson(result, result_file)
  99. def instantiateStackAdvisor(stackName, stackVersion, parentVersions):
  100. """Instantiates StackAdvisor implementation for the specified Stack"""
  101. import imp
  102. with open(STACK_ADVISOR_PATH_TEMPLATE, 'rb') as fp:
  103. default_stack_advisor = imp.load_module('stack_advisor', fp, STACK_ADVISOR_PATH_TEMPLATE, ('.py', 'rb', imp.PY_SOURCE))
  104. className = STACK_ADVISOR_DEFAULT_IMPL_CLASS
  105. stack_advisor = default_stack_advisor
  106. versions = [stackVersion]
  107. versions.extend(parentVersions)
  108. for version in reversed(versions):
  109. try:
  110. path = STACK_ADVISOR_IMPL_PATH_TEMPLATE.format(stackName, version)
  111. with open(path, 'rb') as fp:
  112. stack_advisor = imp.load_module('stack_advisor_impl', fp, path, ('.py', 'rb', imp.PY_SOURCE))
  113. className = STACK_ADVISOR_IMPL_CLASS_TEMPLATE.format(stackName, version.replace('.', ''))
  114. print "StackAdvisor implementation for stack {0}, version {1} was loaded".format(stackName, version)
  115. except IOError: # file not found
  116. traceback.print_exc()
  117. print "StackAdvisor implementation for stack {0}, version {1} was not found".format(stackName, version)
  118. try:
  119. clazz = getattr(stack_advisor, className)
  120. print "Returning " + className + " implementation"
  121. return clazz()
  122. except Exception as e:
  123. traceback.print_exc()
  124. print "Returning default implementation"
  125. return default_stack_advisor.DefaultStackAdvisor()
  126. if __name__ == '__main__':
  127. try:
  128. main(sys.argv)
  129. except StackAdvisorException as stack_exception:
  130. traceback.print_exc()
  131. print "Error occured in stack advisor.\nError details: {0}".format(str(stack_exception))
  132. sys.exit(1)
  133. except Exception as e:
  134. traceback.print_exc()
  135. print "Error occured in stack advisor.\nError details: {0}".format(str(e))
  136. sys.exit(2)