mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
d5bff20b10
* Create step to save container image * add error handling for directory creation * Update permissions * remove indirection and add tests * finalize tests * Fix test to also run on linux environments * Update cmd/containerSaveImage.go Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com> * Update resources/metadata/savecontainer.yaml Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com> * Update cmd/containerSaveImage.go Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com> * Update cmd/containerSaveImage_test.go Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com> * Update generated file Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
piperDocker "github.com/SAP/jenkins-library/pkg/docker"
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func containerSaveImage(config containerSaveImageOptions, telemetryData *telemetry.CustomData) {
|
|
var cachePath = "./cache"
|
|
|
|
dClientOptions := piperDocker.ClientOptions{ImageName: config.ContainerImage, RegistryURL: config.ContainerRegistryURL, LocalPath: config.FilePath, IncludeLayers: config.IncludeLayers}
|
|
dClient := &piperDocker.Client{}
|
|
dClient.SetOptions(dClientOptions)
|
|
|
|
err := runContainerSaveImage(&config, telemetryData, cachePath, "", dClient)
|
|
if err != nil {
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
}
|
|
}
|
|
|
|
func runContainerSaveImage(config *containerSaveImageOptions, telemetryData *telemetry.CustomData, cachePath, rootPath string, dClient piperDocker.Download) error {
|
|
err := os.RemoveAll(cachePath)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to prepare cache")
|
|
}
|
|
|
|
err = os.Mkdir(cachePath, 0755)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to create cache")
|
|
}
|
|
|
|
// ensure that download cache is cleaned up at the end
|
|
defer os.RemoveAll(cachePath)
|
|
|
|
imageSource, err := dClient.GetImageSource()
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to get docker image source")
|
|
}
|
|
image, err := dClient.DownloadImageToPath(imageSource, cachePath)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to download docker image")
|
|
}
|
|
|
|
tarfilePath := config.FilePath
|
|
if len(tarfilePath) == 0 {
|
|
tarfilePath = filenameFromContainer(rootPath, config.ContainerImage)
|
|
}
|
|
|
|
tarFile, err := os.Create(tarfilePath)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "failed to create %v for docker image", tarfilePath)
|
|
}
|
|
defer tarFile.Close()
|
|
|
|
if err := os.Chmod(tarfilePath, 0644); err != nil {
|
|
return errors.Wrapf(err, "failed to adapt permissions on %v", tarfilePath)
|
|
}
|
|
|
|
err = dClient.TarImage(tarFile, image)
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to tar container image")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func filenameFromContainer(rootPath, containerImage string) string {
|
|
return filepath.Join(rootPath, strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(containerImage, "/", "_"), ":", "_"), ".", "_")+".tar")
|
|
}
|