99from warnings import warn
1010
1111import docker
12+ import requests
1213
1314
1415class 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
0 commit comments