github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/client/docker/images.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package docker
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"strings"
    21  
    22  	"github.com/docker/distribution/reference"
    23  
    24  	dockerstreams "github.com/docker/cli/cli/streams"
    25  	"github.com/docker/docker/api/types"
    26  	dockerjsonmessage "github.com/docker/docker/pkg/jsonmessage"
    27  
    28  	"github.com/alibaba/sealer/common"
    29  	"github.com/alibaba/sealer/logger"
    30  	"github.com/alibaba/sealer/utils"
    31  )
    32  
    33  func (d Docker) ImagesPull(images []string) error {
    34  	for _, image := range utils.RemoveDuplicate(images) {
    35  		if image == "" {
    36  			continue
    37  		}
    38  		if strings.HasPrefix(image, "#") {
    39  			continue
    40  		}
    41  		if err := d.ImagePull(trimQuotes(strings.TrimSpace(image))); err != nil {
    42  			return fmt.Errorf("image %s pull failed: %v", image, err)
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  func trimQuotes(s string) string {
    49  	if len(s) >= 2 {
    50  		if c := s[len(s)-1]; s[0] == c && (c == '"' || c == '\'') {
    51  			return s[1 : len(s)-1]
    52  		}
    53  	}
    54  	return s
    55  }
    56  
    57  func (d Docker) ImagesPullByImageListFile(fileName string) error {
    58  	data, err := utils.ReadLines(fileName)
    59  	if err != nil {
    60  		logger.Error(fmt.Sprintf("Read image list failed: %v", err))
    61  	}
    62  	return d.ImagesPull(data)
    63  }
    64  
    65  func (d Docker) ImagesPullByList(images []string) error {
    66  	return d.ImagesPull(images)
    67  }
    68  
    69  func (d Docker) ImagePull(image string) error {
    70  	var (
    71  		err   error
    72  		out   io.ReadCloser
    73  		named reference.Named
    74  	)
    75  
    76  	named, err = GetCanonicalImageName(image)
    77  	if err != nil {
    78  		return fmt.Errorf("failed to parse canonical image name %s : %v", image, err)
    79  	}
    80  	opts := GetCanonicalImagePullOptions(named.String())
    81  
    82  	out, err = d.cli.ImagePull(d.ctx, named.String(), opts)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	defer func() {
    87  		_ = out.Close()
    88  	}()
    89  
    90  	err = dockerjsonmessage.DisplayJSONMessagesToStream(out, dockerstreams.NewOut(common.StdOut), nil)
    91  	if err != nil && err != io.ErrClosedPipe {
    92  		logger.Warn("error occurs in display progressing, err: %s", err)
    93  	}
    94  	logger.Info("success to pull docker image: %s ", image)
    95  	return nil
    96  }
    97  
    98  func (d Docker) DockerRmi(imageID string) error {
    99  	if _, err := d.cli.ImageRemove(d.ctx, imageID, types.ImageRemoveOptions{Force: true, PruneChildren: true}); err != nil {
   100  		return err
   101  	}
   102  	return nil
   103  }
   104  
   105  func (d Docker) ImagesList() ([]types.ImageSummary, error) {
   106  	images, err := d.cli.ImageList(d.ctx, types.ImageListOptions{})
   107  	if err != nil {
   108  		return nil, err
   109  	}
   110  	return images, nil
   111  }