-
Notifications
You must be signed in to change notification settings - Fork 64
/
umbrella.go
297 lines (243 loc) · 8.16 KB
/
umbrella.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package packmanager
import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/unikraft/component"
)
// UmbrellaInstance is a singleton of our umbrella package manager.
var UmbrellaInstance *UmbrellaManager
// UmbrellaManager is an ad-hoc package manager capable of cross managing any
// registered package managers.
type UmbrellaManager struct {
packageManagers map[pack.PackageFormat]PackageManager
packageManagerOpts map[pack.PackageFormat][]any
packageManagerConstructors map[pack.PackageFormat]NewManagerConstructor
}
// InitUmbrellaManager creates the instance of the umbrella manager singleton.
// It allows us to do dependency injection for package manager constructors.
func InitUmbrellaManager(ctx context.Context, constructors []func(*UmbrellaManager) error) error {
if UmbrellaInstance != nil {
return errors.New("tried to reinitialize the umbrella manager but it already exists")
}
umbrellaInstance, err := NewUmbrellaManager(ctx, constructors)
if err != nil {
return err
}
UmbrellaInstance = umbrellaInstance
return nil
}
func (u UmbrellaManager) From(sub pack.PackageFormat) (PackageManager, error) {
for _, manager := range u.packageManagers {
if manager.Format() == sub {
return manager, nil
}
}
return nil, fmt.Errorf("unknown package manager: %s", sub)
}
func (u UmbrellaManager) Update(ctx context.Context) error {
for _, manager := range u.packageManagers {
log.G(ctx).WithFields(logrus.Fields{
"format": manager.Format(),
}).Tracef("updating")
err := manager.Update(ctx)
if err != nil {
return err
}
}
return nil
}
func (u UmbrellaManager) SetSources(ctx context.Context, sources ...string) error {
for _, manager := range u.packageManagers {
err := manager.SetSources(ctx, sources...)
if err != nil {
return err
}
}
return nil
}
func (u UmbrellaManager) AddSource(ctx context.Context, source string) error {
for _, manager := range u.packageManagers {
log.G(ctx).WithFields(logrus.Fields{
"format": manager.Format(),
"source": source,
}).Tracef("adding")
err := manager.AddSource(ctx, source)
if err != nil {
return err
}
}
return nil
}
func (u UmbrellaManager) Delete(ctx context.Context, qopts ...QueryOption) error {
for _, manager := range u.packageManagers {
log.G(ctx).
WithField("format", manager.Format()).
WithFields(NewQuery(qopts...).Fields()).
Tracef("deleting")
if err := manager.Delete(ctx, qopts...); err != nil {
return err
}
}
return nil
}
func (u UmbrellaManager) RemoveSource(ctx context.Context, source string) error {
for _, manager := range u.packageManagers {
log.G(ctx).WithFields(logrus.Fields{
"format": manager.Format(),
"source": source,
}).Tracef("removing")
err := manager.RemoveSource(ctx, source)
if err != nil {
return err
}
}
return nil
}
func (u UmbrellaManager) Pack(ctx context.Context, source component.Component, opts ...PackOption) ([]pack.Package, error) {
var ret []pack.Package
for _, manager := range u.packageManagers {
log.G(ctx).WithFields(logrus.Fields{
"format": manager.Format(),
"source": source.Name(),
}).Tracef("packing")
more, err := manager.Pack(ctx, source, opts...)
if err != nil {
return nil, err
}
ret = append(ret, more...)
}
return ret, nil
}
func (u UmbrellaManager) Unpack(ctx context.Context, source pack.Package, opts ...UnpackOption) ([]component.Component, error) {
var ret []component.Component
for _, manager := range u.packageManagers {
log.G(ctx).WithFields(logrus.Fields{
"format": manager.Format(),
"source": source.Name(),
}).Tracef("unpacking")
more, err := manager.Unpack(ctx, source, opts...)
if err != nil {
return nil, err
}
ret = append(ret, more...)
}
return ret, nil
}
func (u UmbrellaManager) Catalog(ctx context.Context, qopts ...QueryOption) ([]pack.Package, error) {
var packages []pack.Package
for _, manager := range u.packageManagers {
pack, err := manager.Catalog(ctx, qopts...)
if err != nil {
log.G(ctx).
WithField("format", manager.Format()).
Debugf("could not query catalog: %v", err)
continue
}
packages = append(packages, pack...)
}
return packages, nil
}
func (u UmbrellaManager) IsCompatible(ctx context.Context, source string, qopts ...QueryOption) (PackageManager, bool, error) {
if source == "" {
return nil, false, fmt.Errorf("cannot determine compatibility of empty source")
}
for _, manager := range u.packageManagers {
log.G(ctx).WithFields(logrus.Fields{
"format": manager.Format(),
"source": source,
}).Debugf("checking compatibility")
pm, compatible, err := manager.IsCompatible(ctx, source, qopts...)
if err == nil && compatible {
return pm, true, nil
} else {
log.G(ctx).
WithField("format", manager.Format()).
Debugf("package manager is not compatible because: %v", err)
}
}
return nil, false, fmt.Errorf("cannot find compatible package manager for source: %s", source)
}
func (u *UmbrellaManager) PackageManagers() map[pack.PackageFormat]PackageManager {
return u.packageManagers
}
func (u *UmbrellaManager) RegisterPackageManager(ctxk pack.PackageFormat, constructor NewManagerConstructor, opts ...any) error {
if u.packageManagerConstructors == nil {
u.packageManagerConstructors = make(map[pack.PackageFormat]NewManagerConstructor)
}
if u.packageManagerOpts == nil {
u.packageManagerOpts = make(map[pack.PackageFormat][]any)
}
if u.packageManagers == nil {
u.packageManagers = make(map[pack.PackageFormat]PackageManager)
}
if _, ok := u.packageManagerConstructors[ctxk]; ok {
return fmt.Errorf("package manager already registered: %s", ctxk)
}
u.packageManagerConstructors[ctxk] = constructor
u.packageManagerOpts[ctxk] = opts
return nil
}
func (u UmbrellaManager) Format() pack.PackageFormat {
return UmbrellaFormat
}
const UmbrellaFormat pack.PackageFormat = "umbrella"
// PackageManagers returns all package managers registered
// with the umbrella package manager.
func PackageManagers() (map[pack.PackageFormat]PackageManager, error) {
if UmbrellaInstance == nil {
return nil, errors.New("umbrella manager not initialized")
}
return UmbrellaInstance.PackageManagers(), nil
}
// WithDefaultUmbrellaManagerInContext returns a context containing the
// default umbrella package manager.
func WithDefaultUmbrellaManagerInContext(ctx context.Context) (context.Context, error) {
if UmbrellaInstance == nil {
return nil, errors.New("umbrella manager not initialized")
}
return WithManagerInContext(ctx, UmbrellaInstance), nil
}
// WithManagerInContext inserts a package manager into a context.
func WithManagerInContext(ctx context.Context, pm PackageManager) context.Context {
return WithPackageManager(ctx, pm)
}
// NewUmbrellaManager returns a `PackageManager` which can be used to manipulate
// multiple `PackageManager`s. The purpose is to be able to package, unpackage,
// search and generally manipulate packages of multiple types simultaneously.
// The user can pass a slice of constructors to determine which package managers
// are to be included.
func NewUmbrellaManager(ctx context.Context, constructors []func(*UmbrellaManager) error) (*UmbrellaManager, error) {
u := &UmbrellaManager{}
for _, reg := range constructors {
if err := reg(u); err != nil {
return nil, fmt.Errorf("failed registering a package manager: %w", err)
}
}
for format, constructor := range u.packageManagerConstructors {
log.G(ctx).WithField("format", format).Trace("initializing package manager")
var opts []any
if pmopts, ok := u.packageManagerOpts[format]; ok {
opts = pmopts
}
manager, err := constructor(ctx, opts...)
if err != nil {
log.G(ctx).
WithField("format", format).
Debugf("could not initialize package manager: %v", err)
continue
}
if format, ok := u.packageManagers[manager.Format()]; ok {
return nil, fmt.Errorf("package manager already registered: %s", format)
}
u.packageManagers[manager.Format()] = manager
}
return u, nil
}