-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupdate_manager.py
More file actions
1669 lines (1498 loc) · 69.6 KB
/
Copy pathupdate_manager.py
File metadata and controls
1669 lines (1498 loc) · 69.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import json
import logging
import os
import subprocess
import tempfile
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
import docker.errors
from docker.types import DeviceRequest, LogConfig, Mount, Ulimit
import sampler
from docker_client import get_api_client, get_docker_client
from users_db import (
UPDATE_HISTORY_RETENTION_DAYS,
get_auto_update_settings,
get_update_history_entry,
list_latest_successful_update_timestamps,
list_update_history,
record_update_history,
set_auto_update_target,
)
EXPERIMENTAL_NOTICE = (
"Experimental feature. It is designed to preserve user data, volumes, "
"configuration, networks and environment, but every update should still "
"be reviewed carefully before applying it."
)
UPDATE_HISTORY_NOTICE = (
f"Update history entries are removed automatically {UPDATE_HISTORY_RETENTION_DAYS} days "
"after they are recorded."
)
UPDATE_REFRESH_MAX_WORKERS = 8
COMPOSE_RECONSTRUCTION_LIMITATION = (
"statainer does not reconstruct Compose projects from running "
"containers alone because Docker metadata does not preserve override "
"merge order, env files, build contexts, secrets, configs, or services "
"that are not currently running."
)
EXTERNAL_SAFE_RECREATE_NOTICE = (
"statainer can still update the running services directly with its "
"safe container recreation workflow, which preserves the current mounts, "
"networks, environment, restart policy, and other materialized runtime "
"settings from Docker."
)
KNOWN_EXTERNAL_COMPOSE_MANAGERS = (
{
'key': 'portainer',
'name': 'Portainer',
'path_fragments': ('/data/compose/',),
'reason': (
"This stack appears to be managed by Portainer. Docker labels point "
"to compose files in Portainer's data directory, which statainer "
"cannot access directly from this host."
),
'action_hint': 'Project updates are disabled here because Portainer owns the compose definition.',
'recovery_hint': (
"Export the stack from Portainer or recover it from the original Git "
"repository, then redeploy it from a host path that is mounted into "
"statainer if you want this application to manage updates."
),
},
{
'key': 'yacht',
'name': 'Yacht',
'path_fragments': ('/config/compose/', 'config/compose/'),
'reason': (
"This stack appears to be managed by Yacht. Docker labels point to "
"compose files inside Yacht's COMPOSE_DIR, which statainer "
"cannot access directly from this host."
),
'action_hint': 'Project updates are disabled here because Yacht owns the compose definition.',
'recovery_hint': (
"Mount Yacht's COMPOSE_DIR into statainer or export the project "
"and redeploy it from a host path accessible to this application."
),
},
)
def _data_dir():
path = os.path.join(os.path.dirname(__file__), 'data', 'update_manager')
os.makedirs(path, exist_ok=True)
return path
def _rollback_dir():
path = os.path.join(_data_dir(), 'rollback_overrides')
os.makedirs(path, exist_ok=True)
return path
def _short_image_token(value):
raw = str(value or '').strip()
if raw.startswith('sha256:'):
raw = raw.split(':', 1)[1]
return raw[:12] if raw else 'unknown'
def _repo_name_from_image_ref(image_ref):
normalized = str(image_ref or '').strip()
if '@' in normalized:
return normalized.split('@', 1)[0]
last_segment = normalized.rsplit('/', 1)[-1]
if ':' in last_segment:
return normalized.rsplit(':', 1)[0]
return normalized
def _container_image_ref(container):
try:
image_ref = str(container.attrs.get('Config', {}).get('Image') or '').strip()
except Exception:
image_ref = ''
if image_ref:
return image_ref
tags = getattr(getattr(container, 'image', None), 'tags', None) or []
return str(tags[0]).strip() if tags else ''
def _local_digest_for_image(image, image_ref):
if image is None:
return None
try:
repo_digests = image.attrs.get('RepoDigests', []) or []
except Exception:
repo_digests = []
repo_name = _repo_name_from_image_ref(image_ref)
for digest_ref in repo_digests:
if digest_ref.startswith(f'{repo_name}@'):
return digest_ref.split('@', 1)[1]
if repo_digests:
return repo_digests[0].split('@', 1)[1] if '@' in repo_digests[0] else repo_digests[0]
image_id = getattr(image, 'id', None)
return image_id.split(':', 1)[1] if isinstance(image_id, str) and ':' in image_id else image_id
def _remote_digest_for_image(client, image_ref):
if not image_ref or '@sha256:' in image_ref:
return None
try:
registry_data = client.images.get_registry_data(image_ref)
return registry_data.id
except Exception as exc:
logging.warning("Unable to resolve registry digest for %s: %s", image_ref, exc)
return None
def _format_version(image_ref, token):
if token:
return f'{image_ref} @ {_short_image_token(token)}'
return image_ref or 'Unknown image'
def _container_version_info(container, client):
image_ref = _container_image_ref(container)
current_image = getattr(container, 'image', None)
current_token = _local_digest_for_image(current_image, image_ref)
latest_token = _remote_digest_for_image(client, image_ref)
return {
'image_ref': image_ref,
'current_token': current_token,
'latest_token': latest_token,
'current_version': _format_version(image_ref, current_token),
'latest_version': _format_version(image_ref, latest_token),
'current_image_id': getattr(current_image, 'id', None),
}
def _container_version_info_for_list(container, client):
cached_details = getattr(sampler, 'update_check_details_cache', {}).get(container.id)
image_ref = _container_image_ref(container)
current_image = getattr(container, 'image', None)
current_token = _local_digest_for_image(current_image, image_ref)
update_available = sampler.update_check_cache.get(container.id)
# The inventory view should render from sampler cache and local Docker state
# only. Falling back to live registry lookups here can stall the entire modal
# on slow or unreachable registries.
if not cached_details:
latest_token = 'update-available' if update_available is True else current_token
return {
'image_ref': image_ref,
'current_token': current_token,
'latest_token': None,
'current_version': _format_version(image_ref, current_token),
'latest_version': _format_version(image_ref, latest_token),
'current_image_id': getattr(current_image, 'id', None),
'error': 'cache-miss',
}
image_ref = cached_details.get('image_ref') or image_ref
current_token = cached_details.get('current_token') or current_token
latest_token = cached_details.get('latest_token')
latest_version = cached_details.get('latest_version')
if not latest_version:
latest_version = _format_version(
image_ref,
latest_token or ('update-available' if update_available is True else current_token),
)
return {
'image_ref': image_ref,
'current_token': current_token,
'latest_token': latest_token,
'current_version': cached_details.get('current_version') or _format_version(image_ref, current_token),
'latest_version': latest_version,
'current_image_id': cached_details.get('current_image_id') or getattr(current_image, 'id', None),
'error': cached_details.get('error'),
}
def _serialize_mount(mount):
mount_type = str(mount.get('Type') or 'volume').strip().lower()
if mount_type == 'tmpfs':
source = ''
else:
source = mount.get('Name') or mount.get('Source') or ''
return {
'target': mount.get('Destination') or mount.get('Target') or '',
'source': source,
'type': mount_type,
'read_only': not bool(mount.get('RW', True)),
'propagation': mount.get('Propagation'),
'no_copy': bool(mount.get('VolumeOptions', {}).get('NoCopy', False)),
}
def _serialize_networks(networks):
serialized = {}
for network_name, config in (networks or {}).items():
aliases = list(config.get('Aliases') or [])
serialized[network_name] = {
'aliases': aliases,
'ipv4_address': config.get('IPAddress') or None,
'ipv6_address': config.get('GlobalIPv6Address') or None,
'link_local_ips': list(config.get('LinkLocalIPv6Address', []) or []),
}
return serialized
def _build_snapshot(container):
attrs = container.attrs or {}
config = attrs.get('Config', {}) or {}
host_config = attrs.get('HostConfig', {}) or {}
network_settings = attrs.get('NetworkSettings', {}) or {}
state = attrs.get('State', {}) or {}
snapshot = {
'name': container.name,
'image_ref': _container_image_ref(container),
'image_id': getattr(getattr(container, 'image', None), 'id', None),
'was_running': str(getattr(container, 'status', None) or state.get('Status') or '').lower() == 'running',
'config': {
'command': config.get('Cmd'),
'hostname': config.get('Hostname') or None,
'user': config.get('User') or None,
'detach': True,
'stdin_open': bool(config.get('OpenStdin')),
'tty': bool(config.get('Tty')),
'ports': sorted((config.get('ExposedPorts') or {}).keys()),
'environment': list(config.get('Env') or []),
'volumes': sorted((config.get('Volumes') or {}).keys()),
'entrypoint': config.get('Entrypoint'),
'working_dir': config.get('WorkingDir') or None,
'domainname': config.get('Domainname') or None,
'labels': dict(config.get('Labels') or {}),
'stop_signal': config.get('StopSignal') or None,
'healthcheck': config.get('Healthcheck') or None,
'stop_timeout': config.get('StopTimeout'),
'runtime': host_config.get('Runtime') or config.get('Runtime') or None,
},
'host_config': {
'auto_remove': bool(host_config.get('AutoRemove', False)),
'blkio_weight': host_config.get('BlkioWeight'),
'blkio_weight_device': host_config.get('BlkioWeightDevice'),
'cap_add': list(host_config.get('CapAdd') or []),
'cap_drop': list(host_config.get('CapDrop') or []),
'cpu_period': host_config.get('CpuPeriod'),
'cpu_quota': host_config.get('CpuQuota'),
'cpu_shares': host_config.get('CpuShares'),
'cpuset_cpus': host_config.get('CpusetCpus'),
'cpuset_mems': host_config.get('CpusetMems'),
'device_cgroup_rules': list(host_config.get('DeviceCgroupRules') or []),
'device_read_bps': host_config.get('BlkioDeviceReadBps'),
'device_read_iops': host_config.get('BlkioDeviceReadIOps'),
'device_write_bps': host_config.get('BlkioDeviceWriteBps'),
'device_write_iops': host_config.get('BlkioDeviceWriteIOps'),
'devices': list(host_config.get('Devices') or []),
'device_requests': list(host_config.get('DeviceRequests') or []),
'dns': list(host_config.get('Dns') or []),
'dns_opt': list(host_config.get('DnsOptions') or []),
'dns_search': list(host_config.get('DnsSearch') or []),
'extra_hosts': dict(host_config.get('ExtraHosts') or {}),
'group_add': list(host_config.get('GroupAdd') or []),
'init': host_config.get('Init'),
'ipc_mode': host_config.get('IpcMode'),
'links': host_config.get('Links'),
'log_config': host_config.get('LogConfig'),
'mem_limit': host_config.get('Memory'),
'mem_reservation': host_config.get('MemoryReservation'),
'mem_swappiness': host_config.get('MemorySwappiness'),
'memswap_limit': host_config.get('MemorySwap'),
'mounts': [_serialize_mount(mount) for mount in attrs.get('Mounts', []) or []],
'nano_cpus': host_config.get('NanoCpus'),
'network_mode': host_config.get('NetworkMode'),
'oom_kill_disable': host_config.get('OomKillDisable'),
'oom_score_adj': host_config.get('OomScoreAdj'),
'pid_mode': host_config.get('PidMode'),
'pids_limit': host_config.get('PidsLimit'),
'port_bindings': host_config.get('PortBindings'),
'privileged': bool(host_config.get('Privileged', False)),
'publish_all_ports': bool(host_config.get('PublishAllPorts', False)),
'read_only': bool(host_config.get('ReadonlyRootfs', False)),
'restart_policy': host_config.get('RestartPolicy'),
'security_opt': list(host_config.get('SecurityOpt') or []),
'shm_size': host_config.get('ShmSize'),
'storage_opt': host_config.get('StorageOpt'),
'sysctls': host_config.get('Sysctls'),
'tmpfs': host_config.get('Tmpfs'),
'ulimits': host_config.get('Ulimits'),
'userns_mode': host_config.get('UsernsMode'),
'uts_mode': host_config.get('UTSMode'),
'volumes_from': list(host_config.get('VolumesFrom') or []),
'runtime': host_config.get('Runtime'),
},
'networks': _serialize_networks(network_settings.get('Networks') or {}),
}
return snapshot
def _mounts_from_snapshot(snapshot_mounts):
mounts = []
for mount in snapshot_mounts or []:
target = mount.get('target')
source = mount.get('source') or ''
mount_type = mount.get('type') or 'volume'
if not target:
continue
kwargs = {
'target': target,
'source': source,
'type': mount_type,
'read_only': bool(mount.get('read_only', False)),
}
if mount.get('propagation'):
kwargs['propagation'] = mount['propagation']
if mount.get('type') == 'volume':
kwargs['no_copy'] = bool(mount.get('no_copy', False))
mounts.append(Mount(**kwargs))
return mounts
def _ulimits_from_snapshot(ulimits):
return [Ulimit(**item) for item in (ulimits or []) if isinstance(item, dict)]
def _device_requests_from_snapshot(device_requests):
return [DeviceRequest(**item) for item in (device_requests or []) if isinstance(item, dict)]
def _log_config_from_snapshot(log_config):
if not isinstance(log_config, dict):
return None
if not log_config.get('Type'):
return None
return LogConfig(type=log_config.get('Type'), config=log_config.get('Config') or {})
def _coerce_extra_hosts(extra_hosts):
if isinstance(extra_hosts, dict):
return extra_hosts
result = {}
for item in extra_hosts or []:
if not isinstance(item, str) or ':' not in item:
continue
host, ip = item.split(':', 1)
result[host] = ip
return result
def _host_config_kwargs(snapshot):
host = dict(snapshot.get('host_config') or {})
kwargs = {
'auto_remove': host.get('auto_remove', False),
'blkio_weight': host.get('blkio_weight'),
'blkio_weight_device': host.get('blkio_weight_device'),
'cap_add': host.get('cap_add') or None,
'cap_drop': host.get('cap_drop') or None,
'cpu_period': host.get('cpu_period'),
'cpu_quota': host.get('cpu_quota'),
'cpu_shares': host.get('cpu_shares'),
'cpuset_cpus': host.get('cpuset_cpus'),
'cpuset_mems': host.get('cpuset_mems'),
'device_cgroup_rules': host.get('device_cgroup_rules') or None,
'device_read_bps': host.get('device_read_bps'),
'device_read_iops': host.get('device_read_iops'),
'device_write_bps': host.get('device_write_bps'),
'device_write_iops': host.get('device_write_iops'),
'devices': host.get('devices') or None,
'device_requests': _device_requests_from_snapshot(host.get('device_requests')),
'dns': host.get('dns') or None,
'dns_opt': host.get('dns_opt') or None,
'dns_search': host.get('dns_search') or None,
'extra_hosts': _coerce_extra_hosts(host.get('extra_hosts')),
'group_add': host.get('group_add') or None,
'init': host.get('init'),
'ipc_mode': host.get('ipc_mode'),
'links': host.get('links'),
'log_config': _log_config_from_snapshot(host.get('log_config')),
'mem_limit': host.get('mem_limit'),
'mem_reservation': host.get('mem_reservation'),
'mem_swappiness': host.get('mem_swappiness'),
'memswap_limit': host.get('memswap_limit'),
'mounts': _mounts_from_snapshot(host.get('mounts')),
'nano_cpus': host.get('nano_cpus'),
'network_mode': host.get('network_mode'),
'oom_kill_disable': host.get('oom_kill_disable'),
'oom_score_adj': host.get('oom_score_adj'),
'pid_mode': host.get('pid_mode'),
'pids_limit': host.get('pids_limit'),
'port_bindings': host.get('port_bindings'),
'privileged': host.get('privileged', False),
'publish_all_ports': host.get('publish_all_ports', False),
'read_only': host.get('read_only', False),
'restart_policy': host.get('restart_policy'),
'security_opt': host.get('security_opt') or None,
'shm_size': host.get('shm_size'),
'storage_opt': host.get('storage_opt'),
'sysctls': host.get('sysctls'),
'tmpfs': host.get('tmpfs'),
'ulimits': _ulimits_from_snapshot(host.get('ulimits')),
'userns_mode': host.get('userns_mode'),
'uts_mode': host.get('uts_mode'),
'volumes_from': host.get('volumes_from') or None,
'runtime': host.get('runtime'),
}
return {key: value for key, value in kwargs.items() if value not in (None, [], {}, ())}
def _restore_networks(client, container_id, snapshot):
networks = snapshot.get('networks') or {}
host_config = snapshot.get('host_config') or {}
primary_network = host_config.get('network_mode')
skip_networks = {'default', 'bridge', 'nat', 'host', 'none', 'container'}
for network_name, config in networks.items():
if primary_network and primary_network.split(':', 1)[0] not in skip_networks and network_name == primary_network:
continue
if primary_network in skip_networks and network_name == 'bridge':
continue
try:
network = client.networks.get(network_name)
connect_kwargs = {}
aliases = [alias for alias in (config.get('aliases') or []) if alias]
if aliases:
connect_kwargs['aliases'] = aliases
if config.get('ipv4_address'):
connect_kwargs['ipv4_address'] = config['ipv4_address']
if config.get('ipv6_address'):
connect_kwargs['ipv6_address'] = config['ipv6_address']
if config.get('link_local_ips'):
connect_kwargs['link_local_ips'] = config['link_local_ips']
network.connect(container_id, **connect_kwargs)
except docker.errors.APIError as exc:
logging.warning("Unable to connect container %s to network %s: %s", container_id, network_name, exc)
def _create_container_from_snapshot(api_client, client, snapshot, image, start_container=True):
host_config = api_client.create_host_config(**_host_config_kwargs(snapshot))
config = snapshot.get('config') or {}
created = api_client.create_container(
image=image,
command=config.get('command'),
hostname=config.get('hostname'),
user=config.get('user'),
detach=True,
stdin_open=config.get('stdin_open', False),
tty=config.get('tty', False),
ports=config.get('ports') or None,
environment=config.get('environment') or None,
volumes=config.get('volumes') or None,
name=snapshot.get('name'),
entrypoint=config.get('entrypoint'),
working_dir=config.get('working_dir'),
domainname=config.get('domainname'),
host_config=host_config,
labels=config.get('labels') or None,
stop_signal=config.get('stop_signal'),
healthcheck=config.get('healthcheck'),
stop_timeout=config.get('stop_timeout'),
runtime=config.get('runtime'),
)
new_container_id = created['Id']
_restore_networks(client, new_container_id, snapshot)
if start_container:
api_client.start(new_container_id)
return new_container_id
def _refresh_update_checks(container_ids):
for container_id in container_ids:
try:
sampler.force_update_check_ids.add(container_id)
except Exception:
logging.warning("Unable to schedule update re-check for %s", container_id)
def _backup_name(base_name):
return f'{base_name}-statainer-backup-{int(time.time())}'
def _rename_back(container, original_name):
try:
container.rename(original_name)
except Exception as exc:
logging.error("Unable to restore backup container name %s: %s", original_name, exc)
def _safe_remove_container(container):
try:
container.remove(v=False, force=False)
except Exception as exc:
logging.warning("Unable to remove backup container %s: %s", getattr(container, 'name', 'unknown'), exc)
def _recreate_single_container(container, target_image, actor_username=None, history_action='update', rollback_of=None):
client = get_docker_client()
api_client = get_api_client()
version_info = _container_version_info(container, client)
snapshot = _build_snapshot(container)
original_name = snapshot['name']
original_running_state = snapshot['was_running']
backup_name = _backup_name(original_name)
backup_container = container
new_container_id = None
notes = []
try:
stop_timeout = snapshot.get('config', {}).get('stop_timeout')
if original_running_state:
backup_container.stop(timeout=stop_timeout)
backup_container.rename(backup_name)
new_container_id = _create_container_from_snapshot(
api_client,
client,
snapshot,
target_image,
start_container=original_running_state,
)
_safe_remove_container(backup_container)
client.containers.get(new_container_id)
_refresh_update_checks([new_container_id])
return {
'ok': True,
'target_name': original_name,
'previous_version': version_info['current_version'],
'new_version': _format_version(snapshot['image_ref'], target_image),
'result': 'success',
'notes': '\n'.join(notes) if notes else None,
'metadata': {
'strategy': 'safe_container_recreate',
'snapshot': snapshot,
'container_name': original_name,
'previous_image_id': version_info['current_image_id'],
'new_image_id': target_image,
'current_container_id': new_container_id,
},
'rollback_of': rollback_of,
}
except Exception as exc:
logging.error("Container recreate failed for %s: %s", original_name, exc)
notes.append(f'Update failed and the previous container was restored: {exc}')
if new_container_id:
try:
client.containers.get(new_container_id).remove(v=False, force=True)
except Exception:
pass
try:
restored = client.containers.get(backup_name)
_rename_back(restored, original_name)
if original_running_state:
restored.start()
except Exception as restore_exc:
notes.append(f'Automatic restore also failed: {restore_exc}')
return {
'ok': False,
'target_name': original_name,
'previous_version': version_info['current_version'],
'new_version': _format_version(snapshot['image_ref'], target_image),
'result': 'failure',
'notes': '\n'.join(notes),
'metadata': {
'strategy': 'safe_container_recreate',
'snapshot': snapshot,
'container_name': original_name,
'previous_image_id': version_info['current_image_id'],
'attempted_image_id': target_image,
},
'rollback_of': rollback_of,
}
def _container_candidate(container, client):
version_info = _container_version_info_for_list(container, client)
checked_at = sampler.update_check_time.get(container.id)
supported, support_reason = _container_support_check(container)
update_available = sampler.update_check_cache.get(container.id) is True
ready = supported
return {
'id': f'container:{container.id}',
'target_id': container.id,
'name': container.name,
'type': 'container',
'current_version': version_info['current_version'],
'latest_version': version_info['latest_version'],
'update_available': update_available,
'update_state': 'ready' if ready else 'blocked',
'state_reason': None if ready else support_reason or 'This container cannot be updated safely with the current metadata.',
'last_checked_at': checked_at,
'entries': [],
'meta': {
'image_ref': version_info['image_ref'],
'current_image_id': version_info['current_image_id'],
},
}
def _build_refresh_details(container, remote_digests):
image_ref = _container_image_ref(container)
current_image = getattr(container, 'image', None)
current_token = _local_digest_for_image(current_image, image_ref)
details = {
'image_ref': image_ref,
'current_token': current_token,
'latest_token': None,
'current_version': _format_version(image_ref, current_token),
'latest_version': _format_version(image_ref, None),
'current_image_id': getattr(current_image, 'id', None),
'update_available': None,
'error': None,
}
if not image_ref:
details['error'] = 'missing-image-reference'
return details
if '@sha256:' in image_ref:
details['current_version'] = image_ref
details['latest_version'] = image_ref
details['error'] = 'pinned-digest-image'
return details
if not current_token:
details['error'] = 'missing-local-digest'
return details
latest_token = remote_digests.get(image_ref)
details['latest_token'] = latest_token
details['latest_version'] = _format_version(image_ref, latest_token)
if not latest_token:
details['error'] = 'registry-unavailable'
return details
details['update_available'] = current_token != latest_token
return details
def _refresh_candidate_checks(containers):
if not containers:
return []
client = get_docker_client()
image_refs = sorted({
image_ref
for image_ref in (_container_image_ref(container) for container in containers)
if image_ref and '@sha256:' not in image_ref
})
remote_digests = {}
if image_refs:
max_workers = min(UPDATE_REFRESH_MAX_WORKERS, len(image_refs))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_map = {
executor.submit(_remote_digest_for_image, client, image_ref): image_ref
for image_ref in image_refs
}
for future in as_completed(future_map):
image_ref = future_map[future]
try:
remote_digests[image_ref] = future.result()
except Exception as exc:
logging.warning("Unable to refresh registry digest for %s: %s", image_ref, exc)
remote_digests[image_ref] = None
refreshed = []
now = time.time()
for container in containers:
details = _build_refresh_details(container, remote_digests)
result = details.get('update_available')
sampler.update_check_details_cache[container.id] = details
sampler.update_check_cache[container.id] = result
sampler.update_check_time[container.id] = now
refreshed.append((container, result))
return refreshed
def _detect_external_compose_manager(paths):
normalized = [str(path or '').strip() for path in (paths or []) if str(path or '').strip()]
for manager in KNOWN_EXTERNAL_COMPOSE_MANAGERS:
for path in normalized:
if any(fragment in path for fragment in manager['path_fragments']):
return manager
return None
def _compose_blocked_metadata(
reason,
*,
block_kind,
working_dir=None,
config_files=None,
missing_files=None,
manager=None,
guidance=None,
action_hint=None,
recovery_hint=None,
):
return {
'ready': False,
'reason': reason,
'block_kind': block_kind,
'management_mode': 'external' if manager else 'unknown',
'manager_key': manager.get('key') if manager else None,
'manager_name': manager.get('name') if manager else None,
'working_dir': working_dir,
'config_files': list(config_files or []),
'missing_files': list(missing_files or []),
'guidance': list(guidance or []),
'action_hint': action_hint,
'recovery_hint': recovery_hint,
'auto_recovery_supported': False,
'update_strategy': None,
'update_mode_label': None,
}
def _compose_paths_from_labels(project_name, containers):
working_dirs = set()
config_files = set()
services = set()
for container in containers:
labels = container.attrs.get('Config', {}).get('Labels', {}) or {}
services.add(labels.get('com.docker.compose.service') or container.name)
working_dir = str(labels.get('com.docker.compose.project.working_dir') or '').strip()
config_label = str(labels.get('com.docker.compose.project.config_files') or '').strip()
if working_dir:
working_dirs.add(working_dir)
if config_label:
config_files.add(config_label)
if len(working_dirs) != 1 or len(config_files) != 1:
return _compose_blocked_metadata(
'Compose metadata is incomplete or inconsistent across services.',
block_kind='inconsistent_metadata',
guidance=[
(
"statainer could not resolve one canonical Compose "
"project directory and file set from the running services."
),
(
"This usually means the stack was recreated or partially "
"managed outside a single docker compose project."
),
COMPOSE_RECONSTRUCTION_LIMITATION,
],
action_hint='Project updates are disabled until the stack is relinked to a single Compose project on disk.',
recovery_hint=(
"Redeploy or import the stack from one canonical Compose project "
"directory so every service advertises the same working directory "
"and config file set."
),
)
working_dir = next(iter(working_dirs))
raw_files = next(iter(config_files))
files = []
for entry in raw_files.split(','):
entry = entry.strip()
if not entry:
continue
files.append(entry if os.path.isabs(entry) else os.path.join(working_dir, entry))
missing = [path for path in files if not os.path.exists(path)]
if missing:
manager = _detect_external_compose_manager([working_dir, *files, *missing])
if manager:
guidance = [
(
"statainer can only run project updates when it can read "
"the original Compose files from the host filesystem."
),
COMPOSE_RECONSTRUCTION_LIMITATION,
]
return _compose_blocked_metadata(
manager['reason'],
block_kind='missing_compose_files',
working_dir=working_dir,
config_files=files,
missing_files=missing,
manager=manager,
guidance=guidance,
action_hint=manager['action_hint'],
recovery_hint=manager['recovery_hint'],
)
return _compose_blocked_metadata(
'Compose files referenced by Docker labels are missing on this host.',
block_kind='missing_compose_files',
working_dir=working_dir,
config_files=files,
missing_files=missing,
guidance=[
(
"statainer can only run project updates when the compose "
"files advertised by Docker labels still exist on disk."
),
(
"This usually means the stack was deployed from another "
"management tool, a temporary checkout, or a path that is no "
"longer mounted here."
),
(
"If you use OpenMediaVault Compose, restore or remount the "
"plugin's compose shared folder so the recorded project path "
"exists again before retrying."
),
COMPOSE_RECONSTRUCTION_LIMITATION,
],
action_hint='Project updates are disabled until the original Compose files are accessible on this host.',
recovery_hint=(
"Restore the original compose directory, remount the missing "
"path, or redeploy/import the stack from its compose files."
),
)
return {
'ready': True,
'working_dir': working_dir,
'config_files': files,
'services': sorted(services),
'management_mode': 'host',
'manager_key': 'docker-compose',
'manager_name': 'Docker Compose',
'block_kind': None,
'missing_files': [],
'guidance': [],
'action_hint': None,
'recovery_hint': None,
'auto_recovery_supported': False,
}
def _project_candidate(project_name, containers, client):
metadata = _compose_paths_from_labels(project_name, containers)
entries = []
checked_at = None
update_available = False
for container in containers:
version_info = _container_version_info_for_list(container, client)
labels = container.attrs.get('Config', {}).get('Labels', {}) or {}
service_name = labels.get('com.docker.compose.service') or container.name
checked_value = sampler.update_check_time.get(container.id)
if checked_value is not None:
checked_at = checked_value if checked_at is None else max(checked_at, checked_value)
if sampler.update_check_cache.get(container.id) is True:
update_available = True
entries.append({
'service': service_name,
'container_id': container.id,
'container_name': container.name,
'current_version': version_info['current_version'],
'latest_version': version_info['latest_version'],
'image_ref': version_info['image_ref'],
'current_image_id': version_info['current_image_id'],
})
metadata = dict(metadata)
ready = bool(metadata.get('ready'))
state_reason = None if ready else metadata.get('reason')
if not ready and metadata.get('block_kind') == 'missing_compose_files':
unsupported_reason = None
for container in containers:
supported, reason = _container_support_check(container)
if not supported:
unsupported_reason = reason
break
if unsupported_reason is None:
guidance = list(metadata.get('guidance') or [])
guidance.insert(0, EXTERNAL_SAFE_RECREATE_NOTICE)
metadata.update({
'ready': True,
'auto_recovery_supported': True,
'update_strategy': 'external_project_safe_recreate',
'update_mode_label': 'External safe recreate',
'guidance': guidance,
'action_hint': (
"Compose files are unavailable, so statainer will "
"update the running services directly."
),
})
ready = True
state_reason = None
return {
'id': f'project:{project_name}',
'target_id': project_name,
'name': project_name,
'type': 'project',
'current_version': '; '.join(f"{entry['service']}={entry['current_version']}" for entry in entries),
'latest_version': '; '.join(f"{entry['service']}={entry['latest_version']}" for entry in entries),
'update_available': update_available,
'update_state': 'ready' if ready else 'blocked',
'state_reason': state_reason,
'last_checked_at': checked_at,
'entries': entries,
'meta': metadata,
}
def _find_project_service_container(client, project_name, service_name, fallback_name=None):
for container in client.containers.list(all=True):
labels = container.attrs.get('Config', {}).get('Labels', {}) or {}
if labels.get('com.docker.compose.project') != project_name:
continue
if (labels.get('com.docker.compose.service') or container.name) == service_name:
return container
if fallback_name:
return client.containers.get(fallback_name)
raise docker.errors.NotFound(f"Service {service_name} was not found for project {project_name}.")
def _sanitize_repo_fragment(value):
token = ''.join(ch if ch.isalnum() or ch in '-._' else '-' for ch in str(value or '').lower())
return token.strip('-._') or 'item'
def _tag_for_rollback(client, image_id, project_name, service_name, history_id):
repository = f"statainer-rollback/{_sanitize_repo_fragment(project_name)}-{_sanitize_repo_fragment(service_name)}"
tag = f'history-{history_id}'
image = client.images.get(image_id)
image.tag(repository, tag=tag)
return f'{repository}:{tag}'
def _write_override_file(project_name, service_tags, history_id):
override_path = os.path.join(
_rollback_dir(),
f'{_sanitize_repo_fragment(project_name)}-history-{history_id}.override.yml',
)
lines = ['services:']
for service_name, image_ref in service_tags.items():
lines.append(f' {service_name}:')
lines.append(f' image: {image_ref}')
with open(override_path, 'w', encoding='utf-8') as handle:
handle.write('\n'.join(lines) + '\n')
return override_path
def _compose_command(metadata, project_name, extra_args, override_file=None):
command = [
'docker',
'compose',
'--ansi',
'never',
'--project-name',
project_name,
'--project-directory',
metadata['working_dir'],
]
for config_file in metadata['config_files']:
command.extend(['-f', config_file])
if override_file:
command.extend(['-f', override_file])
command.extend(extra_args)
return command
def _run_compose(metadata, project_name, extra_args, override_file=None):
command = _compose_command(metadata, project_name, extra_args, override_file=override_file)
return subprocess.run(
command,
cwd=metadata['working_dir'],
capture_output=True,
text=True,
check=False,