-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontroller.go
More file actions
218 lines (193 loc) · 5.24 KB
/
controller.go
File metadata and controls
218 lines (193 loc) · 5.24 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
package controller
import (
"errors"
"os"
"path/filepath"
"strconv"
"sync"
"github.com/runner-x/runner-x/engine/controller/writerremover"
print2 "github.com/runner-x/runner-x/util/print"
"github.com/runner-x/runner-x/engine/runtime"
)
type Controller interface {
SubmitRequest(runprops *Props) *CtrlRunOutput
}
type CtrlErr error
type CtrlRunOutput struct {
ControllerErr CtrlErr
RunOutput *runtime.RunOutput
CommandErr error
}
// Props store the commands to run before and after the isolated run command
type Props struct {
Data *writerremover.Blob
PreRunProps *runtime.RunProps
RunProps *runtime.RunProps
}
type AsyncController struct {
agents map[uint]*agentData
}
type agentData struct {
rwmutex sync.RWMutex
agent runtime.Runtime
writerRemover writerremover.BlobWriterRemover
claim chan struct{}
}
func NewAsyncControllerWithMap(agents map[uint]*agentData) *AsyncController {
for _, a := range agents {
if a.claim == nil {
a.claim = make(chan struct{}, 1)
a.claim <- struct{}{}
}
}
return &AsyncController{agents}
}
func NewAsyncController(size uint, provider runtime.ArgProvider, parentWorkdir string, pattern string) *AsyncController {
agents := make(map[uint]*agentData)
for i := uint(0); i < size; i++ {
key := uint(i + 1)
workdir := filepath.Join(parentWorkdir, pattern+strconv.FormatInt(int64(key), 10))
if parentWorkdir != "" {
if err := os.MkdirAll(workdir, 0o755); err != nil {
print2.DebugPrintf("failed to create runner workdir %q: %v", workdir, err)
}
}
agents[key] = &agentData{
rwmutex: sync.RWMutex{},
agent: runtime.NewRuntimeAgentWithIds("agent"+strconv.FormatInt(int64(key), 10), int(key), provider, workdir),
writerRemover: writerremover.NewWorkdirWriter(workdir, 0644),
claim: newClaim(),
}
}
return &AsyncController{agents}
}
func newClaim() chan struct{} {
ch := make(chan struct{}, 1)
ch <- struct{}{}
return ch
}
func (a *agentData) tryClaim() bool {
if a.claim == nil {
// legacy: behave like "no claim" semantics
return true
}
select {
case <-a.claim:
return true
default:
return false
}
}
func (a *agentData) releaseClaim() {
if a.claim == nil {
return
}
select {
case a.claim <- struct{}{}:
default:
}
}
var (
NoRunnerIsReady = CtrlErr(errors.New("no runner available"))
InvalidInput = CtrlErr(errors.New("invalid input"))
PreRunError = CtrlErr(errors.New("error in pre-run hook before command run"))
PreRunWriteError = CtrlErr(errors.New("error writing sourcecode"))
PostRunPurgeError = CtrlErr(errors.New("error in post-run workflow"))
)
// SubmitRequest will run a command on the first runner agent it finds that is ready
func (ac *AsyncController) SubmitRequest(runprops *Props) *CtrlRunOutput {
// RunProps cannot be nil, you want to run something after all, right?!
if runprops == nil || runprops.RunProps == nil {
return &CtrlRunOutput{
ControllerErr: InvalidInput,
RunOutput: nil,
CommandErr: nil,
}
}
for _, agentData := range ac.agents {
if !agentData.agent.IsReady() {
continue
}
if !agentData.tryClaim() {
continue
}
defer agentData.releaseClaim()
// unpack these, easier to reference below
agent := agentData.agent
writerRemover := agentData.writerRemover
preRunProps := runprops.PreRunProps
runProps := runprops.RunProps
data := runprops.Data
// pre-pre run props is to actually write some the blob
err := writerRemover.Write(data)
if err != nil {
print2.DebugPrintf("error writing file before running command: %v", err)
return &CtrlRunOutput{
ControllerErr: PreRunWriteError,
RunOutput: nil,
CommandErr: nil,
}
}
if preRunProps != nil && len(preRunProps.RunArgs) > 0 {
preRunOut, commandErr := agent.SafeRunCmd(preRunProps)
if commandErr != nil {
print2.DebugPrintf("error preparing command: output=%v\n \nerror=%v", preRunOut, commandErr)
return &CtrlRunOutput{
ControllerErr: nil,
RunOutput: preRunOut,
CommandErr: commandErr,
}
}
}
timeout := runProps.Timeout
if timeout <= 0 {
timeout = runtime.DefaultTimeout
}
nprocs := runProps.Nprocs
if nprocs <= 0 {
nprocs = runtime.DefaultNproc
}
fsize := runProps.Fsize
if fsize <= 0 {
fsize = runtime.DefaultFsize
}
cputime := runProps.Cputime
if cputime <= 0 {
cputime = runtime.DefaultCputime
}
stacksize := runProps.Stacksize
if stacksize <= 0 {
stacksize = runtime.DefaultStackSize
}
// the actual command must be run as non-root user
runOutput, commandErr := agent.SafeRunCmd(&runtime.RunProps{
RunArgs: runProps.RunArgs,
Timeout: timeout,
Nprocs: nprocs,
Fsize: fsize,
Cputime: cputime,
Stacksize: stacksize,
Uid: agentData.agent.RuntimeUid(),
Gid: agentData.agent.RuntimeGid(),
})
err = writerRemover.Remove()
if err != nil {
print2.DebugPrintf("error cleaning up")
return &CtrlRunOutput{
ControllerErr: PostRunPurgeError,
RunOutput: runOutput,
CommandErr: commandErr,
}
}
return &CtrlRunOutput{
ControllerErr: nil,
RunOutput: runOutput,
CommandErr: commandErr,
}
}
return &CtrlRunOutput{
ControllerErr: NoRunnerIsReady,
RunOutput: nil,
CommandErr: nil,
}
}