docker.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import datetime
  2. import os
  3. import subprocess
  4. import sys
  5. import time
  6. import docker
  7. from openhands import __version__ as oh_version
  8. from openhands.core.logger import openhands_logger as logger
  9. from openhands.runtime.builder.base import RuntimeBuilder
  10. class DockerRuntimeBuilder(RuntimeBuilder):
  11. def __init__(self, docker_client: docker.DockerClient):
  12. self.docker_client = docker_client
  13. version_info = self.docker_client.version()
  14. server_version = version_info.get('Version', '')
  15. if tuple(map(int, server_version.split('.'))) < (18, 9):
  16. raise RuntimeError('Docker server version must be >= 18.09 to use BuildKit')
  17. self.max_lines = 10
  18. self.log_lines = [''] * self.max_lines
  19. def build(
  20. self,
  21. path: str,
  22. tags: list[str],
  23. use_local_cache: bool = False,
  24. extra_build_args: list[str] | None = None,
  25. ) -> str:
  26. """Builds a Docker image using BuildKit and handles the build logs appropriately.
  27. Args:
  28. path (str): The path to the Docker build context.
  29. tags (list[str]): A list of image tags to apply to the built image.
  30. use_local_cache (bool, optional): Whether to use and update the local build cache. Defaults to True.
  31. extra_build_args (list[str], optional): Additional arguments to pass to the Docker build command. Defaults to None.
  32. Returns:
  33. str: The name of the built Docker image.
  34. Raises:
  35. RuntimeError: If the Docker server version is incompatible or if the build process fails.
  36. Note:
  37. This method uses Docker BuildKit for improved build performance and caching capabilities.
  38. If `use_local_cache` is True, it will attempt to use and update the build cache in a local directory.
  39. The `extra_build_args` parameter allows for passing additional Docker build arguments as needed.
  40. """
  41. self.docker_client = docker.from_env()
  42. version_info = self.docker_client.version()
  43. server_version = version_info.get('Version', '')
  44. if tuple(map(int, server_version.split('.'))) < (18, 9):
  45. raise RuntimeError('Docker server version must be >= 18.09 to use BuildKit')
  46. target_image_hash_name = tags[0]
  47. target_image_repo, target_image_hash_tag = target_image_hash_name.split(':')
  48. target_image_tag = tags[1].split(':')[1] if len(tags) > 1 else None
  49. # Check if the image exists and pull if necessary
  50. self.image_exists(target_image_repo)
  51. buildx_cmd = [
  52. 'docker',
  53. 'buildx',
  54. 'build',
  55. '--progress=plain',
  56. f'--build-arg=OPENHANDS_RUNTIME_VERSION={oh_version}',
  57. f'--build-arg=OPENHANDS_RUNTIME_BUILD_TIME={datetime.datetime.now().isoformat()}',
  58. f'--tag={target_image_hash_name}',
  59. '--load',
  60. ]
  61. cache_dir = '/tmp/.buildx-cache'
  62. if use_local_cache and self._is_cache_usable(cache_dir):
  63. buildx_cmd.extend(
  64. [
  65. f'--cache-from=type=local,src={cache_dir}',
  66. f'--cache-to=type=local,dest={cache_dir},mode=max',
  67. ]
  68. )
  69. if extra_build_args:
  70. buildx_cmd.extend(extra_build_args)
  71. buildx_cmd.append(path) # must be last!
  72. print('================ DOCKER BUILD STARTED ================')
  73. if sys.stdout.isatty():
  74. sys.stdout.write('\n' * self.max_lines)
  75. sys.stdout.flush()
  76. try:
  77. process = subprocess.Popen(
  78. buildx_cmd,
  79. stdout=subprocess.PIPE,
  80. stderr=subprocess.STDOUT,
  81. universal_newlines=True,
  82. bufsize=1,
  83. )
  84. if process.stdout:
  85. for line in iter(process.stdout.readline, ''):
  86. line = line.strip()
  87. if line:
  88. self._output_logs(line)
  89. return_code = process.wait()
  90. if return_code != 0:
  91. raise subprocess.CalledProcessError(
  92. return_code,
  93. process.args,
  94. output=None,
  95. stderr=None,
  96. )
  97. except subprocess.CalledProcessError as e:
  98. logger.error(f'Image build failed:\n{e}')
  99. logger.error(f'Command output:\n{e.output}')
  100. raise
  101. except subprocess.TimeoutExpired:
  102. logger.error('Image build timed out')
  103. raise
  104. except FileNotFoundError as e:
  105. logger.error(f'Python executable not found: {e}')
  106. raise
  107. except PermissionError as e:
  108. logger.error(
  109. f'Permission denied when trying to execute the build command:\n{e}'
  110. )
  111. raise
  112. except Exception as e:
  113. logger.error(f'An unexpected error occurred during the build process: {e}')
  114. raise
  115. logger.info(f'Image [{target_image_hash_name}] build finished.')
  116. if target_image_tag:
  117. image = self.docker_client.images.get(target_image_hash_name)
  118. image.tag(target_image_repo, target_image_tag)
  119. logger.info(
  120. f'Re-tagged image [{target_image_hash_name}] with more generic tag [{target_image_tag}]'
  121. )
  122. # Check if the image is built successfully
  123. image = self.docker_client.images.get(target_image_hash_name)
  124. if image is None:
  125. raise RuntimeError(
  126. f'Build failed: Image {target_image_hash_name} not found'
  127. )
  128. tags_str = (
  129. f'{target_image_hash_tag}, {target_image_tag}'
  130. if target_image_tag
  131. else target_image_hash_tag
  132. )
  133. logger.info(
  134. f'Image {target_image_repo} with tags [{tags_str}] built successfully'
  135. )
  136. return target_image_hash_name
  137. def image_exists(self, image_name: str, pull_from_repo: bool = True) -> bool:
  138. """Check if the image exists in the registry (try to pull it first) or in the local store.
  139. Args:
  140. image_name (str): The Docker image to check (<image repo>:<image tag>)
  141. pull_from_repo (bool): Whether to pull from the remote repo if the image not present locally
  142. Returns:
  143. bool: Whether the Docker image exists in the registry or in the local store
  144. """
  145. if not image_name:
  146. logger.error(f'Invalid image name: `{image_name}`')
  147. return False
  148. try:
  149. logger.debug(f'Checking, if image exists locally:\n{image_name}')
  150. self.docker_client.images.get(image_name)
  151. logger.debug('Image found locally.')
  152. return True
  153. except docker.errors.ImageNotFound:
  154. if not pull_from_repo:
  155. logger.debug(
  156. f'Image {image_name} not found locally'
  157. )
  158. return False
  159. try:
  160. logger.debug(
  161. 'Image not found locally. Trying to pull it, please wait...'
  162. )
  163. layers: dict[str, dict[str, str]] = {}
  164. previous_layer_count = 0
  165. for line in self.docker_client.api.pull(
  166. image_name, stream=True, decode=True
  167. ):
  168. self._output_build_progress(line, layers, previous_layer_count)
  169. previous_layer_count = len(layers)
  170. logger.debug('Image pulled')
  171. return True
  172. except docker.errors.ImageNotFound:
  173. logger.debug('Could not find image locally or in registry.')
  174. return False
  175. except Exception as e:
  176. msg = 'Image could not be pulled: '
  177. ex_msg = str(e)
  178. if 'Not Found' in ex_msg:
  179. msg += 'image not found in registry.'
  180. else:
  181. msg += f'{ex_msg}'
  182. logger.debug(msg)
  183. return False
  184. def _output_logs(self, new_line: str) -> None:
  185. """Display the last 10 log_lines in the console (not for file logging).
  186. This will create the effect of a rolling display in the console.
  187. '\033[F' moves the cursor up one line.
  188. '\033[2K\r' clears the line and moves the cursor to the beginning of the line.
  189. """
  190. if not sys.stdout.isatty():
  191. logger.debug(new_line)
  192. return
  193. self.log_lines.pop(0)
  194. self.log_lines.append(new_line[:80])
  195. sys.stdout.write('\033[F' * (self.max_lines))
  196. sys.stdout.flush()
  197. for line in self.log_lines:
  198. sys.stdout.write('\033[2K' + line + '\n')
  199. sys.stdout.flush()
  200. def _output_build_progress(
  201. self, current_line: dict, layers: dict, previous_layer_count: int
  202. ) -> None:
  203. if 'id' in current_line and 'progressDetail' in current_line:
  204. layer_id = current_line['id']
  205. if layer_id not in layers:
  206. layers[layer_id] = {'status': '', 'progress': '', 'last_logged': 0}
  207. if 'status' in current_line:
  208. layers[layer_id]['status'] = current_line['status']
  209. if 'progress' in current_line:
  210. layers[layer_id]['progress'] = current_line['progress']
  211. if 'progressDetail' in current_line:
  212. progress_detail = current_line['progressDetail']
  213. if 'total' in progress_detail and 'current' in progress_detail:
  214. total = progress_detail['total']
  215. current = progress_detail['current']
  216. percentage = min(
  217. (current / total) * 100, 100
  218. ) # Ensure it doesn't exceed 100%
  219. else:
  220. percentage = (
  221. 100 if layers[layer_id]['status'] == 'Download complete' else 0
  222. )
  223. if sys.stdout.isatty():
  224. sys.stdout.write('\033[F' * previous_layer_count)
  225. for lid, layer_data in sorted(layers.items()):
  226. sys.stdout.write('\033[2K\r')
  227. status = layer_data['status']
  228. progress = layer_data['progress']
  229. if status == 'Download complete':
  230. print(f'Layer {lid}: Download complete')
  231. elif status == 'Already exists':
  232. print(f'Layer {lid}: Already exists')
  233. else:
  234. print(f'Layer {lid}: {progress} {status}')
  235. sys.stdout.flush()
  236. elif percentage != 0 and (
  237. percentage - layers[layer_id]['last_logged'] >= 10 or percentage == 100
  238. ):
  239. logger.debug(
  240. f'Layer {layer_id}: {layers[layer_id]["progress"]} {layers[layer_id]["status"]}'
  241. )
  242. layers[layer_id]['last_logged'] = percentage
  243. elif 'status' in current_line:
  244. logger.debug(current_line['status'])
  245. def _prune_old_cache_files(self, cache_dir: str, max_age_days: int = 7) -> None:
  246. """
  247. Prune cache files older than the specified number of days.
  248. Args:
  249. cache_dir (str): The path to the cache directory.
  250. max_age_days (int): The maximum age of cache files in days.
  251. """
  252. try:
  253. current_time = time.time()
  254. max_age_seconds = max_age_days * 24 * 60 * 60
  255. for root, _, files in os.walk(cache_dir):
  256. for file in files:
  257. file_path = os.path.join(root, file)
  258. try:
  259. file_age = current_time - os.path.getmtime(file_path)
  260. if file_age > max_age_seconds:
  261. os.remove(file_path)
  262. logger.debug(f'Removed old cache file: {file_path}')
  263. except Exception as e:
  264. logger.warning(f'Error processing cache file {file_path}: {e}')
  265. except Exception as e:
  266. logger.warning(f'Error during build cache pruning: {e}')
  267. def _is_cache_usable(self, cache_dir: str) -> bool:
  268. """
  269. Check if the cache directory is usable (exists and is writable).
  270. Args:
  271. cache_dir (str): The path to the cache directory.
  272. Returns:
  273. bool: True if the cache directory is usable, False otherwise.
  274. """
  275. if not os.path.exists(cache_dir):
  276. try:
  277. os.makedirs(cache_dir, exist_ok=True)
  278. logger.debug(f'Created cache directory: {cache_dir}')
  279. except OSError as e:
  280. logger.debug(f'Failed to create cache directory {cache_dir}: {e}')
  281. return False
  282. if not os.access(cache_dir, os.W_OK):
  283. logger.warning(
  284. f'Cache directory {cache_dir} is not writable. Caches will not be used for Docker builds.'
  285. )
  286. return False
  287. self._prune_old_cache_files(cache_dir)
  288. logger.debug(f'Cache directory {cache_dir} is usable')
  289. return True