Skip to content

Commit

Permalink
util: add helper to pull containers
Browse files Browse the repository at this point in the history
Add a helper function to check if a container image exists locally, if not,
copy the image into the local containers-storage.
  • Loading branch information
kingsleyzissou committed Mar 12, 2024
1 parent 552095b commit 7cae771
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bib/cmd/bootc-image-builder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/osbuild/bootc-image-builder/bib/internal/setup"
"github.com/osbuild/bootc-image-builder/bib/internal/util"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/cloud/awscloud"
Expand Down Expand Up @@ -101,6 +102,13 @@ func loadConfig(path string) (*BuildConfig, error) {
}

func makeManifest(c *ManifestConfig, cacheRoot string) (manifest.OSBuildManifest, error) {
if !c.Remote {
err := util.EnsureContainerIsInStore(c.Imgref)
if err != nil {
return nil, err
}
}

manifest, err := Manifest(c)
if err != nil {
return nil, err
Expand Down
45 changes: 45 additions & 0 deletions bib/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,48 @@ func RunCmdSync(cmdName string, args ...string) error {
}
return nil
}

func checkContainerImageExists(storageRef string) (bool, error) {
cmd := exec.Command("skopeo", "inspect", storageRef)
err := cmd.Run()
if err != nil {
if cmd.ProcessState.ExitCode() == 1 {
return false, nil
}
return false, err
}
return true, err
}

func pullContainerImage(imgref, storageRef string) error {
return RunCmdSync(
"skopeo",
"copy",
fmt.Sprintf("docker://%s", imgref),
storageRef,
)
}

func EnsureContainerIsInStore(imgref string) error {
driver := "overlay"
graphRoot := "/var/lib/containers/storage"
runRoot := "/run/containers/storage"
storageImgRef := fmt.Sprintf("containers-storage:[%s@%s+%s]%s", driver, graphRoot, runRoot, imgref)

exists, err := checkContainerImageExists(storageImgRef)
if err != nil {
return fmt.Errorf("Failed to check if container is in local store: %v", err)
}

if exists {
fmt.Println("Container image exists in local store")
return nil
}

err = pullContainerImage(imgref, storageImgRef)
if err != nil {
return fmt.Errorf("Failed to pull container: %v", err)
}

return nil
}

0 comments on commit 7cae771

Please sign in to comment.