-
Notifications
You must be signed in to change notification settings - Fork 50
/
state.go
579 lines (513 loc) · 14.7 KB
/
state.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package state
import (
"context"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
"github.com/cashapp/hermit/archive"
"github.com/cashapp/hermit/cache"
"github.com/cashapp/hermit/errors"
"github.com/cashapp/hermit/internal/dao"
"github.com/cashapp/hermit/manifest"
"github.com/cashapp/hermit/platform"
"github.com/cashapp/hermit/sources"
"github.com/cashapp/hermit/ui"
"github.com/cashapp/hermit/util"
"github.com/cashapp/hermit/vfs"
)
// DefaultSources if no others are defined.
var DefaultSources = []string{"https://github.com/cashapp/hermit-packages.git"}
type precompiledAutoMirror struct {
re *regexp.Regexp
groups map[string]int
mirror string
}
// AutoMirror defines a dynamically generated mirror URL mapping.
type AutoMirror struct {
// Origin URL regex to generate a mirror from. Named patterns will be substituted into mirror.
Origin string
// Mirror URL to add.
Mirror string
}
// Config for Hermit's global state.
type Config struct {
// List of sources to sync packages from.
Sources []string
// Auto-generated mirrors.
AutoMirrors []AutoMirror
// Builtin sources.
Builtin *sources.BuiltInSource
LockTimeout time.Duration
}
// State is the global hermit state shared between all local environments
type State struct {
root string // Path to the state directory
cacheDir string // Path to the root of the Hermit cache.
pkgDir string // Path to unpacked packages.
sourcesDir string // Path to extracted sources.
binaryDir string // Path to directory with symlinks to package binaries
config Config
autoMirrors []precompiledAutoMirror
cache *cache.Cache
dao *dao.DAO
lock *util.FileLock
lockTimeout time.Duration
}
// Open the global Hermit state.
//
// See cache.Open for details on downloadStrategies.
func Open(stateDir string, config Config, cache *cache.Cache) (*State, error) {
if config.Builtin == nil {
return nil, errors.Errorf("state.Config.Builtin not provided")
}
pkgDir := filepath.Join(stateDir, "pkg")
cacheDir := filepath.Join(stateDir, "cache")
sourcesDir := filepath.Join(stateDir, "sources")
binaryDir := filepath.Join(stateDir, "binaries")
dao, err := dao.Open(stateDir)
if err != nil {
return nil, errors.WithStack(err)
}
if config.Sources == nil {
config.Sources = DefaultSources
}
autoMirrors, err := validateAndCompileAutoMirrors(config)
if err != nil {
return nil, errors.WithStack(err)
}
s := &State{
dao: dao,
autoMirrors: autoMirrors,
root: stateDir,
cacheDir: cacheDir,
sourcesDir: sourcesDir,
binaryDir: binaryDir,
config: config,
pkgDir: pkgDir,
cache: cache,
lock: util.NewLock(filepath.Join(stateDir, ".lock"), 1*time.Second),
lockTimeout: config.LockTimeout,
}
return s, nil
}
func validateAndCompileAutoMirrors(config Config) ([]precompiledAutoMirror, error) {
autoMirrors := []precompiledAutoMirror{}
for _, mirror := range config.AutoMirrors {
re, err := regexp.Compile(mirror.Origin)
if err != nil {
return nil, errors.Errorf("auto-mirror key %q is not a valid regular expression", mirror.Origin)
}
pam := precompiledAutoMirror{
re: re,
mirror: mirror.Mirror,
groups: map[string]int{},
}
for id, name := range re.SubexpNames() {
pam.groups[name] = id
}
os.Expand(mirror.Mirror, func(name string) string {
_, ok := pam.groups[name]
if !ok {
err = errors.Errorf("unknown capture group %q in auto-mirror", name)
}
return ""
})
if err != nil {
return nil, errors.WithStack(err)
}
autoMirrors = append(autoMirrors, pam)
}
return autoMirrors, nil
}
// Resolve package reference without an active environment.
func (s *State) Resolve(l *ui.UI, matcher manifest.Selector) (*manifest.Package, error) {
resolver, err := s.resolver(l)
if err != nil {
return nil, errors.WithStack(err)
}
return resolver.Resolve(l, matcher)
}
// Search for packages without an active environment.
func (s *State) Search(l *ui.UI, glob string) (manifest.Packages, error) {
resolver, err := s.resolver(l)
if err != nil {
return nil, errors.WithStack(err)
}
pkgs, err := resolver.Search(l, glob)
if err != nil {
return nil, errors.WithStack(err)
}
return pkgs, nil
}
// Config returns the configuration stored in the global state.
func (s *State) Config() Config {
return s.config
}
// SourcesDir returns the global directory for manifests
func (s *State) SourcesDir() string {
return s.sourcesDir
}
// Root returns the root directory for the hermit state
func (s *State) Root() string {
return s.root
}
// PkgDir returns path to the directory for extracted packages
func (s *State) PkgDir() string {
return s.pkgDir
}
// BinaryDir returns path to the directory for bin symlinks
func (s *State) BinaryDir() string {
return s.binaryDir
}
func (s *State) resolver(l *ui.UI) (*manifest.Resolver, error) {
ss, err := s.Sources(l)
if err != nil {
return nil, err
}
return manifest.New(ss, manifest.Config{
State: s.Root(),
Platform: platform.Platform{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
},
})
}
// Sources associated with the State.
func (s *State) Sources(l *ui.UI) (*sources.Sources, error) {
ss, err := sources.ForURIs(l, s.SourcesDir(), "", s.config.Sources)
if err != nil {
return nil, errors.WithStack(err)
}
ss.Prepend(s.config.Builtin)
return ss, nil
}
func (s *State) acquireLock(log ui.Logger) (*util.FileLock, error) {
log.Tracef("timeout for acquiring the lock is %s", s.lockTimeout)
ctx, cancel := context.WithTimeout(context.Background(), s.lockTimeout)
err := s.lock.Acquire(ctx, log)
cancel()
if err != nil {
return nil, errors.Wrap(err, "failed to acquire lock")
}
return s.lock, nil
}
// ReadPackageState updates the package fields from the global database
func (s *State) ReadPackageState(pkg *manifest.Package) {
if _, err := os.Stat(pkg.Root); err == nil {
pkg.State = manifest.PackageStateInstalled
} else if s.cache.IsCached(pkg.SHA256, pkg.Source) {
pkg.State = manifest.PackageStateDownloaded
}
// We are ignoring the error as we might be updating a non exiting package
dbInfo, _ := s.dao.GetPackage(pkg.String())
if dbInfo == nil {
dbInfo = &dao.Package{}
}
pkg.ETag = dbInfo.Etag
pkg.UpdatedAt = dbInfo.UpdateCheckedAt
}
// WritePackageState updates the fields and usage time stamp of the given package
func (s *State) WritePackageState(p *manifest.Package) error {
var updatedAt = time.Time{}
if p.UpdateInterval > 0 {
updatedAt = p.UpdatedAt
}
pkg := &dao.Package{
Etag: p.ETag,
UpdateCheckedAt: updatedAt,
}
return s.dao.UpdatePackage(p.Reference.String(), pkg)
}
func (s *State) removeRecursive(b *ui.Task, dest string) error {
_, err := os.Stat(dest)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return errors.WithStack(err)
}
task := b.SubTask("remove")
lock, err := s.acquireLock(b)
if err != nil {
return errors.WithStack(err)
}
defer lock.Release(b)
task.Debugf("chmod -R +w %s", dest)
_ = filepath.Walk(dest, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.WithStack(err)
}
err = os.Chmod(path, info.Mode()|0200)
if errors.Is(err, os.ErrNotExist) {
task.Debugf("file did not exist during removal %q", path)
return nil
}
return errors.WithStack(err)
})
task.Debugf("rm -rf %s", dest)
return errors.WithStack(os.RemoveAll(dest))
}
// CacheAndUnpack downloads a package and extracts it if it is not present.
//
// If the package has already been extracted, this is a no-op
func (s *State) CacheAndUnpack(b *ui.Task, p *manifest.Package) error {
// Double-checked locking. We check without the lock first, and then check
// again after acquiring the lock.
if (s.isExtracted(p) && s.areBinariesLinked(p)) || p.Source == "/" {
return nil
}
lock, err := s.acquireLock(b)
if err != nil {
return errors.WithStack(err)
}
defer lock.Release(b)
if !s.isExtracted(p) {
if err := s.extract(b, p); err != nil {
return errors.WithStack(err)
}
}
if !s.areBinariesLinked(p) {
if err := s.linkBinaries(p); err != nil {
return errors.WithStack(err)
}
}
return nil
}
// CacheAndDigest Utility for Caching all platform artefacts.
//
// This method will only cache the values and get a digest.
func (s *State) CacheAndDigest(b *ui.Task, p *manifest.Package) (string, error) {
actualDigest := ""
var err error
if !s.isCached(p) {
mirrors := make([]string, len(p.Mirrors))
copy(mirrors, p.Mirrors)
mirrors = append(mirrors, s.generateMirrors(p.Source)...)
_, _, actualDigest, err = s.cache.Download(b, p.SHA256, p.Source, mirrors...)
if err != nil {
return "", errors.WithStack(err)
}
} else if p.SHA256 != "" {
// If the manifest has SHA256 value then the package installation must have
// checked that. So just use it.
actualDigest = p.SHA256
} else {
// if the artifact is cached then just calculate the digest.
path := s.cache.Path(p.SHA256, p.Source)
actualDigest, err = util.Sha256LocalFile(path)
if err != nil {
return "", errors.WithStack(err)
}
}
return actualDigest, nil
}
func (s *State) linkBinaries(p *manifest.Package) error {
dir := filepath.Join(s.binaryDir, p.Reference.String())
// clean up the binaryDir before
if err := os.RemoveAll(dir); err != nil {
return errors.WithStack(err)
}
if err := os.MkdirAll(dir, 0700); err != nil {
return errors.WithStack(err)
}
bins, err := p.ResolveBinaries()
if err != nil {
return errors.WithStack(err)
}
for _, bin := range bins {
to := filepath.Join(dir, filepath.Base(bin))
if dest, err := os.Readlink(to); err == nil && dest == bin {
continue
}
if err := os.Symlink(bin, to); err != nil {
return errors.WithStack(err)
}
}
return nil
}
func (s *State) extract(b *ui.Task, p *manifest.Package) error {
var (
path string
etag string
err error
)
if !s.isCached(p) {
mirrors := make([]string, len(p.Mirrors))
copy(mirrors, p.Mirrors)
mirrors = append(mirrors, s.generateMirrors(p.Source)...)
path, etag, _, err = s.cache.Download(b, p.SHA256, p.Source, mirrors...)
p.ETag = etag
if err != nil {
return errors.WithStack(err)
}
} else {
path = s.cache.Path(p.SHA256, p.Source)
}
finalise, err := archive.Extract(b, path, p)
if err != nil {
return errors.WithStack(err)
}
// Copy manifest referred files
for _, file := range p.Files {
err = vfs.CopyFile(file.FS, file.FromPath, file.ToPath)
if err != nil {
return errors.WithStack(err)
}
}
if _, err = p.Trigger(b, manifest.EventUnpack); err != nil {
_ = os.RemoveAll(p.Dest)
return errors.WithStack(err)
}
return errors.WithStack(finalise())
}
func (s *State) isCached(p *manifest.Package) bool {
return s.cache.IsCached(p.SHA256, p.Source)
}
func (s *State) isExtracted(p *manifest.Package) bool {
_, err := os.Stat(p.Root)
return err == nil
}
func (s *State) areBinariesLinked(p *manifest.Package) bool {
binaries, err := p.ResolveBinaries()
if err != nil {
return false
}
for _, bin := range binaries {
linkPath := filepath.Join(s.binaryDir, p.Reference.String(), filepath.Base(bin))
if _, err := os.Stat(linkPath); err != nil {
return false
}
// also checks the link destination matches the binary path.
ld, err := os.Readlink(linkPath)
if err != nil {
return false
}
if bin != ld {
return false
}
}
return true
}
// CleanPackages removes all extracted packages
func (s *State) CleanPackages(b *ui.UI) error {
// TODO: Uninstall packages from their configured root so that eg. external packages can be uninstalled.
lock, err := s.acquireLock(b)
if err != nil {
return err
}
defer lock.Release(b)
bins, err := os.ReadDir(s.binaryDir)
if err != nil && !os.IsNotExist(err) {
return errors.WithStack(err)
}
for _, entry := range bins {
path := filepath.Join(s.binaryDir, entry.Name())
if err = s.removeRecursive(b.Task(entry.Name()), path); err != nil {
return errors.WithStack(err)
}
}
entries, err := os.ReadDir(s.pkgDir)
if err != nil && !os.IsNotExist(err) {
return errors.WithStack(err)
}
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), "hermit@") {
continue
}
path := filepath.Join(s.pkgDir, entry.Name())
if err = s.removeRecursive(b.Task(entry.Name()), path); err != nil {
return errors.WithStack(err)
}
}
return nil
}
// CleanCache clears the download cache
func (s *State) CleanCache(b ui.Logger) error {
lock, err := s.acquireLock(b)
if err != nil {
return err
}
defer lock.Release(b)
b.Debugf("rm -rf %q", s.cacheDir)
return os.RemoveAll(s.cacheDir)
}
// UpgradeChannel checks if the given binary has changed in its channel, and if so, downloads it.
//
// If the channel is upgraded this will return a clone of the updated manifest.
func (s *State) UpgradeChannel(b *ui.Task, pkg *manifest.Package) error {
if !pkg.Reference.IsChannel() {
panic("UpgradeChannel can only be used with channel packages")
}
name := pkg.Reference.String()
mirrors := make([]string, len(pkg.Mirrors))
copy(mirrors, pkg.Mirrors)
mirrors = append(mirrors, s.generateMirrors(pkg.Source)...)
etag, err := s.cache.ETag(b, pkg.Source, mirrors...)
if err != nil {
b.Warnf("Could not check updates for %s. Skipping update. Error: %s", name, err)
} else if etag == "" {
b.Warnf("No ETag found for %s. Skipping update.", name)
} else if etag != pkg.ETag {
b.Infof("Fetching a new version for %s", name)
if err := s.evictPackage(b, pkg); err != nil {
return errors.WithStack(err)
}
if err := s.CacheAndUnpack(b, pkg); err != nil {
return errors.WithStack(err)
}
etag = pkg.ETag
} else {
b.Infof("No update required")
}
if etag == "" {
etag = pkg.ETag
}
pkg.UpdatedAt = time.Now()
dpkg := &dao.Package{
Etag: etag,
UpdateCheckedAt: time.Now(),
}
return errors.WithStack(s.dao.UpdatePackage(name, dpkg))
}
func (s *State) removePackage(task *ui.Task, pkg *manifest.Package) error {
err := s.removeRecursive(task, filepath.Join(s.binaryDir, pkg.Reference.String()))
if err != nil {
return errors.WithStack(err)
}
err = s.removeRecursive(task, pkg.Dest)
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (s *State) evictPackage(b *ui.Task, pkg *manifest.Package) error {
lock, err := s.acquireLock(b)
if err != nil {
return errors.WithStack(err)
}
defer lock.Release(b)
if err := s.cache.Evict(b, pkg.SHA256, pkg.Source); err != nil {
return errors.WithStack(err)
}
if err := s.removePackage(b, pkg); err != nil {
return errors.WithStack(err)
}
return nil
}
// Return the generated mirrors that match URL.
func (s *State) generateMirrors(url string) (mirrors []string) {
for _, pam := range s.autoMirrors {
matches := pam.re.FindStringSubmatch(url)
if matches == nil {
continue
}
mirror := os.Expand(pam.mirror, func(key string) string {
return matches[pam.groups[key]]
})
mirrors = append(mirrors, mirror)
}
return
}