From 872c41482f2a32ae699a8057e7f8e7c997588ee9 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Mon, 6 Feb 2012 11:20:24 -0800 Subject: [PATCH] Fixing up gunthers new adjustments. --- devstack/components/horizon.py | 9 ++++---- devstack/downloader.py | 14 ++++++++++++ devstack/opts.py | 15 +++++++------ devstack/shell.py | 39 +++++++++++++++++----------------- stack | 28 +++++++++++++++++------- 5 files changed, 66 insertions(+), 39 deletions(-) diff --git a/devstack/components/horizon.py b/devstack/components/horizon.py index 26a7620c..20e39a61 100644 --- a/devstack/components/horizon.py +++ b/devstack/components/horizon.py @@ -84,10 +84,11 @@ class HorizonInstaller(comp.PythonInstallComponent): return places def _get_symlinks(self): - return { - comp.PythonInstallComponent._get_target_config_name(self, HORIZON_APACHE_CONF):\ - sh.joinpths(*HORIZON_APACHE_TGT) - } + src = self._get_target_config_name(HORIZON_APACHE_CONF) + tgt = sh.joinpths(*HORIZON_APACHE_TGT) + links = dict() + links[src] = tgt + return links def _check_ug(self): (user, group) = self._get_apache_user_group() diff --git a/devstack/downloader.py b/devstack/downloader.py index f33fbdce..221d1ee3 100644 --- a/devstack/downloader.py +++ b/devstack/downloader.py @@ -20,15 +20,29 @@ import re from devstack import log as logging from devstack import shell as sh +from devstack import env LOG = logging.getLogger("devstack.downloader") EXT_REG = re.compile(r"^(.*?)\.git\s*$", re.IGNORECASE) GIT_MASTER_BRANCH = "master" +GIT_CACHE_DIR_ENV = "GIT_CACHE_DIR" + + +def _git_cache_download(storewhere, uri, branch=None): + cdir = env.get_key(GIT_CACHE_DIR_ENV) + if cdir and sh.isdir(cdir): + #TODO + pass + return False def _gitdownload(storewhere, uri, branch=None): dirsmade = sh.mkdirslist(storewhere) LOG.info("Downloading from %s to %s" % (uri, storewhere)) + #check if already done + if _git_cache_download(storewhere, uri, branch): + return dirsmade + #have to do it... cmd = ["git", "clone"] + [uri, storewhere] sh.execute(*cmd) if branch and branch != GIT_MASTER_BRANCH: diff --git a/devstack/opts.py b/devstack/opts.py index 6ec55f60..e636a301 100644 --- a/devstack/opts.py +++ b/devstack/opts.py @@ -24,6 +24,7 @@ from devstack import version HELP_WIDTH = 80 LOG = logging.getLogger("devstack.opts") +DEF_DIR_NAME = 'stack' def _format_list(in_list): @@ -31,9 +32,12 @@ def _format_list(in_list): return "[" + ", ".join(sorted_list) + "]" +def _get_def_dir(): + return sh.joinpths(sh.gethomedir(), DEF_DIR_NAME) + + def parse(): - #version version_str = "%prog v" + version.version_string() help_formatter = IndentedHelpFormatter(width=HELP_WIDTH) parser = OptionParser(version=version_str, formatter=help_formatter) @@ -55,8 +59,8 @@ def parse(): dest="dir", metavar="DIR", help=("empty root DIR for install or " - "DIR with existing components for start/stop/uninstall"), - default='/opt/stack' if sh.geteuid() == 0 else sh.joinpths(sh.gethomedir(), 'stack')) + "DIR with existing components for start/stop/uninstall (default: %default)"), + default=_get_def_dir()) base_group.add_option("-i", "--ignore-deps", action="store_false", dest="ensure_deps", @@ -93,10 +97,7 @@ def parse(): output['ref_components'] = options.r_component output['action'] = options.action output['force'] = options.force - if options.ensure_deps: - output['ignore_deps'] = False - else: - output['ignore_deps'] = True + output['ignore_deps'] = not options.ensure_deps output['keep_packages'] = options.keep_packages output['extras'] = args return output diff --git a/devstack/shell.py b/devstack/shell.py index fe187dd9..a3ef615b 100644 --- a/devstack/shell.py +++ b/devstack/shell.py @@ -95,10 +95,11 @@ def execute(*cmd, **kwargs): for (k, v) in env_overrides.items(): process_env[k] = str(v) + rc = None + result = None try: if run_as_root: root_mode() - obj = subprocess.Popen(execute_cmd, stdin=stdin_fh, stdout=stdout_fh, @@ -107,22 +108,19 @@ def execute(*cmd, **kwargs): cwd=cwd, shell=shell, env=process_env) - result = None if process_input is not None: result = obj.communicate(str(process_input)) else: result = obj.communicate() - if (stdin_fh != subprocess.PIPE and obj.stdin and close_stdin): obj.stdin.close() - rc = obj.returncode LOG.debug('Cmd result had exit code: %s' % rc) - finally: - user_mode() + if run_as_root: + user_mode() if (not ignore_exit_code) and (rc not in check_exit_code): (stdout, stderr) = result @@ -188,10 +186,10 @@ def prompt_password(pw_prompt=None): return rc.strip() -def chown_r(path, uid, gid): +def chown_r(path, uid, gid, run_as_root=True): try: - root_mode() - + if run_as_root: + root_mode() if(isdir(path)): LOG.debug("Changing ownership of %s to %s:%s" % (path, uid, gid)) os.chown(path, uid, gid) @@ -205,7 +203,8 @@ def chown_r(path, uid, gid): os.chown(joinpths(root, f), uid, gid) LOG.debug("Changing ownership of %s to %s:%s" % (joinpths(root, f), uid, gid)) finally: - user_mode() + if run_as_root: + user_mode() def password(prompt_=None, pw_len=8): @@ -293,12 +292,12 @@ def deldir(path, run_as_root=False): try: if run_as_root: root_mode() - if isdir(path): LOG.debug("Recursively deleting directory tree starting at \"%s\"" % (path)) shutil.rmtree(path) finally: - user_mode() + if run_as_root: + user_mode() def rmdir(path, quiet=True, run_as_root=False): @@ -307,7 +306,6 @@ def rmdir(path, quiet=True, run_as_root=False): try: if run_as_root: root_mode() - LOG.debug("Deleting directory \"%s\" with the cavet that we will fail if it's not empty." % (path)) os.rmdir(path) LOG.debug("Deleted directory \"%s\"" % (path)) @@ -317,24 +315,23 @@ def rmdir(path, quiet=True, run_as_root=False): else: pass finally: - user_mode() + if run_as_root: + user_mode() def symlink(source, link, force=True, run_as_root=True): try: if run_as_root: root_mode() - path = dirname(link) mkdirslist(path) - LOG.debug("Creating symlink from %s => %s" % (link, source)) if force and exists(link): unlink(link, True) os.symlink(source, link) - finally: - user_mode() + if run_as_root: + user_mode() def exists(path): @@ -383,7 +380,8 @@ def getuid(username): def gethomedir(): - return pwd.getpwuid(geteuid())[5] + #TODO will just using os.path.expanduser("~") work?? + return pwd.getpwuid(geteuid()).pw_dir def getgid(groupname): @@ -469,7 +467,8 @@ def replace_in_file(fname, search, replace, run_as_root=False): line = line.replace(search, replace) print line, finally: - user_mode() + if run_as_root: + user_mode() def copy_replace_file(fsrc, fdst, map_): diff --git a/stack b/stack index 5efd57c9..6c2fb7fb 100755 --- a/stack +++ b/stack @@ -29,30 +29,42 @@ if(log_fn == None): log_fn = os.path.normpath(os.path.join("conf", 'logging.ini')) logging.config.fileConfig(log_fn) -#this handles our option parsing from devstack import opts -from devstack import utils +from devstack import settings from devstack import shell as sh +from devstack import utils #these are the program runtimes that actually do the running from devstack.progs import actions +#the user who's user id should be 0 +ROOT_USER = 'root' + def main(): - # will need root to setup openstack + #names! + prog_name = os.path.basename(sys.argv[0]) + pretty_name = settings.PROG_NICE_NAME + + #do this first so people can see the help message... + args = opts.parse() + + #will need root to setup openstack if not sh.got_root(): - print("%s needs to run as root" % os.path.basename(sys.argv[0])) + msg = "%s needs to be ran as %s!" % (pretty_name, ROOT_USER) + print(utils.color_text(msg, 'red', True)) + msg = "Perhaps you should try %s" % \ + (utils.color_text("sudo %s" % (prog_name), "red", True)) + print(msg) return 1 - # drop to usermode + #drop to usermode sh.user_mode() - - args = opts.parse() try: # now let's go started_ok = actions.run(args) if not started_ok: - me = utils.color_text((os.path.basename(sys.argv[0])), "red", True) + me = utils.color_text(prog_name, "red", True) me += " " + utils.color_text('--help', 'red') print("Perhaps you should try %s" % (me)) return 1