Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

networks.py 7.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from ..api import APIClient
  2. from ..utils import version_gte
  3. from .containers import Container
  4. from .resource import Model, Collection
  5. class Network(Model):
  6. """
  7. A Docker network.
  8. """
  9. @property
  10. def name(self):
  11. """
  12. The name of the network.
  13. """
  14. return self.attrs.get('Name')
  15. @property
  16. def containers(self):
  17. """
  18. The containers that are connected to the network, as a list of
  19. :py:class:`~docker.models.containers.Container` objects.
  20. """
  21. return [
  22. self.client.containers.get(cid) for cid in
  23. (self.attrs.get('Containers') or {}).keys()
  24. ]
  25. def connect(self, container, *args, **kwargs):
  26. """
  27. Connect a container to this network.
  28. Args:
  29. container (str): Container to connect to this network, as either
  30. an ID, name, or :py:class:`~docker.models.containers.Container`
  31. object.
  32. aliases (:py:class:`list`): A list of aliases for this endpoint.
  33. Names in that list can be used within the network to reach the
  34. container. Defaults to ``None``.
  35. links (:py:class:`list`): A list of links for this endpoint.
  36. Containers declared in this list will be linkedto this
  37. container. Defaults to ``None``.
  38. ipv4_address (str): The IP address of this container on the
  39. network, using the IPv4 protocol. Defaults to ``None``.
  40. ipv6_address (str): The IP address of this container on the
  41. network, using the IPv6 protocol. Defaults to ``None``.
  42. link_local_ips (:py:class:`list`): A list of link-local (IPv4/IPv6)
  43. addresses.
  44. driver_opt (dict): A dictionary of options to provide to the
  45. network driver. Defaults to ``None``.
  46. Raises:
  47. :py:class:`docker.errors.APIError`
  48. If the server returns an error.
  49. """
  50. if isinstance(container, Container):
  51. container = container.id
  52. return self.client.api.connect_container_to_network(
  53. container, self.id, *args, **kwargs
  54. )
  55. def disconnect(self, container, *args, **kwargs):
  56. """
  57. Disconnect a container from this network.
  58. Args:
  59. container (str): Container to disconnect from this network, as
  60. either an ID, name, or
  61. :py:class:`~docker.models.containers.Container` object.
  62. force (bool): Force the container to disconnect from a network.
  63. Default: ``False``
  64. Raises:
  65. :py:class:`docker.errors.APIError`
  66. If the server returns an error.
  67. """
  68. if isinstance(container, Container):
  69. container = container.id
  70. return self.client.api.disconnect_container_from_network(
  71. container, self.id, *args, **kwargs
  72. )
  73. def remove(self):
  74. """
  75. Remove this network.
  76. Raises:
  77. :py:class:`docker.errors.APIError`
  78. If the server returns an error.
  79. """
  80. return self.client.api.remove_network(self.id)
  81. class NetworkCollection(Collection):
  82. """
  83. Networks on the Docker server.
  84. """
  85. model = Network
  86. def create(self, name, *args, **kwargs):
  87. """
  88. Create a network. Similar to the ``docker network create``.
  89. Args:
  90. name (str): Name of the network
  91. driver (str): Name of the driver used to create the network
  92. options (dict): Driver options as a key-value dictionary
  93. ipam (IPAMConfig): Optional custom IP scheme for the network.
  94. check_duplicate (bool): Request daemon to check for networks with
  95. same name. Default: ``None``.
  96. internal (bool): Restrict external access to the network. Default
  97. ``False``.
  98. labels (dict): Map of labels to set on the network. Default
  99. ``None``.
  100. enable_ipv6 (bool): Enable IPv6 on the network. Default ``False``.
  101. attachable (bool): If enabled, and the network is in the global
  102. scope, non-service containers on worker nodes will be able to
  103. connect to the network.
  104. scope (str): Specify the network's scope (``local``, ``global`` or
  105. ``swarm``)
  106. ingress (bool): If set, create an ingress network which provides
  107. the routing-mesh in swarm mode.
  108. Returns:
  109. (:py:class:`Network`): The network that was created.
  110. Raises:
  111. :py:class:`docker.errors.APIError`
  112. If the server returns an error.
  113. Example:
  114. A network using the bridge driver:
  115. >>> client.networks.create("network1", driver="bridge")
  116. You can also create more advanced networks with custom IPAM
  117. configurations. For example, setting the subnet to
  118. ``192.168.52.0/24`` and gateway address to ``192.168.52.254``.
  119. .. code-block:: python
  120. >>> ipam_pool = docker.types.IPAMPool(
  121. subnet='192.168.52.0/24',
  122. gateway='192.168.52.254'
  123. )
  124. >>> ipam_config = docker.types.IPAMConfig(
  125. pool_configs=[ipam_pool]
  126. )
  127. >>> client.networks.create(
  128. "network1",
  129. driver="bridge",
  130. ipam=ipam_config
  131. )
  132. """
  133. resp = self.client.api.create_network(name, *args, **kwargs)
  134. return self.get(resp['Id'])
  135. def get(self, network_id, *args, **kwargs):
  136. """
  137. Get a network by its ID.
  138. Args:
  139. network_id (str): The ID of the network.
  140. verbose (bool): Retrieve the service details across the cluster in
  141. swarm mode.
  142. scope (str): Filter the network by scope (``swarm``, ``global``
  143. or ``local``).
  144. Returns:
  145. (:py:class:`Network`) The network.
  146. Raises:
  147. :py:class:`docker.errors.NotFound`
  148. If the network does not exist.
  149. :py:class:`docker.errors.APIError`
  150. If the server returns an error.
  151. """
  152. return self.prepare_model(
  153. self.client.api.inspect_network(network_id, *args, **kwargs)
  154. )
  155. def list(self, *args, **kwargs):
  156. """
  157. List networks. Similar to the ``docker network ls`` command.
  158. Args:
  159. names (:py:class:`list`): List of names to filter by.
  160. ids (:py:class:`list`): List of ids to filter by.
  161. filters (dict): Filters to be processed on the network list.
  162. Available filters:
  163. - ``driver=[<driver-name>]`` Matches a network's driver.
  164. - `label` (str|list): format either ``"key"``, ``"key=value"``
  165. or a list of such.
  166. - ``type=["custom"|"builtin"]`` Filters networks by type.
  167. greedy (bool): Fetch more details for each network individually.
  168. You might want this to get the containers attached to them.
  169. Returns:
  170. (list of :py:class:`Network`) The networks on the server.
  171. Raises:
  172. :py:class:`docker.errors.APIError`
  173. If the server returns an error.
  174. """
  175. greedy = kwargs.pop('greedy', False)
  176. resp = self.client.api.networks(*args, **kwargs)
  177. networks = [self.prepare_model(item) for item in resp]
  178. if greedy and version_gte(self.client.api._version, '1.28'):
  179. for net in networks:
  180. net.reload()
  181. return networks
  182. def prune(self, filters=None):
  183. return self.client.api.prune_networks(filters=filters)
  184. prune.__doc__ = APIClient.prune_networks.__doc__