-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontainer_updates.py
More file actions
1159 lines (958 loc) · 45.5 KB
/
container_updates.py
File metadata and controls
1159 lines (958 loc) · 45.5 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
# container_updates.py - Update management for containers managed by Composr
import os
import json
import time
import logging
import requests
import subprocess
import re
from datetime import datetime, timezone
from typing import Dict, List, Optional, Tuple
import docker
import yaml
from concurrent.futures import ThreadPoolExecutor, as_completed
logger = logging.getLogger(__name__)
class ContainerUpdateManager:
def __init__(self, compose_dir, extra_compose_dirs, metadata_dir='/app'):
self.compose_dir = compose_dir
self.extra_compose_dirs = extra_compose_dirs if extra_compose_dirs else []
self.metadata_dir = metadata_dir
self.update_cache_file = os.path.join(metadata_dir, 'container_updates_cache.json')
self.update_settings_file = os.path.join(metadata_dir, 'container_update_settings.json')
self.default_settings = {
'auto_check_enabled': True,
'check_interval_hours': 6,
'notify_on_updates': True,
'auto_update_enabled': False, # Safer default
'auto_update_schedule': 'manual', # manual, daily, weekly
'auto_update_tags': ['stable', 'prod'],
'exclude_patterns': ['dev', 'test'], # Skip these tags
'exclude_container_patterns': [], # Skip containers matching these name patterns
'include_patterns': ['stable', 'main', 'prod', r'^\d+\.\d+\.\d+$'], # Add version regex
'backup_before_update': True,
'rollback_on_failure': True,
'max_concurrent_updates': 3,
'scheduled_repull_enabled': False,
'repull_interval_hours': 24,
'repull_tags': ['latest', 'main', 'stable'],
}
self.settings = self.load_settings()
def load_settings(self) -> Dict:
"""Load container update settings"""
try:
if os.path.exists(self.update_settings_file):
with open(self.update_settings_file, 'r') as f:
settings = json.load(f)
return {**self.default_settings, **settings}
return self.default_settings
except Exception as e:
logger.error(f"Failed to load container update settings: {e}")
return self.default_settings
def save_settings(self):
"""Save container update settings"""
try:
with open(self.update_settings_file, 'w') as f:
json.dump(self.settings, f, indent=2)
except Exception as e:
logger.error(f"Failed to save container update settings: {e}")
def get_all_containers_with_images(self, host_manager) -> List[Dict]:
"""Get all containers with their current image information"""
all_containers = []
hosts_status = host_manager.get_hosts_status()
for host_name, status_info in hosts_status.items():
if status_info['connected']:
client = host_manager.get_client(host_name)
if client:
try:
containers = client.containers.list(all=True)
for container in containers:
labels = container.labels or {}
# Get image information
image_info = self.parse_image_name(container.image.tags[0] if container.image.tags else container.image.id)
container_info = {
'id': container.short_id,
'name': container.name,
'host': host_name,
'status': container.status,
'image_full': container.image.tags[0] if container.image.tags else container.image.id,
'image_name': image_info['name'],
'image_tag': image_info['tag'],
'image_registry': image_info['registry'],
'compose_project': labels.get('com.docker.compose.project'),
'compose_service': labels.get('com.docker.compose.service'),
'compose_file': labels.get('com.docker.compose.project.config_files'),
'created': container.attrs.get('Created', ''),
'is_compose_managed': bool(labels.get('com.docker.compose.project')),
'update_available': False,
'latest_version': None,
'last_checked': None
}
all_containers.append(container_info)
except Exception as e:
logger.error(f"Failed to get containers from host {host_name}: {e}")
return all_containers
def parse_image_name(self, image_full: str) -> Dict:
"""Parse Docker image name into components"""
try:
# Handle different image formats:
# nginx:latest
# nginx:1.21
# docker.io/nginx:latest
# ghcr.io/user/app:v1.0.0
# localhost:5000/myapp:latest
if '@sha256:' in image_full:
# Handle digest format
image_full = image_full.split('@')[0]
# Split registry/namespace and image:tag
parts = image_full.split('/')
if '.' in parts[0] or ':' in parts[0] or parts[0] == 'localhost':
# Has registry
registry = parts[0]
if len(parts) == 2:
# registry/image:tag
image_part = parts[1]
namespace = None
else:
# registry/namespace/image:tag
namespace = '/'.join(parts[1:-1])
image_part = parts[-1]
else:
# No explicit registry (Docker Hub)
registry = 'docker.io'
if len(parts) == 1:
# image:tag (official image)
image_part = parts[0]
namespace = 'library'
else:
# user/image:tag
namespace = '/'.join(parts[:-1])
image_part = parts[-1]
# Split image name and tag
if ':' in image_part:
image_name, tag = image_part.rsplit(':', 1)
else:
image_name = image_part
tag = 'latest'
return {
'registry': registry,
'namespace': namespace,
'name': image_name,
'tag': tag,
'full_name': f"{namespace}/{image_name}" if namespace else image_name
}
except Exception as e:
logger.error(f"Failed to parse image name {image_full}: {e}")
return {
'registry': 'unknown',
'namespace': None,
'name': image_full,
'tag': 'unknown',
'full_name': image_full
}
def check_for_container_updates(self, containers: List[Dict]) -> Dict:
"""Check for available updates for all containers"""
try:
logger.info(f"Checking for updates on {len(containers)} containers...")
update_results = {
'total_checked': len(containers),
'updates_available': 0,
'check_errors': 0,
'containers': {},
'last_check': time.time()
}
# Use thread pool for concurrent checks
max_workers = min(self.settings['max_concurrent_updates'], len(containers))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
# Submit all update checks
future_to_container = {
executor.submit(self.check_single_container_update, container): container
for container in containers
}
# Collect results
for future in as_completed(future_to_container):
container = future_to_container[future]
container_id = f"{container['host']}:{container['name']}"
try:
result = future.result()
update_results['containers'][container_id] = result
if result['update_available']:
update_results['updates_available'] += 1
except Exception as e:
logger.error(f"Update check failed for {container_id}: {e}")
update_results['check_errors'] += 1
update_results['containers'][container_id] = {
'update_available': False,
'error': str(e),
'last_checked': time.time()
}
# Cache results
self.save_update_cache(update_results)
logger.info(f"Update check complete: {update_results['updates_available']} updates available")
return update_results
except Exception as e:
logger.error(f"Failed to check for container updates: {e}")
return {
'total_checked': 0,
'updates_available': 0,
'check_errors': 1,
'containers': {},
'error': str(e),
'last_check': time.time()
}
def check_single_container_update(self, container: Dict) -> Dict:
"""Check for updates for a single container"""
try:
image_info = {
'registry': container['image_registry'],
'name': container['image_name'],
'tag': container['image_tag']
}
# Skip certain tags based on settings
if self.should_skip_image(image_info, container['name']):
return {
'update_available': False,
'reason': 'skipped_by_settings',
'current_tag': image_info['tag'],
'last_checked': time.time()
}
# Check different update strategies based on image tag
if image_info['tag'] in ['latest', 'main', 'master', 'stable']:
# For latest-style tags, check if image has been updated
return self.check_latest_tag_update(container, image_info)
elif self.is_version_tag(image_info['tag']):
# For version tags, check for newer versions
return self.check_version_tag_update(container, image_info)
else:
# For other tags, just check if image exists and has updates
return self.check_generic_tag_update(container, image_info)
except Exception as e:
logger.error(f"Failed to check updates for {container['name']}: {e}")
return {
'update_available': False,
'error': str(e),
'last_checked': time.time()
}
def should_skip_image(self, image_info: Dict, container_name: str = '') -> bool:
"""Check if image should be skipped based on settings"""
tag = image_info['tag']
# Check tag exclude patterns
for pattern in self.settings['exclude_patterns']:
if re.search(pattern, tag):
return True
# Check container name exclude patterns
if container_name:
for pattern in self.settings.get('exclude_container_patterns', []):
if re.search(pattern, container_name):
return True
# If include patterns are specified, only include matching ones
if self.settings['include_patterns']:
for pattern in self.settings['include_patterns']:
if re.search(pattern, tag):
return False
return True # Skip if doesn't match any include pattern
return False
def is_version_tag(self, tag: str) -> bool:
"""Check if tag looks like a version number"""
# Match patterns like: 1.0.0, v1.0.0, 2.1, v2.1, 1.0.0-beta, etc.
version_patterns = [
r'^v?\d+\.\d+\.\d+', # 1.0.0, v1.0.0
r'^v?\d+\.\d+', # 1.0, v1.0
r'^v?\d+', # 1, v1
r'^\d+\.\d+\.\d+', # 1.0.0
]
for pattern in version_patterns:
if re.match(pattern, tag):
return True
return False
def check_latest_tag_update(self, container: Dict, image_info: Dict) -> Dict:
"""Check if a 'latest' style tag has been updated"""
try:
# For Docker Hub images, check the last_updated field
if image_info['registry'] == 'docker.io':
return self.check_dockerhub_update(container, image_info)
# For other registries, try registry API
return self.check_registry_update(container, image_info)
except Exception as e:
logger.debug(f"Latest tag check failed for {container['name']}: {e}")
return {
'update_available': False,
'error': str(e),
'last_checked': time.time()
}
def check_dockerhub_update(self, container: Dict, image_info: Dict) -> Dict:
"""Check Docker Hub for image updates"""
try:
# Docker Hub API v2
if image_info.get('namespace') == 'library':
# Official image
url = f"https://registry.hub.docker.com/v2/repositories/library/{image_info['name']}/tags/{image_info['tag']}"
else:
# User/org image
url = f"https://registry.hub.docker.com/v2/repositories/{image_info['namespace']}/{image_info['name']}/tags/{image_info['tag']}"
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
remote_updated = data.get('last_updated', '')
# Compare with container creation time
if remote_updated:
remote_time = datetime.fromisoformat(remote_updated.replace('Z', '+00:00'))
container_time = datetime.fromisoformat(container['created'].replace('Z', '+00:00'))
update_available = remote_time > container_time
return {
'update_available': update_available,
'current_tag': image_info['tag'],
'remote_updated': remote_updated,
'container_created': container['created'],
'check_method': 'dockerhub_api',
'last_checked': time.time()
}
return {
'update_available': False,
'reason': 'no_timestamp_data',
'last_checked': time.time()
}
except requests.RequestException as e:
logger.debug(f"Docker Hub API request failed: {e}")
return {
'update_available': False,
'error': f'Docker Hub API error: {str(e)}',
'last_checked': time.time()
}
def check_version_tag_update(self, container: Dict, image_info: Dict) -> Dict:
"""Check for newer version tags"""
try:
# Get all tags for the image
available_tags = self.get_available_tags(image_info)
if not available_tags:
return {
'update_available': False,
'reason': 'could_not_fetch_tags',
'last_checked': time.time()
}
current_version = image_info['tag']
latest_version = self.find_latest_version_tag(available_tags, current_version)
if latest_version and latest_version != current_version:
return {
'update_available': True,
'current_tag': current_version,
'latest_tag': latest_version,
'available_tags': available_tags[:10], # Limit for response size
'check_method': 'version_comparison',
'last_checked': time.time()
}
return {
'update_available': False,
'current_tag': current_version,
'latest_tag': current_version,
'check_method': 'version_comparison',
'last_checked': time.time()
}
except Exception as e:
logger.debug(f"Version tag check failed: {e}")
return {
'update_available': False,
'error': str(e),
'last_checked': time.time()
}
def get_available_tags(self, image_info: Dict) -> List[str]:
"""Get all available tags for an image"""
try:
if image_info['registry'] == 'docker.io':
return self.get_dockerhub_tags(image_info)
else:
# For other registries, would need specific implementations
return []
except Exception as e:
logger.debug(f"Failed to get tags for {image_info['name']}: {e}")
return []
def get_dockerhub_tags(self, image_info: Dict) -> List[str]:
"""Get tags from Docker Hub"""
try:
if image_info.get('namespace') == 'library':
url = f"https://registry.hub.docker.com/v2/repositories/library/{image_info['name']}/tags/"
else:
url = f"https://registry.hub.docker.com/v2/repositories/{image_info['namespace']}/{image_info['name']}/tags/"
all_tags = []
# Docker Hub paginates results
while url:
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
tags = [tag['name'] for tag in data.get('results', [])]
all_tags.extend(tags)
url = data.get('next') # Next page
# Limit to avoid excessive API calls
if len(all_tags) > 100:
break
return all_tags
except Exception as e:
logger.debug(f"Failed to get Docker Hub tags: {e}")
return []
def find_latest_version_tag(self, tags: List[str], current_tag: str) -> Optional[str]:
"""Find the latest version tag from a list of tags"""
try:
# Filter to version-like tags only
version_tags = [tag for tag in tags if self.is_version_tag(tag)]
if not version_tags:
return None
# Sort versions (simple string sort works for most cases)
# For more complex version sorting, could use packaging.version
version_tags.sort(key=self.version_sort_key, reverse=True)
# Find latest that's newer than current
current_sort_key = self.version_sort_key(current_tag)
for tag in version_tags:
if self.version_sort_key(tag) > current_sort_key:
return tag
return None
except Exception as e:
logger.debug(f"Failed to find latest version: {e}")
return None
def version_sort_key(self, version: str):
"""Create sort key for version string"""
try:
# Remove 'v' prefix if present
clean_version = version.lstrip('v')
# Split into parts and convert to integers where possible
parts = []
for part in clean_version.split('.'):
# Extract numeric part
numeric_part = re.match(r'(\d+)', part)
if numeric_part:
parts.append(int(numeric_part.group(1)))
else:
parts.append(0)
# Pad to ensure consistent comparison
while len(parts) < 3:
parts.append(0)
return tuple(parts)
except Exception:
# Fallback to string comparison
return (version,)
def check_generic_tag_update(self, container: Dict, image_info: Dict) -> Dict:
"""Check for updates on non-version, non-latest tags"""
# For generic tags, we can't easily determine if updates are available
# unless we check image digests, which requires registry API access
return {
'update_available': False,
'reason': 'generic_tag_no_check',
'current_tag': image_info['tag'],
'last_checked': time.time()
}
def check_registry_update(self, container: Dict, image_info: Dict) -> Dict:
"""Check non-Docker Hub registries for updates"""
# Implementation would depend on the specific registry API
# GitHub Container Registry, Azure Container Registry, etc. have different APIs
return {
'update_available': False,
'reason': 'registry_not_supported',
'last_checked': time.time()
}
def save_update_cache(self, update_results: Dict):
"""Save update check results to cache"""
try:
with open(self.update_cache_file, 'w') as f:
json.dump(update_results, f, indent=2)
except Exception as e:
logger.error(f"Failed to save update cache: {e}")
def load_update_cache(self) -> Dict:
"""Load cached update results"""
try:
if os.path.exists(self.update_cache_file):
with open(self.update_cache_file, 'r') as f:
return json.load(f)
except Exception as e:
logger.debug(f"Failed to load update cache: {e}")
return {'containers': {}, 'last_check': 0}
def update_container(self, container_id: str, host: str, target_tag: str, host_manager) -> Dict:
"""Update a specific container to a new tag"""
try:
logger.info(f"Updating container {container_id} on {host} to {target_tag}")
client = host_manager.get_client(host)
if not client:
return {
'success': False,
'error': f'Host {host} not available'
}
container = client.containers.get(container_id)
# Check if it's a compose-managed container
labels = container.labels or {}
if labels.get('com.docker.compose.project'):
return self.update_compose_container(container, target_tag, host, host_manager)
else:
return self.update_standalone_container(container, target_tag, client)
except Exception as e:
logger.error(f"Failed to update container {container_id}: {e}")
return {
'success': False,
'error': str(e)
}
def update_compose_container(self, container, target_tag: str, host: str, host_manager) -> Dict:
"""Update a compose-managed container"""
try:
labels = container.labels
project = labels.get('com.docker.compose.project')
service = labels.get('com.docker.compose.service')
config_file = labels.get('com.docker.compose.project.config_files')
if not all([project, service, config_file]):
return {
'success': False,
'error': 'Missing compose metadata'
}
if not os.path.exists(config_file):
return {
'success': False,
'error': f'Compose file not found: {config_file}'
}
# Update the compose file
update_result = self.update_compose_file_image(config_file, service, target_tag)
if not update_result['success']:
return update_result
# Deploy the updated compose
return self.deploy_updated_compose(config_file, service, host, host_manager)
except Exception as e:
logger.error(f"Failed to update compose container: {e}")
return {
'success': False,
'error': str(e)
}
def update_compose_file_image(self, compose_file: str, service: str, target_tag: str) -> Dict:
"""Update image tag in compose file, preserving formatting and comments"""
try:
# Read original content
with open(compose_file, 'r') as f:
original_content = f.read()
# Backup original file
backup_file = f"{compose_file}.backup-{int(time.time())}"
with open(backup_file, 'w') as f:
f.write(original_content)
# Validate service and get current image via yaml parse (read-only)
compose_data = yaml.safe_load(original_content)
if 'services' not in compose_data or service not in compose_data['services']:
return {
'success': False,
'error': f'Service {service} not found in compose file'
}
service_config = compose_data['services'][service]
if 'image' not in service_config:
return {
'success': False,
'error': f'No image specified for service {service}'
}
current_image = service_config['image']
image_parts = current_image.split(':')
if len(image_parts) >= 2:
new_image = ':'.join(image_parts[:-1]) + ':' + target_tag
else:
new_image = current_image + ':' + target_tag
# Replace image in file text to preserve formatting and comments
new_content = self._replace_image_in_text(original_content, service, current_image, new_image)
if new_content is None:
return {
'success': False,
'error': f'Could not locate image line for service {service} in compose file'
}
with open(compose_file, 'w') as f:
f.write(new_content)
logger.info(f"Updated {service} image: {current_image} -> {new_image}")
return {
'success': True,
'backup_file': backup_file,
'old_image': current_image,
'new_image': new_image
}
except Exception as e:
logger.error(f"Failed to update compose file: {e}")
return {
'success': False,
'error': str(e)
}
def _replace_image_in_text(self, content: str, service: str, old_image: str, new_image: str) -> Optional[str]:
"""Replace image tag in compose file text while preserving formatting"""
# Fast path: if the old image string is unique in the file, just replace it
if content.count(old_image) == 1:
return content.replace(old_image, new_image)
# Slow path: find the service section and replace only within it
lines = content.split('\n')
in_service = False
service_indent = None
result_lines = []
replaced = False
for line in lines:
if replaced:
result_lines.append(line)
continue
stripped = line.lstrip()
current_indent = len(line) - len(stripped)
# Detect service header (e.g., " myservice:" or "myservice:")
if stripped == f'{service}:':
in_service = True
service_indent = current_indent
result_lines.append(line)
continue
if in_service:
# We've left the service block if indentation dropped back to service level
if stripped and not stripped.startswith('#') and current_indent <= service_indent:
in_service = False
result_lines.append(line)
continue
# Replace the image line within this service
if stripped.startswith('image:') and old_image in line:
result_lines.append(line.replace(old_image, new_image, 1))
replaced = True
continue
result_lines.append(line)
return '\n'.join(result_lines) if replaced else None
def deploy_updated_compose(self, compose_file: str, service: str, host: str, host_manager) -> Dict:
"""Deploy updated compose configuration"""
try:
compose_dir = os.path.dirname(compose_file)
compose_filename = os.path.basename(compose_file)
# Setup environment for compose command
env = os.environ.copy()
# Set DOCKER_HOST for remote hosts
if host != 'local':
host_info = host_manager.get_host_info(host) if hasattr(host_manager, 'get_host_info') else None
if host_info:
docker_url = host_info.get('url', '')
if docker_url:
env['DOCKER_HOST'] = docker_url
else:
logger.warning(f"No URL found for host {host}, deploying to local Docker")
else:
logger.warning(f"Could not get info for host {host}, deploying to local Docker")
# Pull new image
pull_cmd = ['docker-compose', '-f', compose_filename, 'pull', service]
pull_result = subprocess.run(
pull_cmd,
cwd=compose_dir,
env=env,
capture_output=True,
text=True,
timeout=300
)
if pull_result.returncode != 0:
logger.warning(f"Pull warnings: {pull_result.stderr}")
# Recreate the service
up_cmd = ['docker-compose', '-f', compose_filename, 'up', '-d', '--force-recreate', service]
up_result = subprocess.run(
up_cmd,
cwd=compose_dir,
env=env,
capture_output=True,
text=True,
timeout=300
)
if up_result.returncode == 0:
return {
'success': True,
'message': f'Successfully updated and restarted {service}',
'output': up_result.stdout
}
else:
return {
'success': False,
'error': f'Deploy failed: {up_result.stderr}',
'output': up_result.stdout
}
except subprocess.TimeoutExpired:
return {
'success': False,
'error': 'Update deployment timed out'
}
except Exception as e:
logger.error(f"Failed to deploy updated compose: {e}")
return {
'success': False,
'error': str(e)
}
def update_standalone_container(self, container, target_tag: str, client) -> Dict:
"""Update a standalone (non-compose) container"""
try:
# Get container configuration
container_config = container.attrs
# Build new image name
current_image = container_config['Config']['Image']
image_parts = current_image.split(':')
if len(image_parts) >= 2:
new_image = ':'.join(image_parts[:-1]) + ':' + target_tag
else:
new_image = current_image + ':' + target_tag
# Pull new image
try:
client.images.pull(new_image)
except Exception as e:
return {
'success': False,
'error': f'Failed to pull image {new_image}: {str(e)}'
}
# Stop and remove old container
was_running = container.status == 'running'
if was_running:
container.stop()
old_name = container.name
container.remove()
# Recreate container with new image
host_config = container_config.get('HostConfig', {})
config = container_config.get('Config', {})
new_container = client.containers.run(
image=new_image,
name=old_name,
detach=True,
ports=host_config.get('PortBindings', {}),
volumes=host_config.get('Binds', []),
environment=config.get('Env', []),
restart_policy=host_config.get('RestartPolicy', {}),
network_mode=host_config.get('NetworkMode', 'default'),
command=config.get('Cmd'),
entrypoint=config.get('Entrypoint'),
working_dir=config.get('WorkingDir'),
labels=config.get('Labels', {})
)
return {
'success': True,
'message': f'Successfully updated {old_name} to {new_image}',
'old_image': current_image,
'new_image': new_image,
'new_container_id': new_container.short_id
}
except Exception as e:
logger.error(f"Failed to update standalone container: {e}")
return {
'success': False,
'error': str(e)
}
def is_safe_update(self, current_version: str, new_version: str) -> bool:
"""Check if update is safe (patch version only)"""
try:
# Remove 'v' prefix if present
current = current_version.lstrip('v')
new = new_version.lstrip('v')
# Parse versions (e.g., "1.2.3" -> [1, 2, 3])
current_parts = [int(x) for x in current.split('.')]
new_parts = [int(x) for x in new.split('.')]
# Pad to same length
while len(current_parts) < 3:
current_parts.append(0)
while len(new_parts) < 3:
new_parts.append(0)
# Safe update = same major.minor, higher patch
# 1.2.3 → 1.2.4 ✅ Safe
# 1.2.3 → 1.3.0 ❌ Not safe (minor change)
# 1.2.3 → 2.0.0 ❌ Not safe (major change)
if current_parts[0] == new_parts[0] and current_parts[1] == new_parts[1]:
# Same major.minor, check if patch is higher
return new_parts[2] > current_parts[2]
return False
except (ValueError, IndexError):
# If we can't parse versions, not safe
return False
def should_auto_update(self, container: Dict, update_info: Dict) -> bool:
"""Check if container should be auto-updated"""
try:
# Must be enabled
if not self.settings.get('auto_update_enabled', False):
return False
# Skip if explicitly excluded
image_info = self.parse_image_name(container['image_full'])
if self.should_skip_image(image_info, container['name']):
return False
# Only auto-update if it's a safe update
if update_info.get('current_tag') and update_info.get('latest_tag'):
if not self.is_safe_update(update_info['current_tag'], update_info['latest_tag']):
logger.info(f"Skipping auto-update for {container['name']}: not a safe patch update")
return False
# Check auto-update patterns
auto_tags = self.settings.get('auto_update_tags', ['stable', 'prod'])
if auto_tags:
current_tag = update_info.get('current_tag', '')
tag_matches = any(pattern.lower() in current_tag.lower() for pattern in auto_tags)
if not tag_matches:
logger.info(f"Skipping auto-update for {container['name']}: tag doesn't match auto-update patterns")
return False
return True
except Exception as e:
logger.error(f"Error checking auto-update for {container['name']}: {e}")
return False
def should_scheduled_repull(self, container: Dict) -> bool:
"""Check if container should be repulled on schedule"""
try:
# Must be enabled
if not self.settings.get('scheduled_repull_enabled', False):
return False
# Skip if explicitly excluded
image_info = self.parse_image_name(container['image_full'])
if self.should_skip_image(image_info, container['name']):
return False
# Check repull patterns
repull_tags = self.settings.get('repull_tags', ['latest', 'main', 'stable'])
if repull_tags:
current_tag = image_info.get('tag', '')
tag_matches = any(pattern.lower() in current_tag.lower() for pattern in repull_tags)
if not tag_matches:
return False
# Check if enough time has passed since last repull
last_repull = container.get('last_repull', 0)
repull_interval = self.settings.get('repull_interval_hours', 24) * 3600
return (time.time() - last_repull) >= repull_interval
except Exception as e:
logger.error(f"Error checking scheduled repull for {container['name']}: {e}")
return False
def perform_auto_updates(self, host_manager) -> Dict:
"""Perform automatic safe updates and scheduled repulls"""
try:
logger.info("Performing automatic updates and scheduled repulls...")
# Get all containers
containers = self.get_all_containers_with_images(host_manager)
if not containers:
return {'auto_updates': 0, 'repulls': 0, 'errors': 0}
# Check for updates first
update_results = self.check_for_container_updates(containers)
auto_updates = 0
repulls = 0
errors = 0
# Process each container
for container in containers:
container_key = f"{container['host']}:{container['name']}"
update_info = update_results['containers'].get(container_key, {})
try:
# 1. Check for auto-updates (safe version bumps)
if update_info.get('update_available') and self.should_auto_update(container, update_info):
logger.info(f"Auto-updating {container['name']} from {update_info['current_tag']} to {update_info['latest_tag']}")
result = self.update_container(
container_id=container['id'],
host=container['host'],
target_tag=update_info['latest_tag'],
host_manager=host_manager
)
if result['success']:
auto_updates += 1
logger.info(f"Successfully auto-updated {container['name']}")