anvil/stack
2012-02-29 12:46:07 -08:00

146 lines
4.6 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 logging.config
import os
import sys
import time
import traceback
from devstack import settings
#this needs to happen immediately (or thats what it seems)
LOG_FN = os.getenv(settings.STACK_LOG_ENV_LOCATION)
if not LOG_FN or not os.path.isfile(LOG_FN):
LOG_FN = settings.STACK_LOG_CFG_LOCATION
logging.config.fileConfig(LOG_FN)
from devstack import date
from devstack import log as lg
from devstack import opts
from devstack import shell as sh
from devstack import utils
from devstack.progs import actions
from devstack.progs import common
LOG = lg.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",
}
def dump_config(config_obj):
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))
passwords_gotten = config_obj.pws
full_cfgs = config_obj.configs_fetched
db_dsns = config_obj.db_dsns
if passwords_gotten or full_cfgs or db_dsns:
if passwords_gotten:
LOG.info("Passwords:")
map_print(passwords_gotten)
if full_cfgs:
filtered = dict((k, v) for (k, v) in full_cfgs.items() if k not in passwords_gotten)
if filtered:
LOG.info("Configs:")
map_print(filtered)
if db_dsns:
LOG.info("Data source names:")
map_print(db_dsns)
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
(rep, maxlen) = utils.welcome(_WELCOME_MAP.get(action))
print(utils.center_text("Action Runner", rep, maxlen))
#here on out we should be using the logger (and not print)
start_time = time.time()
config = common.get_config()
pkg_manager = common.get_packager(distro, args.get('keep_old'))
components = utils.parse_components(args.pop("components"))
runner = actions.ActionRunner(distro, action, rootdir, config, 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)
return True
def main():
#do this first so people can see the help message...
args = opts.parse()
prog_name = sys.argv[0]
#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())