Skip to content

Commit 1e9c441

Browse files
committed
downloads the image separately
This makes it possible to cancel the operation after downloading.
1 parent 2486e4c commit 1e9c441

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

core/oci.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ func padString(str string, size int) string {
6363
}
6464
}
6565

66+
// OciExportRootFs pulls an image from a registry
67+
func OciPullImage(imageName string) error {
68+
pt, err := prometheus.NewPrometheus(
69+
"/var/lib/abroot/storage",
70+
"overlay",
71+
settings.Cnf.MaxParallelDownloads,
72+
)
73+
if err != nil {
74+
PrintVerboseErr("OciPullImage", 0, err)
75+
return err
76+
}
77+
78+
if strings.HasPrefix(imageName, "localhost/") {
79+
return nil
80+
}
81+
82+
err = pullImageWithProgressbar(pt, "remoteimage", imageName)
83+
if err != nil {
84+
PrintVerboseErr("OciPullImage", 1, err)
85+
return err
86+
}
87+
88+
return nil
89+
}
90+
6691
// OciExportRootFs generates a rootfs from an image recipe file
6792
func OciExportRootFs(buildImageName string, imageRecipe *ImageRecipe, transDir string, dest string) error {
6893
PrintVerboseInfo("OciExportRootFs", "running...")
@@ -111,32 +136,16 @@ func OciExportRootFs(buildImageName string, imageRecipe *ImageRecipe, transDir s
111136
return err
112137
}
113138

114-
pulledImage := false
115-
// pull image
116-
if !strings.HasPrefix(imageRecipe.From, "localhost/") {
117-
err = pullImageWithProgressbar(pt, buildImageName, imageRecipe)
118-
if err != nil {
119-
PrintVerboseErr("OciExportRootFs", 6.1, err)
120-
return err
121-
}
122-
pulledImage = true
123-
}
124-
125139
// build image
126140
imageBuild, err := pt.BuildContainerFile(imageRecipePath, buildImageName)
127141
if err != nil {
128142
PrintVerboseErr("OciExportRootFs", 7, err)
129143
return err
130144
}
131145

132-
if pulledImage {
133-
// This is safe because BuildContainerFile layers on top of the base image
134-
// So this won't delete the actual layers, only the image reference
135-
_, err = pt.Store.DeleteImage(imageRecipe.From, true)
136-
if err != nil {
137-
PrintVerboseWarn("OciExportRootFs", 7.5, "could not delete downloaded image", err)
138-
}
139-
}
146+
// This is safe because BuildContainerFile layers on top of the base image
147+
// So this won't delete the actual layers, only the image reference
148+
_, _ = pt.Store.DeleteImage(imageRecipe.From, true)
140149

141150
// mount image
142151
mountDir, err := pt.MountImage(imageBuild.TopLayer)
@@ -214,7 +223,7 @@ func checkImageSize(imageDir string, filesystemMount string) error {
214223
// and reports the download progress using pterm progressbars. Each blob has
215224
// its own bar, similar to how docker and podman report downloads in their
216225
// respective CLIs
217-
func pullImageWithProgressbar(pt *prometheus.Prometheus, name string, image *ImageRecipe) error {
226+
func pullImageWithProgressbar(pt *prometheus.Prometheus, name string, imageName string) error {
218227
PrintVerboseInfo("pullImageWithProgressbar", "running...")
219228

220229
progressCh := make(chan types.ProgressProperties)
@@ -225,7 +234,7 @@ func pullImageWithProgressbar(pt *prometheus.Prometheus, name string, image *Ima
225234
defer close(manifestCh)
226235
defer close(errorCh)
227236

228-
err := pt.PullImageAsync(image.From, name, progressCh, manifestCh, errorCh)
237+
err := pt.PullImageAsync(imageName, name, progressCh, manifestCh, errorCh)
229238
if err != nil {
230239
PrintVerboseErr("pullImageWithProgressbar", 0, err)
231240
return err

core/system.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, deleteBeforeCopy bo
287287
}, nil, 90, &goodies.NoErrorHandler{}, false)
288288

289289
// Stage 3: Make a imageRecipe with user packages
290+
// then download the image
290291
// ------------------------------------------------
291292
PrintVerboseSimple("[Stage 3] -------- ABSystemRunOperation")
292293

@@ -351,6 +352,15 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation, deleteBeforeCopy bo
351352
content,
352353
)
353354

355+
// Stage 3.2: Download image
356+
if !dryRun {
357+
err = OciPullImage(imageName)
358+
if err != nil {
359+
PrintVerboseErr("ABSystem.RunOperation", 3.5, err)
360+
return err
361+
}
362+
}
363+
354364
// Stage 4: Extract the rootfs
355365
// ------------------------------------------------
356366
PrintVerboseSimple("[Stage 4] -------- ABSystemRunOperation")

0 commit comments

Comments
 (0)