Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [close #101] Add progress bar for image pull #191

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 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,60 @@ func OciExportRootFs(buildImageName string, imageRecipe *ImageRecipe, transDir s
return nil
}

func pullImageWithProgressbar(pt *prometheus.Prometheus, name string, image *ImageRecipe) error {
matbme marked this conversation as resolved.
Show resolved Hide resolved
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
Loading