Skip to content

Commit

Permalink
Report pulled image size
Browse files Browse the repository at this point in the history
#752

Signed-off-by: Nieves Montero <[email protected]>
  • Loading branch information
nievesmontero committed Jan 16, 2023
1 parent 936a157 commit 3819924
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ func getFullyQualifiedImageFromRepoTags(image string) (string, error) {
imageFull = image
} else {
info, err := podman.Inspect("image", image)

if err != nil {
return "", fmt.Errorf("failed to inspect image %s", image)
}
Expand Down Expand Up @@ -646,6 +647,7 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
}

func pullImage(image, release string) (bool, error) {
logrus.Debugf("This is the image we're looking for %s", image)
if ok := utils.ImageReferenceCanBeID(image); ok {
logrus.Debugf("Looking for image %s", image)

Expand All @@ -666,18 +668,19 @@ func pullImage(image, release string) (bool, error) {
}

var imageFull string
var imageSize string = "docker://"

if hasDomain {
imageFull = image

} else {
var err error
imageFull, err = utils.GetFullyQualifiedImageFromDistros(image, release)
if err != nil {
return false, fmt.Errorf("image %s not found in local storage and known registries", image)
}
}

logrus.Debugf("Looking for image %s", imageFull)
imageSize += imageFull

if _, err := podman.ImageExists(imageFull); err == nil {
return true, nil
Expand All @@ -698,9 +701,15 @@ func pullImage(image, release string) (bool, error) {
}

if promptForDownload {

infoSize, err := podman.InspectSkopeo("image", imageSize)
if err != nil {
return false, fmt.Errorf("cannot get the size of the image")
}

fmt.Println("Image required to create toolbox container.")

prompt := fmt.Sprintf("Download %s (500MB)? [y/N]:", imageFull)
prompt := fmt.Sprintf("Download %s (%vMB)? [y/N]:", imageFull, infoSize)
shouldPullImage = askForConfirmation(prompt)
}

Expand Down
43 changes: 43 additions & 0 deletions src/pkg/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func ImageExists(image string) (bool, error) {
//
// Parameter 'typearg' takes in values 'container' or 'image' that is passed to the --type flag
func Inspect(typearg string, target string) (map[string]interface{}, error) {

var stdout bytes.Buffer

logLevelString := LogLevel.String()
Expand All @@ -271,6 +272,7 @@ func Inspect(typearg string, target string) (map[string]interface{}, error) {
}

output := stdout.Bytes()

var info []map[string]interface{}

if err := json.Unmarshal(output, &info); err != nil {
Expand All @@ -280,6 +282,47 @@ func Inspect(typearg string, target string) (map[string]interface{}, error) {
return info[0], nil
}

func InspectSkopeo(typearg string, target string) (interface{}, error) {

var stdout bytes.Buffer

args := []string{"inspect", target}

if err := shell.Run("skopeo", nil, &stdout, nil, args...); err != nil {
return nil, err
}

output := stdout.Bytes()

var info interface{}

if err := json.Unmarshal(output, &info); err != nil {
return nil, err
}

m := info.(map[string]interface{})
var totalSize float64 = 0
for k, v := range m {

if k == "LayersData" {
vv := v.([]interface{})

for _, u := range vv {

uu := u.(map[string]interface{})
for j, w := range uu {

if j == "Size" {
totalSize += w.(float64)
}
}
}
}
}
totalSizeIS := (totalSize / 1000000)
return int(totalSizeIS), nil
}

func IsToolboxContainer(container string) (bool, error) {
info, err := Inspect("container", container)
if err != nil {
Expand Down

0 comments on commit 3819924

Please sign in to comment.