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
//go:build linux || darwin
package hostsvc
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/host"
)
var _ host.Units = (*proc)(nil)
type proc struct {
socket string
prefix string
installed string
}
func (p *proc) StartInstalled(ctx context.Context, name string) (UnitStatus, error) {
spec, err := ReadInstalledSpec(p.installed, name)
if err != nil {
return UnitStatus{}, err
}
return p.Start(ctx, spec)
}
func openProc(ctx context.Context, cfg Config) (Manager, error) {
p := &proc{socket: cfg.ProcSocket, prefix: cfg.Prefix(), installed: cfg.InstalledDir}
if _, err := p.Hello(ctx); err != nil {
return nil, err
}
return p, nil
}
func (p *proc) dial(ctx context.Context, timeout time.Duration) (net.Conn, error) {
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
var d net.Dialer
c, err := d.DialContext(ctx, "unix", p.socket)
if err != nil {
return nil, fmt.Errorf("%w: dialing devsup at %s: %w", ErrUnreachable, p.socket, err)
}
if dl, ok := ctx.Deadline(); ok {
c.SetDeadline(dl)
}
return c, nil
}
func (p *proc) call(ctx context.Context, req ProcRequest) (ProcResponse, error) {
timeout := DefaultCallTimeout
if req.Op == ProcOpStop || req.Op == ProcOpStopSlice || req.Op == ProcOpShutdown || req.Op == ProcOpCheck {
timeout = ProcStopCallTimeout
}
if _, ok := ctx.Deadline(); !ok {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
c, err := p.dial(ctx, timeout)
if err != nil {
return ProcResponse{}, err
}
defer c.Close()
stop := context.AfterFunc(ctx, func() { c.SetDeadline(time.Unix(1, 0)) })
defer stop()
if err := json.NewEncoder(c).Encode(req); err != nil {
return ProcResponse{}, fmt.Errorf("%w: writing %s to devsup: %w", ErrUnreachable, req.Op, err)
}
line, err := bufio.NewReader(c).ReadBytes('\n')
if err != nil {
return ProcResponse{}, fmt.Errorf("%w: reading the %s answer from devsup: %w", ErrUnreachable, req.Op, err)
}
var resp ProcResponse
if err := json.Unmarshal(line, &resp); err != nil {
return ProcResponse{}, fmt.Errorf("hostsvc: devsup answered %s with %q: %w", req.Op, line, err)
}
return resp, resp.Error()
}
func (p *proc) owns(name string) error {
if !strings.HasPrefix(name, p.prefix) {
return fmt.Errorf("hostsvc: unit %s is outside this manager's prefix %s", name, p.prefix)
}
return nil
}
func (p *proc) Hello(ctx context.Context) (ProcHello, error) {
resp, err := p.call(ctx, ProcRequest{Op: ProcOpHello})
if err != nil {
return ProcHello{}, err
}
if resp.Hello == nil {
return ProcHello{}, fmt.Errorf("hostsvc: devsup at %s answered hello without its identity", p.socket)
}
return *resp.Hello, nil
}
func (p *proc) Capabilities() Capabilities { return ProcCapabilities() }
func (p *proc) Start(ctx context.Context, spec UnitSpec) (UnitStatus, error) {
if err := ValidateSpec(spec); err != nil {
return UnitStatus{}, err
}
if err := p.owns(spec.Name); err != nil {
return UnitStatus{}, err
}
if err := RefuseUnsupported(p.Capabilities(), spec); err != nil {
return UnitStatus{}, err
}
resp, err := p.call(ctx, ProcRequest{Op: ProcOpStart, Spec: &spec})
if err != nil {
return UnitStatus{}, fmt.Errorf("hostsvc: starting %s: %w", spec.Name, err)
}
return *resp.Status, nil
}
func (p *proc) Inspect(ctx context.Context, name string) (UnitStatus, error) {
if err := ValidateName(name); err != nil {
return UnitStatus{}, err
}
resp, err := p.call(ctx, ProcRequest{Op: ProcOpInspect, Name: name})
if err != nil {
return UnitStatus{}, fmt.Errorf("hostsvc: inspecting %s: %w", name, err)
}
return *resp.Status, nil
}
func (p *proc) Stop(ctx context.Context, name string) error {
if err := ValidateName(name); err != nil {
return err
}
if _, err := p.call(ctx, ProcRequest{Op: ProcOpStop, Name: name}); err != nil {
return fmt.Errorf("hostsvc: stopping %s: %w", name, err)
}
return nil
}
func (p *proc) StopSlice(ctx context.Context, slice string) error {
if err := ValidateSlice(slice); err != nil {
return err
}
if _, err := p.call(ctx, ProcRequest{Op: ProcOpStopSlice, Slice: slice}); err != nil {
return fmt.Errorf("hostsvc: stopping %s: %w", slice, err)
}
return nil
}
func (p *proc) ResetFailed(ctx context.Context, name string) error {
if err := ValidateName(name); err != nil {
return err
}
if _, err := p.call(ctx, ProcRequest{Op: ProcOpResetFailed, Name: name}); err != nil {
return fmt.Errorf("hostsvc: resetting %s: %w", name, err)
}
return nil
}
func (p *proc) List(ctx context.Context, prefix string) ([]UnitStatus, error) {
if err := ValidatePrefix(prefix); err != nil {
return nil, err
}
resp, err := p.call(ctx, ProcRequest{Op: ProcOpList, Prefix: prefix})
if err != nil {
return nil, fmt.Errorf("hostsvc: listing %s*: %w", prefix, err)
}
return resp.Statuses, nil
}
func (p *proc) Identify(ctx context.Context, name string, pid int) (Identity, error) {
if err := ValidateName(name); err != nil {
return Identity{}, err
}
resp, err := p.call(ctx, ProcRequest{Op: ProcOpIdentify, Name: name, PID: pid})
if err != nil {
return Identity{}, err
}
return *resp.Identity, nil
}
func (p *proc) Check(ctx context.Context, want Identity) (Observation, error) {
if err := ValidateName(want.Unit); err != nil {
return Observation{}, err
}
if obs, foreign := ForeignIdentity(p.Capabilities(), want); foreign {
return obs, nil
}
resp, err := p.call(ctx, ProcRequest{Op: ProcOpCheck, Identity: &want})
if errors.Is(err, ErrUnreachable) {
return Observation{Verdict: VerdictUnknown, Detail: err.Error()}, nil
}
if err != nil {
return Observation{Detail: err.Error()}, err
}
return *resp.Observation, nil
}
func (p *proc) Watch(ctx context.Context, prefix string) (<-chan UnitEvent, error) {
if err := ValidatePrefix(prefix); err != nil {
return nil, err
}
var d net.Dialer
dctx, cancel := context.WithTimeout(ctx, DefaultCallTimeout)
c, err := d.DialContext(dctx, "unix", p.socket)
cancel()
if err != nil {
return nil, fmt.Errorf("%w: dialing devsup at %s: %w", ErrUnreachable, p.socket, err)
}
if err := json.NewEncoder(c).Encode(ProcRequest{Op: ProcOpWatch, Prefix: prefix}); err != nil {
c.Close()
return nil, fmt.Errorf("%w: writing watch to devsup: %w", ErrUnreachable, err)
}
r := bufio.NewReader(c)
line, err := r.ReadBytes('\n')
if err != nil {
c.Close()
return nil, fmt.Errorf("%w: reading the watch answer from devsup: %w", ErrUnreachable, err)
}
var first ProcResponse
if err := json.Unmarshal(line, &first); err != nil || first.Error() != nil {
c.Close()
return nil, fmt.Errorf("hostsvc: devsup refused watch: %v %v", err, first.Error())
}
ch := make(chan UnitEvent, 64)
stop := context.AfterFunc(ctx, func() { c.Close() })
go func() {
defer close(ch)
defer stop()
defer c.Close()
for {
line, err := r.ReadBytes('\n')
if err != nil {
return
}
var resp ProcResponse
if json.Unmarshal(line, &resp) != nil || resp.Event == nil {
continue
}
select {
case ch <- *resp.Event:
case <-ctx.Done():
return
}
}
}()
return ch, nil
}
func (p *proc) Shutdown(ctx context.Context) error {
_, err := p.call(ctx, ProcRequest{Op: ProcOpShutdown})
return err
}
func (p *proc) Close() error { return nil }
func OpenProcClient(ctx context.Context, cfg Config) (ProcClient, error) {
cfg.Backend = BackendProc
if err := cfg.Validate(); err != nil {
return nil, err
}
m, err := openProc(ctx, cfg)
if err != nil {
return nil, err
}
return m.(*proc), nil
}