forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
190 lines (168 loc) · 5.57 KB
/
handlers.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2020 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 devicestate
import (
"fmt"
"os/exec"
"time"
"gopkg.in/tomb.v2"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
)
func (m *DeviceManager) doMarkPreseeded(t *state.Task, _ *tomb.Tomb) error {
st := t.State()
st.Lock()
defer st.Unlock()
snaps, err := snapstate.All(st)
if err != nil {
return err
}
systemKey, err := interfaces.RecordedSystemKey()
if err != nil {
return fmt.Errorf("cannot get recorded system key: %v", err)
}
if m.preseed {
var preseeded bool
// the "preseeded" flag on this task is set to allow skipping the logic
// below in case this handler is retried in preseeding mode due to an
// EnsureBefore(0) done somewhere else.
// XXX: we should probably drop the flag from the task now that we have
// one on the state.
if err := t.Get("preseeded", &preseeded); err != nil && err != state.ErrNoState {
return err
}
if !preseeded {
preseeded = true
t.Set("preseeded", preseeded)
// unmount all snaps
// TODO: move to snapstate.UnmountAllSnaps.
for _, snapSt := range snaps {
info, err := snapSt.CurrentInfo()
if err != nil {
return err
}
logger.Debugf("unmounting snap %s at %s", info.InstanceName(), info.MountDir())
if _, err := exec.Command("umount", "-d", "-l", info.MountDir()).CombinedOutput(); err != nil {
return err
}
}
st.Set("preseeded", preseeded)
st.Set("preseed-system-key", systemKey)
st.Set("preseed-time", timeNow())
// do not mark this task done as this makes it racy against taskrunner tear down (the next task
// could start). Let this task finish after snapd restart when preseed mode is off.
st.RequestRestart(state.StopDaemon)
}
return &state.Retry{Reason: "mark-preseeded will be marked done when snapd is executed in normal mode"}
}
// normal snapd run after snapd restart (not in preseed mode anymore)
st.Set("seed-restart-system-key", systemKey)
if err := m.setTimeOnce("seed-restart-time", startTime); err != nil {
return err
}
return nil
}
type seededSystem struct {
// System carries the recovery system label that was used to seed the
// current system
System string `json:"system"`
Model string `json:"model"`
// BrandID is the brand account ID
BrandID string `json:"brand-id"`
// Revision of the model assertion
Revision int `json:"revision"`
// Timestamp of model assertion
Timestamp time.Time `json:"timestamp"`
// SeedTime holds the timestamp when the system was seeded
SeedTime time.Time `json:"seed-time"`
}
func (s *seededSystem) sameAs(other *seededSystem) bool {
// in theory the system labels are unique, however be extra paranoid and
// check all model related fields too
return s.System == other.System &&
s.Model == other.Model &&
s.BrandID == other.BrandID &&
s.Revision == other.Revision
}
func (m *DeviceManager) recordSeededSystem(st *state.State, whatSeeded *seededSystem) error {
var seeded []seededSystem
if err := st.Get("seeded-systems", &seeded); err != nil && err != state.ErrNoState {
return err
}
for _, sys := range seeded {
if sys.sameAs(whatSeeded) {
return nil
}
}
// contrary to the usual approach of appending new entries to the list
// like we do with modeenv, the recently seeded system is added at the
// front, as it is not considered candidate like for the other entries,
// but rather it describes the currently existing
seeded = append([]seededSystem{*whatSeeded}, seeded...)
st.Set("seeded-systems", seeded)
return nil
}
func (m *DeviceManager) doMarkSeeded(t *state.Task, _ *tomb.Tomb) error {
st := t.State()
st.Lock()
defer st.Unlock()
if m.preseed {
return fmt.Errorf("internal error: mark-seeded task not expected in pre-seeding mode")
}
deviceCtx, err := DeviceCtx(st, t, nil)
if err != nil {
return fmt.Errorf("cannot get device context: %v", err)
}
if deviceCtx.HasModeenv() && deviceCtx.RunMode() {
modeEnv, err := maybeReadModeenv()
if err != nil {
return err
}
if modeEnv == nil {
return fmt.Errorf("missing modeenv, cannot proceed")
}
// unset recovery_system because that is only needed during install mode
modeEnv.RecoverySystem = ""
err = modeEnv.Write()
if err != nil {
return err
}
}
now := time.Now()
var whatSeeded *seededSystem
if err := t.Get("seed-system", &whatSeeded); err != nil && err != state.ErrNoState {
return err
}
if whatSeeded != nil && deviceCtx.RunMode() {
// record what seeded in the state only when in run mode
whatSeeded.SeedTime = now
if err := m.recordSeededSystem(st, whatSeeded); err != nil {
return fmt.Errorf("cannot record the seeded system: %v", err)
}
}
st.Set("seed-time", now)
st.Set("seeded", true)
// avoid possibly recording the same system multiple times etc.
t.SetStatus(state.DoneStatus)
// make sure we setup a fallback model/consider the next phase
// (registration) timely
st.EnsureBefore(0)
return nil
}