github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/devicestate/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 devicestate 21 22 import ( 23 "github.com/snapcore/snapd/asserts" 24 "github.com/snapcore/snapd/overlord/snapstate" 25 "github.com/snapcore/snapd/overlord/state" 26 ) 27 28 // DeviceCtx picks a device context from state, optional task or an 29 // optionally pre-provided one. Returns ErrNoState if a model 30 // assertion is not yet known. 31 // In particular if task belongs to a remodeling change this will find 32 // the appropriate remodel context. 33 func DeviceCtx(st *state.State, task *state.Task, providedDeviceCtx snapstate.DeviceContext) (snapstate.DeviceContext, error) { 34 if providedDeviceCtx != nil { 35 return providedDeviceCtx, nil 36 } 37 // use the remodelContext if the task is part of a remodel change 38 remodCtx, err := remodelCtxFromTask(task) 39 if err == nil { 40 return remodCtx, nil 41 } 42 if err != nil && err != state.ErrNoState { 43 return nil, err 44 } 45 modelAs, err := findModel(st) 46 if err != nil { 47 return nil, err 48 } 49 50 devMgr := deviceMgr(st) 51 return &modelDeviceContext{groundDeviceContext{ 52 model: modelAs, 53 systemMode: devMgr.SystemMode(), 54 }}, nil 55 } 56 57 type groundDeviceContext struct { 58 model *asserts.Model 59 systemMode string 60 } 61 62 func (dc *groundDeviceContext) Model() *asserts.Model { 63 return dc.model 64 } 65 66 func (dc *groundDeviceContext) GroundContext() snapstate.DeviceContext { 67 return dc 68 } 69 70 func (dc *groundDeviceContext) Store() snapstate.StoreService { 71 panic("retrieved ground context is not intended to drive store operations") 72 } 73 74 func (dc *groundDeviceContext) ForRemodeling() bool { 75 return false 76 } 77 78 func (dc *groundDeviceContext) SystemMode() string { 79 return dc.systemMode 80 } 81 82 func (dc groundDeviceContext) Classic() bool { 83 return dc.model.Classic() 84 } 85 86 func (dc groundDeviceContext) Kernel() string { 87 return dc.model.Kernel() 88 } 89 90 func (dc groundDeviceContext) Base() string { 91 return dc.model.Base() 92 } 93 94 func (dc groundDeviceContext) RunMode() bool { 95 return dc.systemMode == "run" 96 } 97 98 // HasModeenv is true if the grade is set 99 // TODO:UC20: will classic devices with uc20 models have a modeenv? I think so? 100 func (dc groundDeviceContext) HasModeenv() bool { 101 return dc.model.Grade() != asserts.ModelGradeUnset 102 } 103 104 // sanity 105 var _ snapstate.DeviceContext = &groundDeviceContext{} 106 107 type modelDeviceContext struct { 108 groundDeviceContext 109 } 110 111 func (dc *modelDeviceContext) Store() snapstate.StoreService { 112 return nil 113 } 114 115 // sanity 116 var _ snapstate.DeviceContext = &modelDeviceContext{}