-
Notifications
You must be signed in to change notification settings - Fork 161
/
vyos-http.go
118 lines (91 loc) · 2.8 KB
/
vyos-http.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
package sources
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/google/go-github/v56/github"
"golang.org/x/sys/unix"
"github.com/lxc/distrobuilder/shared"
)
type vyos struct {
common
fname string
fpath string
}
func (s *vyos) Run() error {
err := s.downloadImage(s.definition)
if err != nil {
return fmt.Errorf("Failed to download image: %w", err)
}
return s.unpackISO(filepath.Join(s.fpath, s.fname), s.rootfsDir)
}
func (s *vyos) downloadImage(definition shared.Definition) error {
var err error
ctx := context.Background()
client := github.NewClient(nil)
owner := "vyos"
repo := "vyos-rolling-nightly-builds"
latestRelease, _, err := client.Repositories.GetLatestRelease(ctx, owner, repo)
if err != nil {
return fmt.Errorf("Failed to get latest release, %w", err)
}
isoURL := ""
assets := latestRelease.Assets
for _, a := range assets {
ext := filepath.Ext(a.GetName())
if ext == ".iso" {
isoURL = a.GetBrowserDownloadURL()
s.fname = a.GetName()
}
}
if isoURL == "" {
return fmt.Errorf("Failed to get latest release URL.")
}
s.fpath, err = s.DownloadHash(s.definition.Image, isoURL, "", nil)
return err
}
func (s *vyos) unpackISO(filePath string, rootfsDir string) error {
isoDir, err := os.MkdirTemp(s.cacheDir, "temp_")
if err != nil {
return fmt.Errorf("Failed creating temporary directory: %w", err)
}
defer os.RemoveAll(isoDir)
squashfsDir, err := os.MkdirTemp(s.cacheDir, "temp_")
if err != nil {
return fmt.Errorf("Failed creating temporary directory: %w", err)
}
defer os.RemoveAll(squashfsDir)
// this is easier than doing the whole loop thing ourselves
err = shared.RunCommand(s.ctx, nil, nil, "mount", "-t", "iso9660", "-o", "ro", filePath, isoDir)
if err != nil {
return fmt.Errorf("Failed mounting %q: %w", filePath, err)
}
defer func() {
_ = unix.Unmount(isoDir, 0)
}()
squashfsImage := filepath.Join(isoDir, "live", "filesystem.squashfs")
// The squashfs.img contains an image containing the rootfs, so first
// mount squashfs.img
err = shared.RunCommand(s.ctx, nil, nil, "mount", "-t", "squashfs", "-o", "ro", squashfsImage, squashfsDir)
if err != nil {
return fmt.Errorf("Failed mounting %q: %w", squashfsImage, err)
}
defer func() {
_ = unix.Unmount(squashfsDir, 0)
}()
// Remove rootfsDir otherwise rsync will copy the content into the directory
// itself
err = os.RemoveAll(rootfsDir)
if err != nil {
return fmt.Errorf("Failed removing directory %q: %w", rootfsDir, err)
}
s.logger.WithField("file", squashfsImage).Info("Unpacking root image")
// Since rootfs is read-only, we need to copy it to a temporary rootfs
// directory in order to create the minimal rootfs.
err = shared.RsyncLocal(s.ctx, squashfsDir+"/", rootfsDir)
if err != nil {
return fmt.Errorf("Failed running rsync: %w", err)
}
return nil
}