-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.go
More file actions
315 lines (270 loc) · 8.6 KB
/
Copy pathclient.go
File metadata and controls
315 lines (270 loc) · 8.6 KB
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
package prometheus
/* License: GPLv3
Authors:
Mirko Brombin <mirko@fabricators.ltd>
Vanilla OS Contributors <https://github.com/vanilla-os/>
Copyright: 2023
Description:
Prometheus is a simple and accessible library for pulling and mounting
container images. It is designed to be used as a dependency in ABRoot
and Albius.
*/
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/containers/buildah/define"
"github.com/containers/buildah/imagebuildah"
"go.podman.io/image/v5/copy"
"go.podman.io/image/v5/manifest"
"go.podman.io/image/v5/signature"
"go.podman.io/image/v5/storage"
"go.podman.io/image/v5/transports/alltransports"
"go.podman.io/image/v5/types"
cstorage "go.podman.io/storage"
digest "github.com/opencontainers/go-digest"
)
/* NewPrometheus creates a new Prometheus instance, note that currently
* Prometheus only works with custom stores, so you need to pass the
* root graphDriverName to create a new one.
*/
func NewPrometheus(root, graphDriverName string, maxParallelDownloads uint) (*Prometheus, error) {
root = filepath.Clean(root)
if _, err := os.Stat(root); os.IsNotExist(err) {
err = os.MkdirAll(root, 0755)
if err != nil {
return nil, err
}
}
runRoot := filepath.Join(root, "run")
graphRoot := filepath.Join(root, "graph")
store, err := cstorage.GetStore(cstorage.StoreOptions{
RunRoot: runRoot,
GraphRoot: graphRoot,
GraphDriverName: graphDriverName,
})
if err != nil {
return nil, err
}
return &Prometheus{
Store: store,
Config: PrometheusConfig{
Root: root,
GraphDriverName: graphDriverName,
MaxParallelDownloads: maxParallelDownloads,
},
}, nil
}
// PullImage pulls an image from a remote registry and stores it in the
// Prometheus store. It returns the manifest of the pulled image and an
// error if any. Note that the 'docker://' prefix is automatically added
// to the imageName to make it compatible with the alltransports.ParseImageName
// method.
func (p *Prometheus) PullImage(imageName, dstName string) (*OciManifest, digest.Digest, error) {
progressCh := make(chan types.ProgressProperties)
manifestCh := make(chan OciManifest)
errorCh := make(chan error)
defer close(progressCh)
defer close(manifestCh)
defer close(errorCh)
manifest, manifestDigest, err := p.PullManifestOnly(imageName)
if err != nil {
return nil, "", fmt.Errorf("failed to pull manifest %w", err)
}
// +1 to account for manifest
layersAll := len(manifest.LayerInfos()) + 1
layersDone := 0
err = p.pullImage(imageName, dstName, progressCh, manifestCh, errorCh)
if err != nil {
return nil, "", err
}
for {
select {
case report := <-progressCh:
digestShort := report.Artifact.Digest.Encoded()[:12]
switch report.Event {
case types.ProgressEventNewArtifact:
fmt.Printf("[%v/%v] %s: %s\n", layersDone, layersAll, digestShort, "new")
case types.ProgressEventDone, types.ProgressEventSkipped:
layersDone += 1
fmt.Printf("[%v/%v] %s: %s\n", layersDone, layersAll, digestShort, "done")
case types.ProgressEventRead:
percentDone := 100 * float64(report.Offset) / float64(report.Artifact.Size)
fmt.Printf("[%v/%v] %s: %v%%\n", layersDone, layersAll, digestShort, int(percentDone))
}
case manifest := <-manifestCh:
return &manifest, manifestDigest, nil
case err := <-errorCh:
return nil, "", err
}
}
}
// PullImageAsync does the same thing as PullImage, but returns right
// after starting the pull process. The user can track progress in the
// background by reading from the `progressCh` channel, which contains
// information about the current blob and its progress. When the pull
// process is done, the image's manifest will be sent via the `manifestCh`
// channel, which indicates the process is done.
//
// NOTE: The user is responsible for closing both channels once the operation
// completes.
func (p *Prometheus) PullImageAsync(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest, errorCh chan error) error {
err := p.pullImage(imageName, dstName, progressCh, manifestCh, errorCh)
return err
}
func (p *Prometheus) pullImage(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest, errorCh chan error) error {
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://%s", imageName))
if err != nil {
return err
}
destRef, err := storage.Transport.ParseStoreReference(p.Store, dstName)
if err != nil {
return err
}
systemCtx := &types.SystemContext{}
policy, err := signature.DefaultPolicy(systemCtx)
if err != nil {
return err
}
policyCtx, err := signature.NewPolicyContext(policy)
if err != nil {
return err
}
duration, err := time.ParseDuration("100ms")
if err != nil {
return err
}
go func() {
pulledManifestBytes, err := copy.Image(
context.Background(),
policyCtx,
destRef,
srcRef,
©.Options{
MaxParallelDownloads: p.Config.MaxParallelDownloads,
ProgressInterval: duration,
Progress: progressCh,
},
)
if err != nil {
errorCh <- err
return
}
var manifest OciManifest
err = json.Unmarshal(pulledManifestBytes, &manifest)
if err != nil {
errorCh <- err
return
}
manifestCh <- manifest
}()
return nil
}
/* GetImageById returns an image from the Prometheus store by its ID. */
func (p *Prometheus) GetImageById(id string) (cstorage.Image, error) {
images, err := p.Store.Images()
if err != nil {
return cstorage.Image{}, err
}
for _, img := range images {
if img.ID == id {
return img, nil
}
}
err = cstorage.ErrImageUnknown
return cstorage.Image{}, err
}
/* DoesImageExist checks if an image exists in the Prometheus store by its ID.
* It returns a boolean indicating if the image exists and an error if any. */
func (p *Prometheus) DoesImageExist(id string) (bool, error) {
image, err := p.GetImageById(id)
if err != nil {
return false, err
}
if image.ID == id {
return true, nil
}
return false, nil
}
/* MountImage mounts an image from the Prometheus store by its main layer
* digest. It returns the mount path and an error if any. */
func (p *Prometheus) MountImage(layerId string) (string, error) {
mountPath, err := p.Store.Mount(layerId, "")
if err != nil {
return "", err
}
return mountPath, nil
}
/* UnMountImage unmounts an image from the Prometheus store by its main layer
* digest. It returns a boolean indicating if the unmount was successful and
* an error if any. */
func (p *Prometheus) UnMountImage(layerId string, force bool) (bool, error) {
res, err := p.Store.Unmount(layerId, force)
if err != nil {
return res, err
}
return res, nil
}
/* BuildContainerFile builds a dockerfile and returns the manifest of the built
* image and an error if any. */
func (p *Prometheus) BuildContainerFile(dockerfilePath string, imageName string) (cstorage.Image, error) {
id, _, err := imagebuildah.BuildDockerfiles(
context.Background(),
p.Store,
define.BuildOptions{
Output: imageName,
},
dockerfilePath,
)
if err != nil {
return cstorage.Image{}, err
}
image, err := p.GetImageById(id)
if err != nil {
return cstorage.Image{}, err
}
return image, nil
}
func (p *Prometheus) PullManifestOnly(imageName string) (manifest.Manifest, digest.Digest, error) {
systemCtx := &types.SystemContext{}
ctx := context.Background()
srcRef, err := alltransports.ParseImageName(fmt.Sprintf("docker://%s", imageName))
if err != nil {
return nil, "", fmt.Errorf("failed to parse image name: %w", err)
}
source, err := srcRef.NewImageSource(ctx, systemCtx)
if err != nil {
return nil, "", fmt.Errorf("failed to create image source: %w", err)
}
defer source.Close()
manRaw, manMime, err := source.GetManifest(ctx, nil)
if err != nil {
return nil, "", fmt.Errorf("failed to fetch manifest: %w", err)
}
if manifest.MIMETypeIsMultiImage(manMime) {
list, err := manifest.ListFromBlob(manRaw, manMime)
if err != nil {
return nil, "", fmt.Errorf("failed to parse manifest list: %w", err)
}
instanceDigest, err := list.ChooseInstance(systemCtx)
if err != nil {
return nil, "", fmt.Errorf("failed to select platform instance: %w", err)
}
manRaw, manMime, err = source.GetManifest(ctx, &instanceDigest)
if err != nil {
return nil, "", fmt.Errorf("failed to fetch platform manifest: %w", err)
}
}
outputManifest, err := manifest.FromBlob(manRaw, manMime)
if err != nil {
return nil, "", fmt.Errorf("failed to parse platform specific manifest: %w", err)
}
manifestDigest, err := manifest.Digest(manRaw)
if err != nil {
return nil, "", fmt.Errorf("failed to fetch digest of manifest: %w", err)
}
return outputManifest, manifestDigest, nil
}