| 1 | package e2eutils |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "net/http" |
| 12 | "runtime" |
| 13 | |
| 14 | "github.com/opencontainers/go-digest" |
| 15 | |
| 16 | "github.com/coreos/stream-metadata-go/fedoracoreos" |
| 17 | "github.com/coreos/stream-metadata-go/stream" |
| 18 | "github.com/sirupsen/logrus" |
| 19 | ) |
| 20 | |
| 21 | type FcosDownload struct { |
| 22 | DataDir string |
| 23 | } |
| 24 | |
| 25 | type fcosDownloadInfo struct { |
| 26 | Location string |
| 27 | Sha256Sum string |
| 28 | } |
| 29 | |
| 30 | type ArtifactFormat struct { |
| 31 | Artifact string |
| 32 | Format string |
| 33 | } |
| 34 | |
| 35 | func NewFcosDownloader(dataDir string) (*FcosDownload, error) { |
| 36 | return &FcosDownload{ |
| 37 | DataDir: dataDir, |
| 38 | }, nil |
| 39 | } |
| 40 | |
| 41 | func imageName(info *fcosDownloadInfo) string { |
| 42 | urlSplit := strings.Split(info.Location, "/") |
| 43 | return urlSplit[len(urlSplit)-1] |
| 44 | } |
| 45 | |
| 46 | func (downloader *FcosDownload) DownloadImage(artifactType string, formatType string) (string, error) { |
| 47 | info, err := getFCOSDownload(artifactType, formatType) |
| 48 | if err != nil { |
| 49 | return "", err |
| 50 | } |
| 51 | |
| 52 | compressedImage := filepath.Join(downloader.DataDir, imageName(info)) |
| 53 | |
| 54 | |
| 55 | ok, err := downloader.updateAvailable(info, compressedImage) |
| 56 | if err != nil { |
| 57 | return "", err |
| 58 | } |
| 59 | if !ok { |
| 60 | if err := DownloadVMImage(info.Location, compressedImage); err != nil { |
| 61 | return "", err |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | uncompressedImage := "" |
| 66 | if uncompressedImage, err = Decompress(compressedImage); err != nil { |
| 67 | return "", err |
| 68 | } |
| 69 | return uncompressedImage, nil |
| 70 | } |
| 71 | |
| 72 | func (downloader *FcosDownload) updateAvailable(info *fcosDownloadInfo, compressedImage string) (bool, error) { |
| 73 | |
| 74 | |
| 75 | |
| 76 | if _, err := os.Stat(compressedImage); os.IsNotExist(err) { |
| 77 | return false, nil |
| 78 | } |
| 79 | fd, err := os.Open(compressedImage) |
| 80 | if err != nil { |
| 81 | return false, err |
| 82 | } |
| 83 | defer fd.Close() |
| 84 | sum, err := digest.SHA256.FromReader(fd) |
| 85 | if err != nil { |
| 86 | return false, err |
| 87 | } |
| 88 | if sum.Encoded() == info.Sha256Sum { |
| 89 | return true, nil |
| 90 | } |
| 91 | return false, nil |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | func CoreosArch() string { |
| 97 | switch runtime.GOARCH { |
| 98 | case "amd64": |
| 99 | return "x86_64" |
| 100 | case "arm64": |
| 101 | return "aarch64" |
| 102 | case "ppc64le": |
| 103 | return "ppc64le" |
| 104 | case "s390x": |
| 105 | return "s390x" |
| 106 | } |
| 107 | panic(fmt.Sprintf("unknown arch: %s", runtime.GOOS)) |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | func getFCOSDownload(artifactType string, formatType string) (*fcosDownloadInfo, error) { |
| 113 | streamurl := fedoracoreos.GetStreamURL(fedoracoreos.StreamNext) |
| 114 | resp, err := http.Get(streamurl.String()) |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | body, err := io.ReadAll(resp.Body) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | defer func() { |
| 123 | if err := resp.Body.Close(); err != nil { |
| 124 | logrus.Error(err) |
| 125 | } |
| 126 | }() |
| 127 | |
| 128 | var fcosstable stream.Stream |
| 129 | if err := json.Unmarshal(body, &fcosstable); err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | arch, ok := fcosstable.Architectures[CoreosArch()] |
| 133 | if !ok { |
| 134 | return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream") |
| 135 | } |
| 136 | artifacts := arch.Artifacts |
| 137 | if artifacts == nil { |
| 138 | return nil, fmt.Errorf("unable to pull VM image: no artifact in stream") |
| 139 | } |
| 140 | artifact, ok := artifacts[artifactType] |
| 141 | if !ok { |
| 142 | return nil, fmt.Errorf("unable to pull VM image: no qemu artifact in stream") |
| 143 | } |
| 144 | formats := artifact.Formats |
| 145 | if formats == nil { |
| 146 | return nil, fmt.Errorf("unable to pull VM image: no formats in stream") |
| 147 | } |
| 148 | format, ok := formats[formatType] |
| 149 | if !ok { |
| 150 | return nil, fmt.Errorf("unable to pull VM image: no qcow2.xz format in stream") |
| 151 | } |
| 152 | disk := format.Disk |
| 153 | if disk == nil { |
| 154 | return nil, fmt.Errorf("unable to pull VM image: no disk in stream") |
| 155 | } |
| 156 | return &fcosDownloadInfo{ |
| 157 | Location: disk.Location, |
| 158 | Sha256Sum: disk.Sha256, |
| 159 | }, nil |
| 160 | } |