summaryrefslogtreecommitdiff
path: root/pkg/machine/fedora.go
blob: b26921b524ee4e1af2d08cf7036719627392f366 (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
116
117
118
119
120
121
122
123
//go:build amd64 || arm64
// +build amd64 arm64

package machine

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"regexp"

	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
)

const (
	githubURL = "http://github.com/fedora-cloud/docker-brew-fedora/"
)

type FedoraDownload struct {
	Download
}

func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
	imageName, downloadURL, size, err := getFedoraDownload(releaseStream)
	if err != nil {
		return nil, err
	}

	dataDir, err := GetDataDir(vmType)
	if err != nil {
		return nil, err
	}

	f := FedoraDownload{
		Download: Download{
			Arch:      getFcosArch(),
			Artifact:  artifact,
			Format:    Format,
			ImageName: imageName,
			LocalPath: filepath.Join(dataDir, imageName),
			URL:       downloadURL,
			VMName:    vmName,
			Size:      size,
		},
	}
	f.Download.LocalUncompressedFile = f.getLocalUncompressedName()
	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 {
		return false, nil
	}
	return info.Size() == f.Size, nil
}

func truncRead(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}

	defer func() {
		if err := resp.Body.Close(); err != nil {
			logrus.Error(err)
		}
	}()

	body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 10*1024*1024))
	if err != nil {
		return nil, err
	}

	_, _ = io.Copy(io.Discard, resp.Body)

	return body, nil
}

func getFedoraDownload(releaseStream string) (string, *url.URL, int64, error) {
	dirURL := githubURL + "tree/" + releaseStream + "/" + getFcosArch() + "/"
	body, err := truncRead(dirURL)
	if err != nil {
		return "", nil, -1, err
	}

	rx, err := regexp.Compile(`fedora[^\"]+xz`)
	if err != nil {
		return "", nil, -1, err
	}
	file := rx.FindString(string(body))
	if len(file) <= 0 {
		return "", nil, -1, fmt.Errorf("could not locate Fedora download at %s", dirURL)
	}

	rawURL := githubURL + "raw/" + releaseStream + "/" + getFcosArch() + "/"
	newLocation := rawURL + file
	downloadURL, err := url.Parse(newLocation)
	if err != nil {
		return "", nil, -1, errors.Wrapf(err, "invalid URL generated from discovered Fedora file: %s", newLocation)
	}

	resp, err := http.Head(newLocation)
	if err != nil {
		return "", nil, -1, errors.Wrapf(err, "head request failed: %s", newLocation)
	}
	_ = resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return "", nil, -1, fmt.Errorf("head request failed [%d] on download: %s", resp.StatusCode, newLocation)
	}

	return file, downloadURL, resp.ContentLength, nil
}