-
Notifications
You must be signed in to change notification settings - Fork 5
/
runner.go
199 lines (168 loc) · 5.04 KB
/
runner.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
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"os/user"
"strconv"
"sync"
"syscall"
"git.sr.ht/~spc/go-log"
"github.com/google/uuid"
pb "github.com/redhatinsights/yggdrasil/protocol"
)
const preStartErrorExitCode = 254
func dispatch(ctx context.Context, d *pb.Data, s *serverContext) {
event, prs := d.GetMetadata()["event"]
if !prs {
log.Warnln("Message metadata does not contain event field, assuming 'start'")
event = "start"
}
switch event {
case "start":
startScript(ctx, d, s)
case "cancel":
cancel(ctx, d, s)
default:
log.Errorf("Received unknown event '%v'", event)
}
}
func startScript(ctx context.Context, d *pb.Data, s *serverContext) {
jobUUID, jobUUIDP := d.GetMetadata()["job_uuid"]
if !jobUUIDP {
jobUUID = uuid.New().String()
log.Warnf("No job uuid found in job's metadata, will not be able to cancel this job, using autogenerated UUID %v", jobUUID)
}
log.Infof("Starting job %v", jobUUID)
updates := make(chan V1Update)
oa := NewUpdateAggregator(d.GetMetadata()["return_url"], d.GetMessageId())
go oa.Aggregate(updates, &YggdrasilGrpc{})
job := V1JobDefinition{}
job.Script = string(d.GetContent())
log.Tracef("running script : %#v", job.Script)
effectiveUser, effectiveUserP := d.GetMetadata()["effective_user"]
if effectiveUserP && effectiveUser != "" {
job.EffectiveUser = &effectiveUser
}
scriptfile, err := os.CreateTemp(s.workingDirectory, "ygg_rex")
if err != nil {
reportStartError(fmt.Sprintf("failed to create script tmp file: %v", err), updates)
return
}
defer os.Remove(scriptfile.Name())
n2, err := scriptfile.Write([]byte(job.Script))
if err != nil {
reportStartError(fmt.Sprintf("failed to write script to tmp file: %v", err), updates)
return
}
log.Debugf("script of %d bytes written in : %#v", n2, scriptfile.Name())
err = scriptfile.Close()
if err != nil {
reportStartError(fmt.Sprintf("%v", err), updates)
return
}
err = os.Chmod(scriptfile.Name(), 0700)
if err != nil {
reportStartError(fmt.Sprintf("%v", err), updates)
return
}
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("cd ~; export HOME=\"$PWD\"; exec %v", scriptfile.Name()))
// cmd.Env = env
if job.EffectiveUser != nil {
u, err := user.Lookup(*job.EffectiveUser)
if err != nil {
reportStartError(fmt.Sprintf("Unknown effective user '%v'", *job.EffectiveUser), updates)
return
}
uid, _ := strconv.ParseInt(u.Uid, 10, 32)
gid, _ := strconv.ParseInt(u.Gid, 10, 32)
err = os.Chown(scriptfile.Name(), int(uid), int(gid))
if err != nil {
reportStartError(fmt.Sprintf("Failed to change ownership of script: %v", err), updates)
return
}
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}
}
stdout, err := cmd.StdoutPipe()
if err != nil {
reportStartError(fmt.Sprintf("cannot connect to stdout: %v", err), updates)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
reportStartError(fmt.Sprintf("cannot connect to stderr: %v", err), updates)
return
}
if err := cmd.Start(); err != nil {
reportStartError(fmt.Sprintf("cannot run script: %v", err), updates)
return
}
log.Infof("started script process: %v", cmd.Process.Pid)
if jobUUIDP {
s.jobStorage.Set(jobUUID, cmd.Process.Pid)
defer s.jobStorage.Remove(jobUUID)
}
var wg sync.WaitGroup
wg.Add(2)
go func() { outputCollector("stdout", stdout, updates); wg.Done() }()
go func() { outputCollector("stderr", stderr, updates); wg.Done() }()
wg.Wait()
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
updates <- NewExitUpdate(status.ExitStatus())
}
} else {
reportStartError(fmt.Sprintf("script run failed: %v", err), updates)
return
}
} else {
updates <- NewExitUpdate(0)
}
close(updates)
log.Infof("Finished job %v", jobUUID)
}
func reportStartError(message string, updates chan<- V1Update) {
log.Error(message)
updates <- NewOutputUpdate("stderr", message)
updates <- NewExitUpdate(preStartErrorExitCode)
close(updates)
}
func outputCollector(stdtype string, pipe io.ReadCloser, outputs chan<- V1Update) {
buf := make([]byte, 4096)
for {
n, err := pipe.Read(buf)
if n > 0 {
msg := string(buf[:n])
log.Tracef("%v message: %v", stdtype, msg)
outputs <- NewOutputUpdate(stdtype, msg)
}
if err != nil {
if err != io.EOF {
log.Errorf("cannot read from %v: %v", stdtype, err)
}
break
}
}
}
var syscallKill = syscall.Kill
func cancel(ctx context.Context, d *pb.Data, s *serverContext) {
jobUUID, jobUUIDP := d.GetMetadata()["job_uuid"]
if !jobUUIDP {
log.Errorln("No job uuid found in job's metadata, aborting.")
return
}
pid, prs := s.jobStorage.Get(jobUUID)
if !prs {
log.Errorf("Cannot cancel unknown job %v", jobUUID)
return
}
log.Infof("Cancelling job %v, sending SIGTERM to process %v", jobUUID, pid)
if err := syscallKill(pid, syscall.SIGTERM); err != nil {
log.Errorf("Failed to send SIGTERM to process %v: %v", pid, err)
}
}