Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache hub pull with build env #5061

Merged
merged 6 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 41 additions & 27 deletions jina/hubble/hubio.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
from jina.hubble import HubExecutor
from jina.hubble.helper import (
archive_package,
check_requirements_env_variable,
disk_cache_offline,
download_with_resume,
get_cache_db,
get_download_cache_dir,
get_hubble_error_message,
get_hubble_url_v2,
get_request_header,
get_requirements_env_variables,
parse_hub_uri,
upload_file,
get_requirements_env_variables,
check_requirements_env_variable
)
from jina.hubble.hubapi import (
dump_secret,
Expand Down Expand Up @@ -347,43 +347,53 @@ def push(self) -> None:
)

dockerfile = dockerfile.relative_to(work_path)

build_env = None
if self.args.build_env:
build_envs = self.args.build_env.strip().split()
build_env_dict = {}
for index, env in enumerate(build_envs):
env_list = env.split('=')
if (len(env_list) != 2):
raise Exception( f'The --build-env parameter: `{env}` is wrong format. you can use: `--build-env {env}=YOUR_VALUE`.')
if len(env_list) != 2:
raise Exception(
f'The --build-env parameter: `{env}` is wrong format. you can use: `--build-env {env}=YOUR_VALUE`.'
)
if check_requirements_env_variable(env_list[0]) is False:
raise Exception( f'The --build-env parameter key:`{env_list[0]}` can only consist of uppercase letter and number and underline.')
raise Exception(
f'The `--build-env` parameter key:`{env_list[0]}` can only consist of uppercase letter and number and underline.'
)
build_env_dict[env_list[0]] = env_list[1]
build_env = build_env_dict if len(list(build_env_dict.keys()))>0 else None
build_env = build_env_dict if build_env_dict else None

requirements_file = work_path / 'requirements.txt'

requirements_env_variables = []
if requirements_file.exists():
requirements_env_variables = get_requirements_env_variables(requirements_file)
if requirements_file.exists():
requirements_env_variables = get_requirements_env_variables(
requirements_file
)
for index, env in enumerate(requirements_env_variables):
if check_requirements_env_variable(env) is False:
raise Exception( f'The requirements.txt environment variables:`${env}` can only consist of uppercase letter and number and underline.')
raise Exception(
f'The requirements.txt environment variables:`${env}` can only consist of uppercase letter and number and underline.'
)

if len(requirements_env_variables) and not build_env:
env_variables_str = ','.join(requirements_env_variables);
error_str= f'The requirements.txt set environment variables as follows:`{env_variables_str}` should use `--build-env';
env_variables_str = ','.join(requirements_env_variables)
error_str = f'The requirements.txt set environment variables as follows:`{env_variables_str}` should use `--build-env'
for item in requirements_env_variables:
error_str+= f' {item}=YOUR_VALUE'
error_str += f' {item}=YOUR_VALUE'
raise Exception(f'{error_str}` to add it.')
elif len(requirements_env_variables) and build_env:
build_env_keys = list(build_env.keys())
diff_env_variables = list(set(requirements_env_variables).difference(set(build_env_keys)))
diff_env_variables = list(
set(requirements_env_variables).difference(set(build_env_keys))
)
if len(diff_env_variables):
diff_env_variables_str = ",".join(diff_env_variables)
error_str= f'The requirements.txt set environment variables as follows:`{diff_env_variables_str}` should use `--build-env';
error_str = f'The requirements.txt set environment variables as follows:`{diff_env_variables_str}` should use `--build-env'
for item in diff_env_variables:
error_str+= f' {item}=YOUR_VALUE'
error_str += f' {item}=YOUR_VALUE'
raise Exception(f'{error_str}` to add it.')

console = get_rich_console()
Expand Down Expand Up @@ -421,7 +431,7 @@ def push(self) -> None:
if dockerfile:
form_data['dockerfile'] = str(dockerfile)

if build_env:
if build_env:
form_data['buildEnv'] = json.dumps(build_env)

uuid8, secret = load_secret(work_path)
Expand Down Expand Up @@ -606,7 +616,6 @@ def _get_prettyprint_usage(self, console, executor_name, usage_kind=None):

console.print(Panel(param_str, title='Usage', expand=False, width=100))


def _prettyprint_build_env_usage(self, console, build_env, usage_kind=None):
from rich import box
from rich.panel import Panel
Expand All @@ -620,12 +629,17 @@ def _prettyprint_build_env_usage(self, console, build_env, usage_kind=None):
param_str.add_column('Your value')

for index, item in enumerate(build_env):
param_str.add_row(
f'{item}',
'your value'
)
param_str.add_row(f'{item}', 'your value')

console.print(Panel(param_str, title='build_env', subtitle='You have to set the above environment variables', expand=False, width=100))
console.print(
Panel(
param_str,
title='build_env',
subtitle='You have to set the above environment variables',
expand=False,
width=100,
)
)

@staticmethod
@disk_cache_offline(cache_file=str(get_cache_db()))
Expand Down Expand Up @@ -698,7 +712,7 @@ def _send_request_with_retry(url, **kwargs):
image_name=image_name,
archive_url=resp['package']['download'],
md5sum=resp['package']['md5'],
build_env=buildEnv.keys() if buildEnv else []
build_env=list(buildEnv.keys()) if buildEnv else [],
)

@staticmethod
Expand Down Expand Up @@ -854,7 +868,7 @@ def pull(self) -> str:
)

build_env = executor.build_env

presented_id = executor.name if executor.name else executor.uuid
executor_name = (
f'{presented_id}'
Expand Down Expand Up @@ -887,8 +901,8 @@ def pull(self) -> str:
import filelock

if build_env:
self._prettyprint_build_env_usage(console,build_env)
self._prettyprint_build_env_usage(console, build_env)

with filelock.FileLock(get_lockfile(), timeout=-1):
try:
pkg_path, pkg_dist_path = get_dist_path_of_executor(
Expand Down