github.com/SAP/jenkins-library@v1.362.0/cmd/containerSaveImage.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"regexp"
     8  
     9  	piperDocker "github.com/SAP/jenkins-library/pkg/docker"
    10  	"github.com/SAP/jenkins-library/pkg/log"
    11  	"github.com/SAP/jenkins-library/pkg/piperutils"
    12  	"github.com/SAP/jenkins-library/pkg/telemetry"
    13  
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  func containerSaveImage(config containerSaveImageOptions, telemetryData *telemetry.CustomData) {
    18  	var cachePath = "./cache"
    19  
    20  	fileUtils := piperutils.Files{}
    21  
    22  	dClientOptions := piperDocker.ClientOptions{ImageName: config.ContainerImage, RegistryURL: config.ContainerRegistryURL, LocalPath: config.FilePath, ImageFormat: config.ImageFormat}
    23  	dClient := &piperDocker.Client{}
    24  	dClient.SetOptions(dClientOptions)
    25  
    26  	_, err := runContainerSaveImage(&config, telemetryData, cachePath, "", dClient, fileUtils)
    27  	if err != nil {
    28  		log.Entry().WithError(err).Fatal("step execution failed")
    29  	}
    30  }
    31  
    32  func runContainerSaveImage(config *containerSaveImageOptions, telemetryData *telemetry.CustomData, cachePath, rootPath string, dClient piperDocker.Download, fileUtils piperutils.FileUtils) (string, error) {
    33  	if err := correctContainerDockerConfigEnvVar(config, fileUtils); err != nil {
    34  		return "", err
    35  	}
    36  
    37  	tarfilePath := config.FilePath
    38  
    39  	if len(tarfilePath) == 0 {
    40  		tarfilePath = filenameFromContainer(rootPath, config.ContainerImage)
    41  	} else {
    42  		tarfilePath = filepath.Join(rootPath, tarfilePath)
    43  		// tarfilePath is passed as project name that will not consist of the .tar extension hence adding the extension and replacing spaces with _
    44  		if fileExtension := filepath.Ext(tarfilePath); fileExtension != ".tar" {
    45  			tarfilePath = fmt.Sprintf("%s.tar", tarfilePath)
    46  		}
    47  	}
    48  
    49  	log.Entry().Infof("Downloading '%s' to '%s'", config.ContainerImage, tarfilePath)
    50  	if _, err := dClient.DownloadImage(config.ContainerImage, tarfilePath); err != nil {
    51  		return "", errors.Wrap(err, "failed to download docker image")
    52  	}
    53  
    54  	return tarfilePath, nil
    55  }
    56  
    57  func filenameFromContainer(rootPath, containerImage string) string {
    58  	re := regexp.MustCompile("[^a-zA-Z0-9-]")
    59  
    60  	return filepath.Join(rootPath, fmt.Sprintf("%s.tar", re.ReplaceAllString(containerImage, "_")))
    61  }
    62  
    63  func correctContainerDockerConfigEnvVar(config *containerSaveImageOptions, utils piperutils.FileUtils) error {
    64  	dockerConfigDir, err := utils.TempDir("", "docker")
    65  
    66  	if err != nil {
    67  		return errors.Wrap(err, "unable to create docker config dir")
    68  	}
    69  
    70  	dockerConfigFile := fmt.Sprintf("%s/%s", dockerConfigDir, "config.json")
    71  
    72  	if len(config.DockerConfigJSON) > 0 {
    73  		log.Entry().Infof("Docker credentials configuration: %v", config.DockerConfigJSON)
    74  
    75  		if exists, _ := utils.FileExists(config.DockerConfigJSON); exists {
    76  			if _, err = utils.Copy(config.DockerConfigJSON, dockerConfigFile); err != nil {
    77  				return errors.Wrap(err, "unable to copy docker config")
    78  			}
    79  		}
    80  	} else {
    81  		log.Entry().Info("Docker credentials configuration: NONE")
    82  	}
    83  
    84  	if len(config.ContainerRegistryURL) > 0 && len(config.ContainerRegistryUser) > 0 && len(config.ContainerRegistryPassword) > 0 {
    85  		if _, err = piperDocker.CreateDockerConfigJSON(config.ContainerRegistryURL, config.ContainerRegistryUser, config.ContainerRegistryPassword, dockerConfigFile, dockerConfigFile, utils); err != nil {
    86  			log.Entry().Warningf("failed to update Docker config.json: %v", err)
    87  		}
    88  	}
    89  
    90  	os.Setenv("DOCKER_CONFIG", dockerConfigDir)
    91  
    92  	return nil
    93  }