Exemplo n.º 1
0
 def connect_container_to_network(self,
                                  network_name: str,
                                  container_name_or_id: str,
                                  aliases: Optional[List] = None) -> None:
     LOG.debug(
         "Connecting container '%s' to network '%s' with aliases '%s'",
         container_name_or_id,
         network_name,
         aliases,
     )
     cmd = self._docker_cmd()
     cmd += ["network", "connect"]
     if aliases:
         cmd += ["--alias", ",".join(aliases)]
     cmd += [network_name, container_name_or_id]
     try:
         run(cmd)
     except subprocess.CalledProcessError as e:
         stdout_str = to_str(e.stdout)
         if re.match(r".*network (.*) not found.*", stdout_str):
             raise NoSuchNetwork(network_name=network_name)
         elif "No such container" in stdout_str:
             raise NoSuchContainer(container_name_or_id,
                                   stdout=e.stdout,
                                   stderr=e.stderr)
         else:
             raise ContainerException(
                 "Docker process returned with errorcode %s" % e.returncode,
                 e.stdout, e.stderr)
Exemplo n.º 2
0
 def inspect_network(self, network_name: str) -> Dict[str, Union[Dict, str]]:
     try:
         return self.client().networks.get(network_name).attrs
     except NotFound:
         raise NoSuchNetwork(network_name)
     except APIError as e:
         raise ContainerException() from e
Exemplo n.º 3
0
 def disconnect_container_from_network(self, network_name: str,
                                       container_name_or_id: str) -> None:
     LOG.debug("Disconnecting container '%s' from network '%s'",
               container_name_or_id, network_name)
     try:
         try:
             network = self.client().networks.get(network_name)
         except NotFound:
             raise NoSuchNetwork(network_name)
         try:
             network.disconnect(container_name_or_id)
         except NotFound:
             raise NoSuchContainer(container_name_or_id)
     except APIError:
         raise ContainerException()
Exemplo n.º 4
0
 def disconnect_container_from_network(
     self, network_name: str, container_name_or_id: str
 ) -> None:
     LOG.debug(
         "Disconnecting container '%s' from network '%s'", container_name_or_id, network_name
     )
     cmd = self._docker_cmd() + ["network", "disconnect", network_name, container_name_or_id]
     try:
         run(cmd)
     except subprocess.CalledProcessError as e:
         stdout_str = to_str(e.stdout)
         if re.match(r".*network (.*) not found.*", stdout_str):
             raise NoSuchNetwork(network_name=network_name)
         elif "No such container" in stdout_str:
             raise NoSuchContainer(container_name_or_id, stdout=e.stdout, stderr=e.stderr)
         else:
             raise ContainerException(
                 "Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
             )
Exemplo n.º 5
0
 def connect_container_to_network(
     self, network_name: str, container_name_or_id: str, aliases: Optional[List] = None
 ) -> None:
     LOG.debug(
         "Connecting container '%s' to network '%s' with aliases '%s'",
         container_name_or_id,
         network_name,
         aliases,
     )
     try:
         network = self.client().networks.get(network_name)
     except NotFound:
         raise NoSuchNetwork(network_name)
     try:
         network.connect(container=container_name_or_id, aliases=aliases)
     except NotFound:
         raise NoSuchContainer(container_name_or_id)
     except APIError as e:
         raise ContainerException() from e
Exemplo n.º 6
0
 def inspect_network(self,
                     network_name: str) -> Dict[str, Union[Dict, str]]:
     try:
         return self._inspect_object(network_name)
     except NoSuchObject as e:
         raise NoSuchNetwork(network_name=e.object_id)