Load superusers from a yaml file

The idea is the same as for projects.

Usage:
storyboard-db-manage --config-file <config> load_superusers
<superusers.yaml>

Change-Id: Ia7f3f7415ffedb5e449bf7b9ca76a28d0cf31824
This commit is contained in:
Nikita Konovalov 2014-03-04 18:05:17 +04:00
parent 20aadc8701
commit 51f833ca59
3 changed files with 64 additions and 2 deletions

View File

@ -0,0 +1,2 @@
- openid: https://login.launchpad.net/+id/GBnTJEw
email: nkonovalov@mirantis.com

View File

@ -23,7 +23,8 @@ from alembic import config as alembic_config
from alembic import util as alembic_util
from oslo.config import cfg
import storyboard.db.projects_loader as loader
from storyboard.db import projects_loader
from storyboard.db import superusers_loader
gettext.install('storyboard', unicode=1)
@ -71,7 +72,11 @@ def do_revision(config, cmd):
def do_load_projects(config, cmd):
loader.do_load_models(CONF.command.file)
projects_loader.do_load_models(CONF.command.file)
def do_load_superusers(config, cmd):
superusers_loader.do_load_models(CONF.command.file)
def add_command_parsers(subparsers):
@ -104,6 +109,10 @@ def add_command_parsers(subparsers):
parser.add_argument('file', type=str)
parser.set_defaults(func=do_load_projects)
parser = subparsers.add_parser('load_superusers')
parser.add_argument('file', type=str)
parser.set_defaults(func=do_load_superusers)
command_opt = cfg.SubCommandOpt('command',
title='Command',

View File

@ -0,0 +1,51 @@
# Copyright (c) 2014 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings
import yaml
from sqlalchemy.exc import SADeprecationWarning
from storyboard.db.models import User
from storyboard.openstack.common.db.sqlalchemy import session as db_session
warnings.simplefilter("ignore", SADeprecationWarning)
def do_load_models(filename):
config_file = open(filename)
superusers_list = yaml.load(config_file)
session = db_session.get_session(sqlite_fk=True)
with session.begin():
for user in superusers_list:
openid = user.get("openid")
if not openid:
raise Exception("A superuser is missing an openid field")
email = user.get("email") or "unset"
db_user = session.query(User).filter_by(openid=openid).first()
if not db_user:
db_user = User()
db_user.openid = openid
db_user.email = email
db_user.is_superuser = True
else:
continue
session.add(db_user)