From c99b20d83cdbd37c945fa8af1fd0204ff4522941 Mon Sep 17 00:00:00 2001 From: TheoLisin Date: Mon, 20 Nov 2023 14:40:41 +0000 Subject: [PATCH] update sly-net for old agent --- agent/worker/agent.py | 7 +++++++ agent/worker/task_update.py | 30 +++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/agent/worker/agent.py b/agent/worker/agent.py index 9180fad..6ebb86a 100644 --- a/agent/worker/agent.py +++ b/agent/worker/agent.py @@ -18,6 +18,7 @@ import supervisely_lib as sly from worker.agent_utils import TaskDirCleaner, AppDirCleaner +from worker.task_update import check_and_pull_sly_net warnings.filterwarnings(action="ignore", category=UserWarning) @@ -126,6 +127,12 @@ def _update_net_client(self, dc: docker.DockerClient): ) ) return + else: + # pull if update to old agent + agnet_checked_sly_net = os.getenv("AGNET_CHECKED_SLY_NET", None) + cur_id = sly_net_container.attrs.get("Image", None) + if agnet_checked_sly_net is None: + check_and_pull_sly_net(dc, cur_id, self.logger, sly_net_hub_name) network = "supervisely-net-{}".format(constants.TOKEN()) command = sly_net_container.attrs.get("Args") diff --git a/agent/worker/task_update.py b/agent/worker/task_update.py index 0c09bdd..7e91dcb 100644 --- a/agent/worker/task_update.py +++ b/agent/worker/task_update.py @@ -1,5 +1,6 @@ # coding: utf-8 +from logging import Logger import os import json import supervisely_lib as sly @@ -61,9 +62,11 @@ def task_main_func(self): new_envs.append(val) cur_envs = new_envs cur_envs.append("REMOVE_OLD_AGENT={}".format(cur_container_id)) + cur_envs.append("AGNET_CHECKED_SLY_NET=1") # Pull net-client if needed net_container_name = "supervisely-net-client-{}".format(constants.TOKEN()) + sly_net_hub_name = "supervisely/sly-net-client:latest" sly_net_container = None for container in self._docker_api.containers.list(): @@ -76,15 +79,8 @@ def task_main_func(self): "Something goes wrong: can't find sly-net-client attached to this agent" ) else: - ic = ImageCollection(self._docker_api) - sly_net_hub_name = "supervisely/sly-net-client:latest" - docker_hub_image_info = ic.get_registry_data(sly_net_hub_name) - - if sly_net_container.attrs.get("Image", None) == docker_hub_image_info.id: - self.logger.info("sly-net-client is already updated") - else: - self.logger.info("Found new version of sly-net-client. Pulling...") - sly.docker_utils._docker_pull_progress(self._docker_api, sly_net_hub_name, self.logger) + cur_id = sly_net_container.attrs.get("Image", None) + check_and_pull_sly_net(self._docker_api, cur_id, self.logger, sly_net_hub_name) # start new agent container = self._docker_api.containers.run( @@ -119,3 +115,19 @@ def run_shell_command(cmd, print_output=False): if print_output: print(clean_line) return res + + +def check_and_pull_sly_net( + dc: docker.DockerClient, + cur_id: str, + logger: Logger, + sly_net_hub_name: str = "supervisely/sly-net-client:latest", +): + ic = ImageCollection(dc) + docker_hub_image_info = ic.get_registry_data(sly_net_hub_name) + + if cur_id == docker_hub_image_info.id: + logger.info("sly-net-client is already updated") + else: + logger.info("Found new version of sly-net-client. Pulling...") + sly.docker_utils._docker_pull_progress(dc, sly_net_hub_name, logger)