serverUtils.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @OsFamilyFuncImpl(OsFamilyImpl.DEFAULT)
  26. def is_server_runing():
  27. pid_file_path = os.path.join(configDefaults.PID_DIR, PID_NAME)
  28. if os.path.exists(pid_file_path):
  29. try:
  30. f = open(pid_file_path, "r")
  31. except IOError, ex:
  32. raise FatalException(1, str(ex))
  33. pid = f.readline().strip()
  34. if not pid.isdigit():
  35. err = "%s is corrupt. Removing" % (pid_file_path)
  36. f.close()
  37. run_os_command("rm -f " + pid_file_path)
  38. raise NonFatalException(err)
  39. f.close()
  40. retcode, out, err = run_os_command("ps -p " + pid)
  41. if retcode == 0:
  42. return True, int(pid)
  43. else:
  44. return False, None
  45. else:
  46. return False, None
  47. @OsFamilyFuncImpl(OSConst.WINSRV_FAMILY)
  48. def is_server_runing():
  49. from ambari_commons.os_windows import SERVICE_STATUS_STARTING, SERVICE_STATUS_RUNNING, SERVICE_STATUS_STOPPING, \
  50. SERVICE_STATUS_STOPPED, SERVICE_STATUS_NOT_INSTALLED
  51. from ambari_windows_service import AmbariServerService
  52. statusStr = AmbariServerService.QueryStatus()
  53. if statusStr in(SERVICE_STATUS_STARTING, SERVICE_STATUS_RUNNING, SERVICE_STATUS_STOPPING):
  54. return True, ""
  55. elif statusStr == SERVICE_STATUS_STOPPED:
  56. return False, SERVICE_STATUS_STOPPED
  57. elif statusStr == SERVICE_STATUS_NOT_INSTALLED:
  58. return False, SERVICE_STATUS_NOT_INSTALLED
  59. else:
  60. return False, None
  61. #
  62. # Performs HDP stack housekeeping
  63. #
  64. def refresh_stack_hash(properties):
  65. stack_location = get_stack_location(properties)
  66. # Hack: we determine resource dir as a parent dir for stack_location
  67. resources_location = os.path.dirname(stack_location)
  68. resource_files_keeper = ResourceFilesKeeper(resources_location)
  69. try:
  70. print "Organizing resource files at {0}...".format(resources_location,
  71. verbose=get_verbose())
  72. resource_files_keeper.perform_housekeeping()
  73. except KeeperException, ex:
  74. msg = "Can not organize resource files at {0}: {1}".format(
  75. resources_location, str(ex))
  76. raise FatalException(-1, msg)