remote.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import base64
  2. import io
  3. import tarfile
  4. import time
  5. import requests
  6. from openhands.core.logger import openhands_logger as logger
  7. from openhands.runtime.builder import RuntimeBuilder
  8. from openhands.runtime.utils.request import send_request
  9. from openhands.runtime.utils.shutdown_listener import (
  10. should_continue,
  11. sleep_if_should_continue,
  12. )
  13. class RemoteRuntimeBuilder(RuntimeBuilder):
  14. """This class interacts with the remote Runtime API for building and managing container images."""
  15. def __init__(self, api_url: str, api_key: str):
  16. self.api_url = api_url
  17. self.api_key = api_key
  18. self.session = requests.Session()
  19. self.session.headers.update({'X-API-Key': self.api_key})
  20. def build(self, path: str, tags: list[str], platform: str | None = None) -> str:
  21. """Builds a Docker image using the Runtime API's /build endpoint."""
  22. # Create a tar archive of the build context
  23. tar_buffer = io.BytesIO()
  24. with tarfile.open(fileobj=tar_buffer, mode='w:gz') as tar:
  25. tar.add(path, arcname='.')
  26. tar_buffer.seek(0)
  27. # Encode the tar file as base64
  28. base64_encoded_tar = base64.b64encode(tar_buffer.getvalue()).decode('utf-8')
  29. # Prepare the multipart form data
  30. files = [
  31. ('context', ('context.tar.gz', base64_encoded_tar)),
  32. ('target_image', (None, tags[0])),
  33. ]
  34. # Add additional tags if present
  35. for tag in tags[1:]:
  36. files.append(('tags', (None, tag)))
  37. # Send the POST request to /build (Begins the build process)
  38. try:
  39. response = send_request(
  40. self.session,
  41. 'POST',
  42. f'{self.api_url}/build',
  43. files=files,
  44. timeout=30,
  45. )
  46. except requests.exceptions.HTTPError as e:
  47. if e.response.status_code == 429:
  48. logger.warning('Build was rate limited. Retrying in 30 seconds.')
  49. time.sleep(30)
  50. return self.build(path, tags, platform)
  51. else:
  52. raise e
  53. build_data = response.json()
  54. build_id = build_data['build_id']
  55. logger.info(f'Build initiated with ID: {build_id}')
  56. # Poll /build_status until the build is complete
  57. start_time = time.time()
  58. timeout = 30 * 60 # 20 minutes in seconds
  59. while should_continue():
  60. if time.time() - start_time > timeout:
  61. logger.error('Build timed out after 30 minutes')
  62. raise RuntimeError('Build timed out after 30 minutes')
  63. status_response = send_request(
  64. self.session,
  65. 'GET',
  66. f'{self.api_url}/build_status',
  67. params={'build_id': build_id},
  68. )
  69. if status_response.status_code != 200:
  70. logger.error(f'Failed to get build status: {status_response.text}')
  71. raise RuntimeError(
  72. f'Failed to get build status: {status_response.text}'
  73. )
  74. status_data = status_response.json()
  75. status = status_data['status']
  76. logger.info(f'Build status: {status}')
  77. if status == 'SUCCESS':
  78. logger.debug(f"Successfully built {status_data['image']}")
  79. return status_data['image']
  80. elif status in [
  81. 'FAILURE',
  82. 'INTERNAL_ERROR',
  83. 'TIMEOUT',
  84. 'CANCELLED',
  85. 'EXPIRED',
  86. ]:
  87. error_message = status_data.get(
  88. 'error', f'Build failed with status: {status}. Build ID: {build_id}'
  89. )
  90. logger.error(error_message)
  91. raise RuntimeError(error_message)
  92. # Wait before polling again
  93. sleep_if_should_continue(30)
  94. raise RuntimeError('Build interrupted (likely received SIGTERM or SIGINT).')
  95. def image_exists(self, image_name: str, pull_from_repo: bool = True) -> bool:
  96. """Checks if an image exists in the remote registry using the /image_exists endpoint."""
  97. params = {'image': image_name}
  98. response = send_request(
  99. self.session,
  100. 'GET',
  101. f'{self.api_url}/image_exists',
  102. params=params,
  103. )
  104. if response.status_code != 200:
  105. logger.error(f'Failed to check image existence: {response.text}')
  106. raise RuntimeError(f'Failed to check image existence: {response.text}')
  107. result = response.json()
  108. if result['exists']:
  109. logger.debug(
  110. f"Image {image_name} exists. "
  111. f"Uploaded at: {result['image']['upload_time']}, "
  112. f"Size: {result['image']['image_size_bytes'] / 1024 / 1024:.2f} MB"
  113. )
  114. else:
  115. logger.debug(f'Image {image_name} does not exist.')
  116. return result['exists']