-
Notifications
You must be signed in to change notification settings - Fork 3
/
job.go
241 lines (224 loc) · 6.67 KB
/
job.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
humanize "github.com/dustin/go-humanize"
)
type Job struct {
sync.Mutex `json:"-"`
UUID string `json:"uuid"`
CommandLine []string `yaml:"commandLine" json:"command_line"`
Service *Service `json:"-"`
ParsedCommandLine []string `json:"-"`
FileMap map[string]string `json:"-"`
ZipMap map[string]string `json:"-"`
WorkingDirectory string `json:"-"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Status string `json:"status"`
Host string `json:"host"`
Port int `json:"port"`
Address []string `json:"notification_email_address"`
EndPoint string `json:"endpoint"`
// Registered channels
waiters []chan bool
// Running process, NB: the Cmd is started in the Job's WorkingDirectory
cmd *exec.Cmd
Output bytes.Buffer `json:"-"`
}
// Custom JSON output
// see http://choly.ca/post/go-json-marshalling/
func (job *Job) MarshalJSON() ([]byte, error) {
type Alias Job
f := func(t time.Time) string {
if t.IsZero() {
return ""
} else {
return humanize.Time(t)
}
}
return json.Marshal(&struct {
TempString string `json:"output"`
StartTimeHumanized string `json:"start_time_humanized"`
EndTimeHumanized string `json:"end_time_humanized"`
StartTimeFull string `json:"start_time_full"`
EndTimeFull string `json:"end_time_full"`
*Alias
}{
TempString: job.Output.String(),
StartTimeHumanized: humanize.Time(job.StartTime),
EndTimeHumanized: f(job.EndTime),
StartTimeFull: job.StartTime.Format(time.UnixDate),
EndTimeFull: job.EndTime.Format(time.UnixDate),
Alias: (*Alias)(job),
})
}
// Parse the commandline from the HTTP request
// The Job will run in the working directory, so paths must be relative.
// Store away enough information to get files back to the client.
func (job *Job) ParseCommandLine(request *http.Request) error {
log.Printf("Parsing command line for %v", job.UUID)
cl := make([]string, 0)
dir := job.WorkingDirectory
for _, arg := range job.CommandLine {
// Do we start with an #?
key := arg[1:]
prefix := arg[0]
if prefix == '#' {
// Lookup first in form
if request.MultipartForm.Value[key] != nil {
cl = append(cl, request.MultipartForm.Value[key][0])
} else {
// Look up in defaults
cl = append(cl, job.Service.Defaults[key])
}
} else if prefix == '<' {
// Do we have an < to indicate an uploaded file?
v := request.MultipartForm.File[key]
if v == nil {
return fmt.Errorf("Could not find %v in form data", key)
}
header := v[0]
// Save a temp file
fout, err := os.Create(filepath.Join(dir, filepath.Base(header.Filename)))
if err != nil {
return err
}
f, err := header.Open()
count, err := io.Copy(fout, f)
if err != nil {
return err
}
log.Printf("\tSaved uploaded file as %v (%v bytes)", header.Filename, count)
fout.Close()
cl = append(cl, filepath.Base(header.Filename))
} else if prefix == '>' {
// Write a file...
if request.MultipartForm.Value[key] == nil {
return fmt.Errorf("filename must be specified for %v", key)
}
job.FileMap[key] = filepath.Base(request.MultipartForm.Value[key][0])
log.Printf("\tOutput file %v", job.FileMap[key])
cl = append(cl, filepath.Base(request.MultipartForm.Value[key][0]))
} else if prefix == '^' {
// Expect a zip file, create a directory called key, unzip the contents and add to command line
v := request.MultipartForm.File[key]
if v == nil {
return fmt.Errorf("Could not find %v in form data, was expecting uploaded zip file", key)
}
// Save the uploaded file
header := v[0]
f, err := header.Open()
zipFilename := filepath.Join(dir, filepath.Base(header.Filename)+".zip")
fout, err := os.Create(zipFilename)
if err != nil {
return err
}
count, err := io.Copy(fout, f)
if err != nil {
return err
}
fout.Close()
defer os.Remove(zipFilename)
// Make the directory, wrating the contents there
dirName := filepath.Join(dir, filepath.Base(key))
err = os.Mkdir(dirName, 0700)
if err != nil {
return err
}
err = Unzip(zipFilename, dirName)
if err != nil {
return err
}
// If the directory has a single entry and it's a directory, do a quick shuffle
files, err := ioutil.ReadDir(dirName)
if err != nil {
return err
}
if len(files) == 1 && files[0].IsDir() {
// Shuffle
log.Printf("\tFound single directory, moving %v to %v", files[0].Name(), dirName)
os.Rename(filepath.Join(dirName, files[0].Name()), dirName+"-temp")
os.Remove(dirName)
os.Rename(dirName+"-temp", dirName)
}
log.Printf("\tExtracted zip file to %v (%s)", key, humanize.Bytes(uint64(count)))
cl = append(cl, filepath.Base(key))
} else if prefix == '~' {
if request.MultipartForm.Value[key] == nil {
return fmt.Errorf("filename must be specified for %v", key)
}
job.ZipMap[key] = filepath.Base(request.MultipartForm.Value[key][0])
if job.Service.CreateEmptyOutput {
err := os.Mkdir(filepath.Join(dir, job.ZipMap[key]), 0700)
if err != nil {
return err
}
}
log.Printf("\tOutput directory %v", job.ZipMap[key])
cl = append(cl, job.ZipMap[key])
} else {
cl = append(cl, arg)
}
}
log.Printf("Final command line: %v", cl)
job.ParsedCommandLine = cl
return nil
}
func (job *Job) Start() {
cmd := exec.Command(job.ParsedCommandLine[0], job.ParsedCommandLine[1:]...)
cmd.Dir = job.WorkingDirectory
job.StartTime = time.Now()
cmd.Stdout = &job.Output
cmd.Stderr = &job.Output
job.cmd = cmd
job.Status = "pending"
// Launch a go routine to wait
go func() {
jobMutex.Lock()
job.Status = "running"
job.cmd.Start()
err := job.cmd.Wait()
job.EndTime = time.Now()
if err != nil {
job.Status = "error"
} else {
if job.cmd.ProcessState.Success() {
job.Status = "success"
} else {
job.Status = "failed"
}
}
jobMutex.Unlock()
// Notify waiters
log.Printf("%v (%v) completed with status %v", job.Service.EndPoint, job.UUID, job.Status)
for _, c := range job.waiters {
c <- true
}
// Send email here
Email(job)
// Cleanup after 120 minutes
<-time.After(time.Minute * time.Duration(config.CleanupTimeInMinutes))
Cleanup(job)
}()
}
func (job *Job) Wait() {
if job.Status == "running" {
c := make(chan bool)
job.Lock()
job.waiters = append(job.waiters, c)
job.Unlock()
<-c
close(c)
}
}