Skip to content

Commit 87771da

Browse files
authored
fix: update decline in a simple way (#639)
* fix: update decline in a simple way * narrow down auto-update conditions to "all containers missing" and "newly installed" * add missing return * fix all containers missing case (2) * fix ports diff * status show optional services when they are existed * fix status * remove debug prints
1 parent b53ef31 commit 87771da

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

images/utils/launcher/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,16 @@ def pre_start(self):
227227
self.close_other_utils()
228228

229229
def start(self):
230+
up_env = True
230231
try:
231-
self.node_manager.update()
232+
up_env = self.node_manager.update()
232233
except ParallelExecutionError:
233234
pass
234-
self.node_manager.up()
235-
self.pre_start()
235+
236+
if up_env:
237+
self.node_manager.up()
238+
self.pre_start()
239+
236240
self.shell.start(f"{self.config.network} > ", self.handle_command)
237241

238242

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import docker
2+
import docker.errors
3+
from docker.models.containers import Container
4+
from typing import Optional
5+
6+
7+
class DockerTemplate:
8+
def __init__(self):
9+
self.client = docker.from_env()
10+
11+
def get_container(self, name: str) -> Optional[Container]:
12+
try:
13+
return self.client.containers.get(name)
14+
except docker.errors.NotFound:
15+
return None

images/utils/launcher/node/__init__.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .lnd import Lndbtc, Lndltc
2222
from .webui import Webui
2323
from .xud import Xud, XudApiError
24+
from .DockerTemplate import DockerTemplate
2425
from ..config import Config
2526
from ..errors import FatalError
2627
from ..shell import Shell
@@ -125,6 +126,8 @@ def __init__(self, config, shell):
125126
self.cmd_stop = StopCommand(self.get_node, self.shell)
126127
self.cmd_restart = RestartCommand(self.get_node, self.shell)
127128

129+
self.docker_template = DockerTemplate()
130+
128131
@property
129132
def network_name(self):
130133
return self.network + "_default"
@@ -176,7 +179,7 @@ def up(self):
176179
nodes = self.valid_nodes.values()
177180

178181
def print_failed(failed):
179-
print("Failed to start these nodes.")
182+
print("Failed to start these services:")
180183
for f in failed:
181184
print(f"- {f[0].name}: {str(f[1])}")
182185

@@ -219,11 +222,12 @@ def _readable_details(self, details):
219222
diff_keys = [key for key, value in details.items() if not value.same]
220223
return ", ".join(diff_keys)
221224

222-
def update(self):
225+
def update(self) -> bool:
223226
if self.config.disable_update:
224-
return
227+
return True
225228

226229
outdated = False
230+
image_outdated = False
227231

228232
# Step 1. check all images
229233
print("🌍 Checking for updates...")
@@ -237,6 +241,7 @@ def update(self):
237241
if status in ["LOCAL_MISSING", "LOCAL_OUTDATED"]:
238242
print("- Image %s: %s" % (image.name, image.status_message))
239243
outdated = True
244+
image_outdated = True
240245
elif status == "UNAVAILABLE":
241246
all_unavailable_images = [x for x in images if x.status == "UNAVAILABLE"]
242247
raise FatalError("Image(s) not available: %r" % all_unavailable_images)
@@ -277,12 +282,18 @@ def handle_result(container, result):
277282

278283
if not outdated:
279284
print("All up-to-date.")
280-
return
285+
return True
281286

282287
all_containers_missing = functools.reduce(lambda a, b: a and b[0] in ["missing", "external", "disabled"], container_check_result.values(), True)
283288

284289
if all_containers_missing:
285-
answer = "yes"
290+
if self.newly_installed:
291+
answer = "yes"
292+
else:
293+
if image_outdated:
294+
answer = "yes"
295+
else:
296+
return True # FIXME unintended containers (configuration) update
286297
else:
287298
answer = self.shell.yes_or_no("A new version is available. Would you like to upgrade (Warning: this may restart your environment and cancel all open orders)?")
288299

@@ -297,6 +308,9 @@ def handle_result(container, result):
297308
# 2.2) recreate outdated containers
298309
for container, result in container_check_result.items():
299310
container.update(result)
311+
return True
312+
else:
313+
return False
300314

301315
def logs(self, *args):
302316
self.cmd_logs.execute(args)
@@ -313,9 +327,21 @@ def restart(self, *args):
313327
def cli(self, name, *args):
314328
self.get_node(name).cli(" ".join(args), self.shell)
315329

330+
def _get_status_nodes(self):
331+
optional_nodes = ["arby", "boltz", "webui"]
332+
result = {}
333+
for node in self.nodes.values():
334+
if node.name in optional_nodes:
335+
c = self.docker_template.get_container(node.container_name)
336+
if c:
337+
result[node.name] = node
338+
else:
339+
result[node.name] = node
340+
return result
341+
316342
def status(self):
317343
# TODO migrate to ServiceTable
318-
nodes = self.enabled_nodes
344+
nodes = self._get_status_nodes()
319345
names = list(nodes)
320346

321347
BRIGHT_BLACK = "\033[90m"

images/utils/launcher/node/base.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,24 @@ def compare_volumes(self, container: Container) -> CompareResult:
390390
return CompareResult(False, "Volumes are different", old, new)
391391
return CompareResult(True, "", old, new)
392392

393+
def _normalize_docker_port_bindings(self, port_bindings):
394+
result = []
395+
for key, value in port_bindings.items():
396+
if value:
397+
mapping = []
398+
for p in value:
399+
host_ip = p["HostIp"]
400+
if host_ip == "":
401+
host_ip = "0.0.0.0"
402+
host_port = p["HostPort"]
403+
mapping.append(host_ip + ":" + host_port)
404+
result.append(key + "-" + ",".join(mapping))
405+
return result
406+
393407
def compare_ports(self, container: Container) -> CompareResult:
394408
attrs = container.attrs
395-
ports = attrs["NetworkSettings"]["Ports"]
396-
old_ports = [key + "-" + ",".join([p["HostIp"] + ":" + p["HostPort"] for p in value]) for key, value in ports.items() if value is not None]
409+
port_bindings = attrs["HostConfig"]["PortBindings"]
410+
old_ports = self._normalize_docker_port_bindings(port_bindings)
397411

398412
def normalize(value):
399413
if isinstance(value, int):

0 commit comments

Comments
 (0)