forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_apps.go
282 lines (247 loc) · 7.73 KB
/
api_apps.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
// -*- 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 (
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"github.com/snapcore/snapd/client/clientutil"
"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/snap"
"github.com/snapcore/snapd/strutil"
)
var (
appsCmd = &Command{
Path: "/v2/apps",
GET: getAppsInfo,
POST: postApps,
ReadAccess: openAccess{},
WriteAccess: authenticatedAccess{},
}
logsCmd = &Command{
Path: "/v2/logs",
GET: getLogs,
ReadAccess: authenticatedAccess{Polkit: polkitActionManage},
}
)
func getAppsInfo(c *Command, r *http.Request, user *auth.UserState) Response {
query := r.URL.Query()
opts := appInfoOptions{}
switch sel := query.Get("select"); sel {
case "":
// nothing to do
case "service":
opts.service = true
default:
return BadRequest("invalid select parameter: %q", sel)
}
appInfos, rspe := appInfosFor(c.d.overlord.State(), strutil.CommaSeparatedList(query.Get("names")), opts)
if rspe != nil {
return rspe
}
sd := servicestate.NewStatusDecorator(progress.Null)
clientAppInfos, err := clientutil.ClientAppInfosFromSnapAppInfos(appInfos, sd)
if err != nil {
return InternalError("%v", err)
}
return SyncResponse(clientAppInfos)
}
type appInfoOptions struct {
service bool
}
func (opts appInfoOptions) String() string {
if opts.service {
return "service"
}
return "app"
}
// appInfosFor returns a sorted list apps described by names.
//
// - If names is empty, returns all apps of the wanted kinds (which
// could be an empty list).
// - An element of names can be a snap name, in which case all apps
// from the snap of the wanted kind are included in the result (and
// it's an error if the snap has no apps of the wanted kind).
// - An element of names can instead be snap.app, in which case that app is
// included in the result (and it's an error if the snap and app don't
// both exist, or if the app is not a wanted kind)
//
// On error an appropriate *apiError is returned; a nil *apiError means
// no error.
//
// It's a programming error to call this with wanted having neither
// services nor commands set.
func appInfosFor(st *state.State, names []string, opts appInfoOptions) ([]*snap.AppInfo, *apiError) {
snapNames := make(map[string]bool)
requested := make(map[string]bool)
for _, name := range names {
requested[name] = true
name, _ = splitAppName(name)
snapNames[name] = true
}
snaps, err := allLocalSnapInfos(st, false, snapNames)
if err != nil {
return nil, InternalError("cannot list local snaps! %v", err)
}
found := make(map[string]bool)
appInfos := make([]*snap.AppInfo, 0, len(requested))
for _, snp := range snaps {
snapName := snp.info.InstanceName()
apps := make([]*snap.AppInfo, 0, len(snp.info.Apps))
for _, app := range snp.info.Apps {
if !opts.service || app.IsService() {
apps = append(apps, app)
}
}
if len(apps) == 0 && requested[snapName] {
return nil, AppNotFound("snap %q has no %ss", snapName, opts)
}
includeAll := len(requested) == 0 || requested[snapName]
if includeAll {
// want all services in a snap
found[snapName] = true
}
for _, app := range apps {
appName := snapName + "." + app.Name
if includeAll || requested[appName] {
appInfos = append(appInfos, app)
found[appName] = true
}
}
}
for k := range requested {
if !found[k] {
if snapNames[k] {
return nil, SnapNotFound(k, fmt.Errorf("snap %q not found", k))
} else {
snap, app := splitAppName(k)
return nil, AppNotFound("snap %q has no %s %q", snap, opts, app)
}
}
}
sort.Sort(snap.AppInfoBySnapApp(appInfos))
return appInfos, nil
}
// this differs from snap.SplitSnapApp in the handling of the
// snap-only case:
//
// snap.SplitSnapApp("foo") is ("foo", "foo"),
// splitAppName("foo") is ("foo", "").
func splitAppName(s string) (snap, app string) {
if idx := strings.IndexByte(s, '.'); idx > -1 {
return s[:idx], s[idx+1:]
}
return s, ""
}
func getLogs(c *Command, r *http.Request, user *auth.UserState) Response {
query := r.URL.Query()
n := 10
if s := query.Get("n"); s != "" {
m, err := strconv.ParseInt(s, 0, 32)
if err != nil {
return BadRequest(`invalid value for n: %q: %v`, s, err)
}
n = int(m)
}
follow := false
if s := query.Get("follow"); s != "" {
f, err := strconv.ParseBool(s)
if err != nil {
return BadRequest(`invalid value for follow: %q: %v`, s, err)
}
follow = f
}
// only services have logs for now
opts := appInfoOptions{service: true}
appInfos, rspe := appInfosFor(c.d.overlord.State(), strutil.CommaSeparatedList(query.Get("names")), opts)
if rspe != nil {
return rspe
}
if len(appInfos) == 0 {
return AppNotFound("no matching services")
}
reader, err := servicestate.LogReader(appInfos, n, follow)
if err != nil {
return InternalError("cannot get logs: %v", err)
}
return &journalLineReaderSeqResponse{
ReadCloser: reader,
follow: follow,
}
}
var servicestateControl = servicestate.Control
func postApps(c *Command, r *http.Request, user *auth.UserState) Response {
var inst servicestate.Instruction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&inst); err != nil {
return BadRequest("cannot decode request body into service operation: %v", err)
}
// XXX: decoder.More()
if len(inst.Names) == 0 {
// on POST, don't allow empty to mean all
return BadRequest("cannot perform operation on services without a list of services to operate on")
}
st := c.d.overlord.State()
appInfos, rspe := appInfosFor(st, inst.Names, appInfoOptions{service: true})
if rspe != nil {
return rspe
}
if len(appInfos) == 0 {
// can't happen: appInfosFor with a non-empty list of services
// shouldn't ever return an empty appInfos with no error response
return InternalError("no services found")
}
// do not pass flags - only create service-control tasks, do not create
// exec-command tasks for old snapd. These are not needed since we are
// handling momentary snap service commands.
st.Lock()
defer st.Unlock()
tss, err := servicestateControl(st, appInfos, &inst, nil, nil)
if err != nil {
// TODO: use errToResponse here too and introduce a proper error kind ?
if _, ok := err.(servicestate.ServiceActionConflictError); ok {
return Conflict(err.Error())
}
return BadRequest(err.Error())
}
// names received in the request can be snap or snap.app, we need to
// extract the actual snap names before associating them with a change
chg := newChange(st, "service-control", "Running service command", tss, namesToSnapNames(&inst))
st.EnsureBefore(0)
return AsyncResponse(nil, chg.ID())
}
func namesToSnapNames(inst *servicestate.Instruction) []string {
seen := make(map[string]struct{}, len(inst.Names))
for _, snapOrSnapDotApp := range inst.Names {
snapName, _ := snap.SplitSnapApp(snapOrSnapDotApp)
seen[snapName] = struct{}{}
}
names := make([]string, 0, len(seen))
for k := range seen {
names = append(names, k)
}
// keep stable ordering
sort.Strings(names)
return names
}