From 001b3e40f297c637dde4aeee96aa387f413de7d5 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Wed, 20 Apr 2016 05:56:36 -0400 Subject: [PATCH] Abstracted the way we get absolute path Added a staticmethod in utils.Utils named get_real_abspath which abstracts the use of os.path.join(Utils.getRoot, path) --- atomicapp/cli/main.py | 2 +- atomicapp/nulecule/main.py | 9 +++------ atomicapp/plugin.py | 3 +-- atomicapp/providers/kubernetes.py | 2 +- atomicapp/providers/openshift.py | 3 +-- atomicapp/utils.py | 15 +++++++++++++++ 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/atomicapp/cli/main.py b/atomicapp/cli/main.py index 90460204..964d3b83 100644 --- a/atomicapp/cli/main.py +++ b/atomicapp/cli/main.py @@ -474,7 +474,7 @@ def run(self): if hasattr(args, item) and getattr(args, item) is not None: args.cli_answers[item] = getattr(args, item) - lock = LockFile(os.path.join(Utils.getRoot(), LOCK_FILE)) + lock = LockFile(Utils.get_real_abspath(LOCK_FILE)) try: if args.action != 'init': lock.acquire(timeout=-1) diff --git a/atomicapp/nulecule/main.py b/atomicapp/nulecule/main.py index cb861429..a9bc9509 100644 --- a/atomicapp/nulecule/main.py +++ b/atomicapp/nulecule/main.py @@ -74,14 +74,11 @@ def __init__(self, app_spec, destination=None, # Adjust app_spec, destination, and answer file paths if absolute. if os.path.isabs(app_spec): - app_spec = os.path.join(Utils.getRoot(), - app_spec.lstrip('/')) + app_spec = Utils.get_real_abspath(app_spec) if destination and os.path.isabs(destination): - destination = os.path.join(Utils.getRoot(), - destination.lstrip('/')) + destination = Utils.get_real_abspath(destination) if answers_file and os.path.isabs(answers_file): - answers_file = os.path.join(Utils.getRoot(), - answers_file.lstrip('/')) + answers_file = Utils.get_real_abspath(answers_file) # If the user doesn't want the files copied to a permanent # location then he provides 'none'. If that is the case we'll diff --git a/atomicapp/plugin.py b/atomicapp/plugin.py index ae44943a..482e86ce 100644 --- a/atomicapp/plugin.py +++ b/atomicapp/plugin.py @@ -75,8 +75,7 @@ def getConfigFile(self): if PROVIDER_CONFIG_KEY in self.config: self.config_file = self.config[PROVIDER_CONFIG_KEY] if os.path.isabs(self.config_file): - self.config_file = os.path.join(Utils.getRoot(), - self.config_file.lstrip('/')) + self.config_file = Utils.get_real_abspath(self.config_file) else: logger.warning("Configuration option '%s' not found" % PROVIDER_CONFIG_KEY) diff --git a/atomicapp/providers/kubernetes.py b/atomicapp/providers/kubernetes.py index 50e32cdd..5981ccfe 100644 --- a/atomicapp/providers/kubernetes.py +++ b/atomicapp/providers/kubernetes.py @@ -55,7 +55,7 @@ def init(self): if self.container: self.kubectl = self._find_kubectl(Utils.getRoot()) kube_conf_path = "/etc/kubernetes" - host_kube_conf_path = os.path.join(Utils.getRoot(), kube_conf_path.lstrip("/")) + host_kube_conf_path = Utils.get_real_abspath(kube_conf_path) if not os.path.exists(kube_conf_path) and os.path.exists(host_kube_conf_path): if self.dryrun: logger.info("DRY-RUN: link %s from %s" % (kube_conf_path, host_kube_conf_path)) diff --git a/atomicapp/providers/openshift.py b/atomicapp/providers/openshift.py index a31726e6..cb20f26a 100644 --- a/atomicapp/providers/openshift.py +++ b/atomicapp/providers/openshift.py @@ -680,8 +680,7 @@ def _set_config_values(self): self.provider_tls_verify = result[PROVIDER_TLS_VERIFY_KEY] if result[PROVIDER_CA_KEY]: # if we are in container translate path to path on host - self.provider_ca = os.path.join(Utils.getRoot(), - result[PROVIDER_CA_KEY].lstrip('/')) + self.provider_ca = Utils.get_real_abspath(result[PROVIDER_CA_KEY]) else: self.provider_ca = None diff --git a/atomicapp/utils.py b/atomicapp/utils.py index e8917d18..5e0456cc 100644 --- a/atomicapp/utils.py +++ b/atomicapp/utils.py @@ -354,6 +354,21 @@ def getRoot(): else: return "/" + @staticmethod + def get_real_abspath(path): + """ + Take the user provided 'path' and return the real path to the resource + irrespective of the app running location either inside container or + outside. + + Args: + path (str): path to a resource + + Returns: + str: absolute path to resource in the filesystem. + """ + return os.path.join(Utils.getRoot(), path.lstrip('/')) + # generates a unique 12 character UUID @staticmethod def getUniqueUUID():