/
api_snaps.go
314 lines (268 loc) · 8.38 KB
/
api_snaps.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-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 (
"context"
"encoding/json"
"fmt"
"mime"
"net/http"
"strings"
"github.com/snapcore/snapd/i18n"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/auth"
"github.com/snapcore/snapd/overlord/servicestate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/progress"
"github.com/snapcore/snapd/strutil"
)
var (
// see daemon.go:canAccess for details how the access is controlled
snapsCmd = &Command{
Path: "/v2/snaps",
UserOK: true,
PolkitOK: "io.snapcraft.snapd.manage",
GET: getSnapsInfo,
POST: postSnaps,
}
)
func postSnaps(c *Command, r *http.Request, user *auth.UserState) Response {
contentType := r.Header.Get("Content-Type")
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return BadRequest("cannot parse content type: %v", err)
}
if mediaType == "application/json" {
charset := strings.ToUpper(params["charset"])
if charset != "" && charset != "UTF-8" {
return BadRequest("unknown charset in content type: %s", contentType)
}
return snapOpMany(c, r, user)
}
if !strings.HasPrefix(contentType, "multipart/") {
return BadRequest("unknown content type: %s", contentType)
}
return sideloadOrTrySnap(c, r.Body, params["boundary"], user)
}
func snapOpMany(c *Command, r *http.Request, user *auth.UserState) Response {
route := c.d.router.Get(stateChangeCmd.Path)
if route == nil {
return InternalError("cannot find route for change")
}
decoder := json.NewDecoder(r.Body)
var inst snapInstruction
if err := decoder.Decode(&inst); err != nil {
return BadRequest("cannot decode request body into snap instruction: %v", err)
}
// TODO: inst.Amend, etc?
if inst.Channel != "" || !inst.Revision.Unset() || inst.DevMode || inst.JailMode || inst.CohortKey != "" || inst.LeaveCohort || inst.Purge {
return BadRequest("unsupported option provided for multi-snap operation")
}
if err := inst.validate(); err != nil {
return BadRequest("%v", err)
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
if user != nil {
inst.userID = user.ID
}
op := inst.dispatchForMany()
if op == nil {
return BadRequest("unsupported multi-snap operation %q", inst.Action)
}
res, err := op(&inst, st)
if err != nil {
return inst.errToResponse(err)
}
var chg *state.Change
if len(res.Tasksets) == 0 {
chg = st.NewChange(inst.Action+"-snap", res.Summary)
chg.SetStatus(state.DoneStatus)
} else {
chg = newChange(st, inst.Action+"-snap", res.Summary, res.Tasksets, res.Affected)
ensureStateSoon(st)
}
chg.Set("api-data", map[string]interface{}{"snap-names": res.Affected})
return AsyncResponse(res.Result, &Meta{Change: chg.ID()})
}
type snapManyActionFunc func(*snapInstruction, *state.State) (*snapInstructionResult, error)
func (inst *snapInstruction) dispatchForMany() (op snapManyActionFunc) {
switch inst.Action {
case "refresh":
op = snapUpdateMany
case "install":
op = snapInstallMany
case "remove":
op = snapRemoveMany
case "snapshot":
// see api_snapshots.go
op = snapshotMany
}
return op
}
func snapInstallMany(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
for _, name := range inst.Snaps {
if len(name) == 0 {
return nil, fmt.Errorf(i18n.G("cannot install snap with empty name"))
}
}
installed, tasksets, err := snapstateInstallMany(st, inst.Snaps, inst.userID)
if err != nil {
return nil, err
}
var msg string
switch len(inst.Snaps) {
case 0:
return nil, fmt.Errorf("cannot install zero snaps")
case 1:
msg = fmt.Sprintf(i18n.G("Install snap %q"), inst.Snaps[0])
default:
quoted := strutil.Quoted(inst.Snaps)
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Install snaps %s"), quoted)
}
return &snapInstructionResult{
Summary: msg,
Affected: installed,
Tasksets: tasksets,
}, nil
}
func snapUpdateMany(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
// we need refreshed snap-declarations to enforce refresh-control as best as we can, this also ensures that snap-declarations and their prerequisite assertions are updated regularly
if err := assertstateRefreshSnapDeclarations(st, inst.userID); err != nil {
return nil, err
}
// TODO: use a per-request context
updated, tasksets, err := snapstateUpdateMany(context.TODO(), st, inst.Snaps, inst.userID, nil)
if err != nil {
return nil, err
}
var msg string
switch len(updated) {
case 0:
if len(inst.Snaps) != 0 {
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Refresh snaps %s: no updates"), strutil.Quoted(inst.Snaps))
} else {
msg = i18n.G("Refresh all snaps: no updates")
}
case 1:
msg = fmt.Sprintf(i18n.G("Refresh snap %q"), updated[0])
default:
quoted := strutil.Quoted(updated)
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Refresh snaps %s"), quoted)
}
return &snapInstructionResult{
Summary: msg,
Affected: updated,
Tasksets: tasksets,
}, nil
}
func snapRemoveMany(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
removed, tasksets, err := snapstateRemoveMany(st, inst.Snaps)
if err != nil {
return nil, err
}
var msg string
switch len(inst.Snaps) {
case 0:
return nil, fmt.Errorf("cannot remove zero snaps")
case 1:
msg = fmt.Sprintf(i18n.G("Remove snap %q"), inst.Snaps[0])
default:
quoted := strutil.Quoted(inst.Snaps)
// TRANSLATORS: the %s is a comma-separated list of quoted snap names
msg = fmt.Sprintf(i18n.G("Remove snaps %s"), quoted)
}
return &snapInstructionResult{
Summary: msg,
Affected: removed,
Tasksets: tasksets,
}, nil
}
// query many snaps
func getSnapsInfo(c *Command, r *http.Request, user *auth.UserState) Response {
if shouldSearchStore(r) {
logger.Noticef("Jumping to \"find\" to better support legacy request %q", r.URL)
return searchStore(c, r, user)
}
route := c.d.router.Get(snapCmd.Path)
if route == nil {
return InternalError("cannot find route for snaps")
}
query := r.URL.Query()
var all bool
sel := query.Get("select")
switch sel {
case "all":
all = true
case "enabled", "":
all = false
default:
return BadRequest("invalid select parameter: %q", sel)
}
var wanted map[string]bool
if ns := query.Get("snaps"); len(ns) > 0 {
nsl := strutil.CommaSeparatedList(ns)
wanted = make(map[string]bool, len(nsl))
for _, name := range nsl {
wanted[name] = true
}
}
found, err := allLocalSnapInfos(c.d.overlord.State(), all, wanted)
if err != nil {
return InternalError("cannot list local snaps! %v", err)
}
results := make([]*json.RawMessage, len(found))
sd := servicestate.NewStatusDecorator(progress.Null)
for i, x := range found {
name := x.info.InstanceName()
rev := x.info.Revision
url, err := route.URL("name", name)
if err != nil {
logger.Noticef("Cannot build URL for snap %q revision %s: %v", name, rev, err)
continue
}
data, err := json.Marshal(webify(mapLocal(x, sd), url.String()))
if err != nil {
return InternalError("cannot serialize snap %q revision %s: %v", name, rev, err)
}
raw := json.RawMessage(data)
results[i] = &raw
}
return SyncResponse(results, &Meta{Sources: []string{"local"}})
}
func shouldSearchStore(r *http.Request) bool {
// we should jump to the old behaviour iff q is given, or if
// sources is given and either empty or contains the word
// 'store'. Otherwise, local results only.
query := r.URL.Query()
if _, ok := query["q"]; ok {
logger.Debugf("use of obsolete \"q\" parameter: %q", r.URL)
return true
}
if src, ok := query["sources"]; ok {
logger.Debugf("use of obsolete \"sources\" parameter: %q", r.URL)
if len(src) == 0 || strings.Contains(src[0], "store") {
return true
}
}
return false
}