summaryrefslogtreecommitdiff
path: root/pkg/machine/fcos.go
blob: 8bbad458f700bebb07835d9cd57418e28e4a2b10 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package machine

import (
	"crypto/sha256"
	"io"
	"io/ioutil"
	url2 "net/url"
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/containers/storage/pkg/archive"

	"github.com/sirupsen/logrus"

	digest "github.com/opencontainers/go-digest"
)

// These should eventually be moved into machine/qemu as
// they are specific to running qemu
var (
	artifact string = "qemu"
	Format   string = "qcow2.xz"
)

type FcosDownload struct {
	Download
}

func NewFcosDownloader(vmType, vmName string) (DistributionDownload, error) {
	info, err := getFCOSDownload()
	if err != nil {
		return nil, err
	}
	urlSplit := strings.Split(info.Location, "/")
	imageName := urlSplit[len(urlSplit)-1]
	url, err := url2.Parse(info.Location)
	if err != nil {
		return nil, err
	}

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

	fcd := FcosDownload{
		Download: Download{
			Arch:      getFcosArch(),
			Artifact:  artifact,
			Format:    Format,
			ImageName: imageName,
			LocalPath: filepath.Join(dataDir, imageName),
			Sha256sum: info.Sha256Sum,
			URL:       url,
			VMName:    vmName,
		},
	}
	fcd.Download.LocalUncompressedFile = fcd.getLocalUncompressedName()
	return fcd, nil
}

func (f FcosDownload) getLocalUncompressedName() string {
	uncompressedFilename := filepath.Join(filepath.Dir(f.LocalPath), f.VMName+"_"+f.ImageName)
	return strings.TrimSuffix(uncompressedFilename, ".xz")
}

func (f FcosDownload) DownloadImage() error {
	// check if the latest image is already present
	ok, err := UpdateAvailable(&f.Download)
	if err != nil {
		return err
	}
	if !ok {
		if err := DownloadVMImage(f.URL, f.LocalPath); err != nil {
			return err
		}
	}
	uncompressedFileWriter, err := os.OpenFile(f.getLocalUncompressedName(), os.O_CREATE|os.O_RDWR, 0600)
	if err != nil {
		return err
	}
	sourceFile, err := ioutil.ReadFile(f.LocalPath)
	if err != nil {
		return err
	}
	compressionType := archive.DetectCompression(sourceFile)
	f.CompressionType = compressionType.Extension()

	switch f.CompressionType {
	case "tar.xz":
		return decompressXZ(f.LocalPath, uncompressedFileWriter)
	default:
		// File seems to be uncompressed, make a copy
		if err := copyFile(f.LocalPath, uncompressedFileWriter); err != nil {
			return err
		}
	}
	return nil
}

func copyFile(src string, dest *os.File) error {
	source, err := os.Open(src)
	if err != nil {
		return err
	}
	defer func() {
		if err := source.Close(); err != nil {
			logrus.Error(err)
		}
	}()
	_, err = io.Copy(dest, source)
	return err
}

func (f FcosDownload) Get() *Download {
	return &f.Download
}

type fcosDownloadInfo struct {
	CompressionType string
	Location        string
	Release         string
	Sha256Sum       string
}

func UpdateAvailable(d *Download) (bool, error) {
	//	 check the sha of the local image if it exists
	//  get the sha of the remote image
	// == dont bother to pull
	files, err := ioutil.ReadDir(filepath.Dir(d.LocalPath))
	if err != nil {
		return false, err
	}
	for _, file := range files {
		if filepath.Base(d.LocalPath) == file.Name() {
			b, err := ioutil.ReadFile(d.LocalPath)
			if err != nil {
				return false, err
			}
			s := sha256.Sum256(b)
			sum := digest.NewDigestFromBytes(digest.SHA256, s[:])
			if sum.Encoded() == d.Sha256sum {
				return true, nil
			}
		}
	}
	return false, nil
}

func getFcosArch() string {
	var arch string
	// TODO fill in more architectures
	switch runtime.GOARCH {
	case "arm64":
		arch = "aarch64"
	default:
		arch = "x86_64"
	}
	return arch
}