forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_debug.go
417 lines (372 loc) · 12.2 KB
/
api_debug.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2023 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 (
"encoding/json"
"fmt"
"net/http"
"sort"
"time"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/osutil/disks"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/timings"
)
var debugCmd = &Command{
Path: "/v2/debug",
GET: getDebug,
POST: postDebug,
ReadAccess: openAccess{},
WriteAccess: rootAccess{},
}
type debugAction struct {
Action string `json:"action"`
Message string `json:"message"`
Params struct {
ChgID string `json:"chg-id"`
RecoverySystemLabel string `json:"recovery-system-label"`
} `json:"params"`
Snaps []string `json:"snaps"`
}
type connectivityStatus struct {
Connectivity bool `json:"connectivity"`
Unreachable []string `json:"unreachable,omitempty"`
}
func getBaseDeclaration(st *state.State) Response {
bd, err := assertstate.BaseDeclaration(st)
if err != nil {
return InternalError("cannot get base declaration: %s", err)
}
return SyncResponse(map[string]interface{}{
"base-declaration": string(asserts.Encode(bd)),
})
}
func checkConnectivity(st *state.State) Response {
theStore := snapstate.Store(st, nil)
st.Unlock()
defer st.Lock()
checkResult, err := theStore.ConnectivityCheck()
if err != nil {
return InternalError("cannot run connectivity check: %v", err)
}
status := connectivityStatus{Connectivity: true}
for host, reachable := range checkResult {
if !reachable {
status.Connectivity = false
status.Unreachable = append(status.Unreachable, host)
}
}
sort.Strings(status.Unreachable)
return SyncResponse(status)
}
type changeTimings struct {
Status string `json:"status,omitempty"`
Kind string `json:"kind,omitempty"`
Summary string `json:"summary,omitempty"`
Lane int `json:"lane,omitempty"`
ReadyTime time.Time `json:"ready-time,omitempty"`
DoingTime time.Duration `json:"doing-time,omitempty"`
UndoingTime time.Duration `json:"undoing-time,omitempty"`
DoingTimings []*timings.TimingJSON `json:"doing-timings,omitempty"`
UndoingTimings []*timings.TimingJSON `json:"undoing-timings,omitempty"`
}
type debugTimings struct {
ChangeID string `json:"change-id"`
// total duration of the activity - present for ensure and startup timings only
TotalDuration time.Duration `json:"total-duration,omitempty"`
EnsureTimings []*timings.TimingJSON `json:"ensure-timings,omitempty"`
StartupTimings []*timings.TimingJSON `json:"startup-timings,omitempty"`
// ChangeTimings are indexed by task id
ChangeTimings map[string]*changeTimings `json:"change-timings,omitempty"`
}
// minLane determines the lowest lane number for the task
func minLane(t *state.Task) int {
lanes := t.Lanes()
minLane := lanes[0]
for _, l := range lanes[1:] {
if l < minLane {
minLane = l
}
}
return minLane
}
func collectChangeTimings(st *state.State, changeID string) (map[string]*changeTimings, error) {
chg := st.Change(changeID)
if chg == nil {
return nil, fmt.Errorf("cannot find change: %v", changeID)
}
// collect "timings" for tasks of given change
stateTimings, err := timings.Get(st, -1, func(tags map[string]string) bool { return tags["change-id"] == changeID })
if err != nil {
return nil, fmt.Errorf("cannot get timings of change %s: %v", changeID, err)
}
doingTimingsByTask := make(map[string][]*timings.TimingJSON)
undoingTimingsByTask := make(map[string][]*timings.TimingJSON)
for _, tm := range stateTimings {
taskID := tm.Tags["task-id"]
if status, ok := tm.Tags["task-status"]; ok {
switch {
case status == state.DoingStatus.String():
doingTimingsByTask[taskID] = tm.NestedTimings
case status == state.UndoingStatus.String():
undoingTimingsByTask[taskID] = tm.NestedTimings
default:
return nil, fmt.Errorf("unexpected task status %q for timing of task %s", status, taskID)
}
}
}
m := map[string]*changeTimings{}
for _, t := range chg.Tasks() {
m[t.ID()] = &changeTimings{
Kind: t.Kind(),
Status: t.Status().String(),
Summary: t.Summary(),
Lane: minLane(t),
ReadyTime: t.ReadyTime(),
DoingTime: t.DoingTime(),
UndoingTime: t.UndoingTime(),
DoingTimings: doingTimingsByTask[t.ID()],
UndoingTimings: undoingTimingsByTask[t.ID()],
}
}
return m, nil
}
func collectEnsureTimings(st *state.State, ensureTag string, allEnsures bool) ([]*debugTimings, error) {
ensures, err := timings.Get(st, -1, func(tags map[string]string) bool {
return tags["ensure"] == ensureTag
})
if err != nil {
return nil, fmt.Errorf("cannot get timings of ensure %s: %v", ensureTag, err)
}
if len(ensures) == 0 {
return nil, fmt.Errorf("cannot find ensure: %v", ensureTag)
}
// If allEnsures is true, then report all activities of given ensure, otherwise just the latest
first := len(ensures) - 1
if allEnsures {
first = 0
}
var responseData []*debugTimings
var changeTimings map[string]*changeTimings
for _, ensureTm := range ensures[first:] {
ensureChangeID := ensureTm.Tags["change-id"]
// change is optional for ensure timings
if ensureChangeID != "" {
// ignore an error when getting a change, it may no longer be present in the state
changeTimings, _ = collectChangeTimings(st, ensureChangeID)
}
debugTm := &debugTimings{
ChangeID: ensureChangeID,
ChangeTimings: changeTimings,
EnsureTimings: ensureTm.NestedTimings,
TotalDuration: ensureTm.Duration,
}
responseData = append(responseData, debugTm)
}
return responseData, nil
}
func collectStartupTimings(st *state.State, startupTag string, allStarts bool) ([]*debugTimings, error) {
starts, err := timings.Get(st, -1, func(tags map[string]string) bool {
return tags["startup"] == startupTag
})
if err != nil {
return nil, fmt.Errorf("cannot get timings of startup %s: %v", startupTag, err)
}
if len(starts) == 0 {
return nil, fmt.Errorf("cannot find startup: %v", startupTag)
}
// If allStarts is true, then report all activities of given startup, otherwise just the latest
first := len(starts) - 1
if allStarts {
first = 0
}
var responseData []*debugTimings
for _, startTm := range starts[first:] {
debugTm := &debugTimings{
StartupTimings: startTm.NestedTimings,
TotalDuration: startTm.Duration,
}
responseData = append(responseData, debugTm)
}
return responseData, nil
}
func getChangeTimings(st *state.State, changeID, ensureTag, startupTag string, all bool) Response {
// If ensure tag was passed by the client, find its related changes;
// we can have many ensure executions and their changes in the responseData array.
if ensureTag != "" {
responseData, err := collectEnsureTimings(st, ensureTag, all)
if err != nil {
return BadRequest(err.Error())
}
return SyncResponse(responseData)
}
if startupTag != "" {
responseData, err := collectStartupTimings(st, startupTag, all)
if err != nil {
return BadRequest(err.Error())
}
return SyncResponse(responseData)
}
// timings for single change ID
changeTimings, err := collectChangeTimings(st, changeID)
if err != nil {
return BadRequest(err.Error())
}
responseData := []*debugTimings{
{
ChangeID: changeID,
ChangeTimings: changeTimings,
},
}
return SyncResponse(responseData)
}
func getGadgetDiskMapping(st *state.State) Response {
deviceCtx, err := devicestate.DeviceCtx(st, nil, nil)
if err != nil {
return InternalError("cannot get device context: %v", err)
}
gadgetInfo, err := snapstate.GadgetInfo(st, deviceCtx)
if err != nil {
return InternalError("cannot get gadget info: %v", err)
}
gadgetDir := gadgetInfo.MountDir()
mod := deviceCtx.Model()
info, err := gadget.ReadInfoAndValidate(gadgetDir, mod, nil)
if err != nil {
return InternalError("cannot get all disk volume device traits: cannot read gadget: %v", err)
}
// TODO: allow passing in encrypted options info here
// allow implicit system-data on pre-uc20 only
optsMap := map[string]*gadget.DiskVolumeValidationOptions{}
for vol := range info.Volumes {
optsMap[vol] = &gadget.DiskVolumeValidationOptions{
AllowImplicitSystemData: mod.Grade() == asserts.ModelGradeUnset,
}
}
res, err := gadget.AllDiskVolumeDeviceTraits(info.Volumes, optsMap)
if err != nil {
return InternalError("cannot get all disk volume device traits: %v", err)
}
return SyncResponse(res)
}
func getDisks(st *state.State) Response {
disks, err := disks.AllPhysicalDisks()
if err != nil {
return InternalError("cannot get all physical disks: %v", err)
}
vols := make([]*gadget.OnDiskVolume, 0, len(disks))
for _, d := range disks {
vol, err := gadget.OnDiskVolumeFromDisk(d)
if err != nil {
return InternalError("cannot get on disk volume for device %s: %v", d.KernelDeviceNode(), err)
}
vols = append(vols, vol)
}
return SyncResponse(vols)
}
func createRecovery(st *state.State, label string) Response {
if label == "" {
return BadRequest("cannot create a recovery system with no label")
}
chg, err := devicestate.CreateRecoverySystem(st, label, devicestate.CreateRecoverySystemOptions{
TestSystem: true,
})
if err != nil {
return InternalError("cannot create recovery system %q: %v", label, err)
}
ensureStateSoon(st)
return AsyncResponse(nil, chg.ID())
}
func getDebug(c *Command, r *http.Request, user *auth.UserState) Response {
query := r.URL.Query()
aspect := query.Get("aspect")
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
switch aspect {
case "base-declaration":
return getBaseDeclaration(st)
case "connectivity":
return checkConnectivity(st)
case "model":
model, err := c.d.overlord.DeviceManager().Model()
if err != nil {
return InternalError("cannot get model: %v", err)
}
return SyncResponse(map[string]interface{}{
"model": string(asserts.Encode(model)),
})
case "change-timings":
chgID := query.Get("change-id")
ensureTag := query.Get("ensure")
startupTag := query.Get("startup")
all := query.Get("all")
return getChangeTimings(st, chgID, ensureTag, startupTag, all == "true")
case "seeding":
return getSeedingInfo(st)
case "gadget-disk-mapping":
return getGadgetDiskMapping(st)
case "disks":
return getDisks(st)
default:
return BadRequest("unknown debug aspect %q", aspect)
}
}
func postDebug(c *Command, r *http.Request, user *auth.UserState) Response {
var a debugAction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&a); err != nil {
return BadRequest("cannot decode request body into a debug action: %v", err)
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
switch a.Action {
case "add-warning":
st.Warnf("%v", a.Message)
return SyncResponse(true)
case "unshow-warnings":
st.UnshowAllWarnings()
return SyncResponse(true)
case "ensure-state-soon":
ensureStateSoon(st)
return SyncResponse(true)
case "can-manage-refreshes":
return SyncResponse(devicestate.CanManageRefreshes(st))
case "prune":
opTime, err := c.d.overlord.DeviceManager().StartOfOperationTime()
if err != nil {
return BadRequest("cannot get start of operation time: %s", err)
}
st.Prune(opTime, 0, 0, 0)
return SyncResponse(true)
case "stacktraces":
return getStacktraces()
case "create-recovery-system":
return createRecovery(st, a.Params.RecoverySystemLabel)
case "migrate-home":
return migrateHome(st, a.Snaps)
default:
return BadRequest("unknown debug action: %v", a.Action)
}
}