Skip to content

Commit

Permalink
Linter fixes
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 Jan 21, 2020
1 parent db93df9 commit d92ab26
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 143 deletions.
3 changes: 2 additions & 1 deletion docker/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys, os
import sys
import os
from .version import version

DEFAULT_DOCKER_API_VERSION = '1.35'
Expand Down
3 changes: 2 additions & 1 deletion docker/context/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# flake8: noqa
from .context import Context
from .api import ContextAPI
from .api import ContextAPI
97 changes: 51 additions & 46 deletions docker/context/api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import os, json
from .. import errors
from .config import METADATA_DIR, METAFILE
from .context import Context
import json
import os

from docker import errors
from docker.context.config import METADATA_DIR, METAFILE
from docker.context import Context


class ContextAPI(object):
"""Context API.
Contains methods for context management: create, list, remove, get, inspect.
Contains methods for context management:
create, list, remove, get, inspect.
"""
DEFAULT_CONTEXT = Context("default")

@classmethod
def create_context(cls, name, orchestrator = "swarm", endpoint = None, host = None, tls_cfg = None, default_namespace = None, skip_tls_verify = False):
"""Creates a new context.
def create_context(
cls, name, orchestrator="swarm", endpoint=None, host=None,
tls_cfg=None, default_namespace=None, skip_tls_verify=False):
"""Creates a new context.
Returns:
(Context): a Context object.
Raises:
Expand All @@ -24,9 +30,9 @@ def create_context(cls, name, orchestrator = "swarm", endpoint = None, host = No
Example:
>>> import docker
>>> ctx = docker.ContextAPI.create_context(name='test')
>>> print(ctx)
>>> from docker.context import ContextAPI
>>> ctx = ContextAPI.create_context(name='test')
>>> print(ctx.Metadata)
{
"Name": "test",
"Metadata": {
Expand All @@ -37,26 +43,25 @@ def create_context(cls, name, orchestrator = "swarm", endpoint = None, host = No
"Host": "unix:///var/run/docker.sock",
"SkipTLSVerify": false
}
},
"TLSMaterial": {},
"Storage": {
"MetadataPath": "IN MEMORY",
"TLSPath": "IN MEMORY"
}
}
"""
if not name:
raise errors.MissingContextParameter("name")
if name == "default":
raise errors.ContextException('"default" is a reserved context name')
raise errors.ContextException(
'"default" is a reserved context name')
# load the context to check if it already exists
ctx = Context.load_context(name)
if ctx:
raise errors.ContextAlreadyExists(name)

ctx = Context(name, orchestrator)
if endpoint:
ctx.set_endpoint(endpoint, host, tls_cfg, skip_tls_verify = skip_tls_verify, def_namespace=default_namespace)
ctx.set_endpoint(
endpoint, host, tls_cfg,
skip_tls_verify=skip_tls_verify,
def_namespace=default_namespace)
ctx.save()
return ctx

Expand All @@ -69,24 +74,25 @@ def contexts(self):
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
#load from local disk
# load from local disk
names = []
for dirname, dirnames, fnames in os.walk(METADATA_DIR):
for filename in fnames + dirnames:
if filename == METAFILE:
try:
data = json.load(open(os.path.join(dirname, filename), "r"))
data = json.load(
open(os.path.join(dirname, filename), "r"))
names.append(data["Name"])
except:
raise errors.ContextException("Failed to load metafile {}".format(filename))

except Exception as e:
raise errors.ContextException(
"Failed to load metafile {}: {}".format(
filename, e))

contexts = [ContextAPI.DEFAULT_CONTEXT]
for name in names:
contexts.append(Context.load_context(name))
contexts.append(Context.load_context(name))
return contexts



@classmethod
def remove_context(cls, name):
"""Remove a context. Similar to the ``docker context rm`` command.
Expand All @@ -104,22 +110,24 @@ def remove_context(cls, name):
Example:
>>> import docker
>>> docker.ContextAPI.remove_context(name='test')
>>>
>>> from docker.context import ContextAPI
>>> ContextAPI.remove_context(name='test')
>>>
"""
if not name:
raise errors.MissingContextParameter("name")
if name == "default": raise errors.ContextException('context "default" cannot be removed')
if name == "default":
raise errors.ContextException(
'context "default" cannot be removed')
# load the context to check if it already exists
ctx = Context.load_context(name)
if not ctx:
raise errors.ContextNotFound(name)

ctx.cleanup()

@classmethod
def get_context(cls, name = "default"):
def get_context(cls, name="default"):
"""Retrieves a context object.
Args:
name (str): The name of the context
Expand All @@ -130,9 +138,9 @@ def get_context(cls, name = "default"):
Example:
>>> import docker
>>> ctx = docker.ContextAPI.get_context(name='test')
>>> print(ctx)
>>> from docker.context import ContextAPI
>>> ctx = ContextAPI..get_context(name='test')
>>> print(ctx.Metadata)
{
"Name": "test",
"Metadata": {
Expand All @@ -143,23 +151,19 @@ def get_context(cls, name = "default"):
"Host": "unix:///var/run/docker.sock",
"SkipTLSVerify": false
}
},
"TLSMaterial": {},
"Storage": {
"MetadataPath": "/home/user/.docker/contexts/meta/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
"TLSPath": "/home/user/.docker/contexts/tls/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08/"
}
}
"""
if not name:
raise errors.MissingContextParameter("name")
if name == "default": return ContextAPI.DEFAULT_CONTEXT
if name == "default":
return ContextAPI.DEFAULT_CONTEXT
# load the context to check if it already exists
ctx = Context.load_context(name)
return ctx

@classmethod
def inspect_context(cls, name = "default"):
def inspect_context(cls, name="default"):
"""Remove a context. Similar to the ``docker context inspect`` command.
Args:
Expand All @@ -173,16 +177,17 @@ def inspect_context(cls, name = "default"):
Example:
>>> import docker
>>> docker.ContextAPI.remove_context(name='test')
>>> from docker.context import ContextAPI
>>> ContextAPI.remove_context(name='test')
>>>
"""
if not name:
raise errors.MissingContextParameter("name")
if name == "default": return ContextAPI.DEFAULT_CONTEXT()
if name == "default":
return ContextAPI.DEFAULT_CONTEXT()
# load the context to check if it already exists
ctx = Context.load_context(name)
if not ctx:
raise errors.ContextNotFound(name)

return ctx()
return ctx()
27 changes: 19 additions & 8 deletions docker/context/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import os, hashlib, base64
import os
import hashlib
from .. import utils

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

get_context_id = lambda name: hashlib.sha256(name.encode('utf-8')).hexdigest()
get_meta_dir = lambda name: os.path.join(METADATA_DIR, get_context_id(name))
get_meta_file = lambda name: os.path.join(get_meta_dir(name), METAFILE)
get_tls_dir = lambda name, endpoint = "": os.path.join(TLS_DIR, get_context_id(name), endpoint)

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_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)

0 comments on commit d92ab26

Please sign in to comment.