Skip to content

Commit

Permalink
feat: [close #101] Add progress bar for image pull (#191)
Browse files Browse the repository at this point in the history
* Update dependencies

* Pull image before building Containerfile

* Report image pull with pterm

* Document pullImageWithProgressbar
  • Loading branch information
matbme authored Jan 8, 2024
1 parent 9acbbc4 commit ff15f84
Show file tree
Hide file tree
Showing 225 changed files with 8,993 additions and 4,578 deletions.
70 changes: 70 additions & 0 deletions core/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ package core
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/containers/buildah"
"github.com/containers/image/v5/types"
cstypes "github.com/containers/storage/types"
humanize "github.com/dustin/go-humanize"
"github.com/pterm/pterm"
"github.com/vanilla-os/abroot/settings"
"github.com/vanilla-os/prometheus"
)
Expand Down Expand Up @@ -73,6 +78,13 @@ func OciExportRootFs(buildImageName string, imageRecipe *ImageRecipe, transDir s
return err
}

// pull image
err = pullImageWithProgressbar(pt, buildImageName, imageRecipe)
if err != nil {
PrintVerboseErr("OciExportRootFs", 6.1, err)
return err
}

// build image
imageBuild, err := pt.BuildContainerFile(imageRecipePath, buildImageName)
if err != nil {
Expand Down Expand Up @@ -104,6 +116,64 @@ func OciExportRootFs(buildImageName string, imageRecipe *ImageRecipe, transDir s
return nil
}

// pullImageWithProgressbar pulls the image specified in the provided recipe
// and reports the download progress using pterm progressbars. Each blob has
// its own bar, similar to how docker and podman report downloads in their
// respective CLIs
func pullImageWithProgressbar(pt *prometheus.Prometheus, name string, image *ImageRecipe) error {
PrintVerboseInfo("pullImageWithProgressbar", "running...")

progressCh := make(chan types.ProgressProperties)
manifestCh := make(chan prometheus.OciManifest)

defer close(progressCh)
defer close(manifestCh)

err := pt.PullImageAsync(image.From, name, progressCh, manifestCh)
if err != nil {
PrintVerboseErr("pullImageWithProgressbar", 0, err)
return err
}

multi := pterm.DefaultMultiPrinter
bars := map[string]*pterm.ProgressbarPrinter{}

multi.Start()

barFmt := "%s [%s/%s]"
for {
select {
case report := <-progressCh:
digest := report.Artifact.Digest.Encoded()
if pb, ok := bars[digest]; ok {
progressBytes := humanize.Bytes(uint64(report.Offset))
totalBytes := humanize.Bytes(uint64(report.Artifact.Size))

pb.Add(int(report.Offset) - pb.Current)

title := fmt.Sprintf(barFmt, digest[:12], progressBytes, totalBytes)
pb.UpdateTitle(title + strings.Repeat(" ", 28-len(title)))
} else {
totalBytes := humanize.Bytes(uint64(report.Artifact.Size))

title := fmt.Sprintf(barFmt, digest[:12], "0", totalBytes)
newPb, err := pterm.DefaultProgressbar.WithTotal(int(report.Artifact.Size)).WithWriter(multi.NewWriter()).WithMaxWidth(120).WithShowCount(false).Start(title + strings.Repeat(" ", 28-len(title)))
if err != nil {
PrintVerboseErr("pullImageWithProgressbar", 1, err)
return err
}

bars[digest] = newPb
}
case <-manifestCh:
multi.Stop()
return nil
}
}
}

// FindImageWithLabel returns the name of the first image containinig the provided key-value pair
// or an empty string if none was found
// FindImageWithLabel returns the name of the first image containing the
// provided key-value pair or an empty string if none was found
func FindImageWithLabel(key, value string) (string, error) {
Expand Down
3 changes: 2 additions & 1 deletion core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
imageName = settings.Cnf.FullImageName
}
default:
imageName = settings.Cnf.FullImageName + "@" + imageDigest
imageName = strings.Split(settings.Cnf.FullImageName, ":")[0]
imageName += "@" + imageDigest
labels["ABRoot.BaseImageDigest"] = imageDigest
}

Expand Down
70 changes: 36 additions & 34 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ module github.com/vanilla-os/abroot
go 1.21.4

require (
github.com/containers/buildah v1.34.0
github.com/containers/storage v1.51.1-0.20231204015418-15c3cb7881e4
github.com/containers/buildah v1.33.2
github.com/containers/image/v5 v5.29.0
github.com/containers/storage v1.51.0
github.com/dustin/go-humanize v1.0.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/spf13/cobra v1.8.0
github.com/vanilla-os/differ/diff v0.0.0-20231207205048-0a8c0510e12f
github.com/vanilla-os/differ/diff v0.0.0-20231226220222-4f9298cef551
github.com/vanilla-os/orchid v0.4.0
github.com/vanilla-os/prometheus v0.1.6
golang.org/x/sys v0.15.0
github.com/vanilla-os/prometheus v0.2.0
golang.org/x/sys v0.16.0
)

require (
Expand All @@ -19,47 +21,46 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.12.0-rc.1 // indirect
github.com/Microsoft/hcsshim v0.12.0-rc.2 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/containerd/cgroups/v3 v3.0.2 // indirect
github.com/containerd/cgroups/v3 v3.0.3 // indirect
github.com/containerd/containerd v1.7.11 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containerd/typeurl/v2 v2.1.1 // indirect
github.com/containernetworking/cni v1.1.2 // indirect
github.com/containernetworking/plugins v1.4.0 // indirect
github.com/containers/common v0.57.1 // indirect
github.com/containers/image/v5 v5.29.1-0.20231120202631-293b00ba7166 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/luksy v0.0.0-20231127213545-c2b9b9dbf004 // indirect
github.com/containers/ocicrypt v1.1.9 // indirect
github.com/cyberphone/json-canonicalization v0.0.0-20231011164504-785e29786b46 // indirect
github.com/cyberphone/json-canonicalization v0.0.0-20231217050601-ba74d44ecf5f // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/disiqueira/gotree/v3 v3.0.2 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.0 // indirect
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11 // indirect
github.com/docker/go-connections v0.4.1-0.20231110212414-fa09c952e3ea // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/fsouza/go-dockerclient v1.10.0 // indirect
github.com/fsouza/go-dockerclient v1.10.1 // indirect
github.com/go-jose/go-jose/v3 v3.0.1 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.4 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/analysis v0.22.1 // indirect
github.com/go-openapi/errors v0.21.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/loads v0.21.5 // indirect
github.com/go-openapi/runtime v0.26.2 // indirect
github.com/go-openapi/spec v0.20.11 // indirect
github.com/go-openapi/strfmt v0.21.9 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-openapi/validate v0.22.3 // indirect
github.com/go-openapi/spec v0.20.13 // indirect
github.com/go-openapi/strfmt v0.22.0 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-openapi/validate v0.22.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -72,7 +73,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/pgzip v1.2.6 // indirect
github.com/letsencrypt/boulder v0.0.0-20231215175834-164e035915e2 // indirect
github.com/letsencrypt/boulder v0.0.0-20240104140712-c1f7de06e9f8 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
Expand All @@ -90,7 +91,7 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc5 //indirect
github.com/opencontainers/runc v1.1.10 // indirect
github.com/opencontainers/runc v1.1.11 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opencontainers/runtime-tools v0.9.1-0.20230914150019-408c51e934dc // indirect
github.com/opencontainers/selinux v1.11.0 // indirect
Expand All @@ -102,14 +103,15 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/seccomp/libseccomp-golang v0.10.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.7.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.8.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sigstore/fulcio v1.4.3 // indirect
github.com/sigstore/rekor v1.3.4 // indirect
github.com/sigstore/sigstore v1.7.6 // indirect
github.com/sigstore/sigstore v1.8.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6 // indirect
github.com/sylabs/sif/v2 v2.15.0 // indirect
github.com/sylabs/sif/v2 v2.15.1 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/tchap/go-patricia/v2 v2.3.1 // indirect
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 // indirect
Expand All @@ -126,15 +128,15 @@ require (
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/go-jose/go-jose.v2 v2.6.2 // indirect
tags.cncf.io/container-device-interface v0.6.2 // indirect
)
Expand All @@ -156,16 +158,16 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pterm/pterm v0.12.71 // indirect
github.com/pterm/pterm v0.12.74
github.com/rivo/uniseg v0.4.4 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.18.1
github.com/spf13/viper v1.18.2
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit ff15f84

Please sign in to comment.