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

feat(client): migrate to new version server api #863

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions client/starwhale/base/bundle_copy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os.path
from http import HTTPStatus
from pathlib import Path

Expand Down Expand Up @@ -82,7 +83,7 @@ def _check_cloud_obj_existed(self, uri: URI) -> bool:
# TODO: add more params for project
# TODO: tune controller api, use unified params name
ok, _ = self.do_http_request_simple_ret(
path=f"/project/{self.typ}",
path=self._get_remote_instance_rc_url(),
method=HTTPMethod.HEAD,
instance_uri=uri,
params={
Expand Down Expand Up @@ -112,6 +113,13 @@ def _is_existed(self, uri: URI) -> bool:
else:
return self._get_target_path(uri).exists()

def _get_remote_instance_rc_url(self) -> str:
_obj = self.src_uri.object
if self.src_uri.instance_type == InstanceType.CLOUD:
return f"/project/{self.src_uri.project}/{self.typ}/{_obj.name}/version/{_obj.version}/file"
else:
return f"/project/{self.dest_uri.project}/{self.typ}/{_obj.name}/version/{_obj.version}/file"

def _do_upload_bundle_tar(self, progress: Progress) -> None:
file_path = self._get_target_path(self.src_uri)
task_id = progress.add_task(
Expand All @@ -120,7 +128,7 @@ def _do_upload_bundle_tar(self, progress: Progress) -> None:
)

self.do_multipart_upload_file(
url_path=f"/project/{self.typ}/push",
url_path=self._get_remote_instance_rc_url(),
file_path=file_path,
instance_uri=self.dest_uri,
fields={
Expand All @@ -135,11 +143,12 @@ def _do_upload_bundle_tar(self, progress: Progress) -> None:

def _do_download_bundle_tar(self, progress: Progress) -> None:
file_path = self._get_target_path(self.dest_uri)
ensure_dir(os.path.dirname(file_path))
task_id = progress.add_task(f":bowling: download to {file_path}...")

self.do_download_file(
url_path=f"/project/{self.typ}/pull",
dest_path=self._get_target_path(self.dest_uri),
url_path=self._get_remote_instance_rc_url(),
dest_path=file_path,
instance_uri=self.src_uri,
params={
_query_param_map[self.typ]: f"{self.bundle_name}:{self.bundle_version}",
Expand Down Expand Up @@ -191,7 +200,7 @@ def _do_upload_bundle_dir(self, progress: Progress) -> None:
_manifest_path = _workdir / DEFAULT_MANIFEST_NAME
_key = _query_param_map[self.typ]
_name = f"{self.bundle_name}:{self.bundle_version}"
_url_path = f"/project/{self.typ}/push"
_url_path = self._get_remote_instance_rc_url()

task_id = progress.add_task(
f":arrow_up: {_manifest_path.name}",
Expand Down Expand Up @@ -294,7 +303,7 @@ def _do_download_bundle_dir(self, progress: Progress) -> None:
def _download(_target: Path, _part: str, _tid: TaskID) -> None:
self.do_download_file(
# TODO: use /project/{self.typ}/pull api
url_path=f"/project/{self.typ}/pull",
url_path=self._get_remote_instance_rc_url(),
dest_path=_target,
instance_uri=self.src_uri,
params={
Expand Down
16 changes: 8 additions & 8 deletions client/tests/base/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ def setUp(self) -> None:
def test_upload_bundle_file(self, rm: Mocker) -> None:
rm.request(
HTTPMethod.HEAD,
"http://1.1.1.1:8182/api/v1/project/model",
"http://1.1.1.1:8182/api/v1/project/project/model/mnist/version/abcdefg1234/file",
json={"message": "not found"},
status_code=HTTPStatus.NOT_FOUND,
)
rm.request(
HTTPMethod.POST,
"http://1.1.1.1:8182/api/v1/project/model/push",
"http://1.1.1.1:8182/api/v1/project/project/model/mnist/version/abcdefg1234/file",
)

model_dir = self._sw_config.rootdir / "self" / "model" / "mnist" / "ab"
Expand All @@ -58,13 +58,13 @@ def test_upload_bundle_file(self, rm: Mocker) -> None:
def test_download_bundle_file(self, rm: Mocker) -> None:
rm.request(
HTTPMethod.HEAD,
"http://1.1.1.1:8182/api/v1/project/model",
"http://1.1.1.1:8182/api/v1/project/1/model/mnist/version/latest/file",
json={"message": "existed"},
status_code=HTTPStatus.OK,
)
rm.request(
HTTPMethod.GET,
"http://1.1.1.1:8182/api/v1/project/model/pull",
"http://1.1.1.1:8182/api/v1/project/1/model/mnist/version/latest/file",
content=b"test",
)

Expand All @@ -86,13 +86,13 @@ def test_download_bundle_file(self, rm: Mocker) -> None:
def test_upload_bundle_dir(self, rm: Mocker) -> None:
rm.request(
HTTPMethod.HEAD,
"http://1.1.1.1:8182/api/v1/project/dataset",
"http://1.1.1.1:8182/api/v1/project/project/dataset/mnist/version/abcde/file",
json={"message": "already existed"},
status_code=HTTPStatus.OK,
)
rm.request(
HTTPMethod.POST,
"http://1.1.1.1:8182/api/v1/project/dataset/push",
"http://1.1.1.1:8182/api/v1/project/project/dataset/mnist/version/abcde/file",
json={"data": {"upload_id": 1}},
)

Expand Down Expand Up @@ -121,13 +121,13 @@ def test_upload_bundle_dir(self, rm: Mocker) -> None:
def test_download_bundle_dir(self, rm: Mocker) -> None:
rm.request(
HTTPMethod.HEAD,
"http://1.1.1.1:8182/api/v1/project/dataset",
"http://1.1.1.1:8182/api/v1/project/1/dataset/mnist/version/latest/file",
json={"message": "existed"},
status_code=HTTPStatus.OK,
)
rm.request(
HTTPMethod.GET,
"http://1.1.1.1:8182/api/v1/project/dataset/pull",
"http://1.1.1.1:8182/api/v1/project/1/dataset/mnist/version/latest/file",
json={"signature": ["1", "2"]},
)
bc = BundleCopy(
Expand Down