-
Notifications
You must be signed in to change notification settings - Fork 52
/
api_services.go
237 lines (216 loc) · 6.44 KB
/
api_services.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
// Copyright (c) 2014-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"
"strings"
"time"
"github.com/canonical/x-go/strutil"
"github.com/canonical/pebble/internals/overlord/servstate"
"github.com/canonical/pebble/internals/overlord/state"
)
type serviceInfo struct {
Name string `json:"name"`
Startup string `json:"startup"`
Current string `json:"current"`
CurrentSince *time.Time `json:"current-since,omitempty"` // pointer as omitempty doesn't work with time.Time directly
}
func v1GetServices(c *Command, r *http.Request, _ *UserState) Response {
names := strutil.MultiCommaSeparatedList(r.URL.Query()["names"])
servmgr := overlordServiceManager(c.d.overlord)
services, err := servmgr.Services(names)
if err != nil {
return InternalError("%v", err)
}
infos := make([]serviceInfo, 0, len(services))
for _, svc := range services {
info := serviceInfo{
Name: svc.Name,
Startup: string(svc.Startup),
Current: string(svc.Current),
}
if !svc.CurrentSince.IsZero() {
info.CurrentSince = &svc.CurrentSince
}
infos = append(infos, info)
}
return SyncResponse(infos)
}
func v1PostServices(c *Command, r *http.Request, _ *UserState) Response {
var payload struct {
Action string `json:"action"`
Services []string `json:"services"`
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
return BadRequest("cannot decode data from request body: %v", err)
}
var err error
servmgr := overlordServiceManager(c.d.overlord)
switch payload.Action {
case "replan":
if len(payload.Services) != 0 {
return BadRequest("%s accepts no service names", payload.Action)
}
case "autostart":
if len(payload.Services) != 0 {
return BadRequest("%s accepts no service names", payload.Action)
}
services, err := servmgr.DefaultServiceNames()
if err != nil {
return InternalError("%v", err)
}
if len(services) == 0 {
return SyncResponse(&resp{
Type: ResponseTypeError,
Result: &errorResult{Kind: errorKindNoDefaultServices, Message: "no default services"},
Status: 400,
})
}
payload.Services = services
default:
if len(payload.Services) == 0 {
return BadRequest("no services to %s provided", payload.Action)
}
}
st := c.d.overlord.State()
st.Lock()
defer st.Unlock()
var taskSet *state.TaskSet
var services []string
switch payload.Action {
case "start", "autostart":
services, err = servmgr.StartOrder(payload.Services)
if err != nil {
break
}
taskSet, err = servstate.Start(st, services)
case "stop":
services, err = servmgr.StopOrder(payload.Services)
if err != nil {
break
}
taskSet, err = servstate.Stop(st, services)
case "restart":
services, err = servmgr.StopOrder(payload.Services)
if err != nil {
break
}
services = intersectOrdered(payload.Services, services)
var stopTasks *state.TaskSet
stopTasks, err = servstate.Stop(st, services)
if err != nil {
break
}
services, err = servmgr.StartOrder(payload.Services)
if err != nil {
break
}
var startTasks *state.TaskSet
startTasks, err = servstate.Start(st, services)
if err != nil {
break
}
startTasks.WaitAll(stopTasks)
taskSet = state.NewTaskSet()
taskSet.AddAll(stopTasks)
taskSet.AddAll(startTasks)
case "replan":
var stopNames, startNames []string
stopNames, startNames, err = servmgr.Replan()
if err != nil {
break
}
var stopTasks *state.TaskSet
stopTasks, err = servstate.Stop(st, stopNames)
if err != nil {
break
}
var startTasks *state.TaskSet
startTasks, err = servstate.Start(st, startNames)
if err != nil {
break
}
startTasks.WaitAll(stopTasks)
taskSet = state.NewTaskSet()
taskSet.AddAll(stopTasks)
taskSet.AddAll(startTasks)
// Populate a list of services affected by the replan for summary.
replanned := make(map[string]bool)
for _, v := range stopNames {
replanned[v] = true
}
for _, v := range startNames {
replanned[v] = true
}
for k := range replanned {
services = append(services, k)
}
sort.Strings(services)
payload.Services = services
default:
return BadRequest("action %q is unsupported", payload.Action)
}
if err != nil {
return BadRequest("cannot %s services: %v", payload.Action, err)
}
// Use the original requested service name for the summary, not the
// resolved one. But do use the resolved set for the count.
var summary string
switch {
case len(taskSet.Tasks()) == 0:
// Can happen with a replan that has no services to stop/start. A
// change with no tasks needs to be marked Done manually (normally a
// change is marked Done when its last task is finished).
summary = fmt.Sprintf("%s - no services", strings.Title(payload.Action))
change := st.NewChange(payload.Action, summary)
change.SetStatus(state.DoneStatus)
return AsyncResponse(nil, change.ID())
case len(services) == 1:
summary = fmt.Sprintf("%s service %q", strings.Title(payload.Action), payload.Services[0])
default:
summary = fmt.Sprintf("%s service %q and %d more", strings.Title(payload.Action), payload.Services[0], len(services)-1)
}
change := st.NewChange(payload.Action, summary)
change.AddAll(taskSet)
if len(payload.Services) > 0 {
change.Set("service-names", payload.Services)
}
stateEnsureBefore(st, 0)
return AsyncResponse(nil, change.ID())
}
func v1GetService(c *Command, r *http.Request, _ *UserState) Response {
return BadRequest("not implemented")
}
func v1PostService(c *Command, r *http.Request, _ *UserState) Response {
return BadRequest("not implemented")
}
// intersectOrdered returns the intersection of left and right where
// the right's ordering is persisted in the resulting set.
func intersectOrdered(left []string, orderedRight []string) []string {
m := map[string]bool{}
for _, v := range left {
m[v] = true
}
var out []string
for _, v := range orderedRight {
if m[v] {
out = append(out, v)
}
}
return out
}