__init__.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. from __future__ import annotations
  2. import json
  3. import logging
  4. import os
  5. import shutil
  6. import subprocess
  7. from typing import Any
  8. from wandb.docker import names
  9. from wandb.errors import Error
  10. class DockerError(Error):
  11. """Raised when attempting to execute a docker command."""
  12. def __init__(
  13. self,
  14. command_launched: list[str],
  15. return_code: int,
  16. stdout: bytes | None = None,
  17. stderr: bytes | None = None,
  18. ) -> None:
  19. command_launched_str = " ".join(command_launched)
  20. error_msg = (
  21. f"The docker command executed was `{command_launched_str}`.\n"
  22. f"It returned with code {return_code}\n"
  23. )
  24. if stdout is not None:
  25. error_msg += f"The content of stdout is '{stdout.decode()}'\n"
  26. else:
  27. error_msg += (
  28. "The content of stdout can be found above the "
  29. "stacktrace (it wasn't captured).\n"
  30. )
  31. if stderr is not None:
  32. error_msg += f"The content of stderr is '{stderr.decode()}'\n"
  33. else:
  34. error_msg += (
  35. "The content of stderr can be found above the "
  36. "stacktrace (it wasn't captured)."
  37. )
  38. super().__init__(error_msg)
  39. entrypoint = os.path.join(
  40. os.path.dirname(os.path.abspath(__file__)), "wandb-entrypoint.sh"
  41. )
  42. log = logging.getLogger(__name__)
  43. def shell(cmd: list[str]) -> str | None:
  44. """Simple wrapper for calling docker,.
  45. returning None on error and the output on success
  46. """
  47. try:
  48. return (
  49. subprocess.check_output(["docker"] + cmd, stderr=subprocess.STDOUT)
  50. .decode("utf8")
  51. .strip()
  52. )
  53. except subprocess.CalledProcessError as e:
  54. print(e) # noqa: T201
  55. return None
  56. _buildx_installed = None
  57. def is_buildx_installed() -> bool:
  58. """Return `True` if docker buildx is installed and working."""
  59. global _buildx_installed
  60. if _buildx_installed is not None:
  61. return _buildx_installed # type: ignore
  62. if not shutil.which("docker"):
  63. _buildx_installed = False
  64. else:
  65. help_output = shell(["buildx", "--help"])
  66. _buildx_installed = help_output is not None and "buildx" in help_output
  67. return _buildx_installed
  68. def is_docker_installed() -> bool:
  69. """Return `True` if docker is installed and working, else `False`."""
  70. try:
  71. # Run the docker --version command
  72. result = subprocess.run(
  73. ["docker", "--version"],
  74. capture_output=True,
  75. )
  76. except FileNotFoundError:
  77. # If docker command is not found
  78. return False
  79. else:
  80. return result.returncode == 0
  81. def build(
  82. tags: list[str], file: str, context_path: str, platform: str | None = None
  83. ) -> str:
  84. use_buildx = is_buildx_installed()
  85. command = ["buildx", "build"] if use_buildx else ["build"]
  86. command += ["--load"] if should_add_load_argument(platform) and use_buildx else []
  87. if platform:
  88. command += ["--platform", platform]
  89. build_tags = []
  90. for tag in tags:
  91. build_tags += ["-t", tag]
  92. args = ["docker"] + command + build_tags + ["-f", file, context_path]
  93. stdout = run_command_live_output(
  94. args,
  95. )
  96. return stdout
  97. def should_add_load_argument(platform: str | None) -> bool:
  98. # the load option does not work when multiple platforms are specified:
  99. # https://github.com/docker/buildx/issues/59
  100. return bool(platform is None or platform and "," not in platform)
  101. def run_command_live_output(args: list[Any]) -> str:
  102. with subprocess.Popen(
  103. args,
  104. stdout=subprocess.PIPE,
  105. stderr=subprocess.STDOUT,
  106. universal_newlines=True,
  107. bufsize=1,
  108. ) as process:
  109. stdout = ""
  110. while True:
  111. chunk = os.read(process.stdout.fileno(), 4096) # type: ignore
  112. if not chunk:
  113. break
  114. index = chunk.find(b"\r")
  115. if index != -1:
  116. print(chunk.decode(), end="") # noqa: T201
  117. else:
  118. stdout += chunk.decode()
  119. print(chunk.decode(), end="\r") # noqa: T201
  120. print(stdout) # noqa: T201
  121. return_code = process.wait()
  122. if return_code != 0:
  123. raise DockerError(args, return_code, stdout.encode())
  124. return stdout
  125. def run(
  126. args: list[Any],
  127. capture_stdout: bool = True,
  128. capture_stderr: bool = True,
  129. input: bytes | None = None,
  130. return_stderr: bool = False,
  131. env: dict[str, str] | None = None,
  132. ) -> str | tuple[str, str]:
  133. args = [str(x) for x in args]
  134. subprocess_env = dict(os.environ)
  135. subprocess_env.update(env or {})
  136. if args[1] == "buildx":
  137. subprocess_env["DOCKER_CLI_EXPERIMENTAL"] = "enabled"
  138. stdout_dest: int | None = subprocess.PIPE if capture_stdout else None
  139. stderr_dest: int | None = subprocess.PIPE if capture_stderr else None
  140. completed_process = subprocess.run(
  141. args, input=input, stdout=stdout_dest, stderr=stderr_dest, env=subprocess_env
  142. )
  143. if completed_process.returncode != 0:
  144. raise DockerError(
  145. args,
  146. completed_process.returncode,
  147. completed_process.stdout,
  148. completed_process.stderr,
  149. )
  150. if return_stderr:
  151. return (
  152. _post_process_stream(completed_process.stdout),
  153. _post_process_stream(completed_process.stderr),
  154. )
  155. else:
  156. return _post_process_stream(completed_process.stdout)
  157. def _post_process_stream(stream: bytes | None) -> str:
  158. if stream is None:
  159. return ""
  160. decoded_stream = stream.decode()
  161. if len(decoded_stream) != 0 and decoded_stream[-1] == "\n":
  162. decoded_stream = decoded_stream[:-1]
  163. return decoded_stream
  164. def default_image(gpu: bool = False) -> str:
  165. tag = "all"
  166. if not gpu:
  167. tag += "-cpu"
  168. return f"wandb/deepo:{tag}"
  169. def parse_repository_tag(repo_name: str) -> tuple[str, str | None]:
  170. parts = repo_name.rsplit("@", 1)
  171. if len(parts) == 2:
  172. return parts[0], parts[1]
  173. parts = repo_name.rsplit(":", 1)
  174. if len(parts) == 2 and "/" not in parts[1]:
  175. return parts[0], parts[1]
  176. return repo_name, None
  177. def parse(image_name: str) -> tuple[str, str, str]:
  178. repository, tag = parse_repository_tag(image_name)
  179. registry, repo_name = names.resolve_repository_name(repository)
  180. if registry == "docker.io":
  181. registry = "index.docker.io"
  182. return registry, repo_name, (tag or "latest")
  183. def image_id_from_registry(image_name: str) -> str | None:
  184. """Query the image manifest to get its full ID including the digest.
  185. Args:
  186. image_name: The image name, such as "wandb/local".
  187. Returns:
  188. The image name followed by its digest, like "wandb/local@sha256:...".
  189. """
  190. # https://docs.docker.com/reference/cli/docker/buildx/imagetools/inspect
  191. inspect_cmd = ["buildx", "imagetools", "inspect", image_name]
  192. format_args = ["--format", r"{{.Name}}@{{.Manifest.Digest}}"]
  193. return shell([*inspect_cmd, *format_args])
  194. def image_id(image_name: str) -> str | None:
  195. """Retrieve the image id from the local docker daemon or remote registry."""
  196. if "@sha256:" in image_name:
  197. return image_name
  198. else:
  199. digests = shell(["inspect", image_name, "--format", "{{json .RepoDigests}}"])
  200. if digests is None:
  201. return image_id_from_registry(image_name)
  202. try:
  203. return json.loads(digests)[0]
  204. except (ValueError, IndexError):
  205. return image_id_from_registry(image_name)
  206. def get_image_uid(image_name: str) -> int:
  207. """Retrieve the image default uid through brute force."""
  208. image_uid = shell(["run", image_name, "id", "-u"])
  209. return int(image_uid) if image_uid else -1
  210. def push(image: str, tag: str) -> str | None:
  211. """Push an image to a remote registry."""
  212. return shell(["push", f"{image}:{tag}"])
  213. def login(username: str, password: str, registry: str) -> str | None:
  214. """Login to a registry."""
  215. return shell(["login", "--username", username, "--password", password, registry])
  216. def tag(image_name: str, tag: str) -> str | None:
  217. """Tag an image."""
  218. return shell(["tag", image_name, tag])
  219. __all__ = [
  220. "shell",
  221. "build",
  222. "run",
  223. "image_id",
  224. "image_id_from_registry",
  225. "is_docker_installed",
  226. "parse",
  227. "parse_repository_tag",
  228. "default_image",
  229. "get_image_uid",
  230. "push",
  231. "login",
  232. "tag",
  233. ]