-
Notifications
You must be signed in to change notification settings - Fork 110
/
cloud_package_manager.go
453 lines (374 loc) · 12.4 KB
/
cloud_package_manager.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package packages
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"hash"
"hash/crc32"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"go.uber.org/multierr"
pb "go.viam.com/api/app/packages/v1"
"go.viam.com/utils"
"go.viam.com/rdk/config"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
rutils "go.viam.com/rdk/utils"
)
const (
allowedContentType = "application/x-gzip"
)
var (
_ Manager = (*cloudManager)(nil)
_ ManagerSyncer = (*cloudManager)(nil)
)
type cloudManager struct {
resource.Named
// we assume the config is immutable for the lifetime of the process
resource.TriviallyReconfigurable
client pb.PackageServiceClient
httpClient http.Client
packagesDataDir string
packagesDir string
cloudConfig config.Cloud
managedPackages map[PackageName]*config.PackageConfig
mu sync.RWMutex
logger logging.Logger
}
// SubtypeName is a constant that identifies the internal package manager resource subtype string.
const SubtypeName = "packagemanager"
// API is the fully qualified API for the internal package manager service.
var API = resource.APINamespaceRDKInternal.WithServiceType(SubtypeName)
// InternalServiceName is used to refer to/depend on this service internally.
var InternalServiceName = resource.NewName(API, "builtin")
// NewCloudManager creates a new manager with the given package service client and directory to sync to.
func NewCloudManager(
cloudConfig *config.Cloud,
client pb.PackageServiceClient,
packagesDir string,
logger logging.Logger,
) (ManagerSyncer, error) {
packagesDataDir := filepath.Join(packagesDir, "data")
if err := os.MkdirAll(packagesDir, 0o700); err != nil {
return nil, err
}
if err := os.MkdirAll(packagesDataDir, 0o700); err != nil {
return nil, err
}
return &cloudManager{
Named: InternalServiceName.AsNamed(),
client: client,
httpClient: http.Client{Timeout: time.Minute * 30},
cloudConfig: *cloudConfig,
packagesDir: packagesDir,
packagesDataDir: packagesDataDir,
logger: logger.Sublogger("package_manager"),
}, nil
}
// PackagePath returns the package if it exists and is already downloaded. If it does not exist it returns a ErrPackageMissing error.
func (m *cloudManager) PackagePath(name PackageName) (string, error) {
m.mu.RLock()
defer m.mu.RUnlock()
p, ok := m.managedPackages[name]
if !ok {
return "", ErrPackageMissing
}
return p.LocalDataDirectory(m.packagesDir), nil
}
// Close manager.
func (m *cloudManager) Close(ctx context.Context) error {
m.mu.Lock()
defer m.mu.Unlock()
m.httpClient.CloseIdleConnections()
return nil
}
// SyncAll syncs all given packages and removes any not in the list from the local file system.
func (m *cloudManager) Sync(ctx context.Context, packages []config.PackageConfig, modules []config.Module) error {
var outErr error
// Only allow one rdk process to operate on the manager at once. This is generally safe to keep locked for an extended period of time
// since the config reconfiguration process is handled by a single thread.
m.mu.Lock()
defer m.mu.Unlock()
m.logger.Debug("Evaluating package sync...")
newManagedPackages := make(map[PackageName]*config.PackageConfig, len(packages))
// Process the packages that are new or changed
changedPackages, existingPackages := m.validateAndGetChangedPackages(packages)
// Updated managed map with existingPackages
for _, p := range existingPackages {
p := p
newManagedPackages[PackageName(p.Name)] = &p
}
if len(changedPackages) > 0 {
m.logger.Info("Package changes have been detected, starting sync")
}
start := time.Now()
for idx, p := range changedPackages {
p := p
pkgStart := time.Now()
if err := ctx.Err(); err != nil {
return multierr.Append(outErr, err)
}
m.logger.Debugf("Starting package sync [%d/%d] %s:%s", idx+1, len(changedPackages), p.Package, p.Version)
// Lookup the packages http url
includeURL := true
packageType, err := config.PackageTypeToProto(p.Type)
if err != nil {
m.logger.Warnw("failed to get package type", "package", p.Name, "error", err)
}
resp, err := m.client.GetPackage(ctx, &pb.GetPackageRequest{
Id: p.Package,
Version: p.Version,
Type: packageType,
IncludeUrl: &includeURL,
})
if err != nil {
m.logger.Errorf("Failed fetching package details for package %s:%s, %s", p.Package, p.Version, err)
outErr = multierr.Append(outErr, errors.Wrapf(err, "failed loading package url for %s:%s", p.Package, p.Version))
continue
}
m.logger.Debugf("Downloading from %s", sanitizeURLForLogs(resp.Package.Url))
// download package from a http endpoint
err = installPackage(ctx, m.logger, m.packagesDir, resp.Package.Url, p,
func(ctx context.Context, url, dstPath string) (string, string, error) {
statusFile := packageSyncFile{
PackageID: p.Package,
Version: p.Version,
ModifiedTime: time.Now(),
Status: syncStatusDownloading,
TarballChecksum: "",
}
err = writeStatusFile(p, statusFile, m.packagesDir)
if err != nil {
return "", "", err
}
return m.downloadFileFromGCSURL(ctx, url, dstPath, m.cloudConfig.ID, m.cloudConfig.Secret)
},
)
if err != nil {
m.logger.Errorf("Failed downloading package %s:%s from %s, %s", p.Package, p.Version, sanitizeURLForLogs(resp.Package.Url), err)
outErr = multierr.Append(outErr, errors.Wrapf(err, "failed downloading package %s:%s from %s",
p.Package, p.Version, sanitizeURLForLogs(resp.Package.Url)))
continue
}
if p.Type == config.PackageTypeMlModel {
outErr = multierr.Append(outErr, m.mLModelSymlinkCreation(p))
}
// add to managed packages
newManagedPackages[PackageName(p.Name)] = &p
m.logger.Debugf("Package sync complete [%d/%d] %s:%s after %v", idx+1, len(changedPackages), p.Package, p.Version, time.Since(pkgStart))
}
if len(changedPackages) > 0 {
m.logger.Infof("Package sync complete after %v", time.Since(start))
}
// swap for new managed packags.
m.managedPackages = newManagedPackages
return outErr
}
func (m *cloudManager) validateAndGetChangedPackages(
packages []config.PackageConfig,
) ([]config.PackageConfig, []config.PackageConfig) {
changed := make([]config.PackageConfig, 0)
existing := make([]config.PackageConfig, 0)
for _, p := range packages {
// don't consider invalid config as synced or unsynced
if err := p.Validate(""); err != nil {
m.logger.Errorw("package config validation error; skipping", "package", p.Name, "error", err)
continue
}
newPackage := p
if packageIsSynced(p, m.packagesDir, m.logger) {
existing = append(existing, newPackage)
m.logger.Debugf("Package %s:%s already managed, skipping", p.Package, p.Version)
} else {
changed = append(changed, newPackage)
}
}
return changed, existing
}
// Cleanup removes all unknown packages from the working directory.
func (m *cloudManager) Cleanup(ctx context.Context) error {
// Only allow one rdk process to operate on the manager at once. This is generally safe to keep locked for an extended period of time
// since the config reconfiguration process is handled by a single thread.
m.mu.Lock()
defer m.mu.Unlock()
m.logger.Debug("Starting package cleanup...")
var allErrors error
expectedPackageDirectories := map[string]bool{}
for _, pkg := range m.managedPackages {
expectedPackageDirectories[pkg.LocalDataDirectory(m.packagesDir)] = true
}
allErrors = commonCleanup(m.logger, expectedPackageDirectories, m.packagesDataDir)
if allErrors != nil {
return allErrors
}
allErrors = multierr.Append(allErrors, m.mlModelSymlinkCleanup())
return allErrors
}
// symlink packages/package-name to packages/data/ml_model/orgid-package-name-ver for backwards compatibility
// TODO(RSDK-4386) Preserved for backwards compatibility. Could be removed or extended to other types in the future.
func (m *cloudManager) mLModelSymlinkCreation(p config.PackageConfig) error {
symlinkPath, err := rutils.SafeJoinDir(m.packagesDir, p.Name)
if err != nil {
return err
}
if err := linkFile(p.LocalDataDirectory(m.packagesDir), symlinkPath); err != nil {
return errors.Wrapf(err, "failed linking ml_model package %s:%s, %s", p.Package, p.Version, err)
}
return nil
}
// cleanup all symlinks in packages/ directory
// TODO(RSDK-4386) Preserved for backwards compatibility. Could be removed or extended to other types in the future.
func (m *cloudManager) mlModelSymlinkCleanup() error {
var allErrors error
files, err := os.ReadDir(m.packagesDir)
if err != nil {
return err
}
// The only symlinks in this directory are those created for MLModels
for _, f := range files {
if f.Type()&os.ModeSymlink != os.ModeSymlink {
continue
}
// if managed skip removing package
if _, ok := m.managedPackages[PackageName(f.Name())]; ok {
continue
}
m.logger.Infof("Cleaning up unused package link %s", f.Name())
symlinkPath, err := rutils.SafeJoinDir(m.packagesDir, f.Name())
if err != nil {
allErrors = multierr.Append(allErrors, err)
continue
}
// Remove logical symlink to package
if err := os.Remove(symlinkPath); err != nil {
allErrors = multierr.Append(allErrors, err)
}
}
return allErrors
}
func sanitizeURLForLogs(u string) string {
parsed, err := url.Parse(u)
if err != nil {
return ""
}
parsed.RawQuery = ""
return parsed.String()
}
func (m *cloudManager) downloadFileFromGCSURL(
ctx context.Context,
url string,
downloadPath string,
partID string,
partSecret string,
) (string, string, error) {
getReq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
getReq.Header.Add("part_id", partID)
getReq.Header.Add("secret", partSecret)
if err != nil {
return "", "", err
}
//nolint:bodyclose /// closed in UncheckedErrorFunc
resp, err := m.httpClient.Do(getReq)
if err != nil {
return "", "", err
}
defer utils.UncheckedErrorFunc(resp.Body.Close)
if resp.StatusCode != http.StatusOK {
return "", "", fmt.Errorf("invalid status code %d", resp.StatusCode)
}
contentType := resp.Header.Get("Content-Type")
checksum := getGoogleHash(resp.Header, "crc32c")
//nolint:gosec // safe
out, err := os.Create(downloadPath)
if err != nil {
return checksum, contentType, err
}
defer utils.UncheckedErrorFunc(out.Close)
hash := crc32Hash()
w := io.MultiWriter(out, hash)
_, err = io.CopyN(w, resp.Body, maxPackageSize)
if err != nil && !errors.Is(err, io.EOF) {
utils.UncheckedError(os.Remove(downloadPath))
return checksum, contentType, err
}
checksumBytes, err := base64.StdEncoding.DecodeString(checksum)
if err != nil {
return "", "", errors.Wrapf(err, "failed to decode expected checksum: %s", checksum)
}
trimmedChecksumBytes := trimLeadingZeroes(checksumBytes)
trimmedOutHashBytes := trimLeadingZeroes(hash.Sum(nil))
if !bytes.Equal(trimmedOutHashBytes, trimmedChecksumBytes) {
utils.UncheckedError(os.Remove(downloadPath))
return checksum, contentType, errors.Errorf(
"download did not match expected hash:\n"+
" pre-trimmed: %x vs. %x\n"+
" trimmed: %x vs. %x",
checksumBytes, hash.Sum(nil),
trimmedChecksumBytes, trimmedOutHashBytes,
)
}
return checksum, contentType, nil
}
func trimLeadingZeroes(data []byte) []byte {
if len(data) == 0 {
return []byte{}
}
for i, b := range data {
if b != 0x00 {
return data[i:]
}
}
// If all bytes are zero, return a single zero byte
return []byte{0x00}
}
func getGoogleHash(headers http.Header, hashType string) string {
hashes := headers.Values("x-goog-hash")
hashesMap := make(map[string]string, len(hashes))
for _, v := range hashes {
split := strings.SplitN(v, "=", 2)
if len(split) != 2 {
continue
}
hashesMap[split[0]] = split[1]
}
return hashesMap[hashType]
}
func crc32Hash() hash.Hash32 {
return crc32.New(crc32.MakeTable(crc32.Castagnoli))
}
func safeLink(parent, link string) (string, error) {
if filepath.IsAbs(link) {
return link, errors.Errorf("unsafe path link: '%s' with '%s', cannot be absolute path", parent, link)
}
_, err := rutils.SafeJoinDir(parent, link)
if err != nil {
return link, err
}
return link, nil
}
func linkFile(from, to string) error {
link, err := os.Readlink(to)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
if link == from {
return nil
}
// remove any existing link or SymLink will fail.
if link != "" {
utils.UncheckedError(os.Remove(from))
}
return os.Symlink(from, to)
}
// SyncOne is a no-op for cloudManager.
func (m *cloudManager) SyncOne(ctx context.Context, mod config.Module) error {
return nil
}