forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_remodel.go
221 lines (192 loc) · 5.79 KB
/
handlers_remodel.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016-2017 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"
"gopkg.in/tomb.v2"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/naming"
)
func isSameAssertsRevision(err error) bool {
if e, ok := err.(*asserts.RevisionError); ok {
if e.Used == e.Current {
return true
}
}
return false
}
var injectedSetModelError error
// InjectSetModelError will trigger the selected error in the doSetModel
// handler. This is only useful for testing.
func InjectSetModelError(err error) {
injectedSetModelError = err
}
func (m *DeviceManager) doSetModel(t *state.Task, _ *tomb.Tomb) (err error) {
st := t.State()
st.Lock()
defer st.Unlock()
remodCtx, err := remodelCtxFromTask(t)
if err != nil {
return err
}
new := remodCtx.Model()
// unmark no-longer required snaps
var cleanedRequiredSnaps []string
requiredSnaps := getAllRequiredSnapsForModel(new)
// TODO|XXX: have AllByRef
snapStates, err := snapstate.All(st)
if err != nil {
return err
}
for snapName, snapst := range snapStates {
// TODO: remove this type restriction once we remodel
// gadgets and add tests that ensure
// that the required flag is properly set/unset
typ, err := snapst.Type()
if err != nil {
return err
}
if typ != snap.TypeApp && typ != snap.TypeBase && typ != snap.TypeKernel {
continue
}
// clean required flag if no-longer needed
if snapst.Flags.Required && !requiredSnaps.Contains(naming.Snap(snapName)) {
snapst.Flags.Required = false
snapstate.Set(st, snapName, snapst)
cleanedRequiredSnaps = append(cleanedRequiredSnaps, snapName)
}
// TODO: clean "required" flag of "core" if a remodel
// moves from the "core" snap to a different
// bootable base snap.
}
// ensure we undo the cleanedRequiredSnaps if e.g. remodCtx
defer func() {
if err == nil {
return
}
var snapst snapstate.SnapState
for _, snapName := range cleanedRequiredSnaps {
if err := snapstate.Get(st, snapName, &snapst); err == nil {
snapst.Flags.Required = true
snapstate.Set(st, snapName, &snapst)
}
}
}()
// only useful for testing
if injectedSetModelError != nil {
return injectedSetModelError
}
// add the assertion only after everything else was successful
err = assertstate.Add(st, new)
if err != nil && !isSameAssertsRevision(err) {
return err
}
if new.Grade() != asserts.ModelGradeUnset {
// TODO: update ubuntu-boot/device/model
// TODO: reseal for both the old and current model
}
// and finish (this will set the new model)
return remodCtx.Finish()
}
func (m *DeviceManager) cleanupRemodel(t *state.Task, _ *tomb.Tomb) error {
st := t.State()
st.Lock()
defer st.Unlock()
// cleanup the cached remodel context
cleanupRemodelCtx(t.Change())
return nil
}
func (m *DeviceManager) doPrepareRemodeling(t *state.Task, tmb *tomb.Tomb) error {
st := t.State()
st.Lock()
defer st.Unlock()
remodCtx, err := remodelCtxFromTask(t)
if err != nil {
return err
}
current, err := findModel(st)
if err != nil {
return err
}
sto := remodCtx.Store()
if sto == nil {
return fmt.Errorf("internal error: re-registration remodeling should have built a store")
}
// ensure a new session accounting for the new brand/model
st.Unlock()
_, err = sto.EnsureDeviceSession()
st.Lock()
if err != nil {
return fmt.Errorf("cannot get a store session based on the new model assertion: %v", err)
}
chgID := t.Change().ID()
tss, err := remodelTasks(tmb.Context(nil), st, current, remodCtx.Model(), remodCtx, chgID)
if err != nil {
return err
}
allTs := state.NewTaskSet()
for _, ts := range tss {
allTs.AddAll(ts)
}
snapstate.InjectTasks(t, allTs)
st.EnsureBefore(0)
t.SetStatus(state.DoneStatus)
return nil
}
var (
gadgetIsCompatible = gadget.IsCompatible
)
func checkGadgetRemodelCompatible(st *state.State, snapInfo, curInfo *snap.Info, snapf snap.Container, flags snapstate.Flags, deviceCtx snapstate.DeviceContext) error {
if release.OnClassic {
return nil
}
if snapInfo.Type() != snap.TypeGadget {
// We are only interested in gadget snaps.
return nil
}
if deviceCtx == nil || !deviceCtx.ForRemodeling() {
// We are only interesting in a remodeling scenario.
return nil
}
if curInfo == nil {
// snap isn't installed yet, we are likely remodeling to a new
// gadget, identify the old gadget
curInfo, _ = snapstate.GadgetInfo(st, deviceCtx.GroundContext())
}
if curInfo == nil {
return fmt.Errorf("cannot identify the current gadget snap")
}
pendingInfo, err := gadget.ReadInfoFromSnapFile(snapf, deviceCtx.Model())
if err != nil {
return fmt.Errorf("cannot read new gadget metadata: %v", err)
}
currentData, err := gadgetDataFromInfo(curInfo, deviceCtx.GroundContext().Model())
if err != nil {
return fmt.Errorf("cannot read current gadget metadata: %v", err)
}
if err := gadgetIsCompatible(currentData.Info, pendingInfo); err != nil {
return fmt.Errorf("cannot remodel to an incompatible gadget: %v", err)
}
return nil
}