2020-02-06 11:47:45 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2021-11-04 11:19:33 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2020-02-06 11:47:45 +02:00
|
|
|
"fmt"
|
2022-03-23 11:02:00 +02:00
|
|
|
"os"
|
2022-05-13 10:02:46 +02:00
|
|
|
"path"
|
2021-12-20 18:15:13 +02:00
|
|
|
"path/filepath"
|
2022-03-23 11:02:00 +02:00
|
|
|
"regexp"
|
2020-02-06 11:47:45 +02:00
|
|
|
"strings"
|
|
|
|
|
2022-02-07 08:58:41 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2021-11-04 11:19:33 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2022-03-30 13:58:16 +02:00
|
|
|
"github.com/pkg/errors"
|
2021-11-04 11:19:33 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd"
|
2022-03-30 13:58:16 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/authn"
|
2022-03-23 11:02:00 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/crane"
|
2020-02-06 11:47:45 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
2022-03-30 13:58:16 +02:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
2020-02-06 11:47:45 +02:00
|
|
|
)
|
|
|
|
|
2021-11-04 11:19:33 +02:00
|
|
|
// AuthEntry defines base64 encoded username:password required inside a Docker config.json
|
|
|
|
type AuthEntry struct {
|
|
|
|
Auth string `json:"auth,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateDockerConfigJSON creates / updates a Docker config.json with registry credentials
|
|
|
|
func CreateDockerConfigJSON(registryURL, username, password, targetPath, configPath string, utils piperutils.FileUtils) (string, error) {
|
|
|
|
|
|
|
|
if len(targetPath) == 0 {
|
|
|
|
targetPath = configPath
|
|
|
|
}
|
|
|
|
|
|
|
|
dockerConfig := map[string]interface{}{}
|
|
|
|
if exists, _ := utils.FileExists(configPath); exists {
|
|
|
|
dockerConfigContent, err := utils.FileRead(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read file '%v': %w", configPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(dockerConfigContent, &dockerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to unmarshal json file '%v': %w", configPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
credentialsBase64 := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", username, password)))
|
|
|
|
dockerAuth := AuthEntry{Auth: credentialsBase64}
|
|
|
|
|
|
|
|
if dockerConfig["auths"] == nil {
|
|
|
|
dockerConfig["auths"] = map[string]AuthEntry{registryURL: dockerAuth}
|
|
|
|
} else {
|
|
|
|
authEntries, ok := dockerConfig["auths"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("failed to read authentication entries from file '%v': format invalid", configPath)
|
|
|
|
}
|
|
|
|
authEntries[registryURL] = dockerAuth
|
|
|
|
dockerConfig["auths"] = authEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResult, err := json.Marshal(dockerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to marshal Docker config.json: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-20 18:15:13 +02:00
|
|
|
//always create the target path directories if any before writing
|
2021-12-21 08:48:36 +02:00
|
|
|
err = utils.MkdirAll(filepath.Dir(targetPath), 0777)
|
2021-12-20 18:15:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to create directory path for the Docker config.json file %v:%w", targetPath, err)
|
|
|
|
}
|
2021-11-04 11:19:33 +02:00
|
|
|
err = utils.FileWrite(targetPath, jsonResult, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to write Docker config.json: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetPath, nil
|
|
|
|
}
|
|
|
|
|
2020-02-06 11:47:45 +02:00
|
|
|
// Client defines an docker client object
|
|
|
|
type Client struct {
|
|
|
|
imageName string
|
|
|
|
registryURL string
|
|
|
|
localPath string
|
|
|
|
includeLayers bool
|
2022-05-13 18:56:41 +02:00
|
|
|
imageFormat string
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientOptions defines the options to be set on the client
|
|
|
|
type ClientOptions struct {
|
2022-03-23 11:02:00 +02:00
|
|
|
ImageName string
|
|
|
|
RegistryURL string
|
|
|
|
LocalPath string
|
2022-05-13 18:56:41 +02:00
|
|
|
ImageFormat string
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Download interface for download an image to a local path
|
|
|
|
type Download interface {
|
2022-03-23 11:02:00 +02:00
|
|
|
DownloadImage(imageSource, targetFile string) (v1.Image, error)
|
|
|
|
DownloadImageContent(imageSource, targetDir string) (v1.Image, error)
|
2022-03-30 13:58:16 +02:00
|
|
|
GetRemoteImageInfo(string) (v1.Image, error)
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions sets options used for the docker client
|
|
|
|
func (c *Client) SetOptions(options ClientOptions) {
|
|
|
|
c.imageName = options.ImageName
|
|
|
|
c.registryURL = options.RegistryURL
|
|
|
|
c.localPath = options.LocalPath
|
2022-05-13 18:56:41 +02:00
|
|
|
c.imageFormat = options.ImageFormat
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
//DownloadImageContent downloads the image content into the given targetDir. Returns with an error if the targetDir doesnt exist
|
2022-03-23 11:02:00 +02:00
|
|
|
func (c *Client) DownloadImageContent(imageSource, targetDir string) (v1.Image, error) {
|
|
|
|
if fileInfo, err := os.Stat(targetDir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !fileInfo.IsDir() {
|
|
|
|
return nil, fmt.Errorf("specified target is not a directory: %s", targetDir)
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
noOpts := []crane.Option{}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
imageRef, err := c.getImageRef(imageSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
img, err := crane.Pull(imageRef.Name(), noOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
tmpFile, err := os.CreateTemp(".", ".piper-download-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpFile.Name())
|
2021-08-17 15:52:18 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
args := []string{imageRef.Name(), tmpFile.Name()}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
exportCmd := cranecmd.NewCmdExport(&noOpts)
|
|
|
|
exportCmd.SetArgs(args)
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
if err := exportCmd.Execute(); err != nil {
|
|
|
|
return nil, err
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
return img, piperutils.Untar(tmpFile.Name(), targetDir, 0)
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
// DownloadImage downloads the image and saves it as tar at the given path
|
|
|
|
func (c *Client) DownloadImage(imageSource, targetFile string) (v1.Image, error) {
|
|
|
|
noOpts := []crane.Option{}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
imageRef, err := c.getImageRef(imageSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
img, err := crane.Pull(imageRef.Name(), noOpts...)
|
2020-02-06 11:47:45 +02:00
|
|
|
if err != nil {
|
2022-03-23 11:02:00 +02:00
|
|
|
return nil, err
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
tmpFile, err := os.CreateTemp(".", ".piper-download-")
|
2020-02-06 11:47:45 +02:00
|
|
|
if err != nil {
|
2022-03-23 11:02:00 +02:00
|
|
|
return nil, err
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
craneCmd := cranecmd.NewCmdPull(&noOpts)
|
|
|
|
craneCmd.SetOut(log.Writer())
|
|
|
|
craneCmd.SetErr(log.Writer())
|
2022-05-13 18:56:41 +02:00
|
|
|
craneCmd.SetArgs([]string{imageRef.Name(), tmpFile.Name(), "--format=" + c.imageFormat})
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
if err := craneCmd.Execute(); err != nil {
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(tmpFile.Name(), targetFile); err != nil {
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
// GetRemoteImageInfo retrieves information about the image (e.g. digest) without actually downoading it
|
|
|
|
func (c *Client) GetRemoteImageInfo(imageSource string) (v1.Image, error) {
|
|
|
|
ref, err := c.getImageRef(imageSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "parsing image reference")
|
|
|
|
}
|
|
|
|
|
|
|
|
return remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
|
|
|
|
}
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
func (c *Client) getImageRef(image string) (name.Reference, error) {
|
|
|
|
opts := []name.Option{}
|
2022-05-13 10:02:46 +02:00
|
|
|
registry := ""
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
if len(c.registryURL) > 0 {
|
|
|
|
re := regexp.MustCompile(`(?i)^https?://`)
|
2022-05-13 10:02:46 +02:00
|
|
|
registry = re.ReplaceAllString(c.registryURL, "")
|
2022-03-23 11:02:00 +02:00
|
|
|
opts = append(opts, name.WithDefaultRegistry(registry))
|
|
|
|
}
|
|
|
|
|
2022-05-13 10:02:46 +02:00
|
|
|
return name.ParseReference(path.Join(registry, image), opts...)
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
2022-02-07 08:58:41 +02:00
|
|
|
|
|
|
|
// ImageListWithFilePath compiles container image names based on all Dockerfiles found, considering excludes
|
|
|
|
// according to following search pattern: **/Dockerfile*
|
|
|
|
// Return value contains a map with image names and file path
|
|
|
|
// Examples for image names with imageName testImage
|
|
|
|
// * Dockerfile: `imageName`
|
|
|
|
// * sub1/Dockerfile: `imageName-sub1`
|
|
|
|
// * sub2/Dockerfile_proxy: `imageName-sub2-proxy`
|
2022-03-11 10:47:44 +02:00
|
|
|
func ImageListWithFilePath(imageName string, excludes []string, trimDir string, utils piperutils.FileUtils) (map[string]string, error) {
|
2022-02-07 08:58:41 +02:00
|
|
|
|
|
|
|
imageList := map[string]string{}
|
|
|
|
|
|
|
|
pattern := "**/Dockerfile*"
|
|
|
|
|
|
|
|
matches, err := utils.Glob(pattern)
|
|
|
|
if err != nil || len(matches) == 0 {
|
|
|
|
return imageList, fmt.Errorf("failed to retrieve Dockerfiles")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dockerfilePath := range matches {
|
|
|
|
// make sure that the path we have is relative
|
|
|
|
// ToDo: needs rework
|
|
|
|
//dockerfilePath = strings.ReplaceAll(dockerfilePath, cwd, ".")
|
|
|
|
|
|
|
|
if piperutils.ContainsString(excludes, dockerfilePath) {
|
|
|
|
log.Entry().Infof("Discard %v since it is in the exclude list %v", dockerfilePath, excludes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if dockerfilePath == "Dockerfile" {
|
|
|
|
imageList[imageName] = dockerfilePath
|
|
|
|
} else {
|
|
|
|
var finalName string
|
|
|
|
if base := filepath.Base(dockerfilePath); base == "Dockerfile" {
|
2022-03-11 10:47:44 +02:00
|
|
|
subName := strings.ReplaceAll(filepath.Dir(dockerfilePath), string(filepath.Separator), "-")
|
|
|
|
if len(trimDir) > 0 {
|
|
|
|
// allow to remove trailing sub directories
|
|
|
|
// example .ci/app/Dockerfile
|
|
|
|
// with trimDir = .ci/ imagename would only contain app part.
|
|
|
|
subName = strings.TrimPrefix(subName, strings.ReplaceAll(trimDir, "/", "-"))
|
|
|
|
// make sure that subName does not start with a - (e.g. due not configuring trailing slash for trimDir)
|
|
|
|
subName = strings.TrimPrefix(subName, "-")
|
|
|
|
}
|
|
|
|
finalName = fmt.Sprintf("%v-%v", imageName, subName)
|
2022-02-07 08:58:41 +02:00
|
|
|
} else {
|
|
|
|
parts := strings.FieldsFunc(base, func(separator rune) bool {
|
|
|
|
return separator == []rune("-")[0] || separator == []rune("_")[0]
|
|
|
|
})
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return imageList, fmt.Errorf("wrong format of Dockerfile, must be inside a sub-folder or contain a separator")
|
|
|
|
}
|
|
|
|
parts[0] = imageName
|
|
|
|
finalName = strings.Join(parts, "-")
|
|
|
|
}
|
|
|
|
|
|
|
|
imageList[finalName] = dockerfilePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageList, nil
|
|
|
|
}
|