Files
podman/pkg/machine/fedora.go
Ashley Cui b513dc4c1e Clean up cached machine images
When initing machines, we download a machine image, and uncompress and
copy the image for the actual vm image. When a user constantly pulls new
machines, there may be a buildup of old, unused machine images. This
commit cleans ups the unused cached images.

Changes:
- If the machine is pulled from a URL or from the FCOS releases, we pull
  them into XDG_DATA_HOME/containers/podman/machine/vmType/cache
- Cache cleanups only happen if there is a cache miss, and we need to
  pull a new image
- For Fedora and FCOS, we actually use the cache, so we go through the
  cache dir and remove any images older than 2 weeks (FCOS's release cycle), on a cache miss.
- For generic files pulled from a URL, we don't actually cache, so we
  delete the pulled file immediately after creating a machine image
- For generic files from a local path, the original file will never be
  cleaned up

Note that because we cache in a different dir, this will not clean up
old images pulled before this commit.

[NO NEW TESTS NEEDED]

Signed-off-by: Ashley Cui <acui@redhat.com>
2022-07-15 09:10:43 -04:00

98 lines
2.1 KiB
Go

//go:build amd64 || arm64
// +build amd64 arm64
package machine
import (
"errors"
"fmt"
"os"
"net/http"
"net/url"
"path/filepath"
"time"
)
const (
githubLatestReleaseURL = "https://github.com/containers/podman-wsl-fedora/releases/latest/download/rootfs.tar.xz"
)
type FedoraDownload struct {
Download
}
func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
downloadURL, size, err := getFedoraDownload(githubLatestReleaseURL)
if err != nil {
return nil, err
}
cacheDir, err := GetCacheDir(vmType)
if err != nil {
return nil, err
}
imageName := "rootfs.tar.xz"
f := FedoraDownload{
Download: Download{
Arch: getFcosArch(),
Artifact: artifact,
CacheDir: cacheDir,
Format: Format,
ImageName: imageName,
LocalPath: filepath.Join(cacheDir, imageName),
URL: downloadURL,
VMName: vmName,
Size: size,
},
}
dataDir, err := GetDataDir(vmType)
if err != nil {
return nil, err
}
f.Download.LocalUncompressedFile = f.getLocalUncompressedFile(dataDir)
return f, nil
}
func (f FedoraDownload) Get() *Download {
return &f.Download
}
func (f FedoraDownload) HasUsableCache() (bool, error) {
info, err := os.Stat(f.LocalPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
return false, err
}
return info.Size() == f.Size, nil
}
func (f FedoraDownload) CleanCache() error {
// Set cached image to expire after 2 weeks
expire := 14 * 24 * time.Hour
return removeImageAfterExpire(f.CacheDir, expire)
}
func getFedoraDownload(releaseURL string) (*url.URL, int64, error) {
downloadURL, err := url.Parse(releaseURL)
if err != nil {
return nil, -1, fmt.Errorf("invalid URL generated from discovered Fedora file: %s: %w", releaseURL, err)
}
resp, err := http.Head(releaseURL)
if err != nil {
return nil, -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
}
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
}
return downloadURL, resp.ContentLength, nil
}