Skip to content

Commit

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

The following package and file have been created src/pkg/skopeo/skopeo.go
Signed-off-by: Nieves Montero <[email protected]>
  • Loading branch information
nievesmontero committed Jan 26, 2023
1 parent 446d7bf commit b83611d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/briandowns/spinner"
"github.com/containers/toolbox/pkg/podman"
"github.com/containers/toolbox/pkg/shell"
"github.com/containers/toolbox/pkg/skopeo"
"github.com/containers/toolbox/pkg/utils"
"github.com/godbus/dbus/v5"
"github.com/sirupsen/logrus"
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("Looking for image %s", image)
if ok := utils.ImageReferenceCanBeID(image); ok {
logrus.Debugf("Looking for image %s", image)

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

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

if hasDomain {
imageFull = image
Expand All @@ -676,8 +679,7 @@ func pullImage(image, release string) (bool, error) {
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 +700,15 @@ func pullImage(image, release string) (bool, error) {
}

if promptForDownload {

infoSize, err := skopeo.Inspect(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
2 changes: 1 addition & 1 deletion src/pkg/podman/podman.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2019 – 2022 Red Hat Inc.
* Copyright © 2023 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
69 changes: 69 additions & 0 deletions src/pkg/skopeo/skopeo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
<<<<<<< HEAD
* Copyright © 2019 – 2022 Red Hat Inc.
=======
* Copyright © 2023 Red Hat Inc.
>>>>>>> ee10f11 (Move skopeo-related code into a new package)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package skopeo

import (
"bytes"
"encoding/json"

"github.com/containers/toolbox/pkg/shell"
)

func Inspect(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
}

0 comments on commit b83611d

Please sign in to comment.