mirror of https://github.com/docker/docker-py.git
				
				
				
			Skip parsing non-docker endpoints
Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
		
							parent
							
								
									5efe28149a
								
							
						
					
					
						commit
						309ce44052
					
				|  | @ -16,30 +16,42 @@ class Context: | ||||||
|         if not name: |         if not name: | ||||||
|             raise Exception("Name not provided") |             raise Exception("Name not provided") | ||||||
|         self.name = name |         self.name = name | ||||||
|  |         self.context_type = None | ||||||
|         self.orchestrator = orchestrator |         self.orchestrator = orchestrator | ||||||
|  |         self.endpoints = {} | ||||||
|  |         self.tls_cfg = {} | ||||||
|  |         self.meta_path = "IN MEMORY" | ||||||
|  |         self.tls_path = "IN MEMORY" | ||||||
|  | 
 | ||||||
|         if not endpoints: |         if not endpoints: | ||||||
|  |             # set default docker endpoint if no endpoint is set | ||||||
|             default_endpoint = "docker" if ( |             default_endpoint = "docker" if ( | ||||||
|                 not orchestrator or orchestrator == "swarm" |                 not orchestrator or orchestrator == "swarm" | ||||||
|                 ) else orchestrator |                 ) else orchestrator | ||||||
|  | 
 | ||||||
|             self.endpoints = { |             self.endpoints = { | ||||||
|                 default_endpoint: { |                 default_endpoint: { | ||||||
|                     "Host": get_context_host(host, tls), |                     "Host": get_context_host(host, tls), | ||||||
|                     "SkipTLSVerify": not tls |                     "SkipTLSVerify": not tls | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         else: |             return | ||||||
|             for k, v in endpoints.items(): |  | ||||||
|                 ekeys = v.keys() |  | ||||||
|                 for param in ["Host", "SkipTLSVerify"]: |  | ||||||
|                     if param not in ekeys: |  | ||||||
|                         raise ContextException( |  | ||||||
|                             "Missing parameter {} from endpoint {}".format( |  | ||||||
|                                 param, k)) |  | ||||||
|             self.endpoints = endpoints |  | ||||||
| 
 | 
 | ||||||
|         self.tls_cfg = {} |         # check docker endpoints | ||||||
|         self.meta_path = "IN MEMORY" |         for k, v in endpoints.items(): | ||||||
|         self.tls_path = "IN MEMORY" |             if not isinstance(v, dict): | ||||||
|  |                 # unknown format | ||||||
|  |                 raise ContextException("""Unknown endpoint format for | ||||||
|  |                     context {}: {}""".format(name, v)) | ||||||
|  | 
 | ||||||
|  |             self.endpoints[k] = v | ||||||
|  |             if k != "docker": | ||||||
|  |                 continue | ||||||
|  | 
 | ||||||
|  |             self.endpoints[k]["Host"] = v.get("Host", get_context_host( | ||||||
|  |                 host, tls)) | ||||||
|  |             self.endpoints[k]["SkipTLSVerify"] = bool(v.get( | ||||||
|  |                 "SkipTLSVerify", not tls)) | ||||||
| 
 | 
 | ||||||
|     def set_endpoint( |     def set_endpoint( | ||||||
|             self, name="docker", host=None, tls_cfg=None, |             self, name="docker", host=None, tls_cfg=None, | ||||||
|  | @ -59,9 +71,13 @@ class Context: | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def load_context(cls, name): |     def load_context(cls, name): | ||||||
|         name, orchestrator, endpoints = Context._load_meta(name) |         meta = Context._load_meta(name) | ||||||
|         if name: |         if meta: | ||||||
|             instance = cls(name, orchestrator, endpoints=endpoints) |             instance = cls( | ||||||
|  |                 meta["Name"], | ||||||
|  |                 orchestrator=meta["Metadata"].get("StackOrchestrator", None), | ||||||
|  |                 endpoints=meta.get("Endpoints", None)) | ||||||
|  |             instance.context_type = meta["Metadata"].get("Type", None) | ||||||
|             instance._load_certs() |             instance._load_certs() | ||||||
|             instance.meta_path = get_meta_dir(name) |             instance.meta_path = get_meta_dir(name) | ||||||
|             return instance |             return instance | ||||||
|  | @ -69,26 +85,30 @@ class Context: | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def _load_meta(cls, name): |     def _load_meta(cls, name): | ||||||
|         metadata = {} |  | ||||||
|         meta_file = get_meta_file(name) |         meta_file = get_meta_file(name) | ||||||
|         if os.path.isfile(meta_file): |         if not os.path.isfile(meta_file): | ||||||
|             with open(meta_file) as f: |             return None | ||||||
|                 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 = {} | ||||||
|                 metadata["Name"], |         try: | ||||||
|                 metadata["Metadata"].get("StackOrchestrator", None), |             with open(meta_file) as f: | ||||||
|                 metadata["Endpoints"]) |                 metadata = json.load(f) | ||||||
|         return None, None, None |         except (IOError, KeyError, ValueError) as e: | ||||||
|  |             # unknown format | ||||||
|  |             raise Exception("""Detected corrupted meta file for | ||||||
|  |                 context {} : {}""".format(name, e)) | ||||||
|  | 
 | ||||||
|  |         # for docker endpoints, set defaults for | ||||||
|  |         # Host and SkipTLSVerify fields | ||||||
|  |         for k, v in metadata["Endpoints"].items(): | ||||||
|  |             if k != "docker": | ||||||
|  |                 continue | ||||||
|  |             metadata["Endpoints"][k]["Host"] = v.get( | ||||||
|  |                 "Host", get_context_host(None, False)) | ||||||
|  |             metadata["Endpoints"][k]["SkipTLSVerify"] = bool( | ||||||
|  |                 v.get("SkipTLSVerify", True)) | ||||||
|  | 
 | ||||||
|  |         return metadata | ||||||
| 
 | 
 | ||||||
|     def _load_certs(self): |     def _load_certs(self): | ||||||
|         certs = {} |         certs = {} | ||||||
|  | @ -157,6 +177,9 @@ class Context: | ||||||
|         result.update(self.Storage) |         result.update(self.Storage) | ||||||
|         return result |         return result | ||||||
| 
 | 
 | ||||||
|  |     def is_docker_host(self): | ||||||
|  |         return self.context_type is None | ||||||
|  | 
 | ||||||
|     @property |     @property | ||||||
|     def Name(self): |     def Name(self): | ||||||
|         return self.name |         return self.name | ||||||
|  | @ -164,8 +187,12 @@ class Context: | ||||||
|     @property |     @property | ||||||
|     def Host(self): |     def Host(self): | ||||||
|         if not self.orchestrator or self.orchestrator == "swarm": |         if not self.orchestrator or self.orchestrator == "swarm": | ||||||
|             return self.endpoints["docker"]["Host"] |             endpoint = self.endpoints.get("docker", None) | ||||||
|         return self.endpoints[self.orchestrator]["Host"] |             if endpoint: | ||||||
|  |                 return endpoint.get("Host", None) | ||||||
|  |             return None | ||||||
|  | 
 | ||||||
|  |         return self.endpoints[self.orchestrator].get("Host", None) | ||||||
| 
 | 
 | ||||||
|     @property |     @property | ||||||
|     def Orchestrator(self): |     def Orchestrator(self): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue