-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.go
277 lines (238 loc) · 6.71 KB
/
entrypoint.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
An entrypoint that handles several AWS Batch-related tasks.
*/
package main
import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
var (
batchitExe = "batchit" // comment this out in testing
// batchitExe = "echo" // comment this out in 'production'
)
// entrypoint of the entrypoint.
func main() {
// look in env vars to see if user wants scratch space
// if they do, set it up.
// in future, we could at this point mount /app
// (and/or something else) via EBS/EFS if the user
// indicates they want it via some environment variable.
// look in env vars to see if user wants fetch and run
// if so, run it -- don't exit if it errors out but save the error code
// if they don't want fetch and run, run the 'command' ($@ in bash)
// again, don't exit on error but save the error
// if user wanted scratch space, delete those volumes now
// exit with 0 if all was fine or with saved error code if
// fetch-and-run or 'command' failed.
// a somewhat unrelated thing- in job wrapper code,
// there should be an option to sleep for some period of
// time between starting jobs. otherwise we could run into
// RequestLimitExceeded errors when creating volumes
retcode := 0
defer func() { os.Exit(retcode) }()
var scratchSize = getenv("SCRATCH_SIZE", "", true)
var volIds string
if scratchSize != "" {
volIds = makeScratchSpace(scratchSize)
defer tearDownVolumes(volIds)
}
if wantFetchAndRun() {
retcode = runcmd("fetch_and_run.sh", []string{})
} else {
argLen := len(os.Args)
if argLen < 2 {
fmt.Println("You didn't specify a command! Exiting.")
retcode = 1
return
}
prog := os.Args[1]
progArgs := os.Args[2:]
retcode = runcmd(prog, progArgs)
}
fmt.Printf("Exiting with return code %d.\n", retcode)
}
func tearDownVolumes(volIds string) {
progArgs0 := []string{"ddv"}
progArgs1 := strings.Split(volIds, " ")
progArgs := append(progArgs0, progArgs1...)
resultCode, _ := getCmdOutput(batchitExe, progArgs)
if resultCode != 0 {
fmt.Printf("Failed to tear down volumes, exit code %d, exiting.\n", resultCode)
os.Exit(1)
}
}
func wantFetchAndRun() bool {
vars := []string{"BATCH_FILE_TYPE", "BATCH_FILE_S3_URL"}
for i := 0; i < 2; i++ {
if getenv(vars[i], "", false) == "" {
return false
}
}
return true
}
// Get an environment variable or a default if not set.
func getenv(key string, defaultValue string, mustBeNumber bool) string {
val, ok := os.LookupEnv(key)
if !ok {
val = defaultValue
}
if mustBeNumber {
_, err := strconv.Atoi(val)
if err != nil {
if !ok {
}
return ""
}
}
return val
}
type ebsMountArgs struct {
Size string
MountPoint string
VolumeType string
FSType string
Iops string
N string
Keep string
}
// Create a scratch volume using batchit
func makeScratchSpace(scratchSize string) string {
var args ebsMountArgs
args.Size = scratchSize
args.MountPoint = "/scratch"
args.N = "1"
// Let's not use >1 volume for now until problems are sorted out
// sz, _ := strconv.Atoi(args.Size) // we already checked that it's a number
// if sz > 200 {
// args.N = "2"
// }
progArgs := []string{"ebsmount", "--size", args.Size, "--mountpoint",
args.MountPoint, "-n", args.N}
retCode, volIds := getCmdOutput(batchitExe, progArgs)
if retCode != 0 {
fmt.Printf("ebsmount command failed with error %d, exiting.\n", retCode)
os.Exit(1)
}
return volIds
}
// Run a command and get its output.
// Exit if the command fails (may change this...)
func getCmdOutputOld(cmdName string, cmdArgs []string) string {
//FIXME need to see full output (stdout and stderr) while returning stdout
// and if we can do that, we only need 1 function for running commands...
fmt.Printf("getCmdOutput: cmdName is %s and cmdArgs is %v\n", cmdName, cmdArgs)
out, err := exec.Command(cmdName, cmdArgs...).Output()
if err != nil {
log.Fatal(err)
}
ret := string(out[:len(out)])
fmt.Printf("getCmdOutput: returning %s\n", ret)
return ret
}
func getCmdOutput(cmdName string, cmdArgs []string) (resultCode int, output string) {
// docker build current directory
// cmdName := "testt"
// cmdArgs := []string{"build", "."}
var buf bytes.Buffer
log.Printf("runcmd: Preparing to run %s with arguments %s.\n",
cmdName, cmdArgs)
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
os.Exit(1)
}
//cmd.Stderr = cmd.Stdout
stderrReader, err := cmd.StderrPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StderrPipe for Cmd", err)
os.Exit(1)
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("STDOUT: %s\n", scanner.Text())
buf.WriteString(scanner.Text())
buf.WriteString("\n")
}
}()
scannerStderr := bufio.NewScanner(stderrReader)
go func() {
for scannerStderr.Scan() {
fmt.Printf("STDERR: %s\n", scannerStderr.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
//os.Exit(1)
}
err = cmd.Wait()
output = strings.TrimSuffix(buf.String(), "\n")
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Printf("Exit Status: %d", status.ExitStatus())
return status.ExitStatus(), output
}
} else {
log.Printf("Exit Status: 0")
return 0, output
}
return -1, output // should never get here
}
// Run a command and display its stdout and stderr (combined).
// Errors are ok but the exit code is saved,
// with the expectation that entrypoint will exit with that code
// after it cleans up.
func runcmd(cmdName string, cmdArgs []string) int {
// docker build current directory
// cmdName := "testt"
// cmdArgs := []string{"build", "."}
log.Printf("runcmd: Preparing to run %s with arguments %s.\n",
cmdName, cmdArgs)
cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
os.Exit(1)
}
cmd.Stderr = cmd.Stdout
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
}
}()
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
//os.Exit(1)
}
err = cmd.Wait()
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
log.Printf("Exit Status: %d", status.ExitStatus())
return status.ExitStatus()
}
} else {
log.Printf("Exit Status: 0")
return 0
}
return -1 // should never get here
}
// does a list of strings contain a given string?
func contains(haystack []string, needle string) bool {
for _, h := range haystack {
if h == needle {
return true
}
}
return false
}