github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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 return modelDeviceContext{model: modelAs}, nil 50 } 51 52 type modelDeviceContext struct { 53 model *asserts.Model 54 } 55 56 // sanity 57 var _ snapstate.DeviceContext = modelDeviceContext{} 58 59 func (dc modelDeviceContext) Model() *asserts.Model { 60 return dc.model 61 } 62 63 func (dc modelDeviceContext) Store() snapstate.StoreService { 64 return nil 65 } 66 67 func (dc modelDeviceContext) ForRemodeling() bool { 68 return false 69 }