forked from snapcore/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configstate.go
208 lines (185 loc) · 5.87 KB
/
configstate.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 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 configstate implements the manager and state aspects responsible for
// the configuration of snaps.
package configstate
import (
"fmt"
"os"
"time"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/overlord/configstate/config"
"github.com/snapcore/snapd/overlord/configstate/configcore"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/hookstate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/sysconfig"
)
func init() {
snapstate.Configure = Configure
}
func ConfigureHookTimeout() time.Duration {
timeout := 5 * time.Minute
if s := os.Getenv("SNAPD_CONFIGURE_HOOK_TIMEOUT"); s != "" {
if to, err := time.ParseDuration(s); err == nil {
timeout = to
}
}
return timeout
}
func canConfigure(st *state.State, snapName string) error {
// the "core" snap/pseudonym can always be configured as it
// is handled internally
if snapName == "core" {
return nil
}
var snapst snapstate.SnapState
err := snapstate.Get(st, snapName, &snapst)
if err != nil && err != state.ErrNoState {
return err
}
if !snapst.IsInstalled() {
return &snap.NotInstalledError{Snap: snapName}
}
// the "snapd" snap cannot be configured yet
typ, err := snapst.Type()
if err != nil {
return err
}
if typ == snap.TypeSnapd {
return fmt.Errorf(`cannot configure the "snapd" snap, please use "system" instead`)
}
// bases cannot be configured for now
typ, err = snapst.Type()
if err != nil {
return err
}
if typ == snap.TypeBase {
return fmt.Errorf("cannot configure snap %q because it is of type 'base'", snapName)
}
return snapstate.CheckChangeConflict(st, snapName, nil)
}
// ConfigureInstalled returns a taskset to apply the given
// configuration patch for an installed snap. It returns
// snap.NotInstalledError if the snap is not installed.
func ConfigureInstalled(st *state.State, snapName string, patch map[string]interface{}, flags int) (*state.TaskSet, error) {
if err := canConfigure(st, snapName); err != nil {
return nil, err
}
taskset := Configure(st, snapName, patch, flags)
return taskset, nil
}
// Configure returns a taskset to apply the given configuration patch.
func Configure(st *state.State, snapName string, patch map[string]interface{}, flags int) *state.TaskSet {
summary := fmt.Sprintf(i18n.G("Run configure hook of %q snap"), snapName)
// regular configuration hook
hooksup := &hookstate.HookSetup{
Snap: snapName,
Hook: "configure",
Optional: len(patch) == 0,
IgnoreError: flags&snapstate.IgnoreHookError != 0,
TrackError: flags&snapstate.TrackHookError != 0,
// all configure hooks must finish within this timeout
Timeout: ConfigureHookTimeout(),
}
var contextData map[string]interface{}
if flags&snapstate.UseConfigDefaults != 0 {
contextData = map[string]interface{}{"use-defaults": true}
} else if len(patch) > 0 {
contextData = map[string]interface{}{"patch": patch}
}
if hooksup.Optional {
summary = fmt.Sprintf(i18n.G("Run configure hook of %q snap if present"), snapName)
}
task := hookstate.HookTask(st, summary, hooksup, contextData)
return state.NewTaskSet(task)
}
// RemapSnapFromRequest renames a snap as received from an API request
func RemapSnapFromRequest(snapName string) string {
if snapName == "system" {
return "core"
}
return snapName
}
// RemapSnapToResponse renames a snap as about to be sent from an API response
func RemapSnapToResponse(snapName string) string {
if snapName == "core" {
return "system"
}
return snapName
}
func delayedCrossMgrInit() {
devicestate.EarlyConfig = EarlyConfig
}
var (
configcoreExportExperimentalFlags = configcore.ExportExperimentalFlags
configcoreEarly = configcore.Early
)
// EarlyConfig performs any needed early configuration handling during
// managers' startup, it is exposed as a hook to devicestate for invocation.
// preloadGadget if set will be invoked if the system is not yet seeded
// or configured, it should either return ErrNoState, or return
// the gadget.Info for the to-be-seeded gadget and details about
// the model/device as sysconfig.Device.
func EarlyConfig(st *state.State, preloadGadget func() (sysconfig.Device, *gadget.Info, error)) error {
// already configured
configed, err := systemAlreadyConfigured(st)
if err != nil {
return err
}
tr := config.NewTransaction(st)
if configed {
if err := configcoreExportExperimentalFlags(tr); err != nil {
return fmt.Errorf("cannot export experimental config flags: %v", err)
}
return nil
}
if preloadGadget != nil {
dev, gi, err := preloadGadget()
if err != nil {
if err == state.ErrNoState {
// nothing to do
return nil
}
return err
}
values := gadget.SystemDefaults(gi.Defaults)
if err := configcoreEarly(dev, tr, values); err != nil {
return err
}
tr.Commit()
}
return nil
}
func systemAlreadyConfigured(st *state.State) (bool, error) {
var seeded bool
if err := st.Get("seeded", &seeded); err != nil && err != state.ErrNoState {
return false, err
}
if seeded {
return true, nil
}
cfg, err := config.GetSnapConfig(st, "core")
if cfg != nil {
return true, nil
}
return false, err
}