/
service_control.go
120 lines (106 loc) · 3.34 KB
/
service_control.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
// -*- 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 servicestate
import (
"fmt"
"github.com/snapcore/snapd/overlord/snapstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/wrappers"
tomb "gopkg.in/tomb.v2"
)
// ServiceAction encapsulates a single service-related action (such as starting,
// stopping or restarting) run against services of a given snap. The action is
// run for services listed in services attribute, or for all services of the
// snap if services list is empty.
// The names of services are app names (as defined in snap yaml).
type ServiceAction struct {
SnapName string `json:"snap-name"`
Action string `json:"action"`
ActionModifier string `json:"action-modifier,omitempty"`
Services []string `json:"services,omitempty"`
}
func (m *ServiceManager) doServiceControl(t *state.Task, _ *tomb.Tomb) error {
st := t.State()
st.Lock()
defer st.Unlock()
perfTimings := state.TimingsForTask(t)
defer perfTimings.Save(st)
var sc ServiceAction
err := t.Get("service-action", &sc)
if err != nil {
return fmt.Errorf("internal error: cannot get service-action: %v", err)
}
var snapst snapstate.SnapState
if err := snapstate.Get(st, sc.SnapName, &snapst); err != nil {
return err
}
info, err := snapst.CurrentInfo()
if err != nil {
return err
}
svcs := info.Services()
if len(svcs) == 0 {
return nil
}
var services []*snap.AppInfo
if len(sc.Services) == 0 {
// no services specified, take all services of the snap
services = info.Services()
} else {
for _, svc := range sc.Services {
app := info.Apps[svc]
if app == nil {
return fmt.Errorf("no such service: %s", svc)
}
if !app.IsService() {
return fmt.Errorf("%s is not a service", svc)
}
services = append(services, app)
}
}
meter := snapstate.NewTaskProgressAdapterLocked(t)
switch sc.Action {
case "stop":
flags := &wrappers.StopServicesFlags{
Disable: sc.ActionModifier == "disable",
}
if err := wrappers.StopServices(services, flags, snap.StopReasonOther, meter, perfTimings); err != nil {
return err
}
case "start":
startupOrdered, err := snap.SortServices(services)
if err != nil {
return err
}
flags := &wrappers.StartServicesFlags{
Enable: sc.ActionModifier == "enable",
}
if err := wrappers.StartServices(startupOrdered, nil, flags, meter, perfTimings); err != nil {
return err
}
case "restart":
return wrappers.RestartServices(services, nil, meter, perfTimings)
case "reload-or-restart":
flags := &wrappers.RestartServicesFlags{Reload: true}
return wrappers.RestartServices(services, flags, meter, perfTimings)
default:
return fmt.Errorf("unhandled service action: %q", sc.Action)
}
return nil
}