216 lines
6.3 KiB
Plaintext
Raw Normal View History

2012-01-26 13:31:44 -08:00
#!/usr/bin/env python
2012-01-26 12:54:36 -08:00
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2012-01-11 12:47:33 -08:00
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 sys
import time
import traceback
2012-01-11 12:47:33 -08:00
2012-03-15 14:22:14 -07:00
from devstack import cfg
from devstack import cfg_helpers
from devstack import date
2012-03-14 19:37:01 -04:00
from devstack import distro
from devstack import env
from devstack import env_rc
from devstack import log as logging
from devstack import opts
from devstack import passwords
from devstack import persona
from devstack import settings
from devstack import shell as sh
2012-02-06 11:20:24 -08:00
from devstack import utils
from devstack.progs import actions
LOG = logging.getLogger("devstack.stack")
_CFG_GROUPS = {
cfg_helpers.make_id('passwords', None): 'Passwords',
cfg_helpers.make_id('db', None): 'Database info',
2012-03-15 14:22:14 -07:00
# 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))
2012-03-15 14:22:14 -07:00
# First partition into our groups
partitions = dict()
for name in _CFG_ORDERING:
partitions[name] = dict()
2012-03-15 14:22:14 -07:00
# Now put the config cached values into there partitions
for (k, v) in config_cache.items():
for name in _CFG_ORDERING:
entries = partitions[name]
if k.startswith(name):
entries[k] = v
break
2012-03-15 14:22:14 -07:00
# 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 load_rc_files():
fns = [settings.OSRC_FN]
for fn in fns:
try:
LOG.debug("Attempting to load rc file at [%s] which has your environment settings." % (fn))
am_loaded = env_rc.RcReader().load(fn)
LOG.debug("Loaded [%s] settings from rc file [%s]" % (am_loaded, fn))
except IOError:
LOG.warn('Error reading rc file located at [%s]. Skipping loading it.' % (fn))
return len(fns)
def load_verify_persona(fn, dist):
instance = persona.Persona.load_file(fn)
instance.verify(dist)
return instance
def setup_root(root_dir):
if not sh.isdir(root_dir):
sh.mkdir(root_dir)
def run(args):
2012-03-15 14:22:14 -07:00
action = args.pop("action", '').strip().lower()
if action not in actions.get_action_names():
print(utils.color_text("No valid action specified!", "red"))
return False
loaded_rcs = False
2012-03-15 14:22:14 -07:00
root_dir = args.pop("dir")
if not root_dir:
load_rc_files()
loaded_rcs = True
2012-03-15 14:22:14 -07:00
root_dir = env.get_key(env_rc.INSTALL_ROOT)
if not root_dir:
print(utils.color_text("No root directory specified!", "red"))
return False
2012-03-15 18:25:31 -07:00
root_dir = sh.abspth(root_dir)
setup_root(root_dir)
2012-03-15 14:22:14 -07:00
persona_fn = args.pop('persona_fn')
if not persona_fn or not sh.isfile(persona_fn):
print(utils.color_text("No valid persona file name specified!", "red"))
return False
2012-03-16 17:27:30 -07:00
persona_fn = sh.abspth(persona_fn)
2012-03-15 14:22:14 -07:00
# Welcome!
(repeat_string, line_max_len) = utils.welcome(action.upper())
print(utils.center_text("Action Runner", repeat_string, line_max_len))
# !!
2012-03-15 14:22:14 -07:00
# Here on out we should be using the logger (and not print)!!
# !!
2012-03-15 14:22:14 -07:00
# If we didn't load them before, load them now
if not loaded_rcs:
load_rc_files()
loaded_rcs = True
# Stash the dryrun value (if any) into the global configuration
sh.set_dryrun(args.get('dryrun', False))
# Params for the runner...
2012-03-15 12:15:26 -07:00
dist = distro.Distro.get_current()
persona_inst = load_verify_persona(persona_fn, dist)
2012-03-15 14:22:14 -07:00
config = cfg.get_config()
pw_gen = passwords.PasswordGenerator(config, args.get('prompt_for_passwords', True))
runner_factory = actions.get_runner_factory(action)
runner = runner_factory(dist,
config,
pw_gen,
**args)
LOG.info("Starting action %r on %s for distro: %r" % (action, date.rcf8222date(), dist.name))
LOG.info("Using persona: %r" % (persona_fn))
LOG.info("In root directory: %r" % (root_dir))
2012-03-15 14:22:14 -07:00
start_time = time.time()
runner.run(persona_inst, root_dir)
2012-03-15 14:22:14 -07:00
end_time = time.time()
LOG.info("It took (%s) to complete action [%s]" %
(utils.format_secs_taken((end_time - start_time)), action))
LOG.info("After action [%s] your settings which were created or read are:" % (action))
dump_config(config.configs_fetched)
return True
2012-01-11 12:47:33 -08:00
2012-01-28 21:10:51 -08:00
2012-03-08 18:17:05 -08:00
def main():
2012-03-15 14:22:14 -07:00
# Do this first so people can see the help message...
2012-03-08 18:17:05 -08:00
args = opts.parse()
prog_name = sys.argv[0]
# Configure logging
utils.configure_logging(args['verbosity'], args['dryrun'])
2012-03-08 18:17:05 -08:00
LOG.debug("Command line options %s" % (args))
2012-03-15 14:22:14 -07:00
# Will need root to setup openstack
if not sh.got_root():
2012-02-11 00:22:47 -08:00
rest_args = sys.argv[1:]
2012-02-06 20:39:54 -08:00
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))
2012-02-06 11:20:24 -08:00
print(msg)
return 1
try:
2012-03-15 14:22:14 -07:00
# Drop to usermode
2012-02-14 23:41:36 -08:00
sh.user_mode(False)
started_ok = run(args)
2012-02-04 11:12:04 -08:00
if not started_ok:
2012-02-06 11:20:24 -08:00
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
2012-01-28 21:10:51 -08:00
except Exception:
utils.goodbye(False)
traceback.print_exc(file=sys.stdout)
return 1
2012-01-11 12:47:33 -08:00
if __name__ == "__main__":
sys.exit(main())