forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_systems.go
246 lines (214 loc) · 7.42 KB
/
api_systems.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
// -*- 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 (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/gadget"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/devicestate"
"github.com/snapcore/snapd/snap"
)
var systemsCmd = &Command{
Path: "/v2/systems",
GET: getAllSystems,
ReadAccess: authenticatedAccess{},
// this is awkward, we want the postSystemsAction function to be used
// when the label is empty too, but the router will not handle the request
// for /v2/systems with the systemsActionCmd and instead handles it through
// this command, so we need to set the POST for this command to essentially
// forward to that one
POST: postSystemsAction,
WriteAccess: rootAccess{},
}
var systemsActionCmd = &Command{
Path: "/v2/systems/{label}",
GET: getSystemDetails,
ReadAccess: rootAccess{},
POST: postSystemsAction,
WriteAccess: rootAccess{},
}
type systemsResponse struct {
Systems []client.System `json:"systems,omitempty"`
}
func getAllSystems(c *Command, r *http.Request, user *auth.UserState) Response {
var rsp systemsResponse
seedSystems, err := c.d.overlord.DeviceManager().Systems()
if err != nil {
if err == devicestate.ErrNoSystems {
// no systems available
return SyncResponse(&rsp)
}
return InternalError(err.Error())
}
rsp.Systems = make([]client.System, 0, len(seedSystems))
for _, ss := range seedSystems {
// untangle the model
actions := make([]client.SystemAction, 0, len(ss.Actions))
for _, sa := range ss.Actions {
actions = append(actions, client.SystemAction{
Title: sa.Title,
Mode: sa.Mode,
})
}
rsp.Systems = append(rsp.Systems, client.System{
Current: ss.Current,
Label: ss.Label,
Model: client.SystemModelData{
Model: ss.Model.Model(),
BrandID: ss.Model.BrandID(),
DisplayName: ss.Model.DisplayName(),
},
Brand: snap.StoreAccount{
ID: ss.Brand.AccountID(),
Username: ss.Brand.Username(),
DisplayName: ss.Brand.DisplayName(),
Validation: ss.Brand.Validation(),
},
Actions: actions,
})
}
return SyncResponse(&rsp)
}
type oneSystemResponse struct {
// First part is designed to look like `client.System` - the
// only difference is how the model is represented
Current bool `json:"current,omitempty"`
Label string `json:"label,omitempty"`
Model map[string]interface{} `json:"model,omitempty"`
Actions []client.SystemAction `json:"actions,omitempty"`
Brand snap.StoreAccount `json:"brand"`
// Volumes contains the volumes defined from the gadget snap
Volumes map[string]*gadget.Volume `json:"volumes,omitempty"`
// TODO: add "storage-encryption" via the
// devicestate.EncryptionSupportInfo() here too
}
// wrapped for unit tests
var deviceManagerSystemAndGadgetInfo = func(dm *devicestate.DeviceManager, systemLabel string) (*devicestate.System, *gadget.Info, error) {
return dm.SystemAndGadgetInfo(systemLabel)
}
func getSystemDetails(c *Command, r *http.Request, user *auth.UserState) Response {
wantedSystemLabel := muxVars(r)["label"]
deviceMgr := c.d.overlord.DeviceManager()
sys, gadgetInfo, err := deviceManagerSystemAndGadgetInfo(deviceMgr, wantedSystemLabel)
if err != nil {
return InternalError(err.Error())
}
rsp := oneSystemResponse{
Current: sys.Current,
Label: sys.Label,
Brand: snap.StoreAccount{
ID: sys.Brand.AccountID(),
Username: sys.Brand.Username(),
DisplayName: sys.Brand.DisplayName(),
Validation: sys.Brand.Validation(),
},
// no body: we expect models to have empty bodies
Model: sys.Model.Headers(),
Volumes: gadgetInfo.Volumes,
}
for _, sa := range sys.Actions {
rsp.Actions = append(rsp.Actions, client.SystemAction{
Title: sa.Title,
Mode: sa.Mode,
})
}
return SyncResponse(rsp)
}
func postSystemsAction(c *Command, r *http.Request, user *auth.UserState) Response {
systemLabel := muxVars(r)["label"]
// XXX: is there a better way to partially decode?
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return BadRequest("cannot read request body: %v", err)
}
var act struct {
Action string `json:"action"`
}
if err := json.Unmarshal(body, &act); err != nil {
return BadRequest("cannot decode request body into system action: %v", err)
}
switch act.Action {
case "do":
var req client.SystemAction
if err := json.Unmarshal(body, &req); err != nil {
return BadRequest("cannot decode request body into system action: %v", err)
}
return postSystemActionDo(c, systemLabel, &req)
case "reboot":
var req client.SystemAction
if err := json.Unmarshal(body, &req); err != nil {
return BadRequest("cannot decode request body into system action: %v", err)
}
return postSystemActionReboot(c, systemLabel, &req)
case "install":
var req client.SystemActionInstall
if err := json.Unmarshal(body, &req); err != nil {
return BadRequest("cannot decode request body into system action: %v", err)
}
return postSystemActionInstall(c, systemLabel, &req)
default:
return BadRequest("unsupported action %q", act.Action)
}
}
// XXX: should deviceManager return more sensible errors here?
// E.g. UnsupportedActionError{systemLabel, mode}
// SystemDoesNotExistError{systemLabel}
func handleSystemActionErr(err error, systemLabel string) Response {
if os.IsNotExist(err) {
return NotFound("requested seed system %q does not exist", systemLabel)
}
if err == devicestate.ErrUnsupportedAction {
return BadRequest("requested action is not supported by system %q", systemLabel)
}
return InternalError(err.Error())
}
// wrapped for unit tests
var deviceManagerReboot = func(dm *devicestate.DeviceManager, systemLabel, mode string) error {
return dm.Reboot(systemLabel, mode)
}
func postSystemActionReboot(c *Command, systemLabel string, req *client.SystemAction) Response {
dm := c.d.overlord.DeviceManager()
if err := deviceManagerReboot(dm, systemLabel, req.Mode); err != nil {
return handleSystemActionErr(err, systemLabel)
}
return SyncResponse(nil)
}
func postSystemActionDo(c *Command, systemLabel string, req *client.SystemAction) Response {
if systemLabel == "" {
return BadRequest("system action requires the system label to be provided")
}
if req.Mode == "" {
return BadRequest("system action requires the mode to be provided")
}
sa := devicestate.SystemAction{
Title: req.Title,
Mode: req.Mode,
}
if err := c.d.overlord.DeviceManager().RequestSystemAction(systemLabel, sa); err != nil {
return handleSystemActionErr(err, systemLabel)
}
return SyncResponse(nil)
}
func postSystemActionInstall(c *Command, systemLabel string, req *client.SystemActionInstall) Response {
// TODO: call new devicestate.InstallStep()
return BadRequest("system action install is not implemented yet")
}