gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/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 newModelDeviceContext(devMgr, modelAs), nil
    52  }
    53  
    54  type groundDeviceContext struct {
    55  	model      *asserts.Model
    56  	systemMode string
    57  }
    58  
    59  func (dc *groundDeviceContext) Model() *asserts.Model {
    60  	return dc.model
    61  }
    62  
    63  func (dc *groundDeviceContext) GroundContext() snapstate.DeviceContext {
    64  	return dc
    65  }
    66  
    67  func (dc *groundDeviceContext) Store() snapstate.StoreService {
    68  	panic("retrieved ground context is not intended to drive store operations")
    69  }
    70  
    71  func (dc *groundDeviceContext) ForRemodeling() bool {
    72  	return false
    73  }
    74  
    75  func (dc *groundDeviceContext) SystemMode() string {
    76  	return dc.systemMode
    77  }
    78  
    79  func (dc groundDeviceContext) Classic() bool {
    80  	return dc.model.Classic()
    81  }
    82  
    83  func (dc groundDeviceContext) Kernel() string {
    84  	return dc.model.Kernel()
    85  }
    86  
    87  func (dc groundDeviceContext) Base() string {
    88  	return dc.model.Base()
    89  }
    90  
    91  func (dc groundDeviceContext) RunMode() bool {
    92  	return dc.systemMode == "run"
    93  }
    94  
    95  // HasModeenv is true if the grade is set
    96  // TODO:UC20: will classic devices with uc20 models have a modeenv? I think so?
    97  func (dc groundDeviceContext) HasModeenv() bool {
    98  	return dc.model.Grade() != asserts.ModelGradeUnset
    99  }
   100  
   101  // sanity
   102  var _ snapstate.DeviceContext = &groundDeviceContext{}
   103  
   104  type modelDeviceContext struct {
   105  	groundDeviceContext
   106  }
   107  
   108  func newModelDeviceContext(devMgr *DeviceManager, modelAs *asserts.Model) *modelDeviceContext {
   109  	return &modelDeviceContext{groundDeviceContext{
   110  		model:      modelAs,
   111  		systemMode: devMgr.SystemMode(SysAny),
   112  	}}
   113  }
   114  
   115  func (dc *modelDeviceContext) Store() snapstate.StoreService {
   116  	return nil
   117  }
   118  
   119  // sanity
   120  var _ snapstate.DeviceContext = &modelDeviceContext{}
   121  
   122  // SystemModeInfoFromState returns details about the system mode the device is in.
   123  func SystemModeInfoFromState(st *state.State) (*SystemModeInfo, error) {
   124  	return deviceMgr(st).SystemModeInfo()
   125  }