2020-05-18 08:55:25 +02:00
|
|
|
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)
|
|
|
|
|
2022-01-24 10:48:01 +02:00
|
|
|
_, err := runContainerSaveImage(&config, telemetryData, cachePath, "", dClient)
|
2020-05-18 08:55:25 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Entry().WithError(err).Fatal("step execution failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:48:01 +02:00
|
|
|
func runContainerSaveImage(config *containerSaveImageOptions, telemetryData *telemetry.CustomData, cachePath, rootPath string, dClient piperDocker.Download) (string, error) {
|
2020-05-18 08:55:25 +02:00
|
|
|
err := os.RemoveAll(cachePath)
|
|
|
|
if err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrap(err, "failed to prepare cache")
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir(cachePath, 0755)
|
|
|
|
if err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrap(err, "failed to create cache")
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that download cache is cleaned up at the end
|
|
|
|
defer os.RemoveAll(cachePath)
|
|
|
|
|
|
|
|
imageSource, err := dClient.GetImageSource()
|
|
|
|
if err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrap(err, "failed to get docker image source")
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
image, err := dClient.DownloadImageToPath(imageSource, cachePath)
|
|
|
|
if err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrap(err, "failed to download docker image")
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tarfilePath := config.FilePath
|
|
|
|
if len(tarfilePath) == 0 {
|
|
|
|
tarfilePath = filenameFromContainer(rootPath, config.ContainerImage)
|
2021-12-13 17:14:32 +02:00
|
|
|
} else {
|
|
|
|
tarfilePath = filenameFromContainer(rootPath, tarfilePath)
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tarFile, err := os.Create(tarfilePath)
|
|
|
|
if err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrapf(err, "failed to create %v for docker image", tarfilePath)
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
defer tarFile.Close()
|
|
|
|
|
|
|
|
if err := os.Chmod(tarfilePath, 0644); err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrapf(err, "failed to adapt permissions on %v", tarfilePath)
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = dClient.TarImage(tarFile, image)
|
|
|
|
if err != nil {
|
2022-01-24 10:48:01 +02:00
|
|
|
return "", errors.Wrap(err, "failed to tar container image")
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:48:01 +02:00
|
|
|
return tarfilePath, nil
|
2020-05-18 08:55:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func filenameFromContainer(rootPath, containerImage string) string {
|
|
|
|
return filepath.Join(rootPath, strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(containerImage, "/", "_"), ":", "_"), ".", "_")+".tar")
|
|
|
|
}
|