hdp_select.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. import os
  18. import sys
  19. import re
  20. from resource_management.core.logger import Logger
  21. from resource_management.core.exceptions import Fail
  22. from resource_management.core.resources.system import Execute
  23. from resource_management.libraries.functions.default import default
  24. from resource_management.libraries.functions.get_hdp_version import get_hdp_version
  25. from resource_management.libraries.script.script import Script
  26. from resource_management.core.shell import call
  27. from resource_management.libraries.functions.version import format_hdp_stack_version
  28. from resource_management.libraries.functions.version_select_util import get_versions_from_stack_root
  29. HDP_SELECT = '/usr/bin/hdp-select'
  30. HDP_SELECT_PREFIX = ('ambari-python-wrap', HDP_SELECT)
  31. # hdp-select set oozie-server 2.2.0.0-1234
  32. TEMPLATE = HDP_SELECT_PREFIX + ('set',)
  33. # a mapping of Ambari server role to hdp-select component name for all
  34. # non-clients
  35. SERVER_ROLE_DIRECTORY_MAP = {
  36. 'ACCUMULO_MASTER' : 'accumulo-master',
  37. 'ACCUMULO_MONITOR' : 'accumulo-monitor',
  38. 'ACCUMULO_GC' : 'accumulo-gc',
  39. 'ACCUMULO_TRACER' : 'accumulo-tracer',
  40. 'ACCUMULO_TSERVER' : 'accumulo-tablet',
  41. 'ATLAS_SERVER' : 'atlas-server',
  42. 'FLUME_HANDLER' : 'flume-server',
  43. 'FALCON_SERVER' : 'falcon-server',
  44. 'NAMENODE' : 'hadoop-hdfs-namenode',
  45. 'DATANODE' : 'hadoop-hdfs-datanode',
  46. 'SECONDARY_NAMENODE' : 'hadoop-hdfs-secondarynamenode',
  47. 'NFS_GATEWAY' : 'hadoop-hdfs-nfs3',
  48. 'JOURNALNODE' : 'hadoop-hdfs-journalnode',
  49. 'HBASE_MASTER' : 'hbase-master',
  50. 'HBASE_REGIONSERVER' : 'hbase-regionserver',
  51. 'HIVE_METASTORE' : 'hive-metastore',
  52. 'HIVE_SERVER' : 'hive-server2',
  53. 'WEBHCAT_SERVER' : 'hive-webhcat',
  54. 'KAFKA_BROKER' : 'kafka-broker',
  55. 'KNOX_GATEWAY' : 'knox-server',
  56. 'OOZIE_SERVER' : 'oozie-server',
  57. 'RANGER_ADMIN' : 'ranger-admin',
  58. 'RANGER_USERSYNC' : 'ranger-usersync',
  59. 'SPARK_JOBHISTORYSERVER' : 'spark-historyserver',
  60. 'SPARK_THRIFTSERVER' : 'spark-thriftserver',
  61. 'NIMBUS' : 'storm-nimbus',
  62. 'SUPERVISOR' : 'storm-supervisor',
  63. 'HISTORYSERVER' : 'hadoop-mapreduce-historyserver',
  64. 'APP_TIMELINE_SERVER' : 'hadoop-yarn-timelineserver',
  65. 'NODEMANAGER' : 'hadoop-yarn-nodemanager',
  66. 'RESOURCEMANAGER' : 'hadoop-yarn-resourcemanager',
  67. 'ZOOKEEPER_SERVER' : 'zookeeper-server',
  68. # ZKFC is tied to NN since it doesn't have its own componnet in hdp-select and there is
  69. # a requirement that the ZKFC is installed on each NN
  70. 'ZKFC' : 'hadoop-hdfs-namenode'
  71. }
  72. # mapping of service check to hdp-select component
  73. SERVICE_CHECK_DIRECTORY_MAP = {
  74. "HDFS_SERVICE_CHECK" : "hadoop-client",
  75. "TEZ_SERVICE_CHECK" : "hadoop-client",
  76. "PIG_SERVICE_CHECK" : "hadoop-client",
  77. "HIVE_SERVICE_CHECK" : "hadoop-client",
  78. "OOZIE_SERVICE_CHECK" : "hadoop-client",
  79. "MAHOUT_SERVICE_CHECK" : "mahout-client"
  80. }
  81. # /usr/hdp/current/hadoop-client/[bin|sbin|libexec|lib]
  82. # /usr/hdp/2.3.0.0-1234/hadoop/[bin|sbin|libexec|lib]
  83. HADOOP_DIR_TEMPLATE = "/usr/hdp/{0}/{1}/{2}"
  84. # /usr/hdp/current/hadoop-client
  85. # /usr/hdp/2.3.0.0-1234/hadoop
  86. HADOOP_HOME_DIR_TEMPLATE = "/usr/hdp/{0}/{1}"
  87. HADOOP_DIR_DEFAULTS = {
  88. "home": "/usr/lib/hadoop",
  89. "libexec": "/usr/lib/hadoop/libexec",
  90. "sbin": "/usr/lib/hadoop/sbin",
  91. "bin": "/usr/bin",
  92. "lib": "/usr/lib/hadoop/lib"
  93. }
  94. def select(component, version):
  95. """
  96. Executes hdp-select on the specific component and version. Some global
  97. variables that are imported via params/status_params/params_linux will need
  98. to be recalcuated after the hdp-select. However, python does not re-import
  99. existing modules. The only way to ensure that the configuration variables are
  100. recalculated is to call reload(...) on each module that has global parameters.
  101. After invoking hdp-select, this function will also reload params, status_params,
  102. and params_linux.
  103. :param component: the hdp-select component, such as oozie-server
  104. :param version: the version to set the component to, such as 2.2.0.0-1234
  105. """
  106. command = TEMPLATE + (component, version)
  107. Execute(command, sudo=True)
  108. # don't trust the ordering of modules:
  109. # 1) status_params
  110. # 2) params_linux
  111. # 3) params
  112. modules = sys.modules
  113. param_modules = "status_params", "params_linux", "params"
  114. for moduleName in param_modules:
  115. if moduleName in modules:
  116. module = modules.get(moduleName)
  117. reload(module)
  118. Logger.info("After hdp-select {0}, reloaded module {1}".format(component, moduleName))
  119. def get_role_component_current_hdp_version():
  120. """
  121. Gets the current HDP version of the component that this role command is for.
  122. :return: the current HDP version of the specified component or None
  123. """
  124. hdp_select_component = None
  125. role = default("/role", "")
  126. role_command = default("/roleCommand", "")
  127. if role in SERVER_ROLE_DIRECTORY_MAP:
  128. hdp_select_component = SERVER_ROLE_DIRECTORY_MAP[role]
  129. elif role_command == "SERVICE_CHECK" and role in SERVICE_CHECK_DIRECTORY_MAP:
  130. hdp_select_component = SERVICE_CHECK_DIRECTORY_MAP[role]
  131. if hdp_select_component is None:
  132. return None
  133. current_hdp_version = get_hdp_version(hdp_select_component)
  134. if current_hdp_version is None:
  135. Logger.warning("Unable to determine hdp-select version for {0}".format(
  136. hdp_select_component))
  137. else:
  138. Logger.info("{0} is currently at version {1}".format(
  139. hdp_select_component, current_hdp_version))
  140. return current_hdp_version
  141. def get_hadoop_dir(target, force_latest_on_upgrade=False):
  142. """
  143. Return the hadoop shared directory in the following override order
  144. 1. Use default for 2.1 and lower
  145. 2. If 2.2 and higher, use /usr/hdp/current/hadoop-client/{target}
  146. 3. If 2.2 and higher AND for an upgrade, use /usr/hdp/<version>/hadoop/{target}.
  147. However, if the upgrade has not yet invoked hdp-select, return the current
  148. version of the component.
  149. :target: the target directory
  150. :force_latest_on_upgrade: if True, then this will return the "current" directory
  151. without the HDP version built into the path, such as /usr/hdp/current/hadoop-client
  152. """
  153. if not target in HADOOP_DIR_DEFAULTS:
  154. raise Fail("Target {0} not defined".format(target))
  155. hadoop_dir = HADOOP_DIR_DEFAULTS[target]
  156. if Script.is_hdp_stack_greater_or_equal("2.2"):
  157. # home uses a different template
  158. if target == "home":
  159. hadoop_dir = HADOOP_HOME_DIR_TEMPLATE.format("current", "hadoop-client")
  160. else:
  161. hadoop_dir = HADOOP_DIR_TEMPLATE.format("current", "hadoop-client", target)
  162. # if we are not forcing "current" for HDP 2.2, then attempt to determine
  163. # if the exact version needs to be returned in the directory
  164. if not force_latest_on_upgrade:
  165. stack_info = _get_upgrade_stack()
  166. if stack_info is not None:
  167. stack_version = stack_info[1]
  168. # determine if hdp-select has been run and if not, then use the current
  169. # hdp version until this component is upgraded
  170. current_hdp_version = get_role_component_current_hdp_version()
  171. if current_hdp_version is not None and stack_version != current_hdp_version:
  172. stack_version = current_hdp_version
  173. if target == "home":
  174. # home uses a different template
  175. hadoop_dir = HADOOP_HOME_DIR_TEMPLATE.format(stack_version, "hadoop")
  176. else:
  177. hadoop_dir = HADOOP_DIR_TEMPLATE.format(stack_version, "hadoop", target)
  178. return hadoop_dir
  179. def get_hadoop_dir_for_stack_version(target, stack_version):
  180. """
  181. Return the hadoop shared directory for the provided stack version. This is necessary
  182. when folder paths of downgrade-source stack-version are needed after hdp-select.
  183. :target: the target directory
  184. :stack_version: stack version to get hadoop dir for
  185. """
  186. if not target in HADOOP_DIR_DEFAULTS:
  187. raise Fail("Target {0} not defined".format(target))
  188. hadoop_dir = HADOOP_DIR_DEFAULTS[target]
  189. formatted_stack_version = format_hdp_stack_version(stack_version)
  190. if Script.is_hdp_stack_greater_or_equal_to(formatted_stack_version, "2.2"):
  191. # home uses a different template
  192. if target == "home":
  193. hadoop_dir = HADOOP_HOME_DIR_TEMPLATE.format(stack_version, "hadoop")
  194. else:
  195. hadoop_dir = HADOOP_DIR_TEMPLATE.format(stack_version, "hadoop", target)
  196. return hadoop_dir
  197. def _get_upgrade_stack():
  198. """
  199. Gets the stack name and stack version if an upgrade is currently in progress.
  200. :return: the stack name and stack version as a tuple, or None if an
  201. upgrade is not in progress.
  202. """
  203. from resource_management.libraries.functions.default import default
  204. direction = default("/commandParams/upgrade_direction", None)
  205. stack_name = default("/hostLevelParams/stack_name", None)
  206. stack_version = default("/commandParams/version", None)
  207. if direction and stack_name and stack_version:
  208. return (stack_name, stack_version)
  209. return None
  210. def get_hdp_versions(stack_root):
  211. """
  212. Gets list of stack versions installed on the host.
  213. Be default a call to hdp-select versions is made to get the list of installed stack versions.
  214. As a fallback list of installed versions is collected from stack version directories in stack install root.
  215. :param stack_root: Stack install root
  216. :return: Returns list of installed stack versions.
  217. """
  218. code, out = call(HDP_SELECT_PREFIX + ('versions',))
  219. versions = []
  220. if 0 == code:
  221. for line in out.splitlines():
  222. versions.append(line.rstrip('\n'))
  223. if not versions:
  224. versions = get_versions_from_stack_root(stack_root)
  225. return versions
  226. def get_hdp_version_before_install(component_name):
  227. """
  228. Works in the similar way to 'hdp-select status component',
  229. but also works for not yet installed packages.
  230. Note: won't work if doing initial install.
  231. """
  232. component_dir = HADOOP_HOME_DIR_TEMPLATE.format("current", component_name)
  233. if os.path.islink(component_dir):
  234. hdp_version = os.path.basename(os.path.dirname(os.readlink(component_dir)))
  235. match = re.match('[0-9]+.[0-9]+.[0-9]+.[0-9]+-[0-9]+', hdp_version)
  236. if match is None:
  237. Logger.info('Failed to get extracted version with hdp-select in method get_hdp_version_before_install')
  238. return None # lazy fail
  239. return hdp_version
  240. else:
  241. return None