Skip to content

Commit

Permalink
Fix parsing of context file paths
Browse files Browse the repository at this point in the history
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
  • Loading branch information
aiordache committed Feb 4, 2020
1 parent 10a9437 commit 6ce8d73
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
14 changes: 7 additions & 7 deletions docker/context/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

from docker import errors
from docker.context.config import METADATA_DIR
from docker.context.config import get_meta_dir
from docker.context.config import METAFILE
from docker.context.config import get_current_context_name
from docker.context.config import write_context_name_to_docker_config
Expand Down Expand Up @@ -93,7 +93,7 @@ def get_context(cls, name=None):
if not name:
name = get_current_context_name()
if name == "default":
return ContextAPI.DEFAULT_CONTEXT
return cls.DEFAULT_CONTEXT
return Context.load_context(name)

@classmethod
Expand All @@ -106,7 +106,7 @@ def contexts(cls):
If the server returns an error.
"""
names = []
for dirname, dirnames, fnames in os.walk(METADATA_DIR):
for dirname, dirnames, fnames in os.walk(get_meta_dir()):
for filename in fnames + dirnames:
if filename == METAFILE:
try:
Expand All @@ -118,7 +118,7 @@ def contexts(cls):
"Failed to load metafile {}: {}".format(
filename, e))

contexts = [ContextAPI.DEFAULT_CONTEXT]
contexts = [cls.DEFAULT_CONTEXT]
for name in names:
contexts.append(Context.load_context(name))
return contexts
Expand All @@ -129,11 +129,11 @@ def get_current_context(cls):
Returns:
(Context): current context object.
"""
return ContextAPI.get_context()
return cls.get_context()

@classmethod
def set_current_context(cls, name="default"):
ctx = ContextAPI.get_context(name)
ctx = cls.get_context(name)
if not ctx:
raise errors.ContextNotFound(name)

Expand Down Expand Up @@ -197,7 +197,7 @@ def inspect_context(cls, name="default"):
if not name:
raise errors.MissingContextParameter("name")
if name == "default":
return ContextAPI.DEFAULT_CONTEXT()
return cls.DEFAULT_CONTEXT()
ctx = Context.load_context(name)
if not ctx:
raise errors.ContextNotFound(name)
Expand Down
24 changes: 15 additions & 9 deletions docker/context/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
from docker.constants import DEFAULT_UNIX_SOCKET
from docker.utils.config import find_config_file

CONTEXTS_DIR = os.path.join(os.path.dirname(
find_config_file() or ""), "contexts")
METADATA_DIR = os.path.join(CONTEXTS_DIR, "meta")
TLS_DIR = os.path.join(CONTEXTS_DIR, "tls")
METAFILE = "meta.json"


Expand All @@ -20,7 +16,7 @@ def get_current_context_name():
if docker_cfg_path:
try:
cfg = open(docker_cfg_path, "r")
name = json.load(cfg).get("currentContext")
name = json.load(cfg).get("currentContext", "default")
except Exception:
return "default"
return name
Expand Down Expand Up @@ -53,16 +49,26 @@ def get_context_id(name):
return hashlib.sha256(name.encode('utf-8')).hexdigest()


def get_meta_dir(name):
return os.path.join(METADATA_DIR, get_context_id(name))
def get_context_dir():
return os.path.join(os.path.dirname(find_config_file() or ""), "contexts")


def get_meta_dir(name=None):
meta_dir = os.path.join(get_context_dir(), "meta")
if name:
return os.path.join(meta_dir, get_context_id(name))
return meta_dir


def get_meta_file(name):
return os.path.join(get_meta_dir(name), METAFILE)


def get_tls_dir(name, endpoint=""):
return os.path.join(TLS_DIR, get_context_id(name), endpoint)
def get_tls_dir(name=None, endpoint=""):
context_dir = get_context_dir()
if name:
return os.path.join(context_dir, "tls", get_context_id(name), endpoint)
return os.path.join(context_dir, "tls")


def get_context_host(path=None):
Expand Down

0 comments on commit 6ce8d73

Please sign in to comment.