292 lines
12 KiB
Python
Raw Normal View History

2012-01-11 12:47:33 -08:00
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2012-01-26 12:54:36 -08:00
# Copyright (C) 2012 Yahoo! Inc. All Rights Reserved.
2012-01-11 12:47:33 -08:00
#
# 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 io
2012-02-16 15:17:28 -08:00
2012-02-16 15:02:21 -08:00
from urlparse import urlunparse
2012-01-11 12:47:33 -08:00
from devstack import cfg
from devstack import component as comp
from devstack import log as logging
from devstack import shell as sh
from devstack import utils
from devstack.components import db
2012-02-06 21:12:00 -08:00
LOG = logging.getLogger("devstack.components.keystone")
2012-01-24 18:10:38 -08:00
2012-03-15 23:10:15 -07:00
# This db will be dropped then created
2012-01-24 18:03:42 -08:00
DB_NAME = "keystone"
2012-03-15 23:10:15 -07:00
# Subdirs of the git checkout
2012-01-11 12:47:33 -08:00
BIN_DIR = "bin"
2012-01-24 18:03:42 -08:00
2012-03-15 23:10:15 -07:00
# Simple confs
2012-01-24 18:03:42 -08:00
ROOT_CONF = "keystone.conf"
2012-02-16 14:42:55 -08:00
CATALOG_CONF = 'default_catalog.templates'
LOGGING_CONF = "logging.cnf" # WHHHHY U NO LEAVE NAMES SAME!
2012-02-17 14:46:20 -08:00
LOGGING_SOURCE_FN = 'logging.conf.sample'
2012-02-17 13:57:37 -08:00
CONFIGS = [ROOT_CONF, CATALOG_CONF, LOGGING_CONF]
2012-01-24 18:03:42 -08:00
2012-03-15 23:10:15 -07:00
# This is a special conf/init script
MANAGE_DATA_CONF = 'keystone_init.sh'
2012-02-16 15:17:28 -08:00
MANAGE_CMD_ROOT = [sh.joinpths("/", "bin", 'bash')]
MANAGE_ADMIN_USER = 'admin'
MANAGE_DEMO_USER = 'demo'
MANGER_SERVICE_TENANT = 'service'
2012-01-11 12:47:33 -08:00
2012-03-15 23:10:15 -07:00
# Sync db command
SYNC_DB_CMD = [sh.joinpths('%BIN_DIR%', 'keystone-manage'),
'--config-file=%s' % (sh.joinpths('%CONFIG_DIR%', ROOT_CONF)),
'--debug', '-v',
# Available commands:
# db_sync: Sync the database.
# export_legacy_catalog: Export the service catalog from a legacy database.
# import_legacy: Import a legacy database.
# import_nova_auth: Import a dump of nova auth data into keystone.
'db_sync']
2012-03-15 23:10:15 -07:00
# What to start
2012-02-16 15:02:21 -08:00
APP_NAME = 'keystone-all'
APP_OPTIONS = {
APP_NAME: ['--config-file=%s' % (sh.joinpths('%CONFIG_DIR%', ROOT_CONF)),
"--debug", '-v',
'--log-config=%s' % (sh.joinpths('%CONFIG_DIR%', LOGGING_CONF))],
}
2012-03-15 23:10:15 -07:00
# Swift template additions
# TODO: get rid of these
SWIFT_TEMPL_ADDS = ['catalog.RegionOne.object_store.publicURL = http://%SERVICE_HOST%:8080/v1/AUTH_$(tenant_id)s',
'catalog.RegionOne.object_store.publicURL = http://%SERVICE_HOST%:8080/v1/AUTH_$(tenant_id)s',
'catalog.RegionOne.object_store.adminURL = http://%SERVICE_HOST%:8080/',
'catalog.RegionOne.object_store.internalURL = http://%SERVICE_HOST%:8080/v1/AUTH_$(tenant_id)s',
"catalog.RegionOne.object_store.name = Swift Service"]
2012-03-15 23:10:15 -07:00
# Quantum template additions
# TODO: get rid of these
QUANTUM_TEMPL_ADDS = ['catalog.RegionOne.network.publicURL = http://%SERVICE_HOST%:9696/',
'catalog.RegionOne.network.adminURL = http://%SERVICE_HOST%:9696/',
'catalog.RegionOne.network.internalURL = http://%SERVICE_HOST%:9696/',
"catalog.RegionOne.network.name = Quantum Service"]
class KeystoneUninstaller(comp.PythonUninstallComponent):
2012-01-11 12:47:33 -08:00
def __init__(self, *args, **kargs):
comp.PythonUninstallComponent.__init__(self, *args, **kargs)
2012-01-11 12:47:33 -08:00
2012-01-13 19:04:26 -08:00
class KeystoneInstaller(comp.PythonInstallComponent):
2012-01-11 12:47:33 -08:00
def __init__(self, *args, **kargs):
comp.PythonInstallComponent.__init__(self, *args, **kargs)
2012-03-15 14:22:14 -07:00
self.bin_dir = sh.joinpths(self.app_dir, BIN_DIR)
2012-01-11 12:47:33 -08:00
def _get_download_locations(self):
2012-01-31 22:05:38 -08:00
places = list()
places.append({
2012-01-31 22:05:38 -08:00
'uri': ("git", "keystone_repo"),
'branch': ("git", "keystone_branch"),
})
return places
def post_install(self):
2012-02-04 21:08:20 -08:00
comp.PythonInstallComponent.post_install(self)
2012-01-11 12:47:33 -08:00
self._setup_db()
self._sync_db()
self._setup_initer()
2012-01-11 12:47:33 -08:00
def known_options(self):
return set(['swift', 'quantum'])
def _sync_db(self):
2012-03-27 18:15:03 -07:00
LOG.info("Syncing keystone to database named %r", DB_NAME)
mp = self._get_param_map(None)
cmds = [{'cmd': SYNC_DB_CMD}]
utils.execute_template(*cmds, cwd=self.bin_dir, params=mp)
def _get_config_files(self):
return list(CONFIGS)
2012-01-11 12:47:33 -08:00
def _setup_db(self):
2012-03-27 17:50:28 -07:00
LOG.info("Fixing up database named %r", DB_NAME)
db.drop_db(self.cfg, self.pw_gen, self.distro, DB_NAME)
db.create_db(self.cfg, self.pw_gen, self.distro, DB_NAME)
2012-01-11 12:47:33 -08:00
def _setup_initer(self):
LOG.info("Configuring keystone initializer template %r", MANAGE_DATA_CONF)
(_, contents) = utils.load_template(self.component_name, MANAGE_DATA_CONF)
mp = self._get_param_map(MANAGE_DATA_CONF)
contents = utils.param_replace(contents, mp, True)
# FIXME, stop placing in checkout dir...
2012-03-15 14:22:14 -07:00
tgt_fn = sh.joinpths(self.bin_dir, MANAGE_DATA_CONF)
sh.write_file(tgt_fn, contents)
sh.chmod(tgt_fn, 0755)
self.tracewriter.file_touched(tgt_fn)
2012-01-11 12:47:33 -08:00
def _config_adjust(self, contents, name):
2012-02-16 15:02:21 -08:00
if name == ROOT_CONF:
2012-03-15 23:10:15 -07:00
# Use config parser and
# then extract known configs that
2012-03-29 18:59:51 -07:00
# will need locations/directories/files made (or touched)...
2012-02-16 15:02:21 -08:00
with io.BytesIO(contents) as stream:
config = cfg.IgnoreMissingConfigParser(cs=False)
2012-02-16 15:02:21 -08:00
config.readfp(stream)
2012-02-17 13:57:37 -08:00
log_filename = config.get('default', 'log_file')
2012-02-16 15:02:21 -08:00
if log_filename:
LOG.info("Ensuring log file %s exists and is empty." % (log_filename))
log_dir = sh.dirname(log_filename)
if log_dir:
LOG.info("Ensuring log directory %s exists." % (log_dir))
2012-03-08 18:17:05 -08:00
self.tracewriter.dirs_made(*sh.mkdirslist(log_dir))
2012-03-15 23:10:15 -07:00
# Destroy then recreate it (the log file)
2012-02-16 15:02:21 -08:00
sh.unlink(log_filename)
2012-03-08 18:17:05 -08:00
self.tracewriter.file_touched(sh.touch_file(log_filename))
elif name == CATALOG_CONF:
nlines = list()
if 'swift' in self.options:
mp = dict()
mp['SERVICE_HOST'] = self.cfg.get('host', 'ip')
nlines.append("# Swift additions")
nlines.extend(utils.param_replace_list(SWIFT_TEMPL_ADDS, mp))
nlines.append("")
if 'quantum' in self.options:
mp = dict()
mp['SERVICE_HOST'] = self.cfg.get('host', 'ip')
nlines.append("# Quantum additions")
nlines.extend(utils.param_replace_list(QUANTUM_TEMPL_ADDS, mp))
nlines.append("")
if nlines:
nlines.insert(0, contents)
contents = cfg.add_header(name, utils.joinlinesep(*nlines))
return contents
2012-01-11 12:47:33 -08:00
2012-02-17 13:57:37 -08:00
def _get_source_config(self, config_fn):
if config_fn == LOGGING_CONF:
# FIXME, maybe we shouldn't be sucking this from the checkout??
fn = sh.joinpths(self.app_dir, 'etc', LOGGING_SOURCE_FN)
2012-02-17 13:57:37 -08:00
contents = sh.load_file(fn)
return (fn, contents)
return comp.PythonInstallComponent._get_source_config(self, config_fn)
def warm_configs(self):
get_shared_params(self.cfg, self.pw_gen)
def _get_param_map(self, config_fn):
2012-03-15 23:10:15 -07:00
# These be used to fill in the configuration/cmds +
# params with actual values
mp = comp.PythonInstallComponent._get_param_map(self, config_fn)
2012-02-16 15:17:28 -08:00
mp['SERVICE_HOST'] = self.cfg.get('host', 'ip')
2012-03-15 14:22:14 -07:00
mp['DEST'] = self.app_dir
mp['BIN_DIR'] = self.bin_dir
mp['CONFIG_FILE'] = sh.joinpths(self.cfg_dir, ROOT_CONF)
2012-01-25 12:59:12 -08:00
if config_fn == ROOT_CONF:
mp['SQL_CONN'] = db.fetch_dbdsn(self.cfg, self.pw_gen, DB_NAME)
2012-03-15 14:22:14 -07:00
mp['KEYSTONE_DIR'] = self.app_dir
mp.update(get_shared_params(self.cfg, self.pw_gen))
2012-01-25 12:59:12 -08:00
elif config_fn == MANAGE_DATA_CONF:
mp.update(get_shared_params(self.cfg, self.pw_gen))
2012-01-11 12:47:33 -08:00
return mp
class KeystoneRuntime(comp.PythonRuntime):
2012-01-11 12:47:33 -08:00
def __init__(self, *args, **kargs):
comp.PythonRuntime.__init__(self, *args, **kargs)
2012-03-15 14:22:14 -07:00
self.bin_dir = sh.joinpths(self.app_dir, BIN_DIR)
self.wait_time = max(self.cfg.getint('default', 'service_wait_seconds'), 1)
2012-02-16 15:17:28 -08:00
def post_start(self):
2012-03-15 14:22:14 -07:00
tgt_fn = sh.joinpths(self.bin_dir, MANAGE_DATA_CONF)
if sh.is_executable(tgt_fn):
2012-03-15 23:10:15 -07:00
# If its still there, run it
# these environment additions are important
# in that they eventually affect how this script runs
LOG.info("Waiting %s seconds so that keystone can start up before running first time init." % (self.wait_time))
sh.sleep(self.wait_time)
2012-02-16 15:17:28 -08:00
env = dict()
env['ENABLED_SERVICES'] = ",".join(self.instances.keys())
2012-03-15 14:22:14 -07:00
env['BIN_DIR'] = self.bin_dir
2012-02-16 15:17:28 -08:00
setup_cmd = MANAGE_CMD_ROOT + [tgt_fn]
LOG.info("Running %r command to initialize keystone." % (" ".join(setup_cmd)))
2012-03-12 21:21:48 -07:00
sh.execute(*setup_cmd, env_overrides=env, run_as_root=False)
utils.mark_unexecute_file(tgt_fn, env)
2012-02-16 15:17:28 -08:00
def _get_apps_to_start(self):
apps = list()
for app_name in APP_OPTIONS.keys():
apps.append({
'name': app_name,
2012-03-15 14:22:14 -07:00
'path': sh.joinpths(self.bin_dir, app_name),
})
return apps
def _get_app_options(self, app):
return APP_OPTIONS.get(app)
2012-01-24 18:03:42 -08:00
def get_shared_params(config, pw_gen, service_user_name=None):
mp = dict()
host_ip = config.get('host', 'ip')
2012-03-15 23:10:15 -07:00
# These match what is in keystone_init.sh
mp['SERVICE_TENANT_NAME'] = MANGER_SERVICE_TENANT
2012-03-07 10:35:38 -08:00
if service_user_name:
mp['SERVICE_USERNAME'] = str(service_user_name)
mp['ADMIN_USER_NAME'] = MANAGE_ADMIN_USER
mp['DEMO_USER_NAME'] = MANAGE_DEMO_USER
mp['ADMIN_TENANT_NAME'] = mp['ADMIN_USER_NAME']
mp['DEMO_TENANT_NAME'] = mp['DEMO_USER_NAME']
2012-03-15 23:10:15 -07:00
# Tokens and passwords
mp['SERVICE_TOKEN'] = pw_gen.get_password(
"service_token",
'the service admin token',
)
mp['ADMIN_PASSWORD'] = pw_gen.get_password(
'horizon_keystone_admin',
'the horizon and keystone admin',
length=20,
)
mp['SERVICE_PASSWORD'] = pw_gen.get_password(
'service_password',
'service authentication',
)
2012-03-15 23:10:15 -07:00
# Components of the auth endpoint
keystone_auth_host = config.getdefaulted('keystone', 'keystone_auth_host', host_ip)
mp['KEYSTONE_AUTH_HOST'] = keystone_auth_host
keystone_auth_port = config.getdefaulted('keystone', 'keystone_auth_port', '35357')
2012-02-16 15:02:21 -08:00
mp['KEYSTONE_AUTH_PORT'] = keystone_auth_port
keystone_auth_proto = config.getdefaulted('keystone', 'keystone_auth_protocol', 'http')
2012-02-16 15:02:21 -08:00
mp['KEYSTONE_AUTH_PROTOCOL'] = keystone_auth_proto
2012-03-15 23:10:15 -07:00
# Components of the service endpoint
keystone_service_host = config.getdefaulted('keystone', 'keystone_service_host', host_ip)
mp['KEYSTONE_SERVICE_HOST'] = keystone_service_host
keystone_service_port = config.getdefaulted('keystone', 'keystone_service_port', '5000')
2012-02-16 15:02:21 -08:00
mp['KEYSTONE_SERVICE_PORT'] = keystone_service_port
keystone_service_proto = config.getdefaulted('keystone', 'keystone_service_protocol', 'http')
2012-02-16 15:02:21 -08:00
mp['KEYSTONE_SERVICE_PROTOCOL'] = keystone_service_proto
2012-03-15 23:10:15 -07:00
# Uri's of the http/https endpoints
2012-02-16 15:03:27 -08:00
mp['AUTH_ENDPOINT'] = urlunparse((keystone_auth_proto,
"%s:%s" % (keystone_auth_host, keystone_auth_port),
"v2.0", "", "", ""))
mp['SERVICE_ENDPOINT'] = urlunparse((keystone_service_proto,
"%s:%s" % (keystone_service_host, keystone_service_port),
"v2.0", "", "", ""))
return mp