Skip to content
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
25 changes: 21 additions & 4 deletions bootstrap/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from warnings import warn

import docker
import requests


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

def __init__(self, client: docker.DockerClient, low_level_api: docker.APIClient = None) -> None:
self.client: docker.DockerClient = client
Expand Down Expand Up @@ -151,6 +153,8 @@ def start(self, component_name: str) -> bool:
"""
image_version = "stable"

if component_name == "core":
self.core_last_response_time = time.time()
self.config = Bootstrapper.read_config_file()

image = self.config[component_name]
Expand Down Expand Up @@ -198,15 +202,28 @@ def is_running(self, component: str) -> bool:
Returns:
bool: True if the chosen container is running
"""
for container in self.client.containers.list():
if container.name.endswith(component):
return True
return False
if not any(container.name.endswith(component) for container in self.client.containers.list()):
return False

if component == "core":
try:
response = requests.get("http://localhost/version-chooser/v1.0/version/current")
if "core" in response.json()["repository"]:
self.core_last_response_time = time.time()
return True
except Exception as e:
print(f"Could not talk to version chooser for {time.time() - self.core_last_response_time}: {e}")
if time.time() - self.core_last_response_time > 60:
print("Reseting startup.json...")
self.overwrite_config_file_with_defaults()
return False
return True

def remove(self, container: str) -> None:
"""Deletes the chosen container if it exists (needed for updating the running image)"""
try:
old_container = self.client.containers.get(f"companion-{container}")
old_container.stop()
old_container.remove()
except docker.errors.NotFound:
# This exception is raised if the container does not exist
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/bootstrap/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
version="0.0.1",
description="Blue Robotics Ardusub Companion Docker System Bootstrap",
license="MIT",
install_requires=["docker == 5.0.0", "six == 1.15.0"],
install_requires=["docker == 5.0.0", "six == 1.15.0", "requests == 2.26.0"],
)
3 changes: 3 additions & 0 deletions bootstrap/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def set_client(self, client: Any) -> None:
def remove(self) -> None:
self.client.containers.remove(self.name)

def stop(self) -> None:
pass


class FakeContainers:
"""Mocks "Containers" class from docker-py"""
Expand Down
1 change: 1 addition & 0 deletions install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ docker run \
-t \
--restart unless-stopped \
--name companion-bootstrap \
--net=host \
-v $HOME/.config/companion/bootstrap:/root/.config/bootstrap \
-v /var/run/docker.sock:/var/run/docker.sock \
-e COMPANION_CONFIG_PATH=$HOME/.config/companion \
Expand Down