-
Notifications
You must be signed in to change notification settings - Fork 561
/
rest_api.go
165 lines (143 loc) · 4.22 KB
/
rest_api.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 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 agent
import (
"encoding/json"
"net/http"
"strings"
"time"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/systemd"
"github.com/snapcore/snapd/timeout"
)
var restApi = []*Command{
rootCmd,
sessionInfoCmd,
servicesCmd,
}
var (
rootCmd = &Command{
Path: "/",
GET: nil,
}
sessionInfoCmd = &Command{
Path: "/v1/session-info",
GET: sessionInfo,
}
servicesCmd = &Command{
Path: "/v1/services",
POST: postServices,
}
)
func sessionInfo(c *Command, r *http.Request) Response {
m := map[string]interface{}{
"version": c.s.Version,
}
return SyncResponse(m)
}
type serviceInstruction struct {
Action string `json:"action"`
Services []string `json:"services"`
}
var (
stopTimeout = time.Duration(timeout.DefaultTimeout)
killWait = 5 * time.Second
)
func stopOneService(service string, sysd systemd.Systemd) error {
err := sysd.Stop(service, stopTimeout)
if err != nil && systemd.IsTimeout(err) {
// ignore errors for kill; nothing we'd do differently at this point
sysd.Kill(service, "TERM", "")
time.Sleep(killWait)
sysd.Kill(service, "KILL", "")
}
return err
}
func serviceStart(inst *serviceInstruction, sysd systemd.Systemd) Response {
// Refuse to start non-snap services
for _, service := range inst.Services {
if !strings.HasPrefix(service, "snap.") {
return InternalError("cannot start service %v", service)
}
}
startErrors := make(map[string]string)
var started []string
for _, service := range inst.Services {
if err := sysd.Start(service); err != nil {
startErrors[service] = err.Error()
break
}
started = append(started, service)
}
// If we got any failures, attempt to stop the services we started.
if len(startErrors) != 0 {
for _, service := range started {
if err := stopOneService(service, sysd); err != nil {
startErrors[service] = err.Error()
}
}
}
return SyncResponse(startErrors)
}
func serviceStop(inst *serviceInstruction, sysd systemd.Systemd) Response {
// Refuse to start non-snap services
for _, service := range inst.Services {
if !strings.HasPrefix(service, "snap.") {
return InternalError("cannot stop service %v", service)
}
}
stopErrors := make(map[string]string)
for _, service := range inst.Services {
if err := stopOneService(service, sysd); err != nil {
stopErrors[service] = err.Error()
}
}
return SyncResponse(stopErrors)
}
func serviceDaemonReload(inst *serviceInstruction, sysd systemd.Systemd) Response {
if len(inst.Services) != 0 {
return InternalError("daemon-reload should not be called with any services")
}
if err := sysd.DaemonReload(); err != nil {
return InternalError("cannot reload daemon: %v", err)
}
return SyncResponse(nil)
}
var serviceInstructionDispTable = map[string]func(*serviceInstruction, systemd.Systemd) Response{
"start": serviceStart,
"stop": serviceStop,
"daemon-reload": serviceDaemonReload,
}
type dummyReporter struct{}
func (dummyReporter) Notify(string) {}
func postServices(c *Command, r *http.Request) Response {
if contentType := r.Header.Get("Content-Type"); contentType != "application/json" {
return BadRequest("unknown content type: %s", contentType)
}
decoder := json.NewDecoder(r.Body)
var inst serviceInstruction
if err := decoder.Decode(&inst); err != nil {
return BadRequest("cannot decode request body into service instruction: %v", err)
}
impl := serviceInstructionDispTable[inst.Action]
if impl == nil {
return BadRequest("unknown action %s", inst.Action)
}
sysd := systemd.New(dirs.GlobalRootDir, systemd.UserMode, dummyReporter{})
return impl(&inst, sysd)
}