summaryrefslogtreecommitdiff
path: root/pkg/machine/fedora.go
blob: 7ac4692e3373d28a21268fa8807f7d68c53b029e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//go:build amd64 || arm64
// +build amd64 arm64

package machine

import (
	"errors"
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"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, version, size, err := getFedoraDownload(githubLatestReleaseURL)
	if err != nil {
		return nil, err
	}

	cacheDir, err := GetCacheDir(vmType)
	if err != nil {
		return nil, err
	}

	imageName := fmt.Sprintf("fedora-podman-%s.tar.xz", version)

	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, string, 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()
	contentLen := resp.ContentLength

	if resp.StatusCode != http.StatusOK {
		return nil, "", -1, fmt.Errorf("head request failed: %s: %w", releaseURL, err)
	}

	verURL := *downloadURL
	verURL.Path = path.Join(path.Dir(downloadURL.Path), "version")

	resp, err = http.Get(verURL.String())
	if err != nil {
		return nil, "", -1, fmt.Errorf("get request failed: %s: %w", verURL.String(), err)
	}

	bytes, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 1024})
	if err != nil {
		return nil, "", -1, fmt.Errorf("failed reading: %s: %w", verURL.String(), err)
	}
	_ = resp.Body.Close()

	return downloadURL, strings.TrimSpace(string(bytes)), contentLen, nil
}