gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/snapstate/devicectx.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018-2019 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package snapstate 21 22 import ( 23 "github.com/snapcore/snapd/asserts" 24 "github.com/snapcore/snapd/boot" 25 "github.com/snapcore/snapd/overlord/state" 26 ) 27 28 // A DeviceContext provides for operating as a given device and with 29 // its brand store either for normal operation or over a remodeling. 30 type DeviceContext interface { 31 // GroundContext returns a context corresponding to the 32 // original model of the device for a remodel, or a context 33 // equivalent to this one otherwise, except in both cases 34 // Store cannot be used and must panic. 35 GroundContext() DeviceContext 36 37 // Store returns the store service to use under this context or nil if the snapstate store is appropriate. 38 Store() StoreService 39 40 // ForRemodeling returns whether this context is for use over a remodeling. 41 ForRemodeling() bool 42 43 // SystemMode returns the system mode (run,install,recover,...). 44 SystemMode() string 45 46 // DeviceContext should be usable as boot.Device 47 boot.Device 48 } 49 50 // Hook setup by devicestate to pick a device context from state, 51 // optional task or an optionally pre-provided one. It's expected to 52 // return ErrNoState if a model assertion is not yet known. 53 var ( 54 DeviceCtx func(st *state.State, task *state.Task, providedDeviceCtx DeviceContext) (DeviceContext, error) 55 ) 56 57 // Hook setup by devicestate to know whether a remodeling is in progress. 58 var ( 59 Remodeling func(st *state.State) bool 60 ) 61 62 // ModelFromTask returns a model assertion through the device context for the task. 63 func ModelFromTask(task *state.Task) (*asserts.Model, error) { 64 deviceCtx, err := DeviceCtx(task.State(), task, nil) 65 if err != nil { 66 return nil, err 67 } 68 return deviceCtx.Model(), nil 69 } 70 71 // DevicePastSeeding returns a device context if a model assertion is 72 // available and the device is seeded, at that point the device store 73 // is known and seeding done. Otherwise it returns a 74 // ChangeConflictError about being too early unless a pre-provided 75 // DeviceContext is passed in. It will again return a conflict error 76 // during remodeling unless the providedDeviceCtx is for it. 77 func DevicePastSeeding(st *state.State, providedDeviceCtx DeviceContext) (DeviceContext, error) { 78 var seeded bool 79 err := st.Get("seeded", &seeded) 80 if err != nil && err != state.ErrNoState { 81 return nil, err 82 } 83 if Remodeling(st) { 84 // a remodeling is in progress and this is not called 85 // as part of it. The 2nd check should not be needed 86 // in practice. 87 if providedDeviceCtx == nil || !providedDeviceCtx.ForRemodeling() { 88 return nil, &ChangeConflictError{ 89 Message: "remodeling in progress, no other " + 90 "changes allowed until this is done", 91 ChangeKind: "remodel", 92 } 93 } 94 } 95 devCtx, err := DeviceCtx(st, nil, providedDeviceCtx) 96 if err != nil && err != state.ErrNoState { 97 return nil, err 98 } 99 // when seeded devCtx should not be nil except in the rare 100 // case of upgrades from a snapd before the introduction of 101 // the fallback generic/generic-classic model 102 if !seeded || devCtx == nil { 103 return nil, &ChangeConflictError{ 104 Message: "too early for operation, device not yet" + 105 " seeded or device model not acknowledged", 106 ChangeKind: "seed", 107 } 108 } 109 110 return devCtx, nil 111 } 112 113 // DeviceCtxFromState returns a device context if a model assertion is 114 // available. Otherwise it returns a ChangeConflictError about being 115 // too early unless an pre-provided DeviceContext is passed in. 116 func DeviceCtxFromState(st *state.State, providedDeviceCtx DeviceContext) (DeviceContext, error) { 117 deviceCtx, err := DeviceCtx(st, nil, providedDeviceCtx) 118 if err != nil { 119 if err == state.ErrNoState { 120 return nil, &ChangeConflictError{ 121 Message: "too early for operation, device model not yet acknowledged", 122 ChangeKind: "seed", 123 } 124 } 125 return nil, err 126 } 127 return deviceCtx, nil 128 }