Skip to content

Commit

Permalink
Move sly-net pulling in old agent task (#50)
Browse files Browse the repository at this point in the history
* put sly-net pulling in pld agent task

* fix name of constant

* update sly-net for old agent

* add debug msg

* fix local and hub digest diff problem

* added upd skip if no need
  • Loading branch information
TheoLisin authored Nov 21, 2023
1 parent 97f7fd4 commit 69f1f53
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
28 changes: 19 additions & 9 deletions agent/worker/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_if_needed

warnings.filterwarnings(action="ignore", category=UserWarning)

Expand Down Expand Up @@ -106,7 +107,12 @@ def _remove_old_agent(self):
agent_same_token[0].rename(agent_name_start)

def _update_net_client(self, dc: docker.DockerClient):
need_update_env = os.getenv("UPDATE_SLY_NET_AFTER_RESTART", None)
if need_update_env == "0":
return

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 dc.containers.list():
Expand All @@ -118,18 +124,22 @@ def _update_net_client(self, dc: docker.DockerClient):
self.logger.warn(
"Something goes wrong: can't find sly-net-client attached to this agent"
)
self.logger.warn(
(
"Probably you should reastart agent manually using instructions:"
"https://developer.supervisely.com/getting-started/connect-your-computer"
)
)
return
else:
# pull if update too old agent
if need_update_env is None:
need_update = check_and_pull_sly_net_if_needed(
dc, sly_net_container, self.logger, sly_net_hub_name
)

ic = ImageCollection(dc)
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")
if need_update is False:
return
else:
self.logger.info("Found new version of sly-net-client. Updating current container.")
sly.docker_utils._docker_pull_progress(dc, sly_net_hub_name, self.logger)

network = "supervisely-net-{}".format(constants.TOKEN())
command = sly_net_container.attrs.get("Args")
Expand Down
4 changes: 3 additions & 1 deletion agent/worker/task_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ def _exec_command(self, command, add_envs=None, container_id=None):
"SERVER_ADDRESS": self.info["server_address"],
"API_TOKEN": self.info["api_token"],
"AGENT_TOKEN": constants.TOKEN(),
constants._REQUESTS_CA_BUNDLE: constants.REQUESTS_CA_BUNDLE_CONTAINER(),
"PIP_ROOT_USER_ACTION": "ignore",
**add_envs,
},
Expand Down Expand Up @@ -684,6 +683,9 @@ def main_step_envs(self):
if constants.SUPERVISELY_AGENT_FILES() is not None:
envs["AGENT_STORAGE"] = constants.AGENT_FILES_IN_APP_CONTAINER()

if constants.REQUESTS_CA_BUNDLE() is not None:
envs[constants._REQUESTS_CA_BUNDLE] = constants.REQUESTS_CA_BUNDLE_CONTAINER()

# Handle case for some dockerimages where env names with dot sumbol are not supported
final_envs = copy.deepcopy(envs)
for k, v in envs.items():
Expand Down
3 changes: 1 addition & 2 deletions agent/worker/task_dockerized.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def main_step_envs(self):
envs[env_key] = env_val

if constants.REQUESTS_CA_BUNDLE() is not None:
envs["REQUESTS_CA_BUNDLE"] = constants.REQUESTS_CA_BUNDLE_IN_CONTAINER()
envs[constants._REQUESTS_CA_BUNDLE] = constants.REQUESTS_CA_BUNDLE_CONTAINER()

return envs

Expand Down Expand Up @@ -199,7 +199,6 @@ def spawn_container(self, add_envs=None, add_labels=None, entrypoint_func=None):
constants._HTTP_PROXY.lower(): constants.HTTP_PROXY(),
constants._HTTPS_PROXY.lower(): constants.HTTPS_PROXY(),
constants._NO_PROXY.lower(): constants.NO_PROXY(),
constants._REQUESTS_CA_BUNDLE: constants.REQUESTS_CA_BUNDLE_CONTAINER(),
"PIP_ROOT_USER_ACTION": "ignore",
**add_envs,
}
Expand Down
49 changes: 48 additions & 1 deletion agent/worker/task_update.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# coding: utf-8

from logging import Logger
import os
import json
import supervisely_lib as sly
from .task_dockerized import TaskSly
import subprocess
import docker
from docker.models.containers import Container
from docker.models.images import ImageCollection
from docker.errors import DockerException, ImageNotFound
from worker import constants

Expand All @@ -15,7 +19,7 @@ def docker_api(self):
return self._docker_api

@docker_api.setter
def docker_api(self, val):
def docker_api(self, val: docker.DockerClient):
self._docker_api = val

def task_main_func(self):
Expand Down Expand Up @@ -59,6 +63,30 @@ def task_main_func(self):
cur_envs = new_envs
cur_envs.append("REMOVE_OLD_AGENT={}".format(cur_container_id))

# 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():
if container.name == net_container_name:
sly_net_container: Container = container
break

if sly_net_container is None:
self.logger.warn(
"Something goes wrong: can't find sly-net-client attached to this agent"
)
else:
need_update = check_and_pull_sly_net_if_needed(
self._docker_api, sly_net_container, self.logger, sly_net_hub_name
)
if need_update is True:
cur_envs.append("UPDATE_SLY_NET_AFTER_RESTART=1")
else:
cur_envs.append("UPDATE_SLY_NET_AFTER_RESTART=0")

# start new agent
container = self._docker_api.containers.run(
self.info["docker_image"],
runtime=self.info["config"]["docker_runtime"],
Expand Down Expand Up @@ -91,3 +119,22 @@ def run_shell_command(cmd, print_output=False):
if print_output:
print(clean_line)
return res


def check_and_pull_sly_net_if_needed(
dc: docker.DockerClient,
cur_container: Container,
logger: Logger,
sly_net_hub_name: str = "supervisely/sly-net-client:latest",
) -> bool:
ic = ImageCollection(dc)
docker_hub_image_info = ic.get_registry_data(sly_net_hub_name)
name_with_digest: str = cur_container.image.attrs.get("RepoDigests", [""])[0]

if name_with_digest.endswith(docker_hub_image_info.id):
logger.info("sly-net-client is already updated")
return False
else:
logger.info("Found new version of sly-net-client. Pulling...")
sly.docker_utils._docker_pull_progress(dc, sly_net_hub_name, logger)
return True

0 comments on commit 69f1f53

Please sign in to comment.