# vim: tabstop=4 shiftwidth=4 softtabstop=4 # Copyright (C) 2012 Yahoo! Inc. All Rights Reserved. # # 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 from devstack import cfg from devstack import component as comp from devstack import log as logging from devstack import settings from devstack import shell as sh from devstack import utils from devstack.components import db LOG = logging.getLogger("devstack.components.melange") # This db will be dropped then created DB_NAME = 'melange' # Subdirs of the checkout/download BIN_DIR = 'bin' # Basic configs ROOT_CONF = 'melange.conf.sample' ROOT_CONF_REAL_NAME = 'melange.conf' CONFIGS = [ROOT_CONF] CFG_LOC = ['etc', 'melange'] # Sensible defaults DEF_CIDR_RANGE = 'FE-EE-DD-00-00-00/24' # How we sync melange with the db DB_SYNC_CMD = [ {'cmd': ['%BIN_DIR%/melange-manage', '--config-file=%CFG_FILE%', 'db_sync']}, ] # TODO: ??? CIDR_CREATE_CMD = [ {'cmd': ['melange', 'mac_address_range', 'create', 'cidr', '%CIDR_RANGE%']}, ] # What to start APP_OPTIONS = { 'melange-server': ['--config-file', '%CFG_FILE%'], } # Special option that specifies we should make the network cidr using melange CREATE_CIDR = "create-cidr" WAIT_ONLINE_TO = settings.WAIT_ALIVE_SECS class MelangeUninstaller(comp.PythonUninstallComponent): def __init__(self, *args, **kargs): comp.PythonUninstallComponent.__init__(self, *args, **kargs) class MelangeInstaller(comp.PythonInstallComponent): def __init__(self, *args, **kargs): comp.PythonInstallComponent.__init__(self, *args, **kargs) self.bin_dir = sh.joinpths(self.app_dir, BIN_DIR) self.cfg_dir = sh.joinpths(self.app_dir, *CFG_LOC) def _get_download_locations(self): places = list() places.append({ 'uri': ("git", "melange_repo"), 'branch': ("git", "melange_branch"), }) return places def _setup_db(self): LOG.info("Fixing up database named %s.", 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) def post_install(self): comp.PythonInstallComponent.post_install(self) self._setup_db() self._sync_db() def _sync_db(self): LOG.info("Syncing the database with melange.") mp = dict() mp['BIN_DIR'] = self.bin_dir mp['CFG_FILE'] = sh.joinpths(self.cfg_dir, ROOT_CONF_REAL_NAME) utils.execute_template(*DB_SYNC_CMD, params=mp) def _get_config_files(self): return list(CONFIGS) def _config_adjust(self, contents, config_fn): if config_fn == ROOT_CONF: newcontents = contents with io.BytesIO(contents) as stream: config = cfg.IgnoreMissingConfigParser() config.readfp(stream) db_dsn = db.fetch_dbdsn(self.cfg, self.pw_gen, DB_NAME) old_dbsn = config.get('DEFAULT', 'sql_connection') if db_dsn != old_dbsn: config.set('DEFAULT', 'sql_connection', db_dsn) with io.BytesIO() as outputstream: config.write(outputstream) outputstream.flush() newcontents = cfg.add_header(config_fn, outputstream.getvalue()) contents = newcontents return contents def _get_source_config(self, config_fn): if config_fn == ROOT_CONF: srcfn = sh.joinpths(self.cfg_dir, config_fn) contents = sh.load_file(srcfn) return (srcfn, contents) else: return comp.PythonInstallComponent._get_source_config(self, config_fn) def _get_target_config_name(self, config_fn): if config_fn == ROOT_CONF: return sh.joinpths(self.cfg_dir, ROOT_CONF_REAL_NAME) else: return comp.PythonInstallComponent._get_target_config_name(self, config_fn) class MelangeRuntime(comp.PythonRuntime): def __init__(self, *args, **kargs): comp.PythonRuntime.__init__(self, *args, **kargs) self.bin_dir = sh.joinpths(self.app_dir, BIN_DIR) self.cfg_dir = sh.joinpths(self.app_dir, *CFG_LOC) def _get_apps_to_start(self): apps = list() for app_name in APP_OPTIONS.keys(): apps.append({ 'name': app_name, 'path': sh.joinpths(self.bin_dir, app_name), }) return apps def _get_app_options(self, app): return APP_OPTIONS.get(app) def _get_param_map(self, app_name): pmap = comp.PythonRuntime._get_param_map(self, app_name) pmap['CFG_FILE'] = sh.joinpths(self.cfg_dir, ROOT_CONF_REAL_NAME) return pmap def post_start(self): comp.PythonRuntime.post_start(self) # FIXME: This is a bit of a hack. How do we document "flags" like this? flags = [] if CREATE_CIDR in flags: LOG.info("Waiting %s seconds so that the melange server can start up before cidr range creation." % (WAIT_ONLINE_TO)) sh.sleep(WAIT_ONLINE_TO) mp = dict() mp['CIDR_RANGE'] = self.cfg.getdefaulted('melange', 'm_mac_range', DEF_CIDR_RANGE) utils.execute_template(*CIDR_CREATE_CMD, params=mp)