-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
worker_runner_environment.go
95 lines (87 loc) · 2.18 KB
/
worker_runner_environment.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
package actionsrunner
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"github.com/ChristopherHX/github-act-runner/protocol"
)
type WorkerRunnerEnvironment struct {
WorkerArgs []string
}
func (arunner *WorkerRunnerEnvironment) WriteJson(path string, value interface{}) error {
b, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(path, b, 0777)
}
func (arunner *WorkerRunnerEnvironment) ReadJson(path string, value interface{}) error {
cont, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return json.Unmarshal(cont, value)
}
func (arunner *WorkerRunnerEnvironment) Remove(fname string) error {
return os.Remove(fname)
}
func (arunner *WorkerRunnerEnvironment) Printf(format string, a ...interface{}) {
fmt.Printf(format, a...)
}
func (arunner *WorkerRunnerEnvironment) ExecWorker(run *RunRunner, wc WorkerContext, jobreq *protocol.AgentJobRequestMessage, src []byte) error {
jlogger := wc.Logger()
jobExecCtx := wc.JobExecCtx()
if len(arunner.WorkerArgs) <= 0 {
return fmt.Errorf("missing WorkerArgs to execute an external worker")
}
worker := exec.Command(arunner.WorkerArgs[0], arunner.WorkerArgs[1:]...)
in, err := worker.StdinPipe()
if err != nil {
return err
}
er, err := worker.StderrPipe()
if err != nil {
return err
}
out, err := worker.StdoutPipe()
if err != nil {
return err
}
err = worker.Start()
if err != nil {
return err
}
jlogger.Logger.Close()
jlogger.Current().Complete("Succeeded")
jlogger.MoveNext()
mid := make([]byte, 4)
binary.BigEndian.PutUint32(mid, 1) // NewJobRequest
in.Write(mid)
binary.BigEndian.PutUint32(mid, uint32(len(src)))
in.Write(mid)
in.Write(src)
done := make(chan struct{})
defer close(done)
go func() {
select {
case <-jobExecCtx.Done():
binary.BigEndian.PutUint32(mid, 2) // CancelRequest
in.Write(mid)
binary.BigEndian.PutUint32(mid, uint32(len(src)))
in.Write(mid)
in.Write(src)
case <-done:
}
}()
io.Copy(os.Stdout, out)
io.Copy(os.Stdout, er)
worker.Wait()
if exitcode := worker.ProcessState.ExitCode(); exitcode != 0 {
return fmt.Errorf("failed to execute worker: %v", exitcode)
}
return nil
}