remote.py 5.0 KB

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