This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cppforlife/bosh-provisioner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runit_provisioner.go
272 lines (213 loc) · 7.17 KB
/
runit_provisioner.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
package vagrant
import (
"fmt"
"regexp"
"strings"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
const (
runitProvisionerLogTag = "RunitProvisioner"
runitStopStepDuration = 1 * time.Second
)
var (
// Matches 'svlogd -tt /var/vcap/bosh/log'
runitSvlogdRegex = regexp.MustCompile(`\s*svlogd\s+\-tt\s+(.+)\s*`)
// Matches 'down: agent: 3s, normally up; run: log: (pid 15318) 7762s'
runitStatusDownRegex = regexp.MustCompile(`\Adown: [a-z\/]+: \d+`)
runitPossibleRunsvdirPaths = []string{"/usr/sbin/runsvdir-start", "/sbin/runsvdir-start"}
)
// RunitProvisioner installs runit and
// adds specified service under runit's control.
type RunitProvisioner struct {
fs boshsys.FileSystem
cmds SimpleCmds
depsProvisioner DepsProvisioner
runner boshsys.CmdRunner
assetManager AssetManager
logger boshlog.Logger
}
func NewRunitProvisioner(
fs boshsys.FileSystem,
cmds SimpleCmds,
depsProvisioner DepsProvisioner,
runner boshsys.CmdRunner,
assetManager AssetManager,
logger boshlog.Logger,
) RunitProvisioner {
return RunitProvisioner{
fs: fs,
cmds: cmds,
depsProvisioner: depsProvisioner,
runner: runner,
assetManager: assetManager,
logger: logger,
}
}
func (p RunitProvisioner) Provision(name string, stopTimeout time.Duration) error {
p.logger.Info(runitProvisionerLogTag, "Provisioning %s service", name)
err := p.depsProvisioner.InstallRunit()
if err != nil {
return bosherr.WrapError(err, "Installing runit")
}
servicePath, enableServicePath := p.buildServicePaths(name)
err = p.stopRunAndLog(servicePath, enableServicePath, name, stopTimeout)
if err != nil {
return bosherr.WrapError(err, "Stopping run and log")
}
err = p.setUpRun(servicePath, name)
if err != nil {
return bosherr.WrapError(err, "Setting up run")
}
err = p.setUpLog(servicePath, name)
if err != nil {
return bosherr.WrapError(err, "Setting up log")
}
err = p.startRunAndLog(servicePath, enableServicePath, name)
if err != nil {
return bosherr.WrapError(err, "Starting run and log")
}
return nil
}
func (p RunitProvisioner) Deprovision(name string, stopTimeout time.Duration) error {
p.logger.Info(runitProvisionerLogTag, "Deprovisioning %s service", name)
servicePath, enableServicePath := p.buildServicePaths(name)
err := p.stopRunAndLog(servicePath, enableServicePath, name, stopTimeout)
if err != nil {
return bosherr.WrapError(err, "Stopping run and log")
}
return nil
}
func (p RunitProvisioner) buildServicePaths(name string) (string, string) {
servicePath := fmt.Sprintf("/etc/sv/%s", name)
enableServicePath := fmt.Sprintf("/etc/service/%s", name)
return servicePath, enableServicePath
}
// setUpRun sets up script that runit will execute for the primary process
func (p RunitProvisioner) setUpRun(servicePath, name string) error {
p.logger.Info(runitProvisionerLogTag, "Setting up %s run", name)
err := p.cmds.MkdirP(servicePath)
if err != nil {
return err
}
runPath := fmt.Sprintf("%s/run", servicePath)
err = p.assetManager.Place(fmt.Sprintf("%s/%s-run", name, name), runPath)
if err != nil {
return err
}
return p.cmds.ChmodX(runPath)
}
// setUpLog sets up logging destination for the service
func (p RunitProvisioner) setUpLog(servicePath, name string) error {
p.logger.Info(runitProvisionerLogTag, "Setting up %s log", name)
logPath := fmt.Sprintf("%s/log", servicePath)
err := p.cmds.MkdirP(logPath)
if err != nil {
return err
}
logRunPath := fmt.Sprintf("%s/run", logPath)
err = p.assetManager.Place(fmt.Sprintf("%s/%s-log", name, name), logRunPath)
if err != nil {
return err
}
err = p.cmds.ChmodX(logRunPath)
if err != nil {
return err
}
contens, err := p.fs.ReadFileString(logRunPath)
if err != nil {
return err
}
// First match is the whole string
svlogdPaths := runitSvlogdRegex.FindStringSubmatch(contens)
// Create log file destination so that runit process can properly log
if len(svlogdPaths) == 2 {
err = p.cmds.MkdirP(svlogdPaths[1])
if err != nil {
return err
}
}
return nil
}
func (p RunitProvisioner) stopRunAndLog(servicePath, enableServicePath, name string, stopTimeout time.Duration) error {
p.logger.Info(runitProvisionerLogTag, "Stopping %s run", name)
err := p.stopRunsv(name, stopTimeout)
if err != nil {
return bosherr.WrapError(err, "Stopping service")
}
err = p.stopRunsv(fmt.Sprintf("%s/log", name), stopTimeout)
if err != nil {
return bosherr.WrapError(err, "Stopping log service")
}
err = p.fs.RemoveAll(enableServicePath)
if err != nil {
return err
}
// Clear out all service state kept in supervise/ and control/ dirs
return p.fs.RemoveAll(servicePath)
}
func (p RunitProvisioner) startRunAndLog(servicePath, enableServicePath, name string) error {
if name == "monit" {
return p.configureMonitService(enableServicePath)
}
// Enabling service will kick in monitoring
_, _, _, err := p.runner.RunCommand("ln", "-sf", servicePath, enableServicePath)
return err
}
func (p RunitProvisioner) configureMonitService(enableServicePath string) error {
var runsvdirPath string
for _, path := range runitPossibleRunsvdirPaths {
if p.fs.FileExists(path) {
runsvdirPath = path
}
}
if len(runsvdirPath) == 0 {
return bosherr.Error("Failed to find runsvdir-start binary")
}
runsvdirStr, err := p.fs.ReadFileString(runsvdirPath)
if err != nil {
return bosherr.WrapErrorf(err, "Reading '%s'", runsvdirPath)
}
// Already checks sys/run directory; nothing to do
if strings.Contains(runsvdirStr, "/var/vcap/data/sys/run") {
return nil
}
// from https://github.com/cloudfoundry/bosh/blob/master/stemcell_builder/stages/delay_monit_start/apply.sh:
// > if /var/vcap/data/sys/run is not already mounted, the agent must not have been started yet
// > in that case remove /etc/services/monit in order to prevent runsvdir from starting monit
// > (the agent will do that during boostrapping).
sedScript := fmt.Sprintf(
"sed -i '2i if [ x`mount | grep -c /var/vcap/data/sys/run` = x0 ] ; then rm -f %s ; fi' %s",
enableServicePath,
runsvdirPath,
)
_, _, _, err = p.runner.RunCommand("bash", "-c", sedScript)
return err
}
func (p RunitProvisioner) stopRunsv(name string, stopTimeout time.Duration) error {
p.logger.Info(runitProvisionerLogTag, "Stopping runsv")
// Potentially tried to deprovision before ever provisioning
if !p.runner.CommandExists("sv") {
return nil
}
downStdout, _, _, err := p.runner.RunCommand("sv", "down", name)
if err != nil {
p.logger.Error(runitProvisionerLogTag, "Ignoring down error %s", err)
}
// If runsv configuration does not exist, service was never started
if strings.Contains(downStdout, "file does not exist") {
return nil
}
var lastStatusStdout string
var passedDuration time.Duration
for ; passedDuration < stopTimeout; passedDuration += runitStopStepDuration {
lastStatusStdout, _, _, _ = p.runner.RunCommand("sv", "status", name)
if runitStatusDownRegex.MatchString(lastStatusStdout) {
return nil
}
time.Sleep(runitStopStepDuration)
}
return bosherr.Errorf("Failed to stop runsv for %s. Output: %s", name, lastStatusStdout)
}