utils.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # -*- coding: utf-8 -*-
  2. """App utilities: Compat settings, bug-report tool, pickling apps."""
  3. import os
  4. import platform as _platform
  5. import re
  6. from collections import Mapping, namedtuple
  7. from copy import deepcopy
  8. from types import ModuleType
  9. from kombu.utils.url import maybe_sanitize_url
  10. from celery.exceptions import ImproperlyConfigured
  11. from celery.platforms import pyimplementation
  12. from celery.utils.collections import ConfigurationView
  13. from celery.utils.text import pretty
  14. from celery.utils.imports import import_from_cwd, symbol_by_name, qualname
  15. from .defaults import (
  16. _TO_NEW_KEY, _TO_OLD_KEY, _OLD_DEFAULTS, _OLD_SETTING_KEYS,
  17. DEFAULTS, SETTING_KEYS, find,
  18. )
  19. __all__ = [
  20. 'Settings', 'appstr', 'bugreport',
  21. 'filter_hidden_settings', 'find_app',
  22. ]
  23. #: Format used to generate bug-report information.
  24. BUGREPORT_INFO = """
  25. software -> celery:{celery_v} kombu:{kombu_v} py:{py_v}
  26. billiard:{billiard_v} {driver_v}
  27. platform -> system:{system} arch:{arch} imp:{py_i}
  28. loader -> {loader}
  29. settings -> transport:{transport} results:{results}
  30. {human_settings}
  31. """
  32. HIDDEN_SETTINGS = re.compile(
  33. 'API|TOKEN|KEY|SECRET|PASS|PROFANITIES_LIST|SIGNATURE|DATABASE',
  34. re.IGNORECASE,
  35. )
  36. E_MIX_OLD_INTO_NEW = """
  37. Cannot mix new and old setting keys, please rename the
  38. following settings to the new format:
  39. {renames}
  40. """
  41. E_MIX_NEW_INTO_OLD = """
  42. Cannot mix new setting names with old setting names, please
  43. rename the following settings to use the old format:
  44. {renames}
  45. Or change all of the settings to use the new format :)
  46. """
  47. FMT_REPLACE_SETTING = '{replace:<36} -> {with_}'
  48. def appstr(app):
  49. """String used in __repr__ etc, to id app instances."""
  50. return '{0}:{1:#x}'.format(app.main or '__main__', id(app))
  51. class Settings(ConfigurationView):
  52. """Celery settings object.
  53. .. seealso:
  54. :ref:`configuration` for a full list of configuration keys.
  55. """
  56. @property
  57. def broker_read_url(self):
  58. return (
  59. os.environ.get('CELERY_BROKER_READ_URL') or
  60. self.get('broker_read_url') or
  61. self.broker_url
  62. )
  63. @property
  64. def broker_write_url(self):
  65. return (
  66. os.environ.get('CELERY_BROKER_WRITE_URL') or
  67. self.get('broker_write_url') or
  68. self.broker_url
  69. )
  70. @property
  71. def broker_url(self):
  72. return (
  73. os.environ.get('CELERY_BROKER_URL') or
  74. self.first('broker_url', 'broker_host')
  75. )
  76. @property
  77. def task_default_exchange(self):
  78. return self.first(
  79. 'task_default_exchange',
  80. 'task_default_queue',
  81. )
  82. @property
  83. def task_default_routing_key(self):
  84. return self.first(
  85. 'task_default_routing_key',
  86. 'task_default_queue',
  87. )
  88. @property
  89. def timezone(self):
  90. # this way we also support django's time zone.
  91. return self.first('timezone', 'time_zone')
  92. def without_defaults(self):
  93. """Return the current configuration, but without defaults."""
  94. # the last stash is the default settings, so just skip that
  95. return Settings({}, self.maps[:-1])
  96. def value_set_for(self, key):
  97. return key in self.without_defaults()
  98. def find_option(self, name, namespace=''):
  99. """Search for option by name.
  100. Example:
  101. >>> from proj.celery import app
  102. >>> app.conf.find_option('disable_rate_limits')
  103. ('worker', 'prefetch_multiplier',
  104. <Option: type->bool default->False>))
  105. Arguments:
  106. name (str): Name of option, cannot be partial.
  107. namespace (str): Preferred name-space (``None`` by default).
  108. Returns:
  109. Tuple: of ``(namespace, key, type)``.
  110. """
  111. return find(name, namespace)
  112. def find_value_for_key(self, name, namespace='celery'):
  113. """Shortcut to ``get_by_parts(*find_option(name)[:-1])``."""
  114. return self.get_by_parts(*self.find_option(name, namespace)[:-1])
  115. def get_by_parts(self, *parts):
  116. """Return the current value for setting specified as a path.
  117. Example:
  118. >>> from proj.celery import app
  119. >>> app.conf.get_by_parts('worker', 'disable_rate_limits')
  120. False
  121. """
  122. return self['_'.join(part for part in parts if part)]
  123. def table(self, with_defaults=False, censored=True):
  124. filt = filter_hidden_settings if censored else lambda v: v
  125. return filt({
  126. k: v for k, v in (
  127. self if with_defaults else self.without_defaults()).items()
  128. if not k.startswith('_')
  129. })
  130. def humanize(self, with_defaults=False, censored=True):
  131. """Return a human readable text showing configuration changes."""
  132. return '\n'.join(
  133. '{0}: {1}'.format(key, pretty(value, width=50))
  134. for key, value in self.table(with_defaults, censored).items())
  135. def _new_key_to_old(key, convert=_TO_OLD_KEY.get):
  136. return convert(key, key)
  137. def _old_key_to_new(key, convert=_TO_NEW_KEY.get):
  138. return convert(key, key)
  139. _settings_info_t = namedtuple('settings_info_t', (
  140. 'defaults', 'convert', 'key_t', 'mix_error',
  141. ))
  142. _settings_info = _settings_info_t(
  143. DEFAULTS, _TO_NEW_KEY, _old_key_to_new, E_MIX_OLD_INTO_NEW,
  144. )
  145. _old_settings_info = _settings_info_t(
  146. _OLD_DEFAULTS, _TO_OLD_KEY, _new_key_to_old, E_MIX_NEW_INTO_OLD,
  147. )
  148. def detect_settings(conf, preconf={}, ignore_keys=set(), prefix=None,
  149. all_keys=SETTING_KEYS, old_keys=_OLD_SETTING_KEYS):
  150. source = conf
  151. if conf is None:
  152. source, conf = preconf, {}
  153. have = set(source) - ignore_keys
  154. is_in_new = have.intersection(all_keys)
  155. is_in_old = have.intersection(old_keys)
  156. info = None
  157. if is_in_new:
  158. # have new setting names
  159. info, left = _settings_info, is_in_old
  160. if is_in_old and len(is_in_old) > len(is_in_new):
  161. # Majority of the settings are old.
  162. info, left = _old_settings_info, is_in_new
  163. if is_in_old:
  164. # have old setting names, or a majority of the names are old.
  165. if not info:
  166. info, left = _old_settings_info, is_in_new
  167. if is_in_new and len(is_in_new) > len(is_in_old):
  168. # Majority of the settings are new
  169. info, left = _settings_info, is_in_old
  170. else:
  171. # no settings, just use new format.
  172. info, left = _settings_info, is_in_old
  173. if prefix:
  174. # always use new format if prefix is used.
  175. info, left = _settings_info, set()
  176. # only raise error for keys that the user didn't provide two keys
  177. # for (e.g., both ``result_expires`` and ``CELERY_TASK_RESULT_EXPIRES``).
  178. really_left = {key for key in left if info.convert[key] not in have}
  179. if really_left:
  180. # user is mixing old/new, or new/old settings, give renaming
  181. # suggestions.
  182. raise ImproperlyConfigured(info.mix_error.format(renames='\n'.join(
  183. FMT_REPLACE_SETTING.format(replace=key, with_=info.convert[key])
  184. for key in sorted(really_left)
  185. )))
  186. preconf = {info.convert.get(k, k): v for k, v in preconf.items()}
  187. defaults = dict(deepcopy(info.defaults), **preconf)
  188. return Settings(
  189. preconf, [conf, defaults],
  190. (_old_key_to_new, _new_key_to_old),
  191. prefix=prefix,
  192. )
  193. class AppPickler:
  194. """Old application pickler/unpickler (< 3.1)."""
  195. def __call__(self, cls, *args):
  196. kwargs = self.build_kwargs(*args)
  197. app = self.construct(cls, **kwargs)
  198. self.prepare(app, **kwargs)
  199. return app
  200. def prepare(self, app, **kwargs):
  201. app.conf.update(kwargs['changes'])
  202. def build_kwargs(self, *args):
  203. return self.build_standard_kwargs(*args)
  204. def build_standard_kwargs(self, main, changes, loader, backend, amqp,
  205. events, log, control, accept_magic_kwargs,
  206. config_source=None):
  207. return dict(main=main, loader=loader, backend=backend, amqp=amqp,
  208. changes=changes, events=events, log=log, control=control,
  209. set_as_current=False,
  210. config_source=config_source)
  211. def construct(self, cls, **kwargs):
  212. return cls(**kwargs)
  213. def _unpickle_app(cls, pickler, *args):
  214. """Rebuild app for versions 2.5+."""
  215. return pickler()(cls, *args)
  216. def _unpickle_app_v2(cls, kwargs):
  217. """Rebuild app for versions 3.1+."""
  218. kwargs['set_as_current'] = False
  219. return cls(**kwargs)
  220. def filter_hidden_settings(conf):
  221. """Filter sensitive settings."""
  222. def maybe_censor(key, value, mask='*' * 8):
  223. if isinstance(value, Mapping):
  224. return filter_hidden_settings(value)
  225. if isinstance(key, str):
  226. if HIDDEN_SETTINGS.search(key):
  227. return mask
  228. elif 'broker_url' in key.lower():
  229. from kombu import Connection
  230. return Connection(value).as_uri(mask=mask)
  231. elif 'backend' in key.lower():
  232. return maybe_sanitize_url(value, mask=mask)
  233. return value
  234. return {k: maybe_censor(k, v) for k, v in conf.items()}
  235. def bugreport(app):
  236. """Return a string containing information useful in bug-reports."""
  237. import billiard
  238. import celery
  239. import kombu
  240. try:
  241. conn = app.connection()
  242. driver_v = '{0}:{1}'.format(conn.transport.driver_name,
  243. conn.transport.driver_version())
  244. transport = conn.transport_cls
  245. except Exception: # pylint: disable=broad-except
  246. transport = driver_v = ''
  247. return BUGREPORT_INFO.format(
  248. system=_platform.system(),
  249. arch=', '.join(x for x in _platform.architecture() if x),
  250. py_i=pyimplementation(),
  251. celery_v=celery.VERSION_BANNER,
  252. kombu_v=kombu.__version__,
  253. billiard_v=billiard.__version__,
  254. py_v=_platform.python_version(),
  255. driver_v=driver_v,
  256. transport=transport,
  257. results=maybe_sanitize_url(app.conf.result_backend or 'disabled'),
  258. human_settings=app.conf.humanize(),
  259. loader=qualname(app.loader.__class__),
  260. )
  261. def find_app(app, symbol_by_name=symbol_by_name, imp=import_from_cwd):
  262. """Find app by name."""
  263. from .base import Celery
  264. try:
  265. sym = symbol_by_name(app, imp=imp)
  266. except AttributeError:
  267. # last part was not an attribute, but a module
  268. sym = imp(app)
  269. if isinstance(sym, ModuleType) and ':' not in app:
  270. try:
  271. found = sym.app
  272. if isinstance(found, ModuleType):
  273. raise AttributeError()
  274. except AttributeError:
  275. try:
  276. found = sym.celery
  277. if isinstance(found, ModuleType):
  278. raise AttributeError()
  279. except AttributeError:
  280. if getattr(sym, '__path__', None):
  281. try:
  282. return find_app(
  283. '{0}.celery'.format(app),
  284. symbol_by_name=symbol_by_name, imp=imp,
  285. )
  286. except ImportError:
  287. pass
  288. for suspect in vars(sym).values():
  289. if isinstance(suspect, Celery):
  290. return suspect
  291. raise
  292. else:
  293. return found
  294. else:
  295. return found
  296. return sym