-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
command.py
181 lines (142 loc) · 5.72 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import sys
import atexit
import signal
import logging
from pprint import pformat
from logging import NullHandler
import click
from tornado.options import options
from tornado.options import parse_command_line, parse_config_file
from tornado.log import enable_pretty_logging
from celery.bin.base import CeleryCommand
from .app import Flower
from .urls import settings
from .utils import abs_path, prepend_url, strtobool
from .options import DEFAULT_CONFIG_FILE, default_options
from .views.auth import validate_auth_option
logger = logging.getLogger(__name__)
ENV_VAR_PREFIX = 'FLOWER_'
def sigterm_handler(signum, _):
logger.info('%s detected, shutting down', signum)
sys.exit(0)
@click.command(cls=CeleryCommand,
context_settings={
'ignore_unknown_options': True
})
@click.argument("tornado_argv", nargs=-1, type=click.UNPROCESSED)
@click.pass_context
def flower(ctx, tornado_argv):
"""Web based tool for monitoring and administrating Celery clusters."""
warn_about_celery_args_used_in_flower_command(ctx, tornado_argv)
apply_env_options()
apply_options(sys.argv[0], tornado_argv)
extract_settings()
setup_logging()
app = ctx.obj.app
flower_app = Flower(capp=app, options=options, **settings)
atexit.register(flower_app.stop)
signal.signal(signal.SIGTERM, sigterm_handler)
if not ctx.obj.quiet:
print_banner(app, 'ssl_options' in settings)
try:
flower_app.start()
except (KeyboardInterrupt, SystemExit):
pass
def apply_env_options():
"apply options passed through environment variables"
env_options = filter(is_flower_envvar, os.environ)
for env_var_name in env_options:
name = env_var_name.replace(ENV_VAR_PREFIX, '', 1).lower()
value = os.environ[env_var_name]
try:
option = options._options[name] # pylint: disable=protected-access
except KeyError:
option = options._options[name.replace('_', '-')] # pylint: disable=protected-access
if option.multiple:
value = [option.type(i) for i in value.split(',')]
else:
if option.type is bool:
value = bool(strtobool(value))
else:
value = option.type(value)
setattr(options, name, value)
def apply_options(prog_name, argv):
"apply options passed through the configuration file"
argv = list(filter(is_flower_option, argv))
# parse the command line to get --conf option
parse_command_line([prog_name] + argv)
try:
parse_config_file(os.path.abspath(options.conf), final=False)
parse_command_line([prog_name] + argv)
except IOError:
if os.path.basename(options.conf) != DEFAULT_CONFIG_FILE:
raise
def warn_about_celery_args_used_in_flower_command(ctx, flower_args):
celery_options = [option for param in ctx.parent.command.params for option in param.opts]
incorrectly_used_args = []
for arg in flower_args:
arg_name, _, _ = arg.partition("=")
if arg_name in celery_options:
incorrectly_used_args.append(arg_name)
if incorrectly_used_args:
logger.warning(
'You have incorrectly specified the following celery arguments after flower command:'
' %s. '
'Please specify them after celery command instead following this template: '
'celery [celery args] flower [flower args].', incorrectly_used_args
)
def setup_logging():
if options.debug and options.logging == 'info':
options.logging = 'debug'
enable_pretty_logging()
else:
logging.getLogger("tornado.access").addHandler(NullHandler())
logging.getLogger("tornado.access").propagate = False
def extract_settings():
settings['debug'] = options.debug
if options.cookie_secret:
settings['cookie_secret'] = options.cookie_secret
if options.url_prefix:
for name in ['login_url', 'static_url_prefix']:
settings[name] = prepend_url(settings[name], options.url_prefix)
if options.auth:
settings['oauth'] = {
'key': options.oauth2_key or os.environ.get('FLOWER_OAUTH2_KEY'),
'secret': options.oauth2_secret or os.environ.get('FLOWER_OAUTH2_SECRET'),
'redirect_uri': options.oauth2_redirect_uri or os.environ.get('FLOWER_OAUTH2_REDIRECT_URI'),
}
if options.certfile and options.keyfile:
settings['ssl_options'] = dict(certfile=abs_path(options.certfile),
keyfile=abs_path(options.keyfile))
if options.ca_certs:
settings['ssl_options']['ca_certs'] = abs_path(options.ca_certs)
if options.auth and not validate_auth_option(options.auth):
logger.error("Invalid '--auth' option: %s", options.auth)
sys.exit(1)
def is_flower_option(arg):
name, _, _ = arg.lstrip('-').partition("=")
name = name.replace('-', '_')
return hasattr(options, name)
def is_flower_envvar(name):
return name.startswith(ENV_VAR_PREFIX) and \
name[len(ENV_VAR_PREFIX):].lower() in default_options
def print_banner(app, ssl):
if not options.unix_socket:
if options.url_prefix:
prefix_str = f'/{options.url_prefix}/'
else:
prefix_str = ''
logger.info(
"Visit me at http%s://%s:%s%s", 's' if ssl else '',
options.address or '0.0.0.0', options.port,
prefix_str
)
else:
logger.info("Visit me via unix socket file: %s", options.unix_socket)
logger.info('Broker: %s', app.connection().as_uri())
logger.info(
'Registered tasks: \n%s',
pformat(sorted(app.tasks.keys()))
)
logger.debug('Settings: %s', pformat(settings))