mirror of https://github.com/docker/docker-py.git
Implement context management, lifecycle and unittests.
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
This commit is contained in:
parent
fcd0093050
commit
087b3f0a49
|
|
@ -38,7 +38,13 @@ class ContextAPI(object):
|
||||||
>>> print(ctx.Metadata)
|
>>> print(ctx.Metadata)
|
||||||
{
|
{
|
||||||
"Name": "test",
|
"Name": "test",
|
||||||
|
<<<<<<< HEAD
|
||||||
"Metadata": {},
|
"Metadata": {},
|
||||||
|
=======
|
||||||
|
"Metadata": {
|
||||||
|
"StackOrchestrator": "swarm"
|
||||||
|
},
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"docker": {
|
"docker": {
|
||||||
"Host": "unix:///var/run/docker.sock",
|
"Host": "unix:///var/run/docker.sock",
|
||||||
|
|
@ -55,9 +61,13 @@ class ContextAPI(object):
|
||||||
ctx = Context.load_context(name)
|
ctx = Context.load_context(name)
|
||||||
if ctx:
|
if ctx:
|
||||||
raise errors.ContextAlreadyExists(name)
|
raise errors.ContextAlreadyExists(name)
|
||||||
|
<<<<<<< HEAD
|
||||||
endpoint = "docker"
|
endpoint = "docker"
|
||||||
if orchestrator and orchestrator != "swarm":
|
if orchestrator and orchestrator != "swarm":
|
||||||
endpoint = orchestrator
|
endpoint = orchestrator
|
||||||
|
=======
|
||||||
|
endpoint = "docker" if orchestrator == "swarm" else orchestrator
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
ctx = Context(name, orchestrator)
|
ctx = Context(name, orchestrator)
|
||||||
ctx.set_endpoint(
|
ctx.set_endpoint(
|
||||||
endpoint, host, tls_cfg,
|
endpoint, host, tls_cfg,
|
||||||
|
|
@ -79,7 +89,13 @@ class ContextAPI(object):
|
||||||
>>> print(ctx.Metadata)
|
>>> print(ctx.Metadata)
|
||||||
{
|
{
|
||||||
"Name": "test",
|
"Name": "test",
|
||||||
|
<<<<<<< HEAD
|
||||||
"Metadata": {},
|
"Metadata": {},
|
||||||
|
=======
|
||||||
|
"Metadata": {
|
||||||
|
"StackOrchestrator": "swarm"
|
||||||
|
},
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"docker": {
|
"docker": {
|
||||||
"Host": "unix:///var/run/docker.sock",
|
"Host": "unix:///var/run/docker.sock",
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,11 @@ class Context:
|
||||||
self, name="docker", host=None, tls_cfg=None,
|
self, name="docker", host=None, tls_cfg=None,
|
||||||
skip_tls_verify=False, def_namespace=None):
|
skip_tls_verify=False, def_namespace=None):
|
||||||
self.endpoints[name] = {
|
self.endpoints[name] = {
|
||||||
|
<<<<<<< HEAD
|
||||||
"Host": get_context_host(host, not skip_tls_verify),
|
"Host": get_context_host(host, not skip_tls_verify),
|
||||||
|
=======
|
||||||
|
"Host": get_context_host(host),
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
"SkipTLSVerify": skip_tls_verify
|
"SkipTLSVerify": skip_tls_verify
|
||||||
}
|
}
|
||||||
if def_namespace:
|
if def_namespace:
|
||||||
|
|
@ -71,6 +75,7 @@ class Context:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load_context(cls, name):
|
def load_context(cls, name):
|
||||||
|
<<<<<<< HEAD
|
||||||
meta = Context._load_meta(name)
|
meta = Context._load_meta(name)
|
||||||
if meta:
|
if meta:
|
||||||
instance = cls(
|
instance = cls(
|
||||||
|
|
@ -78,6 +83,11 @@ class Context:
|
||||||
orchestrator=meta["Metadata"].get("StackOrchestrator", None),
|
orchestrator=meta["Metadata"].get("StackOrchestrator", None),
|
||||||
endpoints=meta.get("Endpoints", None))
|
endpoints=meta.get("Endpoints", None))
|
||||||
instance.context_type = meta["Metadata"].get("Type", None)
|
instance.context_type = meta["Metadata"].get("Type", None)
|
||||||
|
=======
|
||||||
|
name, orchestrator, endpoints = Context._load_meta(name)
|
||||||
|
if name:
|
||||||
|
instance = cls(name, orchestrator, endpoints=endpoints)
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
instance._load_certs()
|
instance._load_certs()
|
||||||
instance.meta_path = get_meta_dir(name)
|
instance.meta_path = get_meta_dir(name)
|
||||||
return instance
|
return instance
|
||||||
|
|
@ -85,6 +95,7 @@ class Context:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _load_meta(cls, name):
|
def _load_meta(cls, name):
|
||||||
|
<<<<<<< HEAD
|
||||||
meta_file = get_meta_file(name)
|
meta_file = get_meta_file(name)
|
||||||
if not os.path.isfile(meta_file):
|
if not os.path.isfile(meta_file):
|
||||||
return None
|
return None
|
||||||
|
|
@ -109,6 +120,27 @@ class Context:
|
||||||
v.get("SkipTLSVerify", True))
|
v.get("SkipTLSVerify", True))
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
|
=======
|
||||||
|
metadata = {}
|
||||||
|
meta_file = get_meta_file(name)
|
||||||
|
if os.path.isfile(meta_file):
|
||||||
|
with open(meta_file) as f:
|
||||||
|
try:
|
||||||
|
with open(meta_file) as f:
|
||||||
|
metadata = json.load(f)
|
||||||
|
for k, v in metadata["Endpoints"].items():
|
||||||
|
metadata["Endpoints"][k]["SkipTLSVerify"] = bool(
|
||||||
|
v["SkipTLSVerify"])
|
||||||
|
except (IOError, KeyError, ValueError) as e:
|
||||||
|
# unknown format
|
||||||
|
raise Exception("""Detected corrupted meta file for
|
||||||
|
context {} : {}""".format(name, e))
|
||||||
|
|
||||||
|
return (
|
||||||
|
metadata["Name"], metadata["Metadata"]["StackOrchestrator"],
|
||||||
|
metadata["Endpoints"])
|
||||||
|
return None, None, None
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
|
|
||||||
def _load_certs(self):
|
def _load_certs(self):
|
||||||
certs = {}
|
certs = {}
|
||||||
|
|
@ -177,15 +209,19 @@ class Context:
|
||||||
result.update(self.Storage)
|
result.update(self.Storage)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
<<<<<<< HEAD
|
||||||
def is_docker_host(self):
|
def is_docker_host(self):
|
||||||
return self.context_type is None
|
return self.context_type is None
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
@property
|
@property
|
||||||
def Name(self):
|
def Name(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Host(self):
|
def Host(self):
|
||||||
|
<<<<<<< HEAD
|
||||||
if not self.orchestrator or self.orchestrator == "swarm":
|
if not self.orchestrator or self.orchestrator == "swarm":
|
||||||
endpoint = self.endpoints.get("docker", None)
|
endpoint = self.endpoints.get("docker", None)
|
||||||
if endpoint:
|
if endpoint:
|
||||||
|
|
@ -193,6 +229,11 @@ class Context:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return self.endpoints[self.orchestrator].get("Host", None)
|
return self.endpoints[self.orchestrator].get("Host", None)
|
||||||
|
=======
|
||||||
|
if self.orchestrator == "swarm":
|
||||||
|
return self.endpoints["docker"]["Host"]
|
||||||
|
return self.endpoints[self.orchestrator]["Host"]
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Orchestrator(self):
|
def Orchestrator(self):
|
||||||
|
|
@ -200,19 +241,31 @@ class Context:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Metadata(self):
|
def Metadata(self):
|
||||||
|
<<<<<<< HEAD
|
||||||
meta = {}
|
meta = {}
|
||||||
if self.orchestrator:
|
if self.orchestrator:
|
||||||
meta = {"StackOrchestrator": self.orchestrator}
|
meta = {"StackOrchestrator": self.orchestrator}
|
||||||
return {
|
return {
|
||||||
"Name": self.name,
|
"Name": self.name,
|
||||||
"Metadata": meta,
|
"Metadata": meta,
|
||||||
|
=======
|
||||||
|
return {
|
||||||
|
"Name": self.name,
|
||||||
|
"Metadata": {
|
||||||
|
"StackOrchestrator": self.orchestrator
|
||||||
|
},
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
"Endpoints": self.endpoints
|
"Endpoints": self.endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def TLSConfig(self):
|
def TLSConfig(self):
|
||||||
key = self.orchestrator
|
key = self.orchestrator
|
||||||
|
<<<<<<< HEAD
|
||||||
if not key or key == "swarm":
|
if not key or key == "swarm":
|
||||||
|
=======
|
||||||
|
if key == "swarm":
|
||||||
|
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
|
||||||
key = "docker"
|
key = "docker"
|
||||||
if key in self.tls_cfg.keys():
|
if key in self.tls_cfg.keys():
|
||||||
return self.tls_cfg[key]
|
return self.tls_cfg[key]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue