Skip to content

Commit 087b3f0

Browse files
author
Anca Iordache
committed
Implement context management, lifecycle and unittests.
Signed-off-by: Anca Iordache <[email protected]>
1 parent fcd0093 commit 087b3f0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

docker/context/api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ def create_context(
3838
>>> print(ctx.Metadata)
3939
{
4040
"Name": "test",
41+
<<<<<<< HEAD
4142
"Metadata": {},
43+
=======
44+
"Metadata": {
45+
"StackOrchestrator": "swarm"
46+
},
47+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
4248
"Endpoints": {
4349
"docker": {
4450
"Host": "unix:///var/run/docker.sock",
@@ -55,9 +61,13 @@ def create_context(
5561
ctx = Context.load_context(name)
5662
if ctx:
5763
raise errors.ContextAlreadyExists(name)
64+
<<<<<<< HEAD
5865
endpoint = "docker"
5966
if orchestrator and orchestrator != "swarm":
6067
endpoint = orchestrator
68+
=======
69+
endpoint = "docker" if orchestrator == "swarm" else orchestrator
70+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
6171
ctx = Context(name, orchestrator)
6272
ctx.set_endpoint(
6373
endpoint, host, tls_cfg,
@@ -79,7 +89,13 @@ def get_context(cls, name=None):
7989
>>> print(ctx.Metadata)
8090
{
8191
"Name": "test",
92+
<<<<<<< HEAD
8293
"Metadata": {},
94+
=======
95+
"Metadata": {
96+
"StackOrchestrator": "swarm"
97+
},
98+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
8399
"Endpoints": {
84100
"docker": {
85101
"Host": "unix:///var/run/docker.sock",

docker/context/context.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ def set_endpoint(
5757
self, name="docker", host=None, tls_cfg=None,
5858
skip_tls_verify=False, def_namespace=None):
5959
self.endpoints[name] = {
60+
<<<<<<< HEAD
6061
"Host": get_context_host(host, not skip_tls_verify),
62+
=======
63+
"Host": get_context_host(host),
64+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
6165
"SkipTLSVerify": skip_tls_verify
6266
}
6367
if def_namespace:
@@ -71,20 +75,27 @@ def inspect(self):
7175

7276
@classmethod
7377
def load_context(cls, name):
78+
<<<<<<< HEAD
7479
meta = Context._load_meta(name)
7580
if meta:
7681
instance = cls(
7782
meta["Name"],
7883
orchestrator=meta["Metadata"].get("StackOrchestrator", None),
7984
endpoints=meta.get("Endpoints", None))
8085
instance.context_type = meta["Metadata"].get("Type", None)
86+
=======
87+
name, orchestrator, endpoints = Context._load_meta(name)
88+
if name:
89+
instance = cls(name, orchestrator, endpoints=endpoints)
90+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
8191
instance._load_certs()
8292
instance.meta_path = get_meta_dir(name)
8393
return instance
8494
return None
8595

8696
@classmethod
8797
def _load_meta(cls, name):
98+
<<<<<<< HEAD
8899
meta_file = get_meta_file(name)
89100
if not os.path.isfile(meta_file):
90101
return None
@@ -109,6 +120,27 @@ def _load_meta(cls, name):
109120
v.get("SkipTLSVerify", True))
110121

111122
return metadata
123+
=======
124+
metadata = {}
125+
meta_file = get_meta_file(name)
126+
if os.path.isfile(meta_file):
127+
with open(meta_file) as f:
128+
try:
129+
with open(meta_file) as f:
130+
metadata = json.load(f)
131+
for k, v in metadata["Endpoints"].items():
132+
metadata["Endpoints"][k]["SkipTLSVerify"] = bool(
133+
v["SkipTLSVerify"])
134+
except (IOError, KeyError, ValueError) as e:
135+
# unknown format
136+
raise Exception("""Detected corrupted meta file for
137+
context {} : {}""".format(name, e))
138+
139+
return (
140+
metadata["Name"], metadata["Metadata"]["StackOrchestrator"],
141+
metadata["Endpoints"])
142+
return None, None, None
143+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
112144

113145
def _load_certs(self):
114146
certs = {}
@@ -177,42 +209,63 @@ def __call__(self):
177209
result.update(self.Storage)
178210
return result
179211

212+
<<<<<<< HEAD
180213
def is_docker_host(self):
181214
return self.context_type is None
182215

216+
=======
217+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
183218
@property
184219
def Name(self):
185220
return self.name
186221

187222
@property
188223
def Host(self):
224+
<<<<<<< HEAD
189225
if not self.orchestrator or self.orchestrator == "swarm":
190226
endpoint = self.endpoints.get("docker", None)
191227
if endpoint:
192228
return endpoint.get("Host", None)
193229
return None
194230

195231
return self.endpoints[self.orchestrator].get("Host", None)
232+
=======
233+
if self.orchestrator == "swarm":
234+
return self.endpoints["docker"]["Host"]
235+
return self.endpoints[self.orchestrator]["Host"]
236+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
196237

197238
@property
198239
def Orchestrator(self):
199240
return self.orchestrator
200241

201242
@property
202243
def Metadata(self):
244+
<<<<<<< HEAD
203245
meta = {}
204246
if self.orchestrator:
205247
meta = {"StackOrchestrator": self.orchestrator}
206248
return {
207249
"Name": self.name,
208250
"Metadata": meta,
251+
=======
252+
return {
253+
"Name": self.name,
254+
"Metadata": {
255+
"StackOrchestrator": self.orchestrator
256+
},
257+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
209258
"Endpoints": self.endpoints
210259
}
211260

212261
@property
213262
def TLSConfig(self):
214263
key = self.orchestrator
264+
<<<<<<< HEAD
215265
if not key or key == "swarm":
266+
=======
267+
if key == "swarm":
268+
>>>>>>> 64fdb32... Implement context management, lifecycle and unittests.
216269
key = "docker"
217270
if key in self.tls_cfg.keys():
218271
return self.tls_cfg[key]

0 commit comments

Comments
 (0)