-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathoverlord.go
332 lines (291 loc) · 8.15 KB
/
overlord.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Package overlord implements the overall control of a snappy system.
package overlord
import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
"gopkg.in/tomb.v2"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/configstate"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/ifacestate"
"github.com/snapcore/snapd/overlord/patch"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/store"
)
var (
ensureInterval = 5 * time.Minute
pruneInterval = 10 * time.Minute
pruneWait = 24 * time.Hour * 1
abortWait = 24 * time.Hour * 7
pruneMaxChanges = 500
)
// Overlord is the central manager of a snappy system, keeping
// track of all available state managers and related helpers.
type Overlord struct {
stateEng *StateEngine
// ensure loop
loopTomb *tomb.Tomb
ensureLock sync.Mutex
ensureTimer *time.Timer
ensureNext time.Time
pruneTicker *time.Ticker
// restarts
restartHandler func(t state.RestartType)
// managers
snapMgr *snapstate.SnapManager
assertMgr *assertstate.AssertManager
ifaceMgr *ifacestate.InterfaceManager
hookMgr *hookstate.HookManager
configMgr *configstate.ConfigManager
deviceMgr *devicestate.DeviceManager
}
var storeNew = store.New
// New creates a new Overlord with all its state managers.
func New() (*Overlord, error) {
o := &Overlord{
loopTomb: new(tomb.Tomb),
}
backend := &overlordStateBackend{
path: dirs.SnapStateFile,
ensureBefore: o.ensureBefore,
requestRestart: o.requestRestart,
}
s, err := loadState(backend)
if err != nil {
return nil, err
}
o.stateEng = NewStateEngine(s)
hookMgr, err := hookstate.Manager(s)
if err != nil {
return nil, err
}
o.hookMgr = hookMgr
o.stateEng.AddManager(o.hookMgr)
snapMgr, err := snapstate.Manager(s)
if err != nil {
return nil, err
}
o.snapMgr = snapMgr
o.stateEng.AddManager(o.snapMgr)
assertMgr, err := assertstate.Manager(s)
if err != nil {
return nil, err
}
o.assertMgr = assertMgr
o.stateEng.AddManager(o.assertMgr)
ifaceMgr, err := ifacestate.Manager(s, hookMgr, nil, nil)
if err != nil {
return nil, err
}
o.ifaceMgr = ifaceMgr
o.stateEng.AddManager(o.ifaceMgr)
configMgr, err := configstate.Manager(s, hookMgr)
if err != nil {
return nil, err
}
o.configMgr = configMgr
deviceMgr, err := devicestate.Manager(s, hookMgr)
if err != nil {
return nil, err
}
o.deviceMgr = deviceMgr
o.stateEng.AddManager(o.deviceMgr)
// setting up the store
authContext := auth.NewAuthContext(s, o.deviceMgr)
sto := storeNew(nil, authContext)
s.Lock()
snapstate.ReplaceStore(s, sto)
s.Unlock()
return o, nil
}
func loadState(backend state.Backend) (*state.State, error) {
if !osutil.FileExists(dirs.SnapStateFile) {
// fail fast, mostly interesting for tests, this dir is setup
// by the snapd package
stateDir := filepath.Dir(dirs.SnapStateFile)
if !osutil.IsDirectory(stateDir) {
return nil, fmt.Errorf("fatal: directory %q must be present", stateDir)
}
s := state.New(backend)
patch.Init(s)
return s, nil
}
r, err := os.Open(dirs.SnapStateFile)
if err != nil {
return nil, fmt.Errorf("cannot read the state file: %s", err)
}
defer r.Close()
s, err := state.ReadState(backend, r)
if err != nil {
return nil, err
}
// one-shot migrations
err = patch.Apply(s)
if err != nil {
return nil, err
}
return s, nil
}
func (o *Overlord) ensureTimerSetup() {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
o.ensureTimer = time.NewTimer(ensureInterval)
o.ensureNext = time.Now().Add(ensureInterval)
o.pruneTicker = time.NewTicker(pruneInterval)
}
func (o *Overlord) ensureTimerReset() time.Time {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
now := time.Now()
o.ensureTimer.Reset(ensureInterval)
o.ensureNext = now.Add(ensureInterval)
return o.ensureNext
}
func (o *Overlord) ensureBefore(d time.Duration) {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
if o.ensureTimer == nil {
panic("cannot use EnsureBefore before Overlord.Loop")
}
now := time.Now()
next := now.Add(d)
if next.Before(o.ensureNext) || o.ensureNext.Before(now) {
o.ensureTimer.Reset(d)
o.ensureNext = next
}
}
func (o *Overlord) requestRestart(t state.RestartType) {
if o.restartHandler == nil {
logger.Noticef("restart requested but no handler set")
} else {
o.restartHandler(t)
}
}
// SetRestartHandler sets a handler to fulfill restart requests asynchronously.
func (o *Overlord) SetRestartHandler(handleRestart func(t state.RestartType)) {
o.restartHandler = handleRestart
}
// Loop runs a loop in a goroutine to ensure the current state regularly through StateEngine Ensure.
func (o *Overlord) Loop() {
o.ensureTimerSetup()
o.loopTomb.Go(func() error {
for {
o.ensureTimerReset()
// in case of errors engine logs them,
// continue to the next Ensure() try for now
o.stateEng.Ensure()
select {
case <-o.loopTomb.Dying():
return nil
case <-o.ensureTimer.C:
case <-o.pruneTicker.C:
st := o.State()
st.Lock()
st.Prune(pruneWait, abortWait, pruneMaxChanges)
st.Unlock()
}
}
})
}
// Stop stops the ensure loop and the managers under the StateEngine.
func (o *Overlord) Stop() error {
o.loopTomb.Kill(nil)
err1 := o.loopTomb.Wait()
o.stateEng.Stop()
return err1
}
// Settle runs first a state engine Ensure and then wait for activities to settle.
// That's done by waiting for all managers activities to settle while
// making sure no immediate further Ensure is scheduled. Chiefly for tests.
// Cannot be used in conjunction with Loop.
func (o *Overlord) Settle() error {
func() {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
if o.ensureTimer != nil {
panic("cannot use Settle concurrently with other Settle or Loop calls")
}
o.ensureTimer = time.NewTimer(0)
}()
defer func() {
o.ensureLock.Lock()
defer o.ensureLock.Unlock()
o.ensureTimer.Stop()
o.ensureTimer = nil
}()
done := false
var errs []error
for !done {
next := o.ensureTimerReset()
err := o.stateEng.Ensure()
switch ee := err.(type) {
case nil:
case *ensureError:
errs = append(errs, ee.errs...)
default:
errs = append(errs, err)
}
o.stateEng.Wait()
o.ensureLock.Lock()
done = o.ensureNext.Equal(next)
o.ensureLock.Unlock()
}
if len(errs) != 0 {
return &ensureError{errs}
}
return nil
}
// State returns the system state managed by the overlord.
func (o *Overlord) State() *state.State {
return o.stateEng.State()
}
// SnapManager returns the snap manager responsible for snaps under
// the overlord.
func (o *Overlord) SnapManager() *snapstate.SnapManager {
return o.snapMgr
}
// AssertManager returns the assertion manager enforcing assertions
// under the overlord.
func (o *Overlord) AssertManager() *assertstate.AssertManager {
return o.assertMgr
}
// InterfaceManager returns the interface manager maintaining
// interface connections under the overlord.
func (o *Overlord) InterfaceManager() *ifacestate.InterfaceManager {
return o.ifaceMgr
}
// HookManager returns the hook manager responsible for running hooks under the
// overlord.
func (o *Overlord) HookManager() *hookstate.HookManager {
return o.hookMgr
}
// DeviceManager returns the device manager responsible for the device identity and policies
func (o *Overlord) DeviceManager() *devicestate.DeviceManager {
return o.deviceMgr
}