-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-archive.service.ts
More file actions
210 lines (172 loc) · 7.35 KB
/
image-archive.service.ts
File metadata and controls
210 lines (172 loc) · 7.35 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
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { isAbsolute, join, resolve } from 'node:path';
import type { RestoreImagesCommandOptions, SaveImagesCommandOptions } from '../types/cli.types.js';
import type { ImageArchiveManifest } from '../types/docker.types.js';
import type { ProjectContext } from '../types/project.types.js';
import { printCommandHeader } from '../ui/banner.js';
import { printInfo, printSuccess } from '../ui/logger.js';
import { listConfiguredComposeImages } from './compose-project.service.js';
import {
cleanupArchiveWorkspace,
createArchiveWorkspace,
ensureArchiveHelperImage,
extractArchiveBundleToDirectory,
normalizeArchiveBundleOutputPath,
packDirectoryToArchiveBundle
} from './archive-bundle.service.js';
import { runCommand } from '../utils/process.js';
const IMAGE_ARCHIVE_MANIFEST_FILE = 'manifest.json';
const IMAGE_ARCHIVE_PAYLOAD_FILE = 'images.tar';
/**
* Saves the Docker images declared by the selected lab layers into a single bundle archive on disk.
*/
export async function runSaveImagesCommand(
context: ProjectContext,
options: SaveImagesCommandOptions
): Promise<void> {
printCommandHeader({
title: 'Save Docker Images',
summary: 'Export Docker images for the selected Atlas Lab layers',
projectRoot: context.projectRoot,
workingDirectory: context.workingDirectory
});
const outputPath = resolveImageArchiveOutputPath(context.workingDirectory, options.output);
const workspacePath = createArchiveWorkspace('images');
try {
printInfo('Resolving configured Docker image references...', 'compose');
const images = await listConfiguredComposeImages(context, options);
if (images.length === 0) {
throw new Error('No Docker images were resolved for the selected lab layers.');
}
printInfo(`Resolved ${images.length} Docker images for export.`, 'compose');
for (const image of images) {
printInfo(`Queue image ${image}`, 'compose');
}
await ensureArchiveHelperImage(context.projectRoot, 'stack');
const payloadPath = join(workspacePath, IMAGE_ARCHIVE_PAYLOAD_FILE);
printInfo(`Saving Docker images into staging payload ${payloadPath}`, 'stack');
await runCommand('docker', ['image', 'save', '--output', payloadPath, ...images], {
cwd: context.projectRoot,
scope: 'stack'
});
const manifest: ImageArchiveManifest = {
createdAt: new Date().toISOString(),
images,
project: context.projectRoot
};
writeFileSync(
join(workspacePath, IMAGE_ARCHIVE_MANIFEST_FILE),
`${JSON.stringify(manifest, null, 2)}\n`,
'utf8'
);
printInfo(`Embedded image manifest with ${images.length} entries.`, 'stack');
await packDirectoryToArchiveBundle(workspacePath, outputPath, context.projectRoot, 'stack');
} finally {
cleanupArchiveWorkspace(workspacePath);
}
printSuccess(`Docker images saved to ${outputPath}`, 'stack');
}
/**
* Restores a Docker image archive previously exported by `save-images`.
* Also supports the legacy raw `docker image save` tar format.
*/
export async function runRestoreImagesCommand(
context: ProjectContext,
options: RestoreImagesCommandOptions
): Promise<void> {
printCommandHeader({
title: 'Restore Docker Images',
summary: 'Load a Docker image archive from disk into the local daemon',
projectRoot: context.projectRoot,
workingDirectory: context.workingDirectory
});
const inputPath = resolveArchiveInputPath(context.workingDirectory, options.input);
const legacyManifestPath = `${inputPath}.manifest.json`;
if (!existsSync(inputPath)) {
throw new Error(`Image archive not found: ${inputPath}`);
}
if (isLegacyImageArchive(inputPath)) {
printInfo(`Detected legacy Docker image archive ${inputPath}`, 'stack');
if (existsSync(legacyManifestPath)) {
const manifest = parseImageArchiveManifest(legacyManifestPath, readFileSync(legacyManifestPath, 'utf8'));
printInfo(`Validated legacy image manifest with ${manifest.images.length} entries.`, 'stack');
}
printInfo(`Loading Docker image archive ${inputPath}`, 'stack');
await runCommand('docker', ['image', 'load', '--input', inputPath], {
cwd: context.projectRoot,
scope: 'stack'
});
printSuccess(`Docker images restored from ${inputPath}`, 'stack');
return;
}
await ensureArchiveHelperImage(context.projectRoot, 'stack');
const workspacePath = createArchiveWorkspace('images-restore');
try {
await extractArchiveBundleToDirectory(inputPath, workspacePath, context.projectRoot, 'stack');
const manifestPath = join(workspacePath, IMAGE_ARCHIVE_MANIFEST_FILE);
if (!existsSync(manifestPath)) {
throw new Error(`Image archive manifest not found inside bundle: ${manifestPath}`);
}
const manifest = parseImageArchiveManifest(manifestPath, readFileSync(manifestPath, 'utf8'));
printInfo(`Validated bundled image manifest with ${manifest.images.length} entries.`, 'stack');
for (const image of manifest.images) {
printInfo(`Restore image ${image}`, 'stack');
}
const payloadPath = join(workspacePath, IMAGE_ARCHIVE_PAYLOAD_FILE);
if (!existsSync(payloadPath) || !statSync(payloadPath).isFile()) {
throw new Error(`Bundled Docker image payload not found: ${payloadPath}`);
}
printInfo(`Loading bundled Docker image payload ${payloadPath}`, 'stack');
await runCommand('docker', ['image', 'load', '--input', payloadPath], {
cwd: context.projectRoot,
scope: 'stack'
});
} finally {
cleanupArchiveWorkspace(workspacePath);
}
printSuccess(`Docker images restored from ${inputPath}`, 'stack');
}
/**
* Resolves the image archive output file, defaulting under `backups/images`.
*/
function resolveImageArchiveOutputPath(workingDirectory: string, explicitOutput?: string): string {
const defaultFileName = `atlas-lab-images-${createTimestamp()}.tar.gz`;
const defaultPath = join(workingDirectory, 'backups', 'images', defaultFileName);
if (!explicitOutput) {
return defaultPath;
}
const outputPath = isAbsolute(explicitOutput) ? explicitOutput : resolve(workingDirectory, explicitOutput);
if (existsSync(outputPath) && statSync(outputPath).isDirectory()) {
return join(outputPath, defaultFileName);
}
return normalizeArchiveBundleOutputPath(outputPath);
}
/**
* Resolves a user-provided archive input path against the working directory.
*/
function resolveArchiveInputPath(workingDirectory: string, inputPath: string): string {
return isAbsolute(inputPath) ? inputPath : resolve(workingDirectory, inputPath);
}
/**
* Recognizes the previous raw `docker image save` export format.
*/
function isLegacyImageArchive(inputPath: string): boolean {
const normalizedPath = inputPath.toLowerCase();
return normalizedPath.endsWith('.tar') && !normalizedPath.endsWith('.tar.gz');
}
/**
* Validates the JSON manifest embedded in or persisted next to an image archive.
*/
function parseImageArchiveManifest(manifestPath: string, rawManifest: string): ImageArchiveManifest {
const manifest = JSON.parse(rawManifest) as ImageArchiveManifest;
if (!Array.isArray(manifest.images) || manifest.images.length === 0) {
throw new Error(`Invalid image archive manifest: ${manifestPath}`);
}
return manifest;
}
/**
* Produces a filesystem-safe timestamp.
*/
function createTimestamp(): string {
return new Date().toISOString().replace(/[:.]/gu, '-');
}