import sys
import subprocess
-import json
import requests
import os
import argparse
+import re
-def _run(cmd, is_shell=False):
+class RequestHandler:
+ def __init__(self, token=None, timeout=None):
+ if token:
+ self.headers = {"Authorization": "Bearer {}".format(token)}
+ else:
+ self.headers = {}
+ self.timeout = timeout or 5
+
+ def make_request(self, url):
+ try:
+ response = requests.get(url, headers=self.headers, timeout=self.timeout)
+ response.raise_for_status()
+ return response
+ except requests.RequestException as e:
+ raise SystemExit('[onecc-docker] error: {}'.format(e))
+
+
+# 5 sec timeout is set based on github.com/Samsung/ONE/issues/11134
+def _request_recent_version(token=None):
+ response = RequestHandler(
+ token,
+ timeout=5).make_request(url="https://api.github.com/repos/Samsung/ONE/releases")
+ versions = [release_item["tag_name"] for release_item in response.json()]
+
+ for version in versions:
+ # Return the latest version with the given format
+ # to filter out such as 'onert-micro-0.1.0' release
+ # which doesn't contain onecc package.
+ if bool(re.match(r'^\d+\.\d+\.\d+$', version)):
+ return version
+
+ raise SystemExit('[onecc-docker] Failed to get latest onecc version')
+
+
+# 10 sec timeout is set based on github.com/Samsung/ONE/issues/11134
+def _run(cmd, is_shell=False, timeout=10):
result = subprocess.Popen(
cmd, shell=is_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- stdout, stderr = result.communicate()
+ stdout, stderr = result.communicate(timeout=timeout)
stdout = stdout.decode('utf-8')
stderr = stderr.decode('utf-8')
args, onecc_arguments = parser.parse_known_args()
authorization_token = args.token
- LATEST_URL = "https://api.github.com/repos/Samsung/ONE/releases/latest"
- headers = {}
- if authorization_token:
- headers = {"Authorization": "Bearer {}".format(authorization_token)}
- try:
- response = requests.get(LATEST_URL, headers=headers)
- response.raise_for_status()
- except requests.exceptions.RequestException as e:
- raise SystemExit('onecc-docker: error: {}'.format(e))
-
- versions_str = response.content
- versions_json = json.loads(versions_str)
- recent_version = versions_json["tag_name"]
-
+ recent_version = _request_recent_version(authorization_token)
image_name = f"onecc:{recent_version}"
build_arg = f"VERSION={recent_version}"
build_cmd = [
"docker", "build", "-t", image_name, "--build-arg", build_arg, dockerfile_path
]
- print('build Docker image ...')
- _run(build_cmd)
- print('Dockerfile successfully built.')
+ print('[onecc-docker] Build docker image ...')
+ _run(build_cmd, timeout=30)
+ print('[onecc-docker] Docker image is built successfully.')
contianer_name = f"onecc_{recent_version.replace('.','_')}"
user_cmd = ' '.join(onecc_arguments)