anvil/stack
Joshua Harlow 6e4382cb42 Made it so that the config uses a shared cache (which is shared with the password gen).
Adjusted some naming to have slightly shorter variable names (pw_gen).
Removed need for section in password generator.
Adjusted passing of the renamed variable around and the other places pw_gen should be used.

Added helper class that can be used by both pw_gen and cfg to get ids and adjusted print out of the config to
now just print out the shared cache, which seems to make sense to me.
2012-03-12 21:03:08 -07:00

197 lines
6.0 KiB
Python
Executable File

#!/usr/bin/env python
# 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 sys
import time
import traceback
from devstack import cfg_helpers
from devstack import colorlog
from devstack import date
from devstack import log as logging
from devstack import opts
from devstack import passwords
from devstack import settings
from devstack import shell as sh
from devstack import utils
from devstack.progs import actions
from devstack.progs import common
LOG = logging.getLogger("devstack.stack")
# This is used to map an action to a useful string for
# the welcome display
_WELCOME_MAP = {
settings.INSTALL: "INSTALLER",
settings.UNINSTALL: "UNINSTALLER",
settings.START: "STARTER",
settings.STOP: "STOPPER",
}
_CFG_GROUPS = {
cfg_helpers.make_id('passwords', None): 'Passwords',
cfg_helpers.make_id('db', None): 'Database info',
#catch all
cfg_helpers.make_id(None, None): 'Misc configs',
}
_CFG_ORDERING = sorted(_CFG_GROUPS.keys())
_CFG_ORDERING.reverse()
def dump_config(config_cache):
def item_format(key, value):
return "\t%s=%s" % (str(key), str(value))
def map_print(mp):
for key in sorted(mp.keys()):
value = mp.get(key)
LOG.info(item_format(key, value))
#first partition into our groups
partitions = dict()
for name in _CFG_ORDERING:
partitions[name] = dict()
#now put the config cached values into there partition
for (k, v) in config_cache.items():
for name in _CFG_ORDERING:
entries = partitions[name]
if k.startswith(name):
entries[k] = v
break
#now print them..
for name in _CFG_ORDERING:
nice_name = _CFG_GROUPS.get(name)
LOG.info(nice_name + ":")
entries = partitions.get(name)
if entries:
map_print(entries)
def run(args):
(distro, platform) = utils.determine_distro()
if distro is None:
print("Unsupported platform " + utils.color_text(platform, "red") + "!")
return False
action = args.pop("action").strip().lower()
if not (action in settings.ACTIONS):
print(utils.color_text("No valid action specified!", "red"))
return False
rootdir = args.pop("dir")
if not rootdir:
print(utils.color_text("No root directory specified!", "red"))
return False
#welcome!
(repeat_string, line_max_len) = utils.welcome(_WELCOME_MAP.get(action))
print(utils.center_text("Action Runner", repeat_string, line_max_len))
#here on out we should be using the logger (and not print)
start_time = time.time()
# Stash the dryrun value (if any) into the global configuration
sh.set_dryrun(args['dryrun'])
config_kv_cache = dict()
config = common.get_config(kv_cache=config_kv_cache)
pw_gen = passwords.PasswordGenerator(config_kv_cache, config, args['prompt_for_passwords'])
pkg_manager = common.get_packager(distro, args['keep_old'])
components = utils.parse_components(args.pop("components"))
runner = actions.ActionRunner(distro, action, rootdir, config, pw_gen,
pkg_manager, components=components, **args)
LOG.info("Starting action [%s] on %s for distro [%s]" % (action, date.rcf8222date(), distro))
runner.run()
LOG.info("It took (%s) to complete action [%s]" % (common.format_secs_taken((time.time() - start_time)), action))
LOG.info("After action [%s] your settings which were created or read are:" % (action))
dump_config(config_kv_cache)
return True
def configure_logging(args):
# Debug by default
root_logger = logging.getLogger().logger
root_logger.setLevel(logging.DEBUG)
# Set our pretty logger
console_logger = logging.StreamHandler(stream=sys.stdout)
console_format = '%(levelname)s: @%(name)s : %(message)s'
if sh.in_terminal():
console_logger.setFormatter(colorlog.TermFormatter(console_format))
else:
console_logger.setFormatter(logging.Formatter(console_format))
root_logger.addHandler(console_logger)
# Adjust logging verbose level based on the command line switch.
log_level = logging.INFO
if args['verbosity'] >= 2:
log_level = logging.DEBUG
elif args['dryrun']:
log_level = logging.AUDIT
root_logger.setLevel(log_level)
def main():
#do this first so people can see the help message...
args = opts.parse()
prog_name = sys.argv[0]
# Configure logging
configure_logging(args)
LOG.debug("Command line options %s" % (args))
#will need root to setup openstack
if not sh.got_root():
rest_args = sys.argv[1:]
print("This program requires a user with sudo access.")
msg = "Perhaps you should try %s %s" % \
(utils.color_text("sudo %s" % (prog_name), "red", True), " ".join(rest_args))
print(msg)
return 1
try:
#drop to usermode
sh.user_mode(False)
started_ok = run(args)
if not started_ok:
me = utils.color_text(prog_name, "red", True)
me += " " + utils.color_text('--help', 'red')
print("Perhaps you should try %s" % (me))
return 1
else:
utils.goodbye(True)
return 0
except Exception:
utils.goodbye(False)
traceback.print_exc(file=sys.stdout)
return 1
if __name__ == "__main__":
sys.exit(main())