-
-
Notifications
You must be signed in to change notification settings - Fork 356
/
inotify.go
108 lines (86 loc) · 2.24 KB
/
inotify.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
package inotify
import (
"context"
"fmt"
"time"
"github.com/abiosoft/colima/daemon/process"
"github.com/abiosoft/colima/environment"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/sirupsen/logrus"
)
const Name = "inotify"
const volumesInterval = 5 * time.Second
type Args struct {
environment.GuestActions
Dirs []string
Runtime string
}
func CtxKeyArgs() any { return struct{ name string }{name: "inotify_args"} }
// New returns inotify process.
func New() process.Process {
return &inotifyProcess{
log: logrus.WithField("context", "inotify"),
}
}
var _ process.Process = (*inotifyProcess)(nil)
type inotifyProcess struct {
vmVols []string
guest environment.GuestActions
runtime string
log *logrus.Entry
}
// Alive implements process.Process
func (f *inotifyProcess) Alive(ctx context.Context) error {
daemonRunning, _ := ctx.Value(process.CtxKeyDaemon()).(bool)
// if the parent is active, we can assume inotify is active.
if daemonRunning {
return nil
}
return fmt.Errorf("inotify not running")
}
// Dependencies implements process.Process
func (*inotifyProcess) Dependencies() (deps []process.Dependency, root bool) {
return nil, false
}
// Name implements process.Process
func (*inotifyProcess) Name() string {
return Name
}
// Start implements process.Process
func (f *inotifyProcess) Start(ctx context.Context) error {
args, ok := ctx.Value(CtxKeyArgs()).(Args)
if !ok {
return fmt.Errorf("args missing in context")
}
f.vmVols = omitChildrenDirectories(args.Dirs)
f.guest = args.GuestActions
f.runtime = args.Runtime
log := f.log
log.Info("waiting for VM to start")
f.waitForLima(ctx)
log.Info("VM started")
watcher := &defaultWatcher{log: log}
return f.handleEvents(ctx, watcher)
}
// waitForLima waits until lima starts and sets the directory to watch.
func (f *inotifyProcess) waitForLima(ctx context.Context) {
log := f.log
// wait for Lima to finish starting
for {
log.Info("waiting 5 secs for VM")
// 5 second interval
after := time.After(time.Second * 5)
select {
case <-ctx.Done():
return
case <-after:
i, err := limautil.Instance()
if err != nil || !i.Running() {
continue
}
if err := f.guest.RunQuiet("uname", "-a"); err == nil {
return
}
}
}
}