hostUpdate.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. from ambari_commons.exceptions import FatalException
  20. from ambari_server.serverConfiguration import configDefaults, get_java_exe_path, get_ambari_properties, read_ambari_user, \
  21. parse_properties_file, JDBC_DATABASE_PROPERTY
  22. from ambari_commons.logging_utils import print_info_msg, print_warning_msg, print_error_msg
  23. from ambari_server.dbConfiguration import ensure_jdbc_driver_is_installed, LINUX_DBMS_KEYS_LIST
  24. from ambari_server.serverClassPath import ServerClassPath
  25. from ambari_server.setupSecurity import ensure_can_start_under_current_user, generate_env
  26. from ambari_commons.os_utils import run_os_command
  27. from ambari_server.serverUtils import is_server_runing
  28. from ambari_server.userInput import get_YN_input
  29. HOST_UPDATE_HELPER_CMD = "{0} -cp {1} " + \
  30. "org.apache.ambari.server.update.HostUpdateHelper {2}" + \
  31. " > " + configDefaults.SERVER_OUT_FILE + " 2>&1"
  32. def update_host_names(args, options):
  33. services_stopped = get_YN_input("Please, confirm Ambari services are stopped [y/n] (n)? ", False)
  34. if not services_stopped:
  35. print 'Exiting...'
  36. sys.exit(1)
  37. pending_commands = get_YN_input("Please, confirm there are no pending commands on cluster [y/n] (n)? ", False)
  38. if not pending_commands:
  39. print 'Exiting...'
  40. sys.exit(1)
  41. db_backup_done = get_YN_input("Please, confirm you have made backup of the Ambari db [y/n] (n)? ", False)
  42. if not db_backup_done:
  43. print 'Exiting...'
  44. sys.exit(1)
  45. status, pid = is_server_runing()
  46. if status:
  47. raise FatalException(1, "Ambari Server should be stopped")
  48. try:
  49. host_mapping_file_path = args[1]
  50. except IndexError:
  51. #host_mapping file is mandatory
  52. raise FatalException(1, "Invalid number of host update arguments. Probably, you forgot to add json file with "
  53. "host changes.")
  54. if not os.path.isfile(host_mapping_file_path):
  55. raise FatalException(1, "Invalid file path or file doesn't exist")
  56. if not os.access(host_mapping_file_path, os.R_OK):
  57. raise FatalException(1, "File is not readable")
  58. jdk_path = get_java_exe_path()
  59. if jdk_path is None:
  60. print_error_msg("No JDK found, please run the \"setup\" "
  61. "command to install a JDK automatically or install any "
  62. "JDK manually to " + configDefaults.JDK_INSTALL_DIR)
  63. sys.exit(1)
  64. properties = get_ambari_properties()
  65. parse_properties_file(options)
  66. options.database_index = LINUX_DBMS_KEYS_LIST.index(properties[JDBC_DATABASE_PROPERTY])
  67. ensure_jdbc_driver_is_installed(options, get_ambari_properties())
  68. serverClassPath = ServerClassPath(get_ambari_properties(), options)
  69. class_path = serverClassPath.get_full_ambari_classpath_escaped_for_shell()
  70. command = HOST_UPDATE_HELPER_CMD.format(jdk_path, class_path, host_mapping_file_path)
  71. ambari_user = read_ambari_user()
  72. current_user = ensure_can_start_under_current_user(ambari_user)
  73. environ = generate_env(options, ambari_user, current_user)
  74. (retcode, stdout, stderr) = run_os_command(command, env=environ)
  75. print_info_msg("Return code from update host names command, retcode = " + str(retcode))
  76. if retcode > 0:
  77. print_error_msg("Error executing update host names, please check the server logs.")
  78. raise FatalException(1, 'Host names update failed.')
  79. else:
  80. print_info_msg('Host names update completed successfully')