stack_tools.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  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. __all__ = ["get_stack_tool", "get_stack_tool_name", "get_stack_tool_path",
  18. "get_stack_tool_package", "STACK_SELECTOR_NAME", "CONF_SELECTOR_NAME"]
  19. # simplejson is much faster comparing to Python 2.6 json module and has the same functions set.
  20. import ambari_simplejson as json
  21. from resource_management.core.exceptions import Fail
  22. from resource_management.core.logger import Logger
  23. from resource_management.core.utils import pad
  24. STACK_SELECTOR_NAME = "stack_selector"
  25. CONF_SELECTOR_NAME = "conf_selector"
  26. def get_stack_tool(name):
  27. """
  28. Give a tool selector name get the stack-specific tool name, tool path, tool package
  29. :param name: tool selector name
  30. :return: tool_name, tool_path, tool_package
  31. """
  32. from resource_management.libraries.functions.default import default
  33. stack_tools = None
  34. stack_tools_config = default("/configurations/cluster-env/stack_tools", None)
  35. stack_name = default("/hostLevelParams/stack_name", None)
  36. service_name = default("/serviceName", None)
  37. #Get version Advertised tag to decide whether or not to call the selector tools
  38. is_version_advertised = default("/versionAdvertised", True)
  39. if stack_tools_config:
  40. stack_tools = json.loads(stack_tools_config)
  41. if service_name is not None:
  42. if not is_version_advertised:
  43. Logger.warning(format("No \"stack selector tool\" returned as the component does not advertise a version"))
  44. return (None, None, None)
  45. if not stack_tools or not name or name.lower() not in stack_tools:
  46. Logger.warning("Cannot find config for {0} stack tool in {1}".format(str(name), str(stack_tools)))
  47. return (None, None, None)
  48. tool_config = stack_tools[name.lower()]
  49. # Return fixed length (tool_name, tool_path tool_package) tuple
  50. return tuple(pad(tool_config[:3], 3))
  51. def get_stack_tool_name(name):
  52. """
  53. Give a tool selector name get the stack-specific tool name
  54. :param name: tool selector name
  55. :return: tool_name
  56. """
  57. (tool_name, tool_path, tool_package) = get_stack_tool(name)
  58. return tool_name
  59. def get_stack_tool_path(name):
  60. """
  61. Give a tool selector name get the stack-specific tool path
  62. :param name: tool selector name
  63. :return: tool_path
  64. """
  65. (tool_name, tool_path, tool_package) = get_stack_tool(name)
  66. return tool_path
  67. def get_stack_tool_package(name):
  68. """
  69. Give a tool selector name get the stack-specific tool package
  70. :param name: tool selector name
  71. :return: tool_package
  72. """
  73. (tool_name, tool_path, tool_package) = get_stack_tool(name)
  74. return tool_package