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.

volume.py 5.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from .. import errors
  2. from .. import utils
  3. class VolumeApiMixin:
  4. def volumes(self, filters=None):
  5. """
  6. List volumes currently registered by the docker daemon. Similar to the
  7. ``docker volume ls`` command.
  8. Args:
  9. filters (dict): Server-side list filtering options.
  10. Returns:
  11. (dict): Dictionary with list of volume objects as value of the
  12. ``Volumes`` key.
  13. Raises:
  14. :py:class:`docker.errors.APIError`
  15. If the server returns an error.
  16. Example:
  17. >>> client.api.volumes()
  18. {u'Volumes': [{u'Driver': u'local',
  19. u'Mountpoint': u'/var/lib/docker/volumes/foobar/_data',
  20. u'Name': u'foobar'},
  21. {u'Driver': u'local',
  22. u'Mountpoint': u'/var/lib/docker/volumes/baz/_data',
  23. u'Name': u'baz'}]}
  24. """
  25. params = {
  26. 'filters': utils.convert_filters(filters) if filters else None
  27. }
  28. url = self._url('/volumes')
  29. return self._result(self._get(url, params=params), True)
  30. def create_volume(self, name=None, driver=None, driver_opts=None,
  31. labels=None):
  32. """
  33. Create and register a named volume
  34. Args:
  35. name (str): Name of the volume
  36. driver (str): Name of the driver used to create the volume
  37. driver_opts (dict): Driver options as a key-value dictionary
  38. labels (dict): Labels to set on the volume
  39. Returns:
  40. (dict): The created volume reference object
  41. Raises:
  42. :py:class:`docker.errors.APIError`
  43. If the server returns an error.
  44. Example:
  45. >>> volume = client.api.create_volume(
  46. ... name='foobar',
  47. ... driver='local',
  48. ... driver_opts={'foo': 'bar', 'baz': 'false'},
  49. ... labels={"key": "value"},
  50. ... )
  51. ... print(volume)
  52. {u'Driver': u'local',
  53. u'Labels': {u'key': u'value'},
  54. u'Mountpoint': u'/var/lib/docker/volumes/foobar/_data',
  55. u'Name': u'foobar',
  56. u'Scope': u'local'}
  57. """
  58. url = self._url('/volumes/create')
  59. if driver_opts is not None and not isinstance(driver_opts, dict):
  60. raise TypeError('driver_opts must be a dictionary')
  61. data = {
  62. 'Name': name,
  63. 'Driver': driver,
  64. 'DriverOpts': driver_opts,
  65. }
  66. if labels is not None:
  67. if utils.compare_version('1.23', self._version) < 0:
  68. raise errors.InvalidVersion(
  69. 'volume labels were introduced in API 1.23'
  70. )
  71. if not isinstance(labels, dict):
  72. raise TypeError('labels must be a dictionary')
  73. data["Labels"] = labels
  74. return self._result(self._post_json(url, data=data), True)
  75. def inspect_volume(self, name):
  76. """
  77. Retrieve volume info by name.
  78. Args:
  79. name (str): volume name
  80. Returns:
  81. (dict): Volume information dictionary
  82. Raises:
  83. :py:class:`docker.errors.APIError`
  84. If the server returns an error.
  85. Example:
  86. >>> client.api.inspect_volume('foobar')
  87. {u'Driver': u'local',
  88. u'Mountpoint': u'/var/lib/docker/volumes/foobar/_data',
  89. u'Name': u'foobar'}
  90. """
  91. url = self._url('/volumes/{0}', name)
  92. return self._result(self._get(url), True)
  93. @utils.minimum_version('1.25')
  94. def prune_volumes(self, filters=None):
  95. """
  96. Delete unused volumes
  97. Args:
  98. filters (dict): Filters to process on the prune list.
  99. Returns:
  100. (dict): A dict containing a list of deleted volume names and
  101. the amount of disk space reclaimed in bytes.
  102. Raises:
  103. :py:class:`docker.errors.APIError`
  104. If the server returns an error.
  105. """
  106. params = {}
  107. if filters:
  108. params['filters'] = utils.convert_filters(filters)
  109. url = self._url('/volumes/prune')
  110. return self._result(self._post(url, params=params), True)
  111. def remove_volume(self, name, force=False):
  112. """
  113. Remove a volume. Similar to the ``docker volume rm`` command.
  114. Args:
  115. name (str): The volume's name
  116. force (bool): Force removal of volumes that were already removed
  117. out of band by the volume driver plugin.
  118. Raises:
  119. :py:class:`docker.errors.APIError`
  120. If volume failed to remove.
  121. """
  122. params = {}
  123. if force:
  124. if utils.version_lt(self._version, '1.25'):
  125. raise errors.InvalidVersion(
  126. 'force removal was introduced in API 1.25'
  127. )
  128. params = {'force': force}
  129. url = self._url('/volumes/{0}', name, params=params)
  130. resp = self._delete(url)
  131. self._raise_for_status(resp)