forked from synadia-io/nex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprereq.go
407 lines (355 loc) · 9.52 KB
/
prereq.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package nexnode
import (
"archive/tar"
"bufio"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
_ "embed"
)
const (
VM_LINUX_KERNEL_URL string = "https://s3.amazonaws.com/spec.ccfc.min/firecracker-ci/v1.5/x86_64/vmlinux-5.10.186"
VM_LINUX_KERNEL_SHA256 string = "d48d320e320a8cf970184e79e66a833b044a049a4c2c645b9a1abefdb2fe7b31"
CNI_PLUGINS_TARBALL_URL string = "https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz"
CNI_PLUGINS_TARBALL_SHA256 string = "https://github.com/containernetworking/plugins/releases/download/v1.3.0/cni-plugins-linux-amd64-v1.3.0.tgz.sha256"
// TODO: once awslabs fixes their release action, this URL needs to be changed
TC_REDIRECT_CNI_PLUGIN_URL string = "https://github.com/jordan-rash/tc-redirect-tap/releases/download/v0.0.1/tc-redirect-tap-amd64"
TC_REDIRECT_CNI_PLUGIN_SHA256 string = "https://github.com/jordan-rash/tc-redirect-tap/releases/download/v0.0.1/tc-redirect-tap-amd64.sha256"
FIRECRACKER_TARBALL_URL string = "https://github.com/firecracker-microvm/firecracker/releases/download/v1.5.0/firecracker-v1.5.0-x86_64.tgz"
FIRECRACKER_TARBALL_SHA256 string = "https://github.com/firecracker-microvm/firecracker/releases/download/v1.5.0/firecracker-v1.5.0-x86_64.tgz.sha256.txt"
ROOTFS_GZIP_URL string = "https://synadia-nex.s3.us-east-2.amazonaws.com/rootfs.ext4.gz"
ROOTFS_GZIP_SHA256 string = "https://synadia-nex.s3.us-east-2.amazonaws.com/rootfs.ext4.gz.sha256"
)
var (
cyan = color.New(color.FgCyan).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
magenta = color.New(color.FgMagenta).SprintFunc()
green = color.New(color.FgHiGreen).SprintFunc()
)
type initFunc func(*requirement, *NodeConfiguration) error
type requirement struct {
directory string
files []fileSpec
descriptor string
satisfied bool
initFuncs []initFunc
}
type requirements []*requirement
type fileSpec struct {
name string
description string
satisfied bool
}
func CheckPreRequisites(config *NodeConfiguration) error {
required := &requirements{
{
directory: "/opt/cni/bin",
files: []fileSpec{
{name: "host-local", description: "host-local CNI plugin"},
{name: "ptp", description: "ptp CNI plugin"},
{name: "tc-redirect-tap", description: "tc-redirect-tap CNI plugin"},
},
descriptor: "Required CNI Plugins",
satisfied: false,
initFuncs: []initFunc{downloadCNIPlugins, downloadTCRedirectTap},
},
{
directory: "/usr/local/bin",
files: []fileSpec{
{name: "firecracker", description: "Firecracker VM binary"},
},
descriptor: "Required binaries",
satisfied: false,
initFuncs: []initFunc{downloadFirecracker},
},
{
//cniConfig := fmt.Sprintf("/etc/cni/conf.d/%s.conflist", config.CNI.NetworkName)
directory: "/etc/cni/conf.d",
files: []fileSpec{
{name: *config.CNI.NetworkName + ".conflist", description: "CNI Configuration"},
},
descriptor: "CNI configuration requirements",
satisfied: false,
initFuncs: []initFunc{writeCniConf},
},
{
directory: "",
files: []fileSpec{
{name: config.KernelFile, description: "VMLinux Kernel"},
{name: config.RootFsFile, description: "Root Filesystem Template"},
},
descriptor: "User provided files",
satisfied: false,
initFuncs: []initFunc{downloadKernel, downloadRootFS},
},
}
// Verify all directories are present
for _, r := range *required {
fmt.Printf("Validating - %s [%s]\n", magenta(r.descriptor), cyan(r.directory))
allDepsSatified := true
for _, f := range r.files {
path := func() string {
if r.directory == "" {
return f.name
} else {
return filepath.Join(r.directory, f.name)
}
}()
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
allDepsSatified = false
fmt.Printf("\t⛔ Missing Dependency - %s [%s]\n", red(filepath.Join(r.directory, f.name)), cyan(f.description))
} else {
f.satisfied = true
fmt.Printf("\t✅ Dependency Satisfied - %s [%s]\n", green(filepath.Join(r.directory, f.name)), cyan(f.description))
}
}
fmt.Println()
r.satisfied = allDepsSatified
}
for _, r := range *required {
if r.satisfied {
continue
}
var input []byte
var err error
if !config.ForceDepInstall {
fmt.Printf("⛔ You are missing required dependencies for [%s], do you want to install? [y/N] ", red(r.descriptor))
inputReader := bufio.NewReader(os.Stdin)
input, err = inputReader.ReadSlice('\n')
if err != nil {
return err
}
}
if config.ForceDepInstall || strings.ToUpper(string(input)) == "Y\n" {
var path string
for _, f := range r.files {
if r.directory == "" {
path = filepath.Dir(f.name)
} else {
path = r.directory
}
err = os.MkdirAll(path, 0755)
if err != nil {
return err
}
}
for _, iF := range r.initFuncs {
err := iF(r, config)
if err != nil {
return err
}
}
}
}
return nil
}
// func writeCniConf(fileName string, networkName string) error {
func writeCniConf(r *requirement, c *NodeConfiguration) error {
for _, tF := range r.files {
f, err := os.Create(filepath.Join(r.directory, tF.name))
if err != nil {
return err
}
defer f.Close()
var fcConfig map[string]interface{}
err = json.Unmarshal(defaultFcNetConf, &fcConfig)
if err != nil {
return err
}
fcConfig["name"] = c.CNI.NetworkName
raw, err := json.Marshal(fcConfig)
if err != nil {
return err
}
_, err = f.Write(raw)
if err != nil {
return nil
}
}
return nil
}
func downloadKernel(r *requirement, _ *NodeConfiguration) error {
for _, f := range r.files {
// TODO: this is a hack for now
if f.description != "VMLinux Kernel" {
continue
}
respBin, err := http.Get(VM_LINUX_KERNEL_URL)
if err != nil {
return err
}
defer respBin.Body.Close()
// TODO: add sha check
outFile, err := os.Create(f.name)
if err != nil {
fmt.Println(err)
return err
}
_, err = io.Copy(outFile, respBin.Body)
if err != nil {
return err
}
outFile.Close()
}
return nil
}
func downloadFirecracker(_ *requirement, _ *NodeConfiguration) error {
// TODO: firecracker repo made the sha difficult to use
rawData, err := decompressTarFromURL(FIRECRACKER_TARBALL_URL, "")
if err != nil {
return err
}
for {
header, err := rawData.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if header.Name == "release-v1.5.0-x86_64/firecracker-v1.5.0-x86_64" {
outFile, err := os.Create("/usr/local/bin/firecracker")
if err != nil {
fmt.Println(err)
return err
}
_, err = io.Copy(outFile, rawData)
if err != nil {
fmt.Println(err)
return err
}
outFile.Close()
err = os.Chmod(outFile.Name(), 0755)
if err != nil {
fmt.Println(err)
return err
}
}
}
return nil
}
func downloadCNIPlugins(r *requirement, c *NodeConfiguration) error {
rawData, err := decompressTarFromURL(CNI_PLUGINS_TARBALL_URL, CNI_PLUGINS_TARBALL_SHA256)
if err != nil {
return err
}
for {
header, err := rawData.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
f := strings.TrimPrefix(strings.TrimSpace(header.Name), "./")
if f == "ptp" || f == "host-local" {
outFile, err := os.Create(filepath.Join(r.directory, f))
if err != nil {
fmt.Println(err)
return err
}
_, err = io.Copy(outFile, rawData)
if err != nil {
return err
}
outFile.Close()
err = os.Chmod(outFile.Name(), 0755)
if err != nil {
return err
}
}
}
return nil
}
func downloadTCRedirectTap(r *requirement, _ *NodeConfiguration) error {
respBin, err := http.Get(TC_REDIRECT_CNI_PLUGIN_URL)
if err != nil {
return err
}
defer respBin.Body.Close()
// TODO: add sha check
outFile, err := os.Create(filepath.Join(r.directory, "tc-redirect-tap"))
if err != nil {
fmt.Println(err)
return err
}
_, err = io.Copy(outFile, respBin.Body)
if err != nil {
return err
}
outFile.Close()
err = os.Chmod(outFile.Name(), 0755)
if err != nil {
return err
}
return nil
}
func downloadRootFS(r *requirement, _ *NodeConfiguration) error {
for _, f := range r.files {
// TODO: this is a hack for now
if f.description != "Root Filesystem Template" {
continue
}
respTar, err := http.Get(ROOTFS_GZIP_URL)
if err != nil {
return err
}
defer respTar.Body.Close()
uncompressedFile, err := gzip.NewReader(respTar.Body)
if err != nil {
return err
}
outFile, err := os.Create(f.name)
if err != nil {
fmt.Println(err)
return err
}
_, err = io.Copy(outFile, uncompressedFile)
if err != nil {
return err
}
outFile.Close()
}
return nil
}
func decompressTarFromURL(url string, urlSha string) (*tar.Reader, error) {
respTar, err := http.Get(url)
if err != nil {
return nil, err
}
// defer respTar.Body.Close()
// if urlSha != "" {
// respSha, err := http.Get(urlSha)
// if err != nil {
// return nil, err
// }
// defer respSha.Body.Close()
// sha256_b, err := io.ReadAll(respSha.Body)
// if err != nil {
// return nil, err
// }
//
// // sha256_g := sha256.Sum256(tar_b)
//
// if string(sha256_b) != string(sha256_g[:]) {
// fmt.Println(string(sha256_b))
// fmt.Println(string(sha256_g[:]))
// return nil, errors.New("downloaded tar did not match provided SHA256")
// }
// }
uncompressedTar, err := gzip.NewReader(respTar.Body)
if err != nil {
return nil, err
}
rawData := tar.NewReader(uncompressedTar)
return rawData, nil
}
//go:embed templates/fcnet.conflist
var defaultFcNetConf []byte