-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathapps.go
281 lines (247 loc) · 7.75 KB
/
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2016 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 client
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/snapcore/snapd/snap"
)
// AppActivator is a thing that activates the app that is a service in the
// system.
type AppActivator struct {
Name string
// Type describes the type of the unit, either timer or socket
Type string
Active bool
Enabled bool
}
// AppInfo describes a single snap application.
type AppInfo struct {
Snap string `json:"snap,omitempty"`
Name string `json:"name"`
DesktopFile string `json:"desktop-file,omitempty"`
Daemon string `json:"daemon,omitempty"`
DaemonScope snap.DaemonScope `json:"daemon-scope,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Active bool `json:"active,omitempty"`
CommonID string `json:"common-id,omitempty"`
Activators []AppActivator `json:"activators,omitempty"`
}
// IsService returns true if the application is a background daemon.
func (a *AppInfo) IsService() bool {
if a == nil {
return false
}
if a.Daemon == "" {
return false
}
return true
}
// AppOptions represent the options of the Apps call.
type AppOptions struct {
// If Service is true, only return apps that are services
// (app.IsService() is true); otherwise, return all.
Service bool
}
// Apps returns information about all matching apps. Each name can be
// either a snap or a snap.app. If names is empty, list all (that
// satisfy opts).
func (client *Client) Apps(names []string, opts AppOptions) ([]*AppInfo, error) {
q := make(url.Values)
if len(names) > 0 {
q.Add("names", strings.Join(names, ","))
}
if opts.Service {
q.Add("select", "service")
}
var appInfos []*AppInfo
_, err := client.doSync("GET", "/v2/apps", q, nil, nil, &appInfos)
return appInfos, err
}
// LogOptions represent the options of the Logs call.
type LogOptions struct {
N int // The maximum number of log lines to retrieve initially. If <0, no limit.
Follow bool // Whether to continue returning new lines as they appear
}
// A Log holds the information of a single syslog entry
type Log struct {
Timestamp time.Time `json:"timestamp"` // Timestamp of the event, in RFC3339 format to µs precision.
Message string `json:"message"` // The log message itself
SID string `json:"sid"` // The syslog identifier
PID string `json:"pid"` // The process identifier
}
// String will format the log entry with the timestamp in the local timezone
func (l Log) String() string {
return l.fmtLog(time.Local)
}
// StringInUTC will format the log entry with the timestamp in UTC
func (l Log) StringInUTC() string {
return l.fmtLog(time.UTC)
}
func (l Log) fmtLog(timezone *time.Location) string {
if timezone == nil {
timezone = time.Local
}
return fmt.Sprintf("%s %s[%s]: %s", l.Timestamp.In(timezone).Format(time.RFC3339), l.SID, l.PID, l.Message)
}
// Logs asks for the logs of a series of services, by name.
func (client *Client) Logs(names []string, opts LogOptions) (<-chan Log, error) {
query := url.Values{}
if len(names) > 0 {
query.Set("names", strings.Join(names, ","))
}
query.Set("n", strconv.Itoa(opts.N))
if opts.Follow {
query.Set("follow", strconv.FormatBool(opts.Follow))
}
rsp, err := client.raw(context.Background(), "GET", "/v2/logs", query, nil, nil)
if err != nil {
return nil, err
}
if rsp.StatusCode != 200 {
var r response
defer rsp.Body.Close()
if err := decodeInto(rsp.Body, &r); err != nil {
return nil, err
}
return nil, r.err(client, rsp.StatusCode)
}
ch := make(chan Log, 20)
go func() {
// logs come in application/json-seq, described in RFC7464: it's
// a series of <RS><arbitrary, valid JSON><LF>. Decoders are
// expected to skip invalid or truncated or empty records.
scanner := bufio.NewScanner(rsp.Body)
for scanner.Scan() {
buf := scanner.Bytes() // the scanner prunes the ending LF
if len(buf) < 1 {
// truncated record? skip
continue
}
idx := bytes.IndexByte(buf, 0x1E) // find the initial RS
if idx < 0 {
// no RS? skip
continue
}
buf = buf[idx+1:] // drop the initial RS
var log Log
if err := json.Unmarshal(buf, &log); err != nil {
// truncated/corrupted/binary record? skip
continue
}
ch <- log
}
close(ch)
rsp.Body.Close()
}()
return ch, nil
}
// ErrNoNames is returned by Start, Stop, or Restart, when the given
// list of things on which to operate is empty.
var ErrNoNames = errors.New(`"names" must not be empty`)
type appInstruction struct {
Action string `json:"action"`
Names []string `json:"names"`
StartOptions
StopOptions
RestartOptions
}
// StartOptions represent the different options of the Start call.
type StartOptions struct {
// Enable, as well as starting, the listed services. A
// disabled service does not start on boot.
Enable bool `json:"enable,omitempty"`
}
// Start services.
//
// It takes a list of names that can be snaps, of which all their
// services are started, or snap.service which are individual
// services to start; it shouldn't be empty.
func (client *Client) Start(names []string, opts StartOptions) (changeID string, err error) {
if len(names) == 0 {
return "", ErrNoNames
}
buf, err := json.Marshal(appInstruction{
Action: "start",
Names: names,
StartOptions: opts,
})
if err != nil {
return "", err
}
return client.doAsync("POST", "/v2/apps", nil, nil, bytes.NewReader(buf))
}
// StopOptions represent the different options of the Stop call.
type StopOptions struct {
// Disable, as well as stopping, the listed services. A
// service that is not disabled starts on boot.
Disable bool `json:"disable,omitempty"`
}
// Stop services.
//
// It takes a list of names that can be snaps, of which all their
// services are stopped, or snap.service which are individual
// services to stop; it shouldn't be empty.
func (client *Client) Stop(names []string, opts StopOptions) (changeID string, err error) {
if len(names) == 0 {
return "", ErrNoNames
}
buf, err := json.Marshal(appInstruction{
Action: "stop",
Names: names,
StopOptions: opts,
})
if err != nil {
return "", err
}
return client.doAsync("POST", "/v2/apps", nil, nil, bytes.NewReader(buf))
}
// RestartOptions represent the different options of the Restart call.
type RestartOptions struct {
// Reload the services, if possible (i.e. if the App has a
// ReloadCommand, invoque it), instead of restarting.
Reload bool `json:"reload,omitempty"`
}
// Restart services.
//
// It takes a list of names that can be snaps, of which all their
// services are restarted, or snap.service which are individual
// services to restart; it shouldn't be empty. If the service is not
// running, starts it.
func (client *Client) Restart(names []string, opts RestartOptions) (changeID string, err error) {
if len(names) == 0 {
return "", ErrNoNames
}
buf, err := json.Marshal(appInstruction{
Action: "restart",
Names: names,
RestartOptions: opts,
})
if err != nil {
return "", err
}
return client.doAsync("POST", "/v2/apps", nil, nil, bytes.NewReader(buf))
}