serverUtils.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. from ambari_commons.exceptions import FatalException, NonFatalException
  19. from ambari_commons.logging_utils import get_verbose
  20. from ambari_commons.os_family_impl import OsFamilyFuncImpl, OsFamilyImpl
  21. from ambari_commons.os_check import OSConst
  22. from ambari_commons.os_utils import run_os_command
  23. from ambari_server.resourceFilesKeeper import ResourceFilesKeeper, KeeperException
  24. from ambari_server.serverConfiguration import configDefaults, PID_NAME, get_ambari_properties, get_stack_location, \
  25. CLIENT_API_PORT, SSL_API, DEFAULT_SSL_API_PORT, SSL_API_PORT
  26. # Ambari server API properties
  27. SERVER_API_HOST = '127.0.0.1'
  28. SERVER_API_PROTOCOL = 'http'
  29. SERVER_API_SSL_PROTOCOL = 'https'
  30. @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
  31. def is_server_runing():
  32. pid_file_path = os.path.join(configDefaults.PID_DIR, PID_NAME)
  33. if os.path.exists(pid_file_path):
  34. try:
  35. f = open(pid_file_path, "r")
  36. except IOError, ex:
  37. raise FatalException(1, str(ex))
  38. pid = f.readline().strip()
  39. if not pid.isdigit():
  40. err = "%s is corrupt. Removing" % (pid_file_path)
  41. f.close()
  42. run_os_command("rm -f " + pid_file_path)
  43. raise NonFatalException(err)
  44. f.close()
  45. retcode, out, err = run_os_command("ps -p " + pid)
  46. if retcode == 0:
  47. return True, int(pid)
  48. else:
  49. return False, None
  50. else:
  51. return False, None
  52. @OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)
  53. def is_server_runing():
  54. from ambari_commons.os_windows import SERVICE_STATUS_STARTING, SERVICE_STATUS_RUNNING, SERVICE_STATUS_STOPPING, \
  55. SERVICE_STATUS_STOPPED, SERVICE_STATUS_NOT_INSTALLED
  56. from ambari_windows_service import AmbariServerService
  57. statusStr = AmbariServerService.QueryStatus()
  58. if statusStr in(SERVICE_STATUS_STARTING, SERVICE_STATUS_RUNNING, SERVICE_STATUS_STOPPING):
  59. return True, ""
  60. elif statusStr == SERVICE_STATUS_STOPPED:
  61. return False, SERVICE_STATUS_STOPPED
  62. elif statusStr == SERVICE_STATUS_NOT_INSTALLED:
  63. return False, SERVICE_STATUS_NOT_INSTALLED
  64. else:
  65. return False, None
  66. #
  67. # Performs HDP stack housekeeping
  68. #
  69. def refresh_stack_hash(properties):
  70. stack_location = get_stack_location(properties)
  71. # Hack: we determine resource dir as a parent dir for stack_location
  72. resources_location = os.path.dirname(stack_location)
  73. resource_files_keeper = ResourceFilesKeeper(resources_location)
  74. try:
  75. print "Organizing resource files at {0}...".format(resources_location,
  76. verbose=get_verbose())
  77. resource_files_keeper.perform_housekeeping()
  78. except KeeperException, ex:
  79. msg = "Can not organize resource files at {0}: {1}".format(
  80. resources_location, str(ex))
  81. raise FatalException(-1, msg)
  82. #
  83. # Builds ambari-server API base url
  84. # Reads server protocol/port from configuration
  85. # And returns something like
  86. # http://127.0.0.1:8080:/api/v1/
  87. #
  88. def get_ambari_server_api_base(properties):
  89. api_protocol = SERVER_API_PROTOCOL
  90. api_port = CLIENT_API_PORT
  91. api_ssl = False
  92. api_ssl_prop = properties.get_property(SSL_API)
  93. if api_ssl_prop is not None:
  94. api_ssl = api_ssl_prop.lower() == "true"
  95. if api_ssl:
  96. api_protocol = SERVER_API_SSL_PROTOCOL
  97. api_port = DEFAULT_SSL_API_PORT
  98. api_port_prop = properties.get_property(SSL_API_PORT)
  99. if api_port_prop is not None:
  100. api_port = api_port_prop
  101. return '{0}://{1}:{2!s}/api/v1/'.format(api_protocol, SERVER_API_HOST, api_port)