mirror of https://github.com/docker/docker-py.git
Make orchestrator field optional
Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
parent
105efa02a9
commit
31276df6a3
|
@ -14,11 +14,11 @@ class ContextAPI(object):
|
||||||
Contains methods for context management:
|
Contains methods for context management:
|
||||||
create, list, remove, get, inspect.
|
create, list, remove, get, inspect.
|
||||||
"""
|
"""
|
||||||
DEFAULT_CONTEXT = Context("default")
|
DEFAULT_CONTEXT = Context("default", "swarm")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create_context(
|
def create_context(
|
||||||
cls, name, orchestrator="swarm", host=None, tls_cfg=None,
|
cls, name, orchestrator=None, host=None, tls_cfg=None,
|
||||||
default_namespace=None, skip_tls_verify=False):
|
default_namespace=None, skip_tls_verify=False):
|
||||||
"""Creates a new context.
|
"""Creates a new context.
|
||||||
Returns:
|
Returns:
|
||||||
|
@ -38,9 +38,7 @@ class ContextAPI(object):
|
||||||
>>> print(ctx.Metadata)
|
>>> print(ctx.Metadata)
|
||||||
{
|
{
|
||||||
"Name": "test",
|
"Name": "test",
|
||||||
"Metadata": {
|
"Metadata": {},
|
||||||
"StackOrchestrator": "swarm"
|
|
||||||
},
|
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"docker": {
|
"docker": {
|
||||||
"Host": "unix:///var/run/docker.sock",
|
"Host": "unix:///var/run/docker.sock",
|
||||||
|
@ -57,7 +55,9 @@ 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)
|
||||||
endpoint = "docker" if orchestrator == "swarm" else orchestrator
|
endpoint = "docker"
|
||||||
|
if orchestrator and orchestrator != "swarm":
|
||||||
|
endpoint = orchestrator
|
||||||
ctx = Context(name, orchestrator)
|
ctx = Context(name, orchestrator)
|
||||||
ctx.set_endpoint(
|
ctx.set_endpoint(
|
||||||
endpoint, host, tls_cfg,
|
endpoint, host, tls_cfg,
|
||||||
|
@ -79,9 +79,7 @@ class ContextAPI(object):
|
||||||
>>> print(ctx.Metadata)
|
>>> print(ctx.Metadata)
|
||||||
{
|
{
|
||||||
"Name": "test",
|
"Name": "test",
|
||||||
"Metadata": {
|
"Metadata": {},
|
||||||
"StackOrchestrator": "swarm"
|
|
||||||
},
|
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"docker": {
|
"docker": {
|
||||||
"Host": "unix:///var/run/docker.sock",
|
"Host": "unix:///var/run/docker.sock",
|
||||||
|
|
|
@ -11,7 +11,7 @@ from docker.context.config import get_context_host
|
||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
"""A context."""
|
"""A context."""
|
||||||
def __init__(self, name, orchestrator="swarm", host=None, endpoints=None,
|
def __init__(self, name, orchestrator=None, host=None, endpoints=None,
|
||||||
tls=False):
|
tls=False):
|
||||||
if not name:
|
if not name:
|
||||||
raise Exception("Name not provided")
|
raise Exception("Name not provided")
|
||||||
|
@ -19,7 +19,7 @@ class Context:
|
||||||
self.orchestrator = orchestrator
|
self.orchestrator = orchestrator
|
||||||
if not endpoints:
|
if not endpoints:
|
||||||
default_endpoint = "docker" if (
|
default_endpoint = "docker" if (
|
||||||
orchestrator == "swarm"
|
not orchestrator or orchestrator == "swarm"
|
||||||
) else orchestrator
|
) else orchestrator
|
||||||
self.endpoints = {
|
self.endpoints = {
|
||||||
default_endpoint: {
|
default_endpoint: {
|
||||||
|
@ -85,7 +85,8 @@ class Context:
|
||||||
context {} : {}""".format(name, e))
|
context {} : {}""".format(name, e))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
metadata["Name"], metadata["Metadata"]["StackOrchestrator"],
|
metadata["Name"],
|
||||||
|
metadata["Metadata"].get("StackOrchestrator", None),
|
||||||
metadata["Endpoints"])
|
metadata["Endpoints"])
|
||||||
return None, None, None
|
return None, None, None
|
||||||
|
|
||||||
|
@ -162,7 +163,7 @@ class Context:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Host(self):
|
def Host(self):
|
||||||
if self.orchestrator == "swarm":
|
if not self.orchestrator or self.orchestrator == "swarm":
|
||||||
return self.endpoints["docker"]["Host"]
|
return self.endpoints["docker"]["Host"]
|
||||||
return self.endpoints[self.orchestrator]["Host"]
|
return self.endpoints[self.orchestrator]["Host"]
|
||||||
|
|
||||||
|
@ -172,18 +173,19 @@ class Context:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def Metadata(self):
|
def Metadata(self):
|
||||||
|
meta = {}
|
||||||
|
if self.orchestrator:
|
||||||
|
meta = {"StackOrchestrator": self.orchestrator}
|
||||||
return {
|
return {
|
||||||
"Name": self.name,
|
"Name": self.name,
|
||||||
"Metadata": {
|
"Metadata": meta,
|
||||||
"StackOrchestrator": self.orchestrator
|
|
||||||
},
|
|
||||||
"Endpoints": self.endpoints
|
"Endpoints": self.endpoints
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def TLSConfig(self):
|
def TLSConfig(self):
|
||||||
key = self.orchestrator
|
key = self.orchestrator
|
||||||
if key == "swarm":
|
if not key or key == "swarm":
|
||||||
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