1
//go:build amd64 || arm64
16
"github.com/containers/podman/v5/pkg/machine/compression"
17
"github.com/containers/podman/v5/pkg/machine/define"
18
"github.com/containers/podman/v5/pkg/machine/env"
19
"github.com/containers/podman/v5/pkg/machine/ocipull"
20
"github.com/containers/podman/v5/utils"
21
"github.com/sirupsen/logrus"
24
// GenericDownload is used when a user provides a URL
25
// or path for an image
26
type GenericDownload struct {
30
// NewGenericDownloader is used when the disk image is provided by the user
31
func NewGenericDownloader(vmType define.VMType, vmName, pullPath string) (DistributionDownload, error) {
35
dataDir, err := env.GetDataDir(vmType)
39
cacheDir, err := env.GetCacheDir(vmType)
44
// Is pullpath a file or url?
45
if getURL := supportedURL(pullPath); getURL != nil {
46
urlSplit := strings.Split(getURL.Path, "/")
47
imageName = urlSplit[len(urlSplit)-1]
49
dl.LocalPath = filepath.Join(cacheDir, imageName)
51
// Dealing with FilePath
52
imageName = filepath.Base(pullPath)
53
dl.LocalPath = pullPath
56
dl.ImageName = imageName
57
dl.LocalUncompressedFile = dl.GetLocalUncompressedFile(dataDir)
58
// The download needs to be pulled into the datadir
60
gd := GenericDownload{Download: dl}
64
func supportedURL(path string) (url *url2.URL) {
65
getURL, err := url2.Parse(path)
67
// ignore error, probably not a URL, fallback & treat as file path
71
// Check supported scheme. Since URL is passed to net.http, only http
72
// schemes are supported. Also, windows drive paths can resemble a
73
// URL, but with a single letter scheme. These values should be
74
// passed through for interpretation as a file path.
75
switch getURL.Scheme {
85
func (dl Download) GetLocalUncompressedFile(dataDir string) string {
86
compressedFilename := dl.VMName + "_" + dl.ImageName
87
extension := compression.KindFromFile(compressedFilename)
88
uncompressedFile := strings.TrimSuffix(compressedFilename, fmt.Sprintf(".%s", extension.String()))
89
dl.LocalUncompressedFile = filepath.Join(dataDir, uncompressedFile)
90
return dl.LocalUncompressedFile
93
func (g GenericDownload) Get() *Download {
97
func (g GenericDownload) HasUsableCache() (bool, error) {
98
// If we have a URL for this "downloader", we now pull it
99
return g.URL == nil, nil
102
// CleanCache cleans out downloaded uncompressed image files
103
func (g GenericDownload) CleanCache() error {
104
// Remove any image that has been downloaded via URL
105
// We never read from cache for generic downloads
107
if err := os.Remove(g.LocalPath); err != nil && !errors.Is(err, os.ErrNotExist) {
114
func DownloadImage(d DistributionDownload) error {
115
// check if the latest image is already present
116
ok, err := d.HasUsableCache()
121
if err := DownloadVMImage(d.Get().URL, d.Get().ImageName, d.Get().LocalPath); err != nil {
124
// Clean out old cached images, since we didn't find needed image in cache
126
if err = d.CleanCache(); err != nil {
127
logrus.Warnf("error cleaning machine image cache: %s", err)
131
localPath, err := define.NewMachineFile(d.Get().LocalPath, nil)
135
return compression.Decompress(localPath, d.Get().LocalUncompressedFile)
138
// DownloadVMImage downloads a VM image from url to given path
139
// with download status
140
func DownloadVMImage(downloadURL *url2.URL, imageName string, localImagePath string) error {
141
out, err := os.Create(localImagePath)
146
if err := out.Close(); err != nil {
151
resp, err := http.Get(downloadURL.String())
156
if err := resp.Body.Close(); err != nil {
161
if resp.StatusCode != http.StatusOK {
162
return fmt.Errorf("downloading VM image %s: %s", downloadURL, resp.Status)
164
size := resp.ContentLength
165
prefix := "Downloading VM image: " + imageName
166
onComplete := prefix + ": done"
168
p, bar := utils.ProgressBar(prefix, size, onComplete)
170
proxyReader := bar.ProxyReader(resp.Body)
172
if err := proxyReader.Close(); err != nil {
177
if _, err := io.Copy(out, proxyReader); err != nil {
185
func RemoveImageAfterExpire(dir string, expire time.Duration) error {
187
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
188
// Delete any cache files that are older than expiry date
189
if !info.IsDir() && (now.Sub(info.ModTime()) > expire) {
190
err := os.Remove(path)
191
if err != nil && !errors.Is(err, os.ErrNotExist) {
192
logrus.Warnf("unable to clean up cached image: %s", path)
194
logrus.Debugf("cleaning up cached image: %s", path)
202
// AcquireAlternateImage downloads the alternate image the user provided, which
203
// can be a file path or URL
204
func (dl Download) AcquireAlternateImage(inputPath string) (*define.VMFile, error) {
205
g, err := NewGenericDownloader(dl.VMKind, dl.VMName, inputPath)
210
imagePath, err := define.NewMachineFile(g.Get().LocalUncompressedFile, nil)
215
if err := DownloadImage(g); err != nil {
219
return imagePath, nil
222
func isOci(input string) (bool, *ocipull.OCIKind, error) { //nolint:unused
223
inputURL, err := url2.Parse(input)
225
return false, nil, err
227
switch inputURL.Scheme {
228
case ocipull.OCIDir.String():
229
return true, &ocipull.OCIDir, nil
230
case ocipull.OCIRegistry.String():
231
return true, &ocipull.OCIRegistry, nil
233
return false, nil, nil