forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_debug_seeding.go
132 lines (111 loc) · 4.05 KB
/
api_debug_seeding.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2020 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 daemon
import (
"errors"
"time"
"github.com/snapcore/snapd/overlord/state"
)
type seedingInfo struct {
// Seeded is true when the device was seeded (same as "seeded" in the
// state).
Seeded bool `json:"seeded,omitempty"`
// Preseeded is true if snap-preseed was applied.
Preseeded bool `json:"preseeded,omitempty"`
// PreseedStartTime is when snap-preseed was started.
PreseedStartTime *time.Time `json:"preseed-start-time,omitempty"`
// PreseedTime is when snap-preseed work finished (i.e. before firstboot).
PreseedTime *time.Time `json:"preseed-time,omitempty"`
// SeedStartTime is when traditional seeding started (when not preseeding).
SeedStartTime *time.Time `json:"seed-start-time,omitempty"`
// SeedRestartTime is when seeding was started after first boot of preseeded
// image.
SeedRestartTime *time.Time `json:"seed-restart-time,omitempty"`
// SeedTime is when seeding finished.
SeedTime *time.Time `json:"seed-time,omitempty"`
// PreseedSystemKey is the system-key that was created during preseeding
// (when snap-preseed was ran).
PreseedSystemKey interface{} `json:"preseed-system-key,omitempty"`
// SeedRestartSystemKey is the system-key that was created on first boot of the
// preseeded image.
SeedRestartSystemKey interface{} `json:"seed-restart-system-key,omitempty"`
// SeedError is set if no seed change succeeded yet and at
// least one was in error. It is set to the error of the
// oldest known in error one.
SeedError string `json:"seed-error,omitempty"`
}
func getSeedingInfo(st *state.State) Response {
var seeded, preseeded bool
err := st.Get("seeded", &seeded)
if err != nil && !errors.Is(err, state.ErrNoState) {
return InternalError(err.Error())
}
if err = st.Get("preseeded", &preseeded); err != nil && !errors.Is(err, state.ErrNoState) {
return InternalError(err.Error())
}
var preseedSysKey, seedRestartSysKey interface{}
if err := st.Get("preseed-system-key", &preseedSysKey); err != nil && !errors.Is(err, state.ErrNoState) {
return InternalError(err.Error())
}
if err := st.Get("seed-restart-system-key", &seedRestartSysKey); err != nil && !errors.Is(err, state.ErrNoState) {
return InternalError(err.Error())
}
var seedError string
var seedErrorChangeTime time.Time
if !seeded {
for _, chg := range st.Changes() {
if chg.Kind() != "seed" && !chg.IsReady() {
continue
}
if err := chg.Err(); err != nil {
if seedErrorChangeTime.IsZero() || chg.SpawnTime().Before(seedErrorChangeTime) {
seedError = chg.Err().Error()
seedErrorChangeTime = chg.SpawnTime()
}
}
}
}
data := &seedingInfo{
Seeded: seeded,
SeedError: seedError,
Preseeded: preseeded,
PreseedSystemKey: preseedSysKey,
SeedRestartSystemKey: seedRestartSysKey,
}
for _, t := range []struct {
name string
destTm **time.Time
}{
{"preseed-start-time", &data.PreseedStartTime},
{"preseed-time", &data.PreseedTime},
{"seed-start-time", &data.SeedStartTime},
{"seed-restart-time", &data.SeedRestartTime},
{"seed-time", &data.SeedTime},
} {
var tm time.Time
if err := st.Get(t.name, &tm); err != nil && !errors.Is(err, state.ErrNoState) {
return InternalError(err.Error())
}
if !tm.IsZero() {
*t.destTm = &tm
}
}
// XXX: consistency & validity checks, e.g. if preseeded, then need to have
// preseed-start-time, preseeded-time, preseed-system-key etc?
return SyncResponse(data)
}