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.

swarm.py 8.1KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. from docker.api import APIClient
  2. from docker.errors import APIError
  3. from .resource import Model
  4. class Swarm(Model):
  5. """
  6. The server's Swarm state. This a singleton that must be reloaded to get
  7. the current state of the Swarm.
  8. """
  9. id_attribute = 'ID'
  10. def __init__(self, *args, **kwargs):
  11. super().__init__(*args, **kwargs)
  12. if self.client:
  13. try:
  14. self.reload()
  15. except APIError as e:
  16. # FIXME: https://github.com/docker/docker/issues/29192
  17. if e.response.status_code not in (406, 503):
  18. raise
  19. @property
  20. def version(self):
  21. """
  22. The version number of the swarm. If this is not the same as the
  23. server, the :py:meth:`update` function will not work and you will
  24. need to call :py:meth:`reload` before calling it again.
  25. """
  26. return self.attrs.get('Version').get('Index')
  27. def get_unlock_key(self):
  28. return self.client.api.get_unlock_key()
  29. get_unlock_key.__doc__ = APIClient.get_unlock_key.__doc__
  30. def init(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
  31. force_new_cluster=False, default_addr_pool=None,
  32. subnet_size=None, data_path_addr=None, data_path_port=None,
  33. **kwargs):
  34. """
  35. Initialize a new swarm on this Engine.
  36. Args:
  37. advertise_addr (str): Externally reachable address advertised to
  38. other nodes. This can either be an address/port combination in
  39. the form ``192.168.1.1:4567``, or an interface followed by a
  40. port number, like ``eth0:4567``. If the port number is omitted,
  41. the port number from the listen address is used.
  42. If not specified, it will be automatically detected when
  43. possible.
  44. listen_addr (str): Listen address used for inter-manager
  45. communication, as well as determining the networking interface
  46. used for the VXLAN Tunnel Endpoint (VTEP). This can either be
  47. an address/port combination in the form ``192.168.1.1:4567``,
  48. or an interface followed by a port number, like ``eth0:4567``.
  49. If the port number is omitted, the default swarm listening port
  50. is used. Default: ``0.0.0.0:2377``
  51. force_new_cluster (bool): Force creating a new Swarm, even if
  52. already part of one. Default: False
  53. default_addr_pool (list of str): Default Address Pool specifies
  54. default subnet pools for global scope networks. Each pool
  55. should be specified as a CIDR block, like '10.0.0.0/8'.
  56. Default: None
  57. subnet_size (int): SubnetSize specifies the subnet size of the
  58. networks created from the default subnet pool. Default: None
  59. data_path_addr (string): Address or interface to use for data path
  60. traffic. For example, 192.168.1.1, or an interface, like eth0.
  61. data_path_port (int): Port number to use for data path traffic.
  62. Acceptable port range is 1024 to 49151. If set to ``None`` or
  63. 0, the default port 4789 will be used. Default: None
  64. task_history_retention_limit (int): Maximum number of tasks
  65. history stored.
  66. snapshot_interval (int): Number of logs entries between snapshot.
  67. keep_old_snapshots (int): Number of snapshots to keep beyond the
  68. current snapshot.
  69. log_entries_for_slow_followers (int): Number of log entries to
  70. keep around to sync up slow followers after a snapshot is
  71. created.
  72. heartbeat_tick (int): Amount of ticks (in seconds) between each
  73. heartbeat.
  74. election_tick (int): Amount of ticks (in seconds) needed without a
  75. leader to trigger a new election.
  76. dispatcher_heartbeat_period (int): The delay for an agent to send
  77. a heartbeat to the dispatcher.
  78. node_cert_expiry (int): Automatic expiry for nodes certificates.
  79. external_ca (dict): Configuration for forwarding signing requests
  80. to an external certificate authority. Use
  81. ``docker.types.SwarmExternalCA``.
  82. name (string): Swarm's name
  83. labels (dict): User-defined key/value metadata.
  84. signing_ca_cert (str): The desired signing CA certificate for all
  85. swarm node TLS leaf certificates, in PEM format.
  86. signing_ca_key (str): The desired signing CA key for all swarm
  87. node TLS leaf certificates, in PEM format.
  88. ca_force_rotate (int): An integer whose purpose is to force swarm
  89. to generate a new signing CA certificate and key, if none have
  90. been specified.
  91. autolock_managers (boolean): If set, generate a key and use it to
  92. lock data stored on the managers.
  93. log_driver (DriverConfig): The default log driver to use for tasks
  94. created in the orchestrator.
  95. Returns:
  96. (str): The ID of the created node.
  97. Raises:
  98. :py:class:`docker.errors.APIError`
  99. If the server returns an error.
  100. Example:
  101. >>> client.swarm.init(
  102. advertise_addr='eth0', listen_addr='0.0.0.0:5000',
  103. force_new_cluster=False, default_addr_pool=['10.20.0.0/16],
  104. subnet_size=24, snapshot_interval=5000,
  105. log_entries_for_slow_followers=1200
  106. )
  107. """
  108. init_kwargs = {
  109. 'advertise_addr': advertise_addr,
  110. 'listen_addr': listen_addr,
  111. 'force_new_cluster': force_new_cluster,
  112. 'default_addr_pool': default_addr_pool,
  113. 'subnet_size': subnet_size,
  114. 'data_path_addr': data_path_addr,
  115. 'data_path_port': data_path_port,
  116. }
  117. init_kwargs['swarm_spec'] = self.client.api.create_swarm_spec(**kwargs)
  118. node_id = self.client.api.init_swarm(**init_kwargs)
  119. self.reload()
  120. return node_id
  121. def join(self, *args, **kwargs):
  122. return self.client.api.join_swarm(*args, **kwargs)
  123. join.__doc__ = APIClient.join_swarm.__doc__
  124. def leave(self, *args, **kwargs):
  125. return self.client.api.leave_swarm(*args, **kwargs)
  126. leave.__doc__ = APIClient.leave_swarm.__doc__
  127. def reload(self):
  128. """
  129. Inspect the swarm on the server and store the response in
  130. :py:attr:`attrs`.
  131. Raises:
  132. :py:class:`docker.errors.APIError`
  133. If the server returns an error.
  134. """
  135. self.attrs = self.client.api.inspect_swarm()
  136. def unlock(self, key):
  137. return self.client.api.unlock_swarm(key)
  138. unlock.__doc__ = APIClient.unlock_swarm.__doc__
  139. def update(self, rotate_worker_token=False, rotate_manager_token=False,
  140. rotate_manager_unlock_key=False, **kwargs):
  141. """
  142. Update the swarm's configuration.
  143. It takes the same arguments as :py:meth:`init`, except
  144. ``advertise_addr``, ``listen_addr``, and ``force_new_cluster``. In
  145. addition, it takes these arguments:
  146. Args:
  147. rotate_worker_token (bool): Rotate the worker join token. Default:
  148. ``False``.
  149. rotate_manager_token (bool): Rotate the manager join token.
  150. Default: ``False``.
  151. rotate_manager_unlock_key (bool): Rotate the manager unlock key.
  152. Default: ``False``.
  153. Raises:
  154. :py:class:`docker.errors.APIError`
  155. If the server returns an error.
  156. """
  157. # this seems to have to be set
  158. if kwargs.get('node_cert_expiry') is None:
  159. kwargs['node_cert_expiry'] = 7776000000000000
  160. return self.client.api.update_swarm(
  161. version=self.version,
  162. swarm_spec=self.client.api.create_swarm_spec(**kwargs),
  163. rotate_worker_token=rotate_worker_token,
  164. rotate_manager_token=rotate_manager_token,
  165. rotate_manager_unlock_key=rotate_manager_unlock_key
  166. )