redash/manage.py

125 lines
4.3 KiB
Python
Raw Normal View History

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
"""
2014-03-03 10:18:15 +00:00
from flask.ext.script import Manager, prompt_pass
2013-10-25 07:26:07 +00:00
from redash import settings, models, __version__
from redash.wsgi import app
from redash.import_export import import_manager
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-04-19 14:06:47 +00:00
data_sources_manager = Manager(help="Data sources management commands.")
2014-02-09 15:09:07 +00:00
@manager.command
def version():
"""Displays re:dash version."""
print __version__
2014-02-09 15:09:07 +00:00
@manager.command
def runworkers():
2014-05-16 14:57:43 +00:00
"""Prints deprecation warning."""
print "** This command is deprecated. Please use Celery's CLI to control the workers. **"
2013-10-25 07:26:07 +00:00
2014-02-09 15:09:07 +00:00
@manager.shell
def make_shell_context():
from redash.models import db
2014-02-09 15:09:07 +00:00
return dict(app=app, db=db, models=models)
2013-10-25 07:26:07 +00:00
@manager.command
def check_settings():
from types import ModuleType
for name in dir(settings):
item = getattr(settings, name)
if not callable(item) and not name.startswith("__") and not isinstance(item, ModuleType):
print "{} = {}".format(name, item)
2014-02-09 15:09:07 +00:00
@database_manager.command
def create_tables():
"""Creates the database tables."""
from redash.models import create_db, init_db
2014-02-09 15:09:07 +00:00
create_db(True, False)
init_db()
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")
@users_manager.option('--password', dest='password', default=None, help="Password for users who don't use Google Auth (leave blank for prompt).")
@users_manager.option('--groups', dest='groups', default=models.User.DEFAULT_GROUPS, help="Comma seperated list of groups (leave blank for default).")
2014-05-13 15:34:19 +00:00
def create(email, name, groups, is_admin=False, google_auth=False, password=None):
2014-03-03 10:18:15 +00:00
print "Creating user (%s, %s)..." % (email, name)
print "Admin: %r" % is_admin
print "Login with Google Auth: %r\n" % google_auth
2014-05-13 15:34:19 +00:00
if isinstance(groups, basestring):
groups= groups.split(',')
groups.remove('') # in case it was empty string
2014-03-03 10:18:15 +00:00
2014-03-12 11:13:06 +00:00
if is_admin:
2014-05-13 15:34:19 +00:00
groups += ['admin']
2014-03-12 11:13:06 +00:00
2014-05-13 15:34:19 +00:00
user = models.User(email=email, name=name, groups=groups)
2014-03-03 10:18:15 +00:00
if not google_auth:
password = password or prompt_pass("Password")
2014-03-03 10:18:15 +00:00
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-04-19 14:06:47 +00:00
@data_sources_manager.command
def import_from_settings(name=None):
"""Import data source from settings (env variables)."""
name = name or "Default"
data_source = models.DataSource.create(name=name,
type=settings.CONNECTION_ADAPTER,
options=settings.CONNECTION_STRING)
print "Imported data source from settings (id={}).".format(data_source.id)
@data_sources_manager.command
def list():
"""List currently configured data sources"""
for ds in models.DataSource.select():
print "Name: {}\nType: {}\nOptions: {}".format(ds.name, ds.type, ds.options)
@data_sources_manager.command
def new(name, type, options):
"""Create new data source"""
# TODO: validate it's a valid type and in the future, validate the options.
2014-04-19 14:06:47 +00:00
print "Creating {} data source ({}) with options:\n{}".format(type, name, options)
data_source = models.DataSource.create(name=name,
type=type,
options=options)
print "Id: {}".format(data_source.id)
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)
manager.add_command("import", import_manager)
2014-04-19 14:06:47 +00:00
manager.add_command("ds", data_sources_manager)
2014-02-01 15:44:03 +00:00
2013-10-25 07:26:07 +00:00
if __name__ == '__main__':
2014-02-09 15:09:07 +00:00
manager.run()