Skip to content

Commit

Permalink
agent options check
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybart1337 committed Oct 30, 2024
1 parent 2222657 commit 0c5fbb7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ COPY agent /workdir/agent
#ENV PYTHONPATH /workdir:/workdir/src:/workdir/supervisely_lib/worker_proto:$PYTHONPATH
WORKDIR /workdir/agent

ENTRYPOINT ["sh", "-c", "python -u /workdir/agent/main.py"]

ENTRYPOINT ["python", "-u", "/workdir/agent/main.py"]
22 changes: 16 additions & 6 deletions agent/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,22 @@ def init_envs():
try:
agent_utils.check_instance_version()
new_envs, new_volumes, ca_cert = agent_utils.updated_agent_options()
except agent_utils.AgentOptionsNotAvailable:
sly.logger.debug("Can not update agent options", exc_info=True)
sly.logger.warning(
"Can not update agent options. Agent will be started with current options"
)
return
except Exception as e:
if not agent_utils.is_agent_container_ready_to_continue():
sly.logger.error(
"Agent options are not available. Agent will be stopped. Please, check the connection to the server"
)
raise

if isinstance(e, agent_utils.AgentOptionsNotAvailable):
sly.logger.debug("Can not update agent options", exc_info=True)
sly.logger.warning(
"Can not update agent options. Agent will be started with current options"
)
return

raise

if new_envs.get(constants._FORCE_CPU_ONLY, "false") == "true":
runtime = "runc"
runtime_changed = _is_runtime_changed(runtime)
Expand Down
20 changes: 17 additions & 3 deletions agent/worker/agent_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ def get_agent_options(server_address=None, token=None, timeout=60) -> dict:
method,
data={"token": token},
)
if resp.status_code != requests.codes.ok: # pylint: disable=no-member
if resp is None or resp.status_code != requests.codes.ok: # pylint: disable=no-member
try:
text = resp.text
except:
Expand All @@ -641,8 +641,8 @@ def get_instance_version(server_address=None, timeout=60):

api = sly.Api(server_address=server_address)
resp = api.get("instance.version", {})
if resp.status_code != requests.codes.ok: # pylint: disable=no-member
if resp.status_code in (400, 401, 403, 404):
if resp is None or resp.status_code != requests.codes.ok: # pylint: disable=no-member
if resp is not None and resp.status_code in (400, 401, 403, 404):
return None
try:
text = resp.text
Expand Down Expand Up @@ -934,6 +934,20 @@ def _ca_cert_changed(ca_cert) -> str:
return cert_path


def is_agent_container_ready_to_continue():
container_info = get_container_info()
volumes = binds_to_volumes_dict(container_info.get("HostConfig", {}).get("Binds", []))

# should contain at least 3 volumes:
# docker socket
# agent data files
# apps data files
if len(volumes) < 3:
return False

return True


def get_options_changes(envs: dict, volumes: dict, ca_cert: str) -> Tuple[dict, dict, str]:
return _envs_changes(envs), _volumes_changes(volumes), _ca_cert_changed(ca_cert)

Expand Down

0 comments on commit 0c5fbb7

Please sign in to comment.