fix(deploy): correct service selection and recovery validation
ci / Workflows (pull_request) Successful in 6s
ci / Shell (pull_request) Successful in 16s
ci / Python and tests (pull_request) Successful in 6s
ci / Compose (pull_request) Successful in 12s
ci / Formatting (pull_request) Successful in 15s
ci / Dockerfiles (pull_request) Successful in 4s
ci / YAML (pull_request) Successful in 19s
ci / Kubernetes (pull_request) Successful in 7s
ci / image-plan (pull_request) Skipped
ci / Image (${{ matrix.name }}) (pull_request) Skipped
ci / build (pull_request) Skipped
ci / Workflows (pull_request) Successful in 6s
ci / Shell (pull_request) Successful in 16s
ci / Python and tests (pull_request) Successful in 6s
ci / Compose (pull_request) Successful in 12s
ci / Formatting (pull_request) Successful in 15s
ci / Dockerfiles (pull_request) Successful in 4s
ci / YAML (pull_request) Successful in 19s
ci / Kubernetes (pull_request) Successful in 7s
ci / image-plan (pull_request) Skipped
ci / Image (${{ matrix.name }}) (pull_request) Skipped
ci / build (pull_request) Skipped
This commit is contained in:
1 parent
4c7c53e0f2
commit
c4cbd87590
7 files changed
+272
-18
No files matched your search
@@ -42,7 +42,37 @@ def prepare(source_file):
|
||||
images_file = directory / 'compose-images.json'
|
||||
locks = json.loads(images_file.read_text()) if images_file.exists() else previous.get('compose-images', {})
|
||||
release = json.loads((directory / 'release.json').read_text())
|
||||
before = json.loads(json.dumps(config))
|
||||
state = Path(os.environ.get('HOMELAB_STATE', Path.home() / '.local/state/homelab-deploy'))
|
||||
baseline = state / 'compose-configs' / f'{relative.parent.name}.json'
|
||||
if not baseline.exists() and re.fullmatch(r'[0-9]+-[0-9]+', previous.get('run_id', '')):
|
||||
baseline = state / 'runs' / previous['run_id'] / 'compose' / baseline.name
|
||||
bootstrap = not baseline.exists()
|
||||
if not bootstrap:
|
||||
before = json.loads(baseline.read_text())
|
||||
else:
|
||||
# Bootstrap from the persistent configuration, never from the new source.
|
||||
persistent_file = config_repo / relative
|
||||
if persistent_file.exists():
|
||||
before = json.loads(
|
||||
output(
|
||||
'docker',
|
||||
'compose',
|
||||
'--project-directory',
|
||||
str(project_dir),
|
||||
'-f',
|
||||
str(persistent_file),
|
||||
'config',
|
||||
'--format',
|
||||
'json',
|
||||
cwd=config_repo,
|
||||
)
|
||||
)
|
||||
elif output('docker', 'ps', '-aq', '--filter', f'label=com.docker.compose.project={project}'):
|
||||
raise ValueError(f'{project}: no previous Compose configuration; restore it before deploy')
|
||||
else:
|
||||
before = {'name': project, 'services': {}}
|
||||
if before['name'] != project:
|
||||
raise ValueError('Compose project name changed; manual migration is required')
|
||||
for service, settings in config['services'].items():
|
||||
reference = settings.get('image')
|
||||
nextcloud_aio_master = project == 'nextcloud' and service == 'nextcloud-aio-mastercontainer'
|
||||
@@ -63,6 +93,12 @@ def prepare(source_file):
|
||||
pinned = resolve(reference)
|
||||
settings['image'] = pinned
|
||||
locks[reference] = pinned
|
||||
for service, settings in before['services'].items():
|
||||
reference = settings['image']
|
||||
image_repo = reference.split('@')[0].rsplit('/', 1)
|
||||
image_repo[-1] = image_repo[-1].split(':')[0]
|
||||
image_repo = '/'.join(image_repo)
|
||||
nextcloud_aio_master = project == 'nextcloud' and service == 'nextcloud-aio-mastercontainer'
|
||||
# Capture what is running, not the current value of its mutable tag.
|
||||
ids = output(
|
||||
'docker',
|
||||
@@ -74,6 +110,31 @@ def prepare(source_file):
|
||||
f'label=com.docker.compose.service={service}',
|
||||
).splitlines()
|
||||
actual = set()
|
||||
if bootstrap and ids:
|
||||
expected_hash = output(
|
||||
'docker',
|
||||
'compose',
|
||||
'--project-directory',
|
||||
str(project_dir),
|
||||
'-f',
|
||||
str(persistent_file),
|
||||
'config',
|
||||
'--hash',
|
||||
service,
|
||||
cwd=config_repo,
|
||||
).split()[-1]
|
||||
for container in ids:
|
||||
running_hash = output(
|
||||
'docker',
|
||||
'inspect',
|
||||
container,
|
||||
'--format',
|
||||
'{{ index .Config.Labels "com.docker.compose.config-hash" }}',
|
||||
)
|
||||
if running_hash != expected_hash:
|
||||
raise ValueError(
|
||||
f'{project}/{service}: persistent config differs from running config; restore the previous config'
|
||||
)
|
||||
for container in ids:
|
||||
image_id = output('docker', 'inspect', container, '--format', '{{.Image}}')
|
||||
digests = json.loads(output('docker', 'image', 'inspect', image_id, '--format', '{{json .RepoDigests}}'))
|
||||
@@ -95,7 +156,7 @@ def prepare(source_file):
|
||||
images_file.write_text(json.dumps(locks, indent=2) + '\n')
|
||||
print(f'Compose {project}: images pinned; local paths preserved')
|
||||
print(
|
||||
f'Recovery: docker compose --project-directory {project_dir} -p {project} -f {directory}/compose-before/{relative.parent.name}.json up -d --pull never'
|
||||
f'Recovery: docker compose --project-directory {project_dir} -p {project} -f {directory}/compose-before/{relative.parent.name}.json up -d --pull never --remove-orphans'
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user