forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
259 lines (221 loc) · 6.69 KB
/
service.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 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 snappy
import (
"fmt"
"path/filepath"
"time"
"github.com/ubuntu-core/snappy/dirs"
"github.com/ubuntu-core/snappy/i18n"
"github.com/ubuntu-core/snappy/progress"
"github.com/ubuntu-core/snappy/systemd"
)
// A ServiceActor collects the services found by FindServices and lets
// you perform differnt actions (start, stop, etc) on them.
type ServiceActor interface {
Enable() error
Disable() error
Start() error
Stop() error
Restart() error
Status() ([]string, error)
ServiceStatus() ([]*PackageServiceStatus, error)
Logs() ([]systemd.Log, error)
Loglines() ([]string, error)
}
type svcT struct {
m *packageYaml
svc *ServiceYaml
}
type serviceActor struct {
svcs []*svcT
pb progress.Meter
sysd systemd.Systemd
}
// FindServices finds all matching services (empty string matches all)
// and lets you perform different actions (start, stop, etc) on them.
//
// If a snap is specified and no matching snaps are found,
// ErrPackageNotFound is returned. If a snap is specified and the
// matching snaps has no matching services, ErrServiceNotFound is
// returned.
//
// If no snap is specified, an empty result is not an error.
func FindServices(snapName string, serviceName string, pb progress.Meter) (ServiceActor, error) {
var svcs []*svcT
repo := NewMetaLocalRepository()
installed, _ := repo.Installed()
foundSnap := false
for _, part := range installed {
if !part.IsActive() {
continue
}
snap, ok := part.(*SnapPart)
if !ok {
// can't happen
continue
}
if snapName != "" && snapName != snap.Name() {
continue
}
foundSnap = true
yamls := snap.ServiceYamls()
for i := range yamls {
if serviceName != "" && serviceName != yamls[i].Name {
continue
}
s := &svcT{
m: snap.m,
svc: &yamls[i],
}
svcs = append(svcs, s)
}
}
if snapName != "" {
if !foundSnap {
return nil, ErrPackageNotFound
}
if len(svcs) == 0 {
return nil, ErrServiceNotFound
}
}
return &serviceActor{
svcs: svcs,
pb: pb,
sysd: systemd.New(dirs.GlobalRootDir, pb),
}, nil
}
// Status of all the found services.
func (actor *serviceActor) Status() ([]string, error) {
// TODO: make this a [i.String() for i in actor.ServiceStatus()]
var stati []string
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
status, err := actor.sysd.Status(svcname)
if err != nil {
return nil, err
}
status = fmt.Sprintf("%s\t%s\t%s", svc.m.Name, svc.svc.Name, status)
stati = append(stati, status)
}
return stati, nil
}
// A PackageServiceStatus annotates systemd's ServiceStatus with
// package information systemd is unaware of.
type PackageServiceStatus struct {
systemd.ServiceStatus
PackageName string `json:"package_name"`
ServiceName string `json:"service_name"`
}
// ServiceStatus of all the found services.
func (actor *serviceActor) ServiceStatus() ([]*PackageServiceStatus, error) {
var stati []*PackageServiceStatus
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
status, err := actor.sysd.ServiceStatus(svcname)
if err != nil {
return nil, err
}
// TODO: move these into sysd; this is ugly
stati = append(stati, &PackageServiceStatus{
ServiceStatus: *status,
PackageName: svc.m.Name,
ServiceName: svc.svc.Name,
})
}
return stati, nil
}
// Start all the found services.
func (actor *serviceActor) Start() error {
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
if err := actor.sysd.Start(svcname); err != nil {
// TRANSLATORS: the first %s is the package name, the second is the service name; the %v is the error
return fmt.Errorf(i18n.G("unable to start %s's service %s: %v"), svc.m.Name, svc.svc.Name, err)
}
}
return nil
}
// Stop all the found services.
func (actor *serviceActor) Stop() error {
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
if err := actor.sysd.Stop(svcname, time.Duration(svc.svc.StopTimeout)); err != nil {
// TRANSLATORS: the first %s is the package name, the second is the service name; the %v is the error
return fmt.Errorf(i18n.G("unable to stop %s's service %s: %v"), svc.m.Name, svc.svc.Name, err)
}
}
return nil
}
// Restart all the found services.
func (actor *serviceActor) Restart() error {
err := actor.Stop()
if err != nil {
return err
}
return actor.Start()
}
// Enable all the found services.
func (actor *serviceActor) Enable() error {
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
if err := actor.sysd.Enable(svcname); err != nil {
// TRANSLATORS: the first %s is the package name, the second is the service name; the %v is the error
return fmt.Errorf(i18n.G("unable to enable %s's service %s: %v"), svc.m.Name, svc.svc.Name, err)
}
}
actor.sysd.DaemonReload()
return nil
}
// Disable all the found services.
func (actor *serviceActor) Disable() error {
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
if err := actor.sysd.Disable(svcname); err != nil {
// TRANSLATORS: the first %s is the package name, the second is the service name; the %v is the error
return fmt.Errorf(i18n.G("unable to disable %s's service %s: %v"), svc.m.Name, svc.svc.Name, err)
}
}
actor.sysd.DaemonReload()
return nil
}
// Logs for all found services.
func (actor *serviceActor) Logs() ([]systemd.Log, error) {
var svcnames []string
for _, svc := range actor.svcs {
svcname := filepath.Base(generateServiceFileName(svc.m, *svc.svc))
svcnames = append(svcnames, svcname)
}
logs, err := actor.sysd.Logs(svcnames)
if err != nil {
return nil, fmt.Errorf(i18n.G("unable to get logs: %v"), err)
}
return logs, nil
}
// Loglines serializes the logs for all found services
func (actor *serviceActor) Loglines() ([]string, error) {
var lines []string
logs, err := actor.Logs()
if err != nil {
return nil, err
}
for i := range logs {
lines = append(lines, logs[i].String())
}
return lines, nil
}