2014-01-17 09:36:44 +00:00
|
|
|
#!/usr/bin/env python
|
2013-10-27 16:15:35 +00:00
|
|
|
"""
|
2014-01-17 09:14:15 +00:00
|
|
|
CLI to manage redash.
|
2013-10-27 16:15:35 +00:00
|
|
|
"""
|
2013-12-18 07:40:48 +00:00
|
|
|
import atfork
|
|
|
|
atfork.monkeypatch_os_fork_functions()
|
|
|
|
import atfork.stdlib_fixer
|
|
|
|
atfork.stdlib_fixer.fix_logging_module()
|
|
|
|
|
2013-10-25 07:26:07 +00:00
|
|
|
import logging
|
|
|
|
import time
|
2014-02-09 15:09:07 +00:00
|
|
|
from redash import settings, app, db, models, data_manager, __version__
|
2014-03-11 13:13:41 +00:00
|
|
|
from redash.import_export import import_manager
|
2014-03-03 10:18:15 +00:00
|
|
|
from flask.ext.script import Manager, prompt_pass
|
2013-10-25 07:26:07 +00:00
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
manager = Manager(app)
|
|
|
|
database_manager = Manager(help="Manages the database (create/drop tables).")
|
2014-03-03 10:18:15 +00:00
|
|
|
users_manager = Manager(help="Users management commands.")
|
2014-02-09 15:09:07 +00:00
|
|
|
|
|
|
|
@manager.command
|
|
|
|
def version():
|
|
|
|
"""Displays re:dash version."""
|
|
|
|
print __version__
|
|
|
|
|
2014-02-13 11:08:48 +00:00
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
@manager.command
|
|
|
|
def runworkers():
|
|
|
|
"""Starts the re:dash query executors/workers."""
|
2013-10-25 07:26:07 +00:00
|
|
|
|
|
|
|
try:
|
2013-12-06 13:14:39 +00:00
|
|
|
old_workers = data_manager.redis_connection.smembers('workers')
|
|
|
|
data_manager.redis_connection.delete('workers')
|
|
|
|
|
|
|
|
logging.info("Cleaning old workers: %s", old_workers)
|
|
|
|
|
2014-02-05 10:10:13 +00:00
|
|
|
data_manager.start_workers(settings.WORKERS_COUNT, settings.CONNECTION_ADAPTER, settings.CONNECTION_STRING)
|
2013-10-25 07:26:07 +00:00
|
|
|
logging.info("Workers started.")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
data_manager.refresh_queries()
|
2014-03-03 18:17:25 +00:00
|
|
|
data_manager.report_status()
|
2013-11-29 18:40:26 +00:00
|
|
|
except Exception as e:
|
|
|
|
logging.error("Something went wrong with refreshing queries...")
|
|
|
|
logging.exception(e)
|
2013-10-25 07:26:07 +00:00
|
|
|
time.sleep(60)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.warning("Exiting; waiting for threads")
|
|
|
|
data_manager.stop_workers()
|
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
@manager.shell
|
|
|
|
def make_shell_context():
|
|
|
|
return dict(app=app, db=db, models=models)
|
2013-10-25 07:26:07 +00:00
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
@database_manager.command
|
|
|
|
def create_tables():
|
|
|
|
"""Creates the database tables."""
|
|
|
|
from redash.models import create_db
|
2014-01-17 09:23:06 +00:00
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
create_db(True, False)
|
2014-01-31 14:44:19 +00:00
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
@database_manager.command
|
|
|
|
def drop_tables():
|
|
|
|
"""Drop the database tables."""
|
2014-02-01 15:44:03 +00:00
|
|
|
from redash.models import create_db
|
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
create_db(False, True)
|
2014-02-01 15:44:03 +00:00
|
|
|
|
2014-03-03 10:18:15 +00:00
|
|
|
|
|
|
|
@users_manager.option('email', help="User's email")
|
|
|
|
@users_manager.option('name', help="User's full name")
|
2014-03-12 11:13:06 +00:00
|
|
|
@users_manager.option('--admin', dest='is_admin', action="store_true", default=False, help="set user as admin")
|
|
|
|
@users_manager.option('--google', dest='google_auth', action="store_true", default=False, help="user uses Google Auth to login")
|
2014-03-03 10:18:15 +00:00
|
|
|
def create(email, name, is_admin=False, google_auth=False):
|
|
|
|
print "Creating user (%s, %s)..." % (email, name)
|
|
|
|
print "Admin: %r" % is_admin
|
|
|
|
print "Login with Google Auth: %r\n" % google_auth
|
|
|
|
|
2014-03-12 11:13:06 +00:00
|
|
|
permissions = models.User.DEFAULT_PERMISSIONS
|
|
|
|
if is_admin:
|
|
|
|
permissions += ['admin']
|
|
|
|
|
|
|
|
user = models.User(email=email, name=name, permissions=permissions)
|
2014-03-03 10:18:15 +00:00
|
|
|
if not google_auth:
|
|
|
|
password = prompt_pass("Password")
|
|
|
|
user.hash_password(password)
|
|
|
|
|
|
|
|
try:
|
|
|
|
user.save()
|
|
|
|
except Exception, e:
|
|
|
|
print "Failed creating user: %s" % e.message
|
|
|
|
|
|
|
|
|
|
|
|
@users_manager.option('email', help="email address of user to delete")
|
|
|
|
def delete(email):
|
|
|
|
deleted_count = models.User.delete().where(models.User.email == email).execute()
|
|
|
|
print "Deleted %d users." % deleted_count
|
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
manager.add_command("database", database_manager)
|
2014-03-03 10:18:15 +00:00
|
|
|
manager.add_command("users", users_manager)
|
2014-03-11 13:13:41 +00:00
|
|
|
manager.add_command("import", import_manager)
|
2014-02-01 15:44:03 +00:00
|
|
|
|
2013-10-25 07:26:07 +00:00
|
|
|
if __name__ == '__main__':
|
2014-02-09 19:03:24 +00:00
|
|
|
channel = logging.StreamHandler()
|
|
|
|
logging.getLogger().addHandler(channel)
|
|
|
|
logging.getLogger().setLevel(settings.LOG_LEVEL)
|
|
|
|
|
2014-02-09 15:09:07 +00:00
|
|
|
manager.run()
|