-
Notifications
You must be signed in to change notification settings - Fork 79
/
exec.go
115 lines (99 loc) · 2.79 KB
/
exec.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
package exec
import (
"fmt"
"io"
context "golang.org/x/net/context"
"github.com/luizalabs/teresa/pkg/server/app"
"github.com/luizalabs/teresa/pkg/server/database"
"github.com/luizalabs/teresa/pkg/server/spec"
"github.com/luizalabs/teresa/pkg/server/storage"
"github.com/luizalabs/teresa/pkg/server/uid"
)
const (
ExitCodeTimeout = 124
ExitCodeError = 1
)
type Operations interface {
RunCommand(ctx context.Context, user *database.User, appName string, command ...string) (io.ReadCloser, <-chan error)
RunCommandBySpec(ctx context.Context, podSpec *spec.Pod) (io.ReadCloser, <-chan error)
}
type K8sOperations interface {
DeployAnnotation(namespace, deployName, annotation string) (string, error)
PodRun(podSpec *spec.Pod) (io.ReadCloser, <-chan int, error)
IsNotFound(err error) bool
DeletePod(namespace, podName string) error
}
type Defaults struct {
RunnerImage string
StoreImage string
LimitsCPU string
LimitsMemory string
}
type ExecOperations struct {
appOps app.Operations
fs storage.Storage
k8s K8sOperations
defaults *Defaults
}
func (ops *ExecOperations) RunCommand(ctx context.Context, user *database.User, appName string, command ...string) (io.ReadCloser, <-chan error) {
errChan := make(chan error, 1)
a, err := ops.appOps.CheckPermAndGet(user, appName)
if err != nil {
errChan <- err
return nil, errChan
}
currentSlug, err := ops.k8s.DeployAnnotation(a.Name, a.Name, spec.SlugAnnotation)
if err != nil {
if ops.k8s.IsNotFound(err) {
errChan <- ErrDeployNotFound
} else {
errChan <- err
}
return nil, errChan
}
podName := fmt.Sprintf("exec-command-%s-%s", appName, uid.New())
podSpec := spec.NewRunnerPodBuilder(podName, ops.defaults.RunnerImage, ops.defaults.StoreImage).
ForApp(a).
WithSlug(currentSlug).
WithStorage(ops.fs).
WithLimits(ops.defaults.LimitsCPU, ops.defaults.LimitsMemory).
WithArgs(command).
Build()
return ops.RunCommandBySpec(ctx, podSpec)
}
func (ops *ExecOperations) RunCommandBySpec(ctx context.Context, podSpec *spec.Pod) (io.ReadCloser, <-chan error) {
errChan := make(chan error)
r, w := io.Pipe()
go func() {
defer func() {
w.Close()
close(errChan)
}()
podStream, exitCodeChain, err := ops.k8s.PodRun(podSpec)
if err != nil {
errChan <- err
return
}
go io.Copy(w, podStream)
select {
case <-ctx.Done():
go ops.k8s.DeletePod(podSpec.Namespace, podSpec.Name)
errChan <- ctx.Err()
case ec := <-exitCodeChain:
if ec == ExitCodeTimeout {
errChan <- ErrTimeout
} else if ec != 0 {
errChan <- ErrNonZeroExitCode
}
}
}()
return r, errChan
}
func NewOperations(appOps app.Operations, k8s K8sOperations, fs storage.Storage, defaults *Defaults) Operations {
return &ExecOperations{
appOps: appOps,
fs: fs,
k8s: k8s,
defaults: defaults,
}
}