66 lines
2.2 KiB
Plaintext
66 lines
2.2 KiB
Plaintext
cat << EOF | python
|
|
from redash import create_app
|
|
from redash import models
|
|
from redash.query_runner.clickhouse import ClickHouse
|
|
from redash.utils.configuration import ConfigurationContainer
|
|
|
|
app = create_app()
|
|
app.app_context().push()
|
|
|
|
# Get organization
|
|
org = models.Organization.get_by_slug('default')
|
|
|
|
# Get or create group
|
|
group = models.Group.query.filter(models.Group.name == "{{ username }}", models.Group.org == org).first()
|
|
if group:
|
|
print("Group already exists")
|
|
else:
|
|
group = models.Group(name="{{ username }}", org=org, permissions=models.Group.DEFAULT_PERMISSIONS)
|
|
models.db.session.add(group)
|
|
models.db.session.commit()
|
|
print("Created group '{}'".format(group.name))
|
|
{% if is_root %}
|
|
for permission in ["admin", "super_admin"]:
|
|
if permission not in group.permissions:
|
|
print("Adding permission '{}' to group".format(permission))
|
|
group.permissions.append(permission)
|
|
models.db.session.add(group)
|
|
models.db.session.commit()
|
|
{% endif %}
|
|
|
|
# Get or create user
|
|
user = models.User.query.filter(models.User.email == "{{ email }}").first()
|
|
if user:
|
|
print("User already exists")
|
|
else:
|
|
user = models.User(org=org, email="{{ email }}", name="{{ username }}", group_ids=[group.id])
|
|
print("Created user '{}/{}'".format(user.email, user.name))
|
|
user.hash_password("""{{ password }}""")
|
|
models.db.session.add(user)
|
|
models.db.session.commit()
|
|
|
|
# Get or create datasource
|
|
options = ConfigurationContainer(
|
|
{
|
|
"url": "http://{{ VISION_CLICKHOUSE_HOST }}:{{ VISION_CLICKHOUSE_HTTP_PORT }}",
|
|
"user": "{{ username }}",
|
|
"password": "",
|
|
"dbname": "{{ VISION_CLICKHOUSE_DATABASE }}",
|
|
},
|
|
ClickHouse.configuration_schema()
|
|
)
|
|
data_source = models.DataSource.query.filter(models.DataSource.name == "{{ username }}").first()
|
|
if data_source:
|
|
print("Data source already exists")
|
|
else:
|
|
data_source = models.DataSource(
|
|
name="{{ username }}",
|
|
type="clickhouse",
|
|
options=options,
|
|
org=org,
|
|
)
|
|
data_source_group = models.DataSourceGroup(data_source=data_source, group=group)
|
|
models.db.session.add_all([data_source, data_source_group])
|
|
models.db.session.commit()
|
|
print("Created datasource '{}'".format(data_source.name))
|
|
EOF |