forked from Hughendman/superset_zh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
368 lines (315 loc) · 11.8 KB
/
cli.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=C,R,W
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from datetime import datetime
import logging
from subprocess import Popen
from sys import stdout
from colorama import Fore, Style
from flask_migrate import MigrateCommand
from flask_script import Manager
from pathlib2 import Path
import werkzeug.serving
import yaml
from superset import app, data, db, dict_import_export_util, security_manager, utils
config = app.config
celery_app = utils.get_celery_app(config)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
@manager.command
def init():
"""Inits the Superset application"""
utils.get_or_create_main_db()
security_manager.sync_role_definitions()
def debug_run(app, port, use_reloader):
return app.run(
host='0.0.0.0',
port=int(port),
threaded=True,
debug=True,
use_reloader=use_reloader)
def console_log_run(app, port, use_reloader):
from console_log import ConsoleLog
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app.wsgi_app = ConsoleLog(app.wsgi_app, app.logger)
def run():
server = pywsgi.WSGIServer(
('0.0.0.0', int(port)),
app,
handler_class=WebSocketHandler)
server.serve_forever()
if use_reloader:
from gevent import monkey
monkey.patch_all()
run = werkzeug.serving.run_with_reloader(run)
run()
@manager.option(
'-d', '--debug', action='store_true',
help='Start the web server in debug mode')
@manager.option(
'--console-log', action='store_true',
help='Create logger that logs to the browser console (implies -d)')
@manager.option(
'-n', '--no-reload', action='store_false', dest='use_reloader',
default=config.get('FLASK_USE_RELOAD'),
help="Don't use the reloader in debug mode")
@manager.option(
'-a', '--address', default=config.get('SUPERSET_WEBSERVER_ADDRESS'),
help='Specify the address to which to bind the web server')
@manager.option(
'-p', '--port', default=config.get('SUPERSET_WEBSERVER_PORT'),
help='Specify the port on which to run the web server')
@manager.option(
'-w', '--workers',
default=config.get('SUPERSET_WORKERS', 2),
help='Number of gunicorn web server workers to fire up [DEPRECATED]')
@manager.option(
'-t', '--timeout', default=config.get('SUPERSET_WEBSERVER_TIMEOUT'),
help='Specify the timeout (seconds) for the gunicorn web server [DEPRECATED]')
@manager.option(
'-s', '--socket', default=config.get('SUPERSET_WEBSERVER_SOCKET'),
help='Path to a UNIX socket as an alternative to address:port, e.g. '
'/var/run/superset.sock. '
'Will override the address and port values. [DEPRECATED]')
def runserver(debug, console_log, use_reloader, address, port, timeout, workers, socket):
"""Starts a Superset web server."""
debug = debug or config.get('DEBUG') or console_log
if debug:
print(Fore.BLUE + '-=' * 20)
print(
Fore.YELLOW + 'Starting Superset server in ' +
Fore.RED + 'DEBUG' +
Fore.YELLOW + ' mode')
print(Fore.BLUE + '-=' * 20)
print(Style.RESET_ALL)
if console_log:
console_log_run(app, port, use_reloader)
else:
debug_run(app, port, use_reloader)
else:
logging.info(
"The Gunicorn 'superset runserver' command is deprecated. Please "
"use the 'gunicorn' command instead.")
addr_str = ' unix:{socket} ' if socket else' {address}:{port} '
cmd = (
'gunicorn '
'-w {workers} '
'--timeout {timeout} '
'-b ' + addr_str +
'--limit-request-line 0 '
'--limit-request-field_size 0 '
'superset:app').format(**locals())
print(Fore.GREEN + 'Starting server with command: ')
print(Fore.YELLOW + cmd)
print(Style.RESET_ALL)
Popen(cmd, shell=True).wait()
@manager.option(
'-v', '--verbose', action='store_true',
help='Show extra information')
def version(verbose):
"""Prints the current version number"""
print(Fore.BLUE + '-=' * 15)
print(Fore.YELLOW + 'Superset ' + Fore.CYAN + '{version}'.format(
version=config.get('VERSION_STRING')))
print(Fore.BLUE + '-=' * 15)
if verbose:
print('[DB] : ' + '{}'.format(db.engine))
print(Style.RESET_ALL)
@manager.option(
'-t', '--load-test-data', action='store_true',
help='Load additional test data')
def load_examples(load_test_data):
"""Loads a set of Slices and Dashboards and a supporting dataset """
print('Loading examples into {}'.format(db))
data.load_css_templates()
print('Loading energy related dataset')
data.load_energy()
print("Loading [World Bank's Health Nutrition and Population Stats]")
data.load_world_bank_health_n_pop()
print('Loading [Birth names]')
data.load_birth_names()
print('Loading [Random time series data]')
data.load_random_time_series_data()
print('Loading [Random long/lat data]')
data.load_long_lat_data()
print('Loading [Country Map data]')
data.load_country_map_data()
print('Loading [Multiformat time series]')
data.load_multiformat_time_series_data()
print('Loading [Misc Charts] dashboard')
data.load_misc_dashboard()
print('Loading [Paris GeoJson]')
data.load_paris_iris_geojson()
print('Loading [San Francisco population polygons]')
data.load_sf_population_polygons()
print('Loading [Flights data]')
data.load_flights()
print('Loading [BART lines]')
data.load_bart_lines()
print('Loading [Multi Line]')
data.load_multi_line()
if load_test_data:
print('Loading [Unicode test data]')
data.load_unicode_test_data()
print('Loading DECK.gl demo')
data.load_deck_dash()
@manager.option(
'-d', '--datasource',
help=(
'Specify which datasource name to load, if omitted, all '
'datasources will be refreshed'
),
)
@manager.option(
'-m', '--merge',
action='store_true',
help="Specify using 'merge' property during operation.",
default=False,
)
def refresh_druid(datasource, merge):
"""Refresh druid datasources"""
session = db.session()
from superset.connectors.druid.models import DruidCluster
for cluster in session.query(DruidCluster).all():
try:
cluster.refresh_datasources(datasource_name=datasource,
merge_flag=merge)
except Exception as e:
print(
"Error while processing cluster '{}'\n{}".format(
cluster, str(e)))
logging.exception(e)
cluster.metadata_last_refreshed = datetime.now()
print(
'Refreshed metadata from cluster '
'[' + cluster.cluster_name + ']')
session.commit()
@manager.option(
'-p', '--path', dest='path',
help='Path to a single YAML file or path containing multiple YAML '
'files to import (*.yaml or *.yml)')
@manager.option(
'-s', '--sync', dest='sync', default='',
help='comma seperated list of element types to synchronize '
'e.g. "metrics,columns" deletes metrics and columns in the DB '
'that are not specified in the YAML file')
@manager.option(
'-r', '--recursive', dest='recursive', action='store_true',
help='recursively search the path for yaml files')
def import_datasources(path, sync, recursive=False):
"""Import datasources from YAML"""
sync_array = sync.split(',')
p = Path(path)
files = []
if p.is_file():
files.append(p)
elif p.exists() and not recursive:
files.extend(p.glob('*.yaml'))
files.extend(p.glob('*.yml'))
elif p.exists() and recursive:
files.extend(p.rglob('*.yaml'))
files.extend(p.rglob('*.yml'))
for f in files:
logging.info('Importing datasources from file %s', f)
try:
with f.open() as data_stream:
dict_import_export_util.import_from_dict(
db.session,
yaml.safe_load(data_stream),
sync=sync_array)
except Exception as e:
logging.error('Error when importing datasources from file %s', f)
logging.error(e)
@manager.option(
'-f', '--datasource-file', default=None, dest='datasource_file',
help='Specify the the file to export to')
@manager.option(
'-p', '--print', action='store_true', dest='print_stdout',
help='Print YAML to stdout')
@manager.option(
'-b', '--back-references', action='store_true', dest='back_references',
help='Include parent back references')
@manager.option(
'-d', '--include-defaults', action='store_true', dest='include_defaults',
help='Include fields containing defaults')
def export_datasources(print_stdout, datasource_file,
back_references, include_defaults):
"""Export datasources to YAML"""
data = dict_import_export_util.export_to_dict(
session=db.session,
recursive=True,
back_references=back_references,
include_defaults=include_defaults)
if print_stdout or not datasource_file:
yaml.safe_dump(data, stdout, default_flow_style=False)
if datasource_file:
logging.info('Exporting datasources to %s', datasource_file)
with open(datasource_file, 'w') as data_stream:
yaml.safe_dump(data, data_stream, default_flow_style=False)
@manager.option(
'-b', '--back-references', action='store_false',
help='Include parent back references')
def export_datasource_schema(back_references):
"""Export datasource YAML schema to stdout"""
data = dict_import_export_util.export_schema_to_dict(
back_references=back_references)
yaml.safe_dump(data, stdout, default_flow_style=False)
@manager.command
def update_datasources_cache():
"""Refresh sqllab datasources cache"""
from superset.models.core import Database
for database in db.session.query(Database).all():
print('Fetching {} datasources ...'.format(database.name))
try:
database.all_table_names(force=True)
database.all_view_names(force=True)
except Exception as e:
print('{}'.format(str(e)))
@manager.option(
'-w', '--workers',
type=int,
help='Number of celery server workers to fire up')
def worker(workers):
"""Starts a Superset worker for async SQL query execution."""
logging.info(
"The 'superset worker' command is deprecated. Please use the 'celery "
"worker' command instead.")
if workers:
celery_app.conf.update(CELERYD_CONCURRENCY=workers)
elif config.get('SUPERSET_CELERY_WORKERS'):
celery_app.conf.update(
CELERYD_CONCURRENCY=config.get('SUPERSET_CELERY_WORKERS'))
worker = celery_app.Worker(optimization='fair')
worker.start()
@manager.option(
'-p', '--port',
default='5555',
help=('Port on which to start the Flower process'))
@manager.option(
'-a', '--address',
default='localhost',
help=('Address on which to run the service'))
def flower(port, address):
"""Runs a Celery Flower web server
Celery Flower is a UI to monitor the Celery operation on a given
broker"""
BROKER_URL = celery_app.conf.BROKER_URL
cmd = (
'celery flower '
'--broker={BROKER_URL} '
'--port={port} '
'--address={address} '
).format(**locals())
logging.info(
"The 'superset flower' command is deprecated. Please use the 'celery "
"flower' command instead.")
print(Fore.GREEN + 'Starting a Celery Flower instance')
print(Fore.BLUE + '-=' * 40)
print(Fore.YELLOW + cmd)
print(Fore.BLUE + '-=' * 40)
Popen(cmd, shell=True).wait()