Skip to content

Commit 32f0f75

Browse files
Bootstrap: check version-chooser in core and restart it if unresponsive
1 parent c0a73c4 commit 32f0f75

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

bootstrap/bootstrap/bootstrap.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from warnings import warn
1010

1111
import docker
12+
import requests
1213

1314

1415
class Bootstrapper:
@@ -18,6 +19,7 @@ class Bootstrapper:
1819
DOCKER_CONFIG_FILE_PATH = DOCKER_CONFIG_PATH.joinpath("bootstrap/startup.json")
1920
HOST_CONFIG_PATH = os.environ.get("COMPANION_CONFIG_PATH", "/tmp/companion/.config")
2021
CORE_CONTAINER_NAME = "companion-core"
22+
core_last_response_time = time.time()
2123

2224
def __init__(self, client: docker.DockerClient, low_level_api: docker.APIClient = None) -> None:
2325
self.client: docker.DockerClient = client
@@ -151,6 +153,7 @@ def start(self, component_name: str) -> bool:
151153
"""
152154
image_version = "stable"
153155

156+
self.core_last_response_time = time.time()
154157
self.config = Bootstrapper.read_config_file()
155158

156159
image = self.config[component_name]
@@ -198,15 +201,28 @@ def is_running(self, component: str) -> bool:
198201
Returns:
199202
bool: True if the chosen container is running
200203
"""
201-
for container in self.client.containers.list():
202-
if container.name.endswith(component):
203-
return True
204-
return False
204+
if not any(container.name.endswith(component) for container in self.client.containers.list()):
205+
return False
206+
207+
if component == "core":
208+
try:
209+
response = requests.get("http://localhost/version-chooser/v1.0/version/current")
210+
if "core" in response.json()["repository"]:
211+
self.core_last_response_time = time.time()
212+
return True
213+
except Exception as e:
214+
print(f"Could not talk to version chooser for {time.time() - self.core_last_response_time}: {e}")
215+
if time.time() - self.core_last_response_time > 60:
216+
print("Reseting startup.json...")
217+
self.overwrite_config_file_with_defaults()
218+
return False
219+
return True
205220

206221
def remove(self, container: str) -> None:
207222
"""Deletes the chosen container if it exists (needed for updating the running image)"""
208223
try:
209224
old_container = self.client.containers.get(f"companion-{container}")
225+
old_container.stop()
210226
old_container.remove()
211227
except docker.errors.NotFound:
212228
# This exception is raised if the container does not exist

bootstrap/bootstrap/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
version="0.0.1",
88
description="Blue Robotics Ardusub Companion Docker System Bootstrap",
99
license="MIT",
10-
install_requires=["docker == 5.0.0", "six == 1.15.0"],
10+
install_requires=["docker == 5.0.0", "six == 1.15.0", "requests == 2.26.0"],
1111
)

bootstrap/test_bootstrap.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def set_client(self, client: Any) -> None:
5757
def remove(self) -> None:
5858
self.client.containers.remove(self.name)
5959

60+
def stop(self) -> None:
61+
pass
62+
6063

6164
class FakeContainers:
6265
"""Mocks "Containers" class from docker-py"""

install/install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ docker run \
9191
-t \
9292
--restart unless-stopped \
9393
--name companion-bootstrap \
94+
--net=host \
9495
-v $HOME/.config/companion/bootstrap:/root/.config/bootstrap \
9596
-v /var/run/docker.sock:/var/run/docker.sock \
9697
-e COMPANION_CONFIG_PATH=$HOME/.config/companion \

0 commit comments

Comments
 (0)