File: devicectx.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (149 lines) | stat: -rw-r--r-- 4,291 bytes parent folder | download | duplicates (3)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2018-2022 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 (
	"errors"

	"github.com/snapcore/snapd/asserts"
	"github.com/snapcore/snapd/overlord/snapstate"
	"github.com/snapcore/snapd/overlord/state"
)

// DeviceCtx picks a device context from state, optional task or an
// optionally pre-provided one. Returns ErrNoState if a model
// assertion is not yet known.
// In particular if task belongs to a remodeling change this will find
// the appropriate remodel context.
func DeviceCtx(st *state.State, task *state.Task, providedDeviceCtx snapstate.DeviceContext) (snapstate.DeviceContext, error) {
	if providedDeviceCtx != nil {
		return providedDeviceCtx, nil
	}
	// use the remodelContext if the task is part of a remodel change
	remodCtx, err := remodelCtxFromTask(task)
	if err == nil {
		return remodCtx, nil
	}
	if err != nil && !errors.Is(err, state.ErrNoState) {
		return nil, err
	}
	modelAs, err := findModel(st)
	if err != nil {
		return nil, err
	}

	devMgr := deviceMgr(st)
	return newModelDeviceContext(devMgr, modelAs), nil
}

type groundDeviceContext struct {
	model      *asserts.Model
	systemMode string
}

func (dc *groundDeviceContext) Model() *asserts.Model {
	return dc.model
}

func (dc *groundDeviceContext) GroundContext() snapstate.DeviceContext {
	return dc
}

func (dc *groundDeviceContext) Store() snapstate.StoreService {
	panic("retrieved ground context is not intended to drive store operations")
}

func (dc *groundDeviceContext) ForRemodeling() bool {
	return false
}

func (dc *groundDeviceContext) SystemMode() string {
	return dc.systemMode
}

// Classic returns true if the model is marked as a classic model.
//
// TODO: consider refactoring this, since this will return true in cases where
// we might not really consider the system to be a classic system. For example,
// this will return true when we're booting into recovery mode on a hybrid
// system. While the model is a classic model, nothing about the running system
// looks like a classic system.
func (dc groundDeviceContext) Classic() bool {
	return dc.model.Classic()
}

func (dc groundDeviceContext) Kernel() string {
	return dc.model.Kernel()
}

func (dc groundDeviceContext) Base() string {
	return dc.model.Base()
}

func (dc groundDeviceContext) Gadget() string {
	return dc.model.Gadget()
}

func (dc groundDeviceContext) RunMode() bool {
	return dc.systemMode == "run"
}

// HasModeenv is true if the grade is set
func (dc groundDeviceContext) HasModeenv() bool {
	return dc.model.Grade() != asserts.ModelGradeUnset
}

// IsCoreBoot is true if the model sports a kernel.
func (d *groundDeviceContext) IsCoreBoot() bool {
	return d.model.Kernel() != ""
}

// IsClassicBoot is true for classic systems with classic initramfs
// (there are no system modes in this case). If true, the kernel comes
// from debian packages.
func (d *groundDeviceContext) IsClassicBoot() bool {
	return !d.IsCoreBoot()
}

// expected interface is implemented
var _ snapstate.DeviceContext = &groundDeviceContext{}

type modelDeviceContext struct {
	groundDeviceContext
}

func newModelDeviceContext(devMgr *DeviceManager, modelAs *asserts.Model) *modelDeviceContext {
	return &modelDeviceContext{groundDeviceContext{
		model:      modelAs,
		systemMode: devMgr.SystemMode(SysAny),
	}}
}

func (dc *modelDeviceContext) Store() snapstate.StoreService {
	return nil
}

// expected interface is implemented
var _ snapstate.DeviceContext = &modelDeviceContext{}

// SystemModeInfoFromState returns details about the system mode the device is in.
func SystemModeInfoFromState(st *state.State) (*SystemModeInfo, error) {
	return deviceMgr(st).SystemModeInfo()
}