settings.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. # Django settings for Hue.
  18. #
  19. # Local customizations are done by symlinking a file
  20. # as local_settings.py.
  21. import logging
  22. import os
  23. import sys
  24. import desktop.conf
  25. import desktop.log
  26. from desktop.lib.paths import get_desktop_root
  27. import pkg_resources
  28. HUE_DESKTOP_VERSION = pkg_resources.get_distribution("desktop").version or "Unknown"
  29. NICE_NAME = "Hue"
  30. ENV_HUE_PROCESS_NAME = "HUE_PROCESS_NAME"
  31. ENV_DESKTOP_DEBUG = "DESKTOP_DEBUG"
  32. ############################################################
  33. # Part 1: Logging and imports.
  34. ############################################################
  35. # Configure debug mode
  36. DEBUG = True
  37. TEMPLATE_DEBUG = DEBUG
  38. # Start basic logging as soon as possible.
  39. if ENV_HUE_PROCESS_NAME not in os.environ:
  40. _proc = os.path.basename(len(sys.argv) > 1 and sys.argv[1] or sys.argv[0])
  41. os.environ[ENV_HUE_PROCESS_NAME] = _proc
  42. desktop.log.basic_logging(os.environ[ENV_HUE_PROCESS_NAME])
  43. logging.info("Welcome to Hue " + HUE_DESKTOP_VERSION)
  44. # Then we can safely import some more stuff
  45. from desktop import appmanager
  46. from desktop.lib import conf
  47. # Add fancy logging
  48. desktop.log.fancy_logging()
  49. ############################################################
  50. # Part 2: Generic Configuration
  51. ############################################################
  52. # Language code for this installation. All choices can be found here:
  53. # http://www.i18nguy.com/unicode/language-identifiers.html
  54. LANGUAGE_CODE = 'en-us'
  55. SITE_ID = 1
  56. # If you set this to False, Django will make some optimizations so as not
  57. # to load the internationalization machinery.
  58. USE_I18N = True
  59. # If you set this to False, Django will not format dates, numbers and
  60. # calendars according to the current locale.
  61. USE_L10N = True
  62. # If you set this to False, Django will not use timezone-aware datetimes.
  63. USE_TZ = False
  64. # URL that handles the media served from MEDIA_ROOT. Make sure to use a
  65. # trailing slash.
  66. # Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
  67. MEDIA_URL = ''
  68. ############################################################
  69. # Part 3: Django configuration
  70. ############################################################
  71. # Additional locations of static files
  72. STATICFILES_DIRS = ()
  73. # List of callables that know how to import templates from various sources.
  74. TEMPLATE_LOADERS = (
  75. 'django.template.loaders.filesystem.Loader',
  76. 'django.template.loaders.app_directories.Loader'
  77. )
  78. MIDDLEWARE_CLASSES = [
  79. # The order matters
  80. 'desktop.middleware.EnsureSafeMethodMiddleware',
  81. 'desktop.middleware.DatabaseLoggingMiddleware',
  82. 'django.middleware.common.CommonMiddleware',
  83. 'desktop.middleware.SessionOverPostMiddleware',
  84. 'django.contrib.sessions.middleware.SessionMiddleware',
  85. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  86. 'desktop.middleware.SpnegoMiddleware',
  87. 'desktop.middleware.HueRemoteUserMiddleware',
  88. 'django.middleware.locale.LocaleMiddleware',
  89. 'babeldjango.middleware.LocaleMiddleware',
  90. 'desktop.middleware.AjaxMiddleware',
  91. # Must be after Session, Auth, and Ajax. Before everything else.
  92. 'desktop.middleware.LoginAndPermissionMiddleware',
  93. 'django.contrib.messages.middleware.MessageMiddleware',
  94. 'desktop.middleware.NotificationMiddleware',
  95. 'desktop.middleware.JFrameMiddleware',
  96. 'desktop.middleware.ExceptionMiddleware',
  97. 'desktop.middleware.ClusterMiddleware',
  98. 'desktop.middleware.AppSpecificMiddleware',
  99. 'django.middleware.transaction.TransactionMiddleware'
  100. # 'debug_toolbar.middleware.DebugToolbarMiddleware'
  101. ]
  102. if os.environ.get(ENV_DESKTOP_DEBUG):
  103. MIDDLEWARE_CLASSES.append('desktop.middleware.HtmlValidationMiddleware')
  104. logging.debug("Will try to validate generated HTML.")
  105. ROOT_URLCONF = 'desktop.urls'
  106. # Hue runs its own wsgi applications
  107. WSGI_APPLICATION = None
  108. TEMPLATE_DIRS = (
  109. get_desktop_root("core/templates")
  110. )
  111. INSTALLED_APPS = [
  112. 'django.contrib.auth',
  113. 'django.contrib.contenttypes',
  114. 'django.contrib.sessions',
  115. 'django.contrib.sites',
  116. 'django.contrib.admin',
  117. 'django_extensions',
  118. # 'debug_toolbar',
  119. 'south', # database migration tool
  120. # i18n support
  121. 'babeldjango',
  122. # Desktop injects all the other installed apps into here magically.
  123. 'desktop'
  124. ]
  125. # Keep default values up to date
  126. TEMPLATE_CONTEXT_PROCESSORS = (
  127. 'django.contrib.auth.context_processors.auth',
  128. 'django.core.context_processors.debug',
  129. 'django.core.context_processors.i18n',
  130. 'django.core.context_processors.media',
  131. 'django.core.context_processors.request',
  132. 'django.contrib.messages.context_processors.messages',
  133. # Not default
  134. 'desktop.context_processors.app_name',
  135. )
  136. # Desktop doesn't use an auth profile module, because
  137. # because it doesn't mesh very well with the notion
  138. # of having multiple apps. If your app needs
  139. # to store data related to users, it should
  140. # manage its own table with an appropriate foreign key.
  141. AUTH_PROFILE_MODULE=None
  142. LOGIN_REDIRECT_URL = "/"
  143. LOGOUT_REDIRECT_URL = "/" # For djangosaml2 bug.
  144. PYLINTRC = get_desktop_root('.pylintrc')
  145. # Insert our HDFS upload handler
  146. FILE_UPLOAD_HANDLERS = (
  147. 'hadoop.fs.upload.HDFSfileUploadHandler',
  148. 'django.core.files.uploadhandler.MemoryFileUploadHandler',
  149. 'django.core.files.uploadhandler.TemporaryFileUploadHandler',
  150. )
  151. ############################################################
  152. # Part 4: Installation of apps
  153. ############################################################
  154. _config_dir = os.getenv("HUE_CONF_DIR", get_desktop_root("conf"))
  155. # Libraries are loaded and configured before the apps
  156. appmanager.load_libs()
  157. _lib_conf_modules = [dict(module=app.conf, config_key=None) for app in appmanager.DESKTOP_LIBS if app.conf is not None]
  158. appmanager.load_apps()
  159. for app in appmanager.DESKTOP_APPS:
  160. INSTALLED_APPS.extend(app.django_apps)
  161. logging.debug("Installed Django modules: %s" % ",".join(map(str, appmanager.DESKTOP_MODULES)))
  162. # Load app configuration
  163. _app_conf_modules = [dict(module=app.conf, config_key=app.config_key) for app in appmanager.DESKTOP_APPS if app.conf is not None]
  164. _app_conf_modules.append(dict(module=desktop.conf, config_key=None))
  165. conf.initialize(_lib_conf_modules, _config_dir)
  166. conf.initialize(_app_conf_modules, _config_dir)
  167. # Now that we've loaded the desktop conf, set the django DEBUG mode based on the conf.
  168. DEBUG = desktop.conf.DJANGO_DEBUG_MODE.get()
  169. TEMPLATE_DEBUG = DEBUG
  170. ############################################################
  171. # Part 4a: Django configuration that requires bound Desktop
  172. # configs.
  173. ############################################################
  174. # Configure hue admins
  175. ADMINS = []
  176. for admin in desktop.conf.DJANGO_ADMINS.get():
  177. admin_conf = desktop.conf.DJANGO_ADMINS[admin]
  178. if 'name' in admin_conf.bind_to and 'email' in admin_conf.bind_to:
  179. ADMINS.append(((admin_conf.NAME.get(), admin_conf.EMAIL.get())))
  180. ADMINS = tuple(ADMINS)
  181. MANAGERS = ADMINS
  182. # Server Email Address
  183. SERVER_EMAIL = desktop.conf.DJANGO_SERVER_EMAIL.get()
  184. # Email backend
  185. EMAIL_BACKEND = desktop.conf.DJANGO_EMAIL_BACKEND.get()
  186. # Configure database
  187. if os.getenv('DESKTOP_DB_CONFIG'):
  188. conn_string = os.getenv('DESKTOP_DB_CONFIG')
  189. logging.debug("DESKTOP_DB_CONFIG SET: %s" % (conn_string))
  190. default_db = dict(zip(
  191. ["ENGINE", "NAME", "TEST__NAME", "USER", "PASSWORD", "HOST", "PORT"],
  192. conn_string.split(':')))
  193. else:
  194. default_db = {
  195. "ENGINE" : desktop.conf.DATABASE.ENGINE.get(),
  196. "NAME" : desktop.conf.DATABASE.NAME.get(),
  197. "USER" : desktop.conf.DATABASE.USER.get(),
  198. "PASSWORD" : desktop.conf.DATABASE.PASSWORD.get(),
  199. "HOST" : desktop.conf.DATABASE.HOST.get(),
  200. "PORT" : str(desktop.conf.DATABASE.PORT.get()),
  201. # DB used for tests
  202. "TEST_NAME" : get_desktop_root('desktop-test.db')
  203. }
  204. DATABASES = {
  205. 'default': default_db
  206. }
  207. # Configure sessions
  208. SESSION_COOKIE_AGE = desktop.conf.SESSION.TTL.get()
  209. SESSION_COOKIE_SECURE = desktop.conf.SESSION.SECURE.get()
  210. # django-nose test specifics
  211. TEST_RUNNER = 'desktop.lib.test_runners.HueTestRunner'
  212. # Turn off cache middleware
  213. if 'test' in sys.argv:
  214. CACHE_MIDDLEWARE_SECONDS = 0
  215. TIME_ZONE = desktop.conf.TIME_ZONE.get()
  216. # Desktop supports only one authentication backend.
  217. AUTHENTICATION_BACKENDS = (desktop.conf.AUTH.BACKEND.get(),)
  218. EMAIL_HOST = desktop.conf.SMTP.HOST.get()
  219. EMAIL_PORT = desktop.conf.SMTP.PORT.get()
  220. EMAIL_HOST_USER = desktop.conf.SMTP.USER.get()
  221. EMAIL_HOST_PASSWORD = desktop.conf.SMTP.PASSWORD.get()
  222. EMAIL_USE_TLS = desktop.conf.SMTP.USE_TLS.get()
  223. DEFAULT_FROM_EMAIL = desktop.conf.SMTP.DEFAULT_FROM.get()
  224. # Used for securely creating sessions. Should be unique and not shared with anybody.
  225. SECRET_KEY = desktop.conf.SECRET_KEY.get()
  226. if SECRET_KEY == "":
  227. logging.warning("secret_key should be configured")
  228. # SAML
  229. SAML_AUTHENTICATION = 'libsaml.backend.SAML2Backend' in AUTHENTICATION_BACKENDS
  230. if SAML_AUTHENTICATION:
  231. from libsaml.saml_settings import *
  232. INSTALLED_APPS.append('libsaml')
  233. LOGIN_URL = '/saml2/login/'
  234. SESSION_EXPIRE_AT_BROWSER_CLOSE = True
  235. ############################################################
  236. # Necessary for South to not fuzz with tests. Fixed in South 0.7.1
  237. SKIP_SOUTH_TESTS = True
  238. # Set up environment variable so Kerberos libraries look at our private
  239. # ticket cache
  240. os.environ['KRB5CCNAME'] = desktop.conf.KERBEROS.CCACHE_PATH.get()
  241. #######
  242. if desktop.conf.AUTH.USER_GROUP_MEMBERSHIP_SYNCHRONIZATION_BACKEND.get():
  243. MIDDLEWARE_CLASSES.append('desktop.middleware.UserGroupSynchronizationMiddleware')