-
Notifications
You must be signed in to change notification settings - Fork 0
/
drivervbox-imagemanagement.go
160 lines (127 loc) · 3.98 KB
/
drivervbox-imagemanagement.go
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
package drivervbox
import (
"encoding/json"
"errors"
"path"
"github.com/kuttiproject/drivercore"
"github.com/kuttiproject/kuttilog"
"github.com/kuttiproject/workspace"
)
// ImagesVersion defines the image repository version for the current version
// of the driver.
const ImagesVersion = "0.3"
const imagesConfigFile = "driver-vbox-images.json"
// ImagesSourceURL is the location where the master list of images can be found
var ImagesSourceURL = "https://github.com/kuttiproject/driver-vbox-images/releases/download/v" + ImagesVersion + "/" + imagesConfigFile
var (
imagedata = &imageconfigdata{}
imageconfigmanager, _ = workspace.NewFileConfigManager(imagesConfigFile, imagedata)
)
type imageconfigdata struct {
images map[string]*Image
}
func (icd *imageconfigdata) Serialize() ([]byte, error) {
return json.Marshal(icd.images)
}
func (icd *imageconfigdata) Deserialize(data []byte) error {
loaddata := make(map[string]*Image)
err := json.Unmarshal(data, &loaddata)
if err == nil {
icd.images = loaddata
}
return err
}
func (icd *imageconfigdata) SetDefaults() {
icd.images = defaultimages()
}
func vboxCacheDir() (string, error) {
return workspace.CacheSubDir("driver-vbox")
}
func vboxConfigDir() (string, error) {
//return workspace.Configsubdir("vbox")
return workspace.ConfigDir()
}
func defaultimages() map[string]*Image {
return map[string]*Image{}
}
func imagenamefromk8sversion(k8sversion string) string {
return "kutti-" + k8sversion + ".ova"
}
func imagepathfromk8sversion(k8sversion string) (string, error) {
cachedir, err := vboxCacheDir()
if err != nil {
return "", err
}
result := path.Join(cachedir, imagenamefromk8sversion(k8sversion))
return result, nil
}
func addfromfile(k8sversion string, filepath string, checksum string) error {
kuttilog.Println(kuttilog.Info, "Checking image validity...")
filechecksum, err := workspace.ChecksumFile(filepath)
if err != nil {
return err
}
if filechecksum != checksum {
kuttilog.Printf(kuttilog.Debug, "checksum for file %v failed.\nWanted: %v\nGot : %v\n", filepath, checksum, filechecksum)
return errors.New("file is not valid")
}
localfilepath, err := imagepathfromk8sversion(k8sversion)
if err != nil {
return err
}
kuttilog.Println(kuttilog.Info, "Copying image to local cache...")
// A 128 KiB buffer should help
const BUFSIZE = 131072
err = workspace.CopyFile(filepath, localfilepath, BUFSIZE, true)
if err != nil {
return err
}
return nil
}
func removefile(k8sversion string) error {
filename, err := imagepathfromk8sversion(k8sversion)
if err != nil {
return err
}
return workspace.RemoveFile(filename)
}
func fetchimagelist() error {
// Download image list into temp directory
confdir, _ := vboxConfigDir()
tempfilename := "vboximagesnewlist.json"
tempfilepath := path.Join(confdir, tempfilename)
kuttilog.Printf(kuttilog.Debug, "confdir: %v\ntempfilepath: %v\n", confdir, tempfilepath)
kuttilog.Println(kuttilog.Info, "Fetching image list...")
kuttilog.Printf(kuttilog.Debug, "Fetching from %v into %v.", ImagesSourceURL, tempfilepath)
err := workspace.DownloadFile(ImagesSourceURL, tempfilepath)
kuttilog.Printf(kuttilog.Debug, "Error: %v", err)
if err != nil {
return err
}
defer workspace.RemoveFile(tempfilepath)
// Load into object
tempimagedata := &imageconfigdata{}
tempconfigmanager, err := workspace.NewFileConfigManager(tempfilename, tempimagedata)
if err != nil {
return err
}
err = tempconfigmanager.Load()
if err != nil {
return err
}
// Compare against current and update
for key, newimage := range tempimagedata.images {
oldimage := imagedata.images[key]
if oldimage != nil &&
newimage.imageChecksum == oldimage.imageChecksum &&
newimage.imageSourceURL == oldimage.imageSourceURL &&
oldimage.imageStatus == drivercore.ImageStatusDownloaded {
newimage.imageStatus = drivercore.ImageStatusDownloaded
}
}
// Make it current
imagedata.images = tempimagedata.images
// Save as local configuration
imageconfigmanager.Save()
return nil
}