script_runner 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. import os, sys
  18. from os import walk
  19. import os.path
  20. import subprocess
  21. import logging
  22. script_dir = os.path.dirname(os.path.realpath(__file__))
  23. lib_dir = '%s/lib' % script_dir
  24. #sys.argv.append("--cm-managed")
  25. if 'NO_CM' in os.environ.keys():
  26. if 'HUE_CONF_DIR' in os.environ.keys():
  27. if not 'HUE_CONF_DIR_ORIG' in os.environ.keys():
  28. os.environ['HUE_CONF_DIR_ORIG'] = os.environ.get('HUE_CONF_DIR')
  29. if not os.path.isdir(lib_dir):
  30. print("The lib directory is missing. Please download the entire package and not just the script_runner file")
  31. print("git clone https://github.com/cmconner156/hue_scripts.git /opt/hue_scripts")
  32. print("or")
  33. print("cd /opt && rm -Rf hue_scripts && wget https://github.com/cmconner156/hue_scripts/archive/master.zip && unzip master.zip && mv hue_scripts-master hue_scripts && rm -f master.zip")
  34. exit(1)
  35. if not os.environ["USER"] == "root":
  36. print("This script must be run as root")
  37. sys.exit(1)
  38. sys.path.insert(0, lib_dir)
  39. try:
  40. from cm_environment import set_cm_environment, reload_with_cm_env
  41. except ImportError as e:
  42. print("Unable to load cm_environment please send following to support:")
  43. print("sys.path: %s" % sys.path)
  44. print("All files in script_dir: %s" % script_dir)
  45. files = []
  46. for (dirpath, dirnames, filenames) in walk(script_dir):
  47. for file in filenames:
  48. if file == "script_runner" or "lib" in dirpath:
  49. print("%s/%s" % (dirpath, file))
  50. files.append(file)
  51. exit(1)
  52. hue_config = set_cm_environment()
  53. hue_path = hue_config['hue_path']
  54. hue_bin_dir = hue_config['hue_bin_dir']
  55. HUE_CONF_DIR = hue_config['HUE_CONF_DIR']
  56. parcel_dir = hue_config['parcel_dir']
  57. parcel_name = hue_config['parcel_name']
  58. if not "SKIP_RELOAD" in os.environ.keys():
  59. reload_with_cm_env()
  60. if 'NO_CM' in os.environ.keys():
  61. if 'HUE_CONF_DIR_ORIG' in os.environ.keys():
  62. HUE_CONF_DIR = os.environ.get('HUE_CONF_DIR_ORIG')
  63. os.environ['HUE_CONF_DIR'] = HUE_CONF_DIR
  64. activate_file = hue_bin_dir + "/activate_this.py"
  65. hue_bin = hue_bin_dir + "/hue"
  66. import os; activate_this=os.path.join(os.path.dirname(os.path.realpath(activate_file)), 'activate_this.py'); exec(compile(open(activate_this).read(), activate_this, 'exec'), dict(__file__=activate_this)); del activate_this
  67. with open(hue_bin, 'rU') as f:
  68. for line in f:
  69. if "__requires__" in line:
  70. desktop_ver = line.split("'")
  71. __requires__ = desktop_ver[1]
  72. import sys
  73. from pkg_resources import load_entry_point
  74. from django.conf import settings
  75. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'desktop.settings')
  76. from custom_commands.settings import *
  77. settings.INSTALLED_APPS.append('custom_commands')
  78. from desktop.conf import DATABASE as desktop_database
  79. logging.info("Using the following config make sure it looks correct")
  80. try:
  81. import platform
  82. logging.info("Python Version: %s" % platform.python_version())
  83. logging.info("OS Version: %s %s" % (platform.linux_distribution()[0], platform.linux_distribution()[1]))
  84. except:
  85. pass
  86. logging.info("HUE_CONF_DIR: %s" % HUE_CONF_DIR)
  87. logging.info("parcel_dir: %s" % parcel_dir)
  88. logging.info("parcel_name: %s" % parcel_name)
  89. logging.info("hue_bin_dir: %s" % hue_bin_dir)
  90. logging.info("DB Engine: %s" % desktop_database.ENGINE.get())
  91. logging.info("DB Name: %s" % desktop_database.NAME.get())
  92. logging.info("DB User: %s" % desktop_database.USER.get())
  93. logging.info("DB Host: %s" % desktop_database.HOST.get())
  94. logging.info("DB Port: %s" % str(desktop_database.PORT.get()))
  95. if hue_config['LD_LIBRARY_PATH'] is not None:
  96. logging.info("LD_LIBRARY_PATH: %s" % hue_config["LD_LIBRARY_PATH"])
  97. if __name__ == '__main__':
  98. # sys.argv.remove("--cm-managed")
  99. try:
  100. sys.exit(
  101. load_entry_point('desktop', 'console_scripts', 'hue')()
  102. )
  103. except UnboundLocalError, e:
  104. logging.info("Command failed because of bug in older versions of CDH: %s" % e)
  105. logging.info("Patching and re-running")
  106. patch_file = "%s/patches/fix_cm_config_file.patch" % script_dir
  107. patch_cmd = ["patch", "-p1"]
  108. patch_file_in = open(patch_file)
  109. os.chdir(hue_path)
  110. subprocess.call(patch_cmd, stdin=patch_file_in)
  111. os.chdir(script_dir)
  112. sys.exit(
  113. load_entry_point('desktop', 'console_scripts', 'hue')()
  114. )