-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
248 lines (213 loc) · 6.49 KB
/
client.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
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"
"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/storage"
"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/types"
cstorage "github.com/containers/storage"
)
/* 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) {
var err 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, error) {
progressCh := make(chan types.ProgressProperties)
manifestCh := make(chan OciManifest)
defer close(progressCh)
defer close(manifestCh)
err := p.pullImage(imageName, dstName, progressCh, manifestCh)
if err != nil {
return nil, err
}
for {
select {
case report := <-progressCh:
fmt.Printf("%s: %v/%v\n", report.Artifact.Digest.Encoded()[:12], report.Offset, report.Artifact.Size)
case manifest := <-manifestCh:
return &manifest, nil
}
}
}
// 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) error {
err := p.pullImage(imageName, dstName, progressCh, manifestCh)
return err
}
func (p *Prometheus) pullImage(imageName, dstName string, progressCh chan types.ProgressProperties, manifestCh chan OciManifest) 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 {
return
}
var manifest OciManifest
err = json.Unmarshal(pulledManifestBytes, &manifest)
if err != nil {
return
}
// here we remove the 'sha256:' prefix from the digest, so we don't have
// to deal with it later
manifest.Config.Digest = manifest.Config.Digest[7:]
for i := range manifest.Layers {
manifest.Layers[i].Digest = manifest.Layers[i].Digest[7:]
}
manifestCh <- manifest
}()
return nil
}
/* GetImageByDigest returns an image from the Prometheus store by its digest. */
func (p *Prometheus) GetImageByDigest(digest string) (cstorage.Image, error) {
images, err := p.Store.Images()
if err != nil {
return cstorage.Image{}, err
}
for _, img := range images {
if img.ID == digest {
return img, nil
}
}
err = cstorage.ErrImageUnknown
return cstorage.Image{}, err
}
/* DoesImageExist checks if an image exists in the Prometheus store by its
* digest. It returns a boolean indicating if the image exists and an error
* if any. */
func (p *Prometheus) DoesImageExist(digest string) (bool, error) {
image, err := p.GetImageByDigest(digest)
if err != nil {
return false, err
}
if image.ID == digest {
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.GetImageByDigest(id)
if err != nil {
return cstorage.Image{}, err
}
return image, nil
}