-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathlifecycle.go
More file actions
240 lines (201 loc) · 6.13 KB
/
lifecycle.go
File metadata and controls
240 lines (201 loc) · 6.13 KB
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
// Copyright (C) 2017-Present Pivotal Software, Inc. All rights reserved.
//
// This program and the accompanying materials are made available under
// the terms of the under the Apache License, Version 2.0 (the "License”);
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package lifecycle
import (
"bpm/bpm"
"bpm/models"
"bpm/runc/client"
"bpm/usertools"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"time"
specs "github.com/opencontainers/runtime-spec/specs-go"
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/lager"
)
const (
ContainerSigQuitGracePeriod = 5 * time.Second
ContainerStatePollInterval = 1 * time.Second
)
var TimeoutError = errors.New("failed to stop job within timeout")
//go:generate counterfeiter . UserFinder
type UserFinder interface {
Lookup(username string) (specs.User, error)
}
//go:generate counterfeiter . RuncAdapter
type RuncAdapter interface {
CreateJobPrerequisites(systemRoot, jobName, procName string, user specs.User) (string, *os.File, *os.File, error)
BuildSpec(systemRoot, jobName, procName string, cfg *bpm.Config, user specs.User) (specs.Spec, error)
}
//go:generate counterfeiter . RuncClient
type RuncClient interface {
CreateBundle(bundlePath string, jobSpec specs.Spec, user specs.User) error
RunContainer(pidFilePath, bundlePath, containerID string, stdout, stderr io.Writer) error
Exec(containerID, command string, stdin io.Reader, stdout, stderr io.Writer) error
ContainerState(containerID string) (*specs.State, error)
ListContainers() ([]client.ContainerState, error)
SignalContainer(containerID string, signal client.Signal) error
DeleteContainer(containerID string) error
DestroyBundle(bundlePath string) error
}
type RuncLifecycle struct {
clock clock.Clock
runcClient RuncClient
runcAdapter RuncAdapter
systemRoot string
userFinder UserFinder
}
func NewRuncLifecycle(
runcClient RuncClient,
runcAdapter RuncAdapter,
userFinder UserFinder,
clock clock.Clock,
systemRoot string,
) *RuncLifecycle {
return &RuncLifecycle{
clock: clock,
runcClient: runcClient,
runcAdapter: runcAdapter,
systemRoot: systemRoot,
userFinder: userFinder,
}
}
func (j *RuncLifecycle) StartJob(jobName, procName string, cfg *bpm.Config) error {
user, err := j.userFinder.Lookup(usertools.VcapUser)
if err != nil {
return err
}
pidDir, stdout, stderr, err := j.runcAdapter.CreateJobPrerequisites(j.systemRoot, jobName, procName, user)
if err != nil {
return fmt.Errorf("failed to create system files: %s", err.Error())
}
defer stdout.Close()
defer stderr.Close()
spec, err := j.runcAdapter.BuildSpec(j.systemRoot, jobName, procName, cfg, user)
if err != nil {
return err
}
bundlePath := j.bundlePath(jobName, procName)
err = j.runcClient.CreateBundle(bundlePath, spec, user)
if err != nil {
return fmt.Errorf("bundle build failure: %s", err.Error())
}
pidFilePath := filepath.Join(pidDir, fmt.Sprintf("%s.pid", procName))
cid := containerID(jobName, procName)
return j.runcClient.RunContainer(
pidFilePath,
bundlePath,
cid,
stdout,
stderr,
)
}
// GetJob returns the following:
// - job, nil if the job is running (and no errors were encountered)
// - nil,nil if the job is not running and there is no other error
// - nil,error if there is any other error getting the job beyond it not running
func (j *RuncLifecycle) GetJob(jobName, procName string) (*models.Job, error) {
cid := containerID(jobName, procName)
container, err := j.runcClient.ContainerState(cid)
if err != nil {
return nil, err
}
if container == nil {
return nil, nil
}
return &models.Job{
Name: container.ID,
Pid: container.Pid,
Status: container.Status,
}, nil
}
func (j *RuncLifecycle) OpenShell(jobName, procName string, stdin io.Reader, stdout, stderr io.Writer) error {
cid := containerID(jobName, procName)
return j.runcClient.Exec(cid, "/bin/bash", stdin, stdout, stderr)
}
func (j *RuncLifecycle) ListJobs() ([]models.Job, error) {
containers, err := j.runcClient.ListContainers()
if err != nil {
return nil, err
}
var jobs []models.Job
for _, c := range containers {
job := models.Job{
Name: c.ID,
Pid: c.InitProcessPid,
Status: c.Status,
}
jobs = append(jobs, job)
}
return jobs, nil
}
func (j *RuncLifecycle) StopJob(logger lager.Logger, jobName, procName string, exitTimeout time.Duration) error {
cid := containerID(jobName, procName)
err := j.runcClient.SignalContainer(cid, client.Term)
if err != nil {
return err
}
state, err := j.runcClient.ContainerState(cid)
if err != nil {
logger.Error("failed-to-fetch-state", err)
} else {
if state.Status == "stopped" {
return nil
}
}
timeout := j.clock.NewTimer(exitTimeout)
stateTicker := j.clock.NewTicker(ContainerStatePollInterval)
defer stateTicker.Stop()
for {
select {
case <-stateTicker.C():
state, err = j.runcClient.ContainerState(cid)
if err != nil {
logger.Error("failed-to-fetch-state", err)
} else {
if state.Status == "stopped" {
return nil
}
}
case <-timeout.C():
err := j.runcClient.SignalContainer(cid, client.Quit)
if err != nil {
logger.Error("failed-to-sigquit", err)
}
j.clock.Sleep(ContainerSigQuitGracePeriod)
return TimeoutError
}
}
}
func (j *RuncLifecycle) RemoveJob(jobName, procName string) error {
cid := containerID(jobName, procName)
err := j.runcClient.DeleteContainer(cid)
if err != nil {
return err
}
return j.runcClient.DestroyBundle(j.bundlePath(jobName, procName))
}
func (j *RuncLifecycle) bundlePath(jobName, procName string) string {
return filepath.Join(j.systemRoot, "data", "bpm", "bundles", jobName, procName)
}
func containerID(jobName, procName string) string {
if jobName == procName {
return jobName
}
return fmt.Sprintf("%s.%s", jobName, procName)
}