forked from lxc/distrobuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopensuse-http.go
240 lines (186 loc) · 5.42 KB
/
opensuse-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
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
package sources
import (
"crypto/sha256"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"gopkg.in/antchfx/htmlquery.v1"
"github.com/lxc/distrobuilder/shared"
)
type opensuse struct {
common
}
// Run downloads an OpenSUSE tarball.
func (s *opensuse) Run() error {
var baseURL string
var fname string
if s.definition.Source.URL == "" {
s.definition.Source.URL = "https://mirrorcache-us.opensuse.org/download"
}
tarballPath, err := s.getPathToTarball(s.definition.Source.URL, s.definition.Image.Release,
s.definition.Image.ArchitectureMapped)
if err != nil {
return fmt.Errorf("Failed to get tarball path: %w", err)
}
var resp *http.Response
err = shared.Retry(func() error {
resp, err = http.Head(tarballPath)
if err != nil {
return fmt.Errorf("Failed to HEAD %q: %w", tarballPath, err)
}
return nil
}, 3)
if err != nil {
return err
}
baseURL, fname = path.Split(resp.Request.URL.String())
url, err := url.Parse(fmt.Sprintf("%s%s", baseURL, fname))
if err != nil {
return fmt.Errorf("Failed to parse %q: %w", fmt.Sprintf("%s%s", baseURL, fname), err)
}
fpath, err := s.DownloadHash(s.definition.Image, url.String(), "", nil)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", url.String(), err)
}
_, err = s.DownloadHash(s.definition.Image, url.String()+".sha256", "", nil)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", url.String()+".sha256", err)
}
if !s.definition.Source.SkipVerification {
err = s.verifyTarball(filepath.Join(fpath, fname), s.definition)
if err != nil {
return fmt.Errorf("Failed to verify %q: %w", filepath.Join(fpath, fname), err)
}
}
s.logger.WithField("file", filepath.Join(fpath, fname)).Info("Unpacking image")
// Unpack
err = shared.Unpack(filepath.Join(fpath, fname), s.rootfsDir)
if err != nil {
return fmt.Errorf("Failed to unpack %q: %w", filepath.Join(fpath, fname), err)
}
return nil
}
func (s *opensuse) verifyTarball(imagePath string, definition shared.Definition) error {
var err error
var checksum []byte
checksumPath := imagePath + ".sha256"
valid, err := s.VerifyFile(checksumPath, "")
if err == nil && valid {
checksum, err = s.GetSignedContent(checksumPath)
} else {
checksum, err = os.ReadFile(checksumPath)
}
if err != nil {
return fmt.Errorf("Failed to read checksum file: %w", err)
}
image, err := os.Open(imagePath)
if err != nil {
return fmt.Errorf("Failed to open %q: %w", imagePath, err)
}
defer image.Close()
hash := sha256.New()
_, err = io.Copy(hash, image)
if err != nil {
return fmt.Errorf("Failed to copy tarball content: %w", err)
}
result := fmt.Sprintf("%x", hash.Sum(nil))
checksumStr := strings.TrimSpace(strings.Split(string(checksum), " ")[0])
if result != checksumStr {
return fmt.Errorf("Hash mismatch for %s: %s != %s", imagePath, result, checksumStr)
}
return nil
}
func (s *opensuse) getPathToTarball(baseURL string, release string, arch string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", fmt.Errorf("Failed to parse URL %q: %w", baseURL, err)
}
var tarballName string
if strings.ToLower(release) == "tumbleweed" {
u.Path = path.Join(u.Path, "repositories", "Virtualization:", "containers:", "images:", "openSUSE-Tumbleweed")
switch arch {
case "i686", "x86_64":
u.Path = path.Join(u.Path, "container")
case "aarch64":
u.Path = path.Join(u.Path, "container_ARM")
case "ppc64le":
u.Path = path.Join(u.Path, "container_PowerPC")
case "s390x":
u.Path = path.Join(u.Path, "container_zSystems")
default:
return "", fmt.Errorf("Unsupported architecture %q", arch)
}
release = "tumbleweed"
} else {
u.Path = path.Join(u.Path, "distribution", "leap", release, "appliances")
release = "leap"
}
tarballName, err = s.getTarballName(u, release, arch)
if err != nil {
return "", fmt.Errorf("Failed to get tarball name: %w", err)
}
u.Path = path.Join(u.Path, tarballName)
return u.String(), nil
}
func (s *opensuse) getTarballName(u *url.URL, release, arch string) (string, error) {
doc, err := htmlquery.LoadURL(u.String())
if err != nil {
return "", fmt.Errorf("Failed to load URL %q: %w", u.String(), err)
}
if doc == nil {
return "", errors.New("Empty HTML document")
}
// Translate x86 architectures.
if strings.HasSuffix(arch, "86") {
arch = "ix86"
}
nodes := htmlquery.Find(doc, `//a/@href`)
re := regexp.MustCompile(fmt.Sprintf("^opensuse-%s-image.*%s.*\\.tar.xz$", release, arch))
var builds []string
for _, n := range nodes {
text := strings.TrimPrefix(htmlquery.InnerText(n), "./")
if !re.MatchString(text) {
continue
}
if strings.Contains(text, "Build") {
builds = append(builds, text)
} else {
if !s.validateURL(*u, text) {
continue
}
return text, nil
}
}
if len(builds) > 0 {
// Unfortunately, the link to the latest build is missing, hence we need
// to manually select the latest build.
sort.Strings(builds)
for i := len(builds) - 1; i >= 0; i-- {
if !s.validateURL(*u, builds[i]) {
continue
}
return builds[i], nil
}
}
return "", errors.New("Failed to find tarball name")
}
func (s *opensuse) validateURL(u url.URL, tarball string) bool {
u.Path = path.Join(u.Path, tarball)
resp, err := http.Head(u.String())
if err != nil {
return false
}
// Check whether the link to the tarball is valid.
if resp.StatusCode == http.StatusNotFound {
return false
}
return true
}