-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathsimplestreams_images.go
302 lines (248 loc) · 8.74 KB
/
simplestreams_images.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
package lxd
import (
"context"
"crypto/sha256"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"time"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
)
// Image handling functions
// GetImages returns a list of available images as Image structs.
func (r *ProtocolSimpleStreams) GetImages() ([]api.Image, error) {
return r.ssClient.ListImages()
}
// GetImageFingerprints returns a list of available image fingerprints.
func (r *ProtocolSimpleStreams) GetImageFingerprints() ([]string, error) {
// Get all the images from simplestreams
images, err := r.ssClient.ListImages()
if err != nil {
return nil, err
}
// And now extract just the fingerprints
fingerprints := []string{}
for _, img := range images {
fingerprints = append(fingerprints, img.Fingerprint)
}
return fingerprints, nil
}
// GetImagesWithFilter returns a filtered list of available images as Image structs.
func (r *ProtocolSimpleStreams) GetImagesWithFilter(filters []string) ([]api.Image, error) {
return nil, fmt.Errorf("GetImagesWithFilter is not supported by the simplestreams protocol")
}
// GetImage returns an Image struct for the provided fingerprint.
func (r *ProtocolSimpleStreams) GetImage(fingerprint string) (*api.Image, string, error) {
image, err := r.ssClient.GetImage(fingerprint)
if err != nil {
return nil, "", fmt.Errorf("Failed getting image: %w", err)
}
return image, "", err
}
// GetImageFile downloads an image from the server, returning an ImageFileResponse struct.
func (r *ProtocolSimpleStreams) GetImageFile(fingerprint string, req ImageFileRequest) (*ImageFileResponse, error) {
// Quick checks.
if req.MetaFile == nil && req.RootfsFile == nil {
return nil, fmt.Errorf("No file requested")
}
// Attempt to download from host
if shared.PathExists("/dev/lxd/sock") && os.Geteuid() == 0 {
unixURI := fmt.Sprintf("http://unix.socket/1.0/images/%s/export", url.PathEscape(fingerprint))
// Setup the HTTP client
devlxdHTTP, err := unixHTTPClient(nil, "/dev/lxd/sock", nil)
if err == nil {
resp, err := lxdDownloadImage(fingerprint, unixURI, r.httpUserAgent, devlxdHTTP.Do, req)
if err == nil {
return resp, nil
}
}
}
// Use relatively short response header timeout so as not to hold the image lock open too long.
// Deference client and transport in order to clone them so as to not modify timeout of base client.
httpClient := *r.http
httpTransport := httpClient.Transport.(*http.Transport).Clone()
httpTransport.ResponseHeaderTimeout = 30 * time.Second
httpClient.Transport = httpTransport
// Get the file list
files, err := r.ssClient.GetFiles(fingerprint)
if err != nil {
return nil, err
}
// Prepare the response
resp := ImageFileResponse{}
// Download function
download := func(path string, filename string, hash string, target io.WriteSeeker) (int64, error) {
// Try over http
url, err := shared.JoinUrls(fmt.Sprintf("http://%s", strings.TrimPrefix(r.httpHost, "https://")), path)
if err != nil {
return -1, err
}
size, err := shared.DownloadFileHash(context.TODO(), &httpClient, r.httpUserAgent, req.ProgressHandler, req.Canceler, filename, url, hash, sha256.New(), target)
if err != nil {
// Handle cancelation
if err.Error() == "net/http: request canceled" {
return -1, err
}
// Try over https
url, err := shared.JoinUrls(r.httpHost, path)
if err != nil {
return -1, err
}
size, err = shared.DownloadFileHash(context.TODO(), &httpClient, r.httpUserAgent, req.ProgressHandler, req.Canceler, filename, url, hash, sha256.New(), target)
if err != nil {
return -1, err
}
}
return size, nil
}
// Download the LXD image file
meta, ok := files["meta"]
if ok && req.MetaFile != nil {
size, err := download(meta.Path, "metadata", meta.Sha256, req.MetaFile)
if err != nil {
return nil, err
}
parts := strings.Split(meta.Path, "/")
resp.MetaName = parts[len(parts)-1]
resp.MetaSize = size
}
// Download the rootfs
rootfs, ok := files["root"]
if ok && req.RootfsFile != nil {
// Look for deltas (requires xdelta3)
downloaded := false
_, err := exec.LookPath("xdelta3")
if err == nil && req.DeltaSourceRetriever != nil {
for filename, file := range files {
_, srcFingerprint, prefixFound := strings.Cut(filename, "root.delta-")
if !prefixFound {
continue
}
// Check if we have the source file for the delta
srcPath := req.DeltaSourceRetriever(srcFingerprint, "rootfs")
if srcPath == "" {
continue
}
// Create temporary file for the delta
deltaFile, err := os.CreateTemp("", "lxc_image_")
if err != nil {
return nil, err
}
defer func() { _ = deltaFile.Close() }()
defer func() { _ = os.Remove(deltaFile.Name()) }()
// Download the delta
_, err = download(file.Path, "rootfs delta", file.Sha256, deltaFile)
if err != nil {
return nil, err
}
// Create temporary file for the delta
patchedFile, err := os.CreateTemp("", "lxc_image_")
if err != nil {
return nil, err
}
defer func() { _ = patchedFile.Close() }()
defer func() { _ = os.Remove(patchedFile.Name()) }()
// Apply it
_, err = shared.RunCommand("xdelta3", "-f", "-d", "-s", srcPath, deltaFile.Name(), patchedFile.Name())
if err != nil {
return nil, err
}
// Copy to the target
size, err := io.Copy(req.RootfsFile, patchedFile)
if err != nil {
return nil, err
}
parts := strings.Split(rootfs.Path, "/")
resp.RootfsName = parts[len(parts)-1]
resp.RootfsSize = size
downloaded = true
}
}
// Download the whole file
if !downloaded {
size, err := download(rootfs.Path, "rootfs", rootfs.Sha256, req.RootfsFile)
if err != nil {
return nil, err
}
parts := strings.Split(rootfs.Path, "/")
resp.RootfsName = parts[len(parts)-1]
resp.RootfsSize = size
}
}
return &resp, nil
}
// GetImageSecret isn't relevant for the simplestreams protocol.
func (r *ProtocolSimpleStreams) GetImageSecret(fingerprint string) (string, error) {
return "", fmt.Errorf("Private images aren't supported by the simplestreams protocol")
}
// GetPrivateImage isn't relevant for the simplestreams protocol.
func (r *ProtocolSimpleStreams) GetPrivateImage(fingerprint string, secret string) (*api.Image, string, error) {
return nil, "", fmt.Errorf("Private images aren't supported by the simplestreams protocol")
}
// GetPrivateImageFile isn't relevant for the simplestreams protocol.
func (r *ProtocolSimpleStreams) GetPrivateImageFile(fingerprint string, secret string, req ImageFileRequest) (*ImageFileResponse, error) {
return nil, fmt.Errorf("Private images aren't supported by the simplestreams protocol")
}
// GetImageAliases returns the list of available aliases as ImageAliasesEntry structs.
func (r *ProtocolSimpleStreams) GetImageAliases() ([]api.ImageAliasesEntry, error) {
return r.ssClient.ListAliases()
}
// GetImageAliasNames returns the list of available alias names.
func (r *ProtocolSimpleStreams) GetImageAliasNames() ([]string, error) {
// Get all the images from simplestreams
aliases, err := r.ssClient.ListAliases()
if err != nil {
return nil, err
}
// And now extract just the names
names := []string{}
for _, alias := range aliases {
names = append(names, alias.Name)
}
return names, nil
}
// GetImageAlias returns an existing alias as an ImageAliasesEntry struct.
func (r *ProtocolSimpleStreams) GetImageAlias(name string) (*api.ImageAliasesEntry, string, error) {
alias, err := r.ssClient.GetAlias("container", name)
if err != nil {
alias, err = r.ssClient.GetAlias("virtual-machine", name)
if err != nil {
return nil, "", err
}
}
return alias, "", err
}
// GetImageAliasType returns an existing alias as an ImageAliasesEntry struct.
func (r *ProtocolSimpleStreams) GetImageAliasType(imageType string, name string) (*api.ImageAliasesEntry, string, error) {
if imageType == "" {
return r.GetImageAlias(name)
}
alias, err := r.ssClient.GetAlias(imageType, name)
if err != nil {
return nil, "", err
}
return alias, "", err
}
// GetImageAliasArchitectures returns a map of architectures / targets.
func (r *ProtocolSimpleStreams) GetImageAliasArchitectures(imageType string, name string) (map[string]*api.ImageAliasesEntry, error) {
if imageType == "" {
aliases, err := r.ssClient.GetAliasArchitectures("container", name)
if err != nil {
aliases, err = r.ssClient.GetAliasArchitectures("virtual-machine", name)
if err != nil {
return nil, err
}
}
return aliases, nil
}
return r.ssClient.GetAliasArchitectures(imageType, name)
}
// ExportImage exports (copies) an image to a remote server.
func (r *ProtocolSimpleStreams) ExportImage(fingerprint string, image api.ImageExportPost) (Operation, error) {
return nil, fmt.Errorf("Exporting images is not supported by the simplestreams protocol")
}