-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
385 lines (350 loc) · 9.01 KB
/
main.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"text/template"
"time"
"github.com/jessevdk/go-flags"
"github.com/shxsun/goyaml"
)
var quitProgram = false
func renderTemplateFile(outfile string, tmplText string, data interface{}) (err error) {
//tmpl, err := template.ParseFiles(tmplFile)
tmpl, err := template.New("render").Parse(tmplText)
if err != nil {
return
}
fd, err := os.Create(outfile)
if err != nil {
return err
}
defer fd.Close()
err = tmpl.Execute(fd, data)
return
}
func renderWithDefault(tmplFile string, defaultTmpl []byte, data interface{}) (out string, err error) {
buf := &bytes.Buffer{}
if !fileExists(tmplFile) {
ioutil.WriteFile(tmplFile, defaultTmpl, 0644)
}
tmpl, err := template.ParseFiles(tmplFile)
if err != nil {
return
}
err = tmpl.Execute(buf, data)
return string(buf.Bytes()), err
}
func renderFile(outfile string, defaultTmpl []byte, data interface{}) (err error) {
//if !fileExists(tmplFile) {
// ioutil.WriteFile(tmplFile, defaultTmpl, 0644)
//}
// ignore: tmplFile
return renderTemplateFile(outfile, string(defaultTmpl), data)
}
func dumpFile(filename string, data interface{}) (err error) {
out, err := goyaml.Marshal(data)
if err != nil {
return
}
return ioutil.WriteFile(filename, out, 0644)
}
func loadFile(filename string, data interface{}) (err error) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
return
}
return goyaml.Unmarshal(raw, data)
}
func ignore(info os.FileInfo) bool {
if info.IsDir() {
if info.Name() != "." && info.Name() != ".." &&
strings.HasPrefix(info.Name(), ".") { // ignore hidden dir
return true
}
} else {
return strings.HasPrefix(info.Name(), ".")
}
return false
}
func currPathCfg(path string) *GlobalConfig {
return mycnf
}
func pathWalk(path string, depth int) (files []string, err error) {
files = make([]string, 0)
path = filepath.Clean(path) // remove extra /
baseNumSeps := strings.Count(path, string(os.PathSeparator))
err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
pathDepth := strings.Count(path, string(os.PathSeparator)) - baseNumSeps
cfgPath := filepath.Join(path, CONFIG_FILE)
if fileExists(cfgPath) {
localCfg := mycnf
loadFile(cfgPath, localCfg)
}
if pathDepth > depth {
return filepath.SkipDir
}
if ignore(info) {
return filepath.SkipDir
}
} else if info.Mode().IsRegular() && !ignore(info) {
if matched, _ := regexp.Match(mycnf.Include, []byte(info.Name())); matched {
files = append(files, path)
}
}
return nil
})
return
}
type GlobalConfig struct {
Replacer string `yaml:"-"` //short:"i" description:"replacer"`
Command string `short:"c" description:"specify how to process each file"`
Include string `short:"I" long:"include-regex" description:"regex set which file can be included"`
MaxFail int `long:"max-fail" description:"max failed case"`
Path string `short:"p" long:"path" description:"path for search"`
Depth int `short:"d" long:"depth" description:"depth to travel directory tree"`
Verbose bool `short:"v" long:"verbose" description:"show verbose output"`
Timeout string `short:"t" long:"timeout" description:"timeout for each exec"`
Reload bool `short:"r" long:"failed" description:"reload all failed cmd, run again:"`
Exclude string `yaml:"-"`
Version bool `yaml:"-"`
InitYaml bool `yaml:"-" long:"init" description:"create a sample .trival.yml and exit"`
Result string `yaml:"html" long:"html" description:"output result as html"`
Notify []string `yaml:"notify" long:"notify" description:"notify people when task finish"`
}
var mycnf = &GlobalConfig{
Depth: 0,
Path: "./",
Replacer: "{}",
Version: false,
Result: "test.html",
Timeout: "30m",
Command: "./{}",
Include: ".*",
Verbose: false,
}
func init() {
if fileExists(CONFIG_FILE) {
loadFile(CONFIG_FILE, mycnf)
}
args, err := flags.Parse(mycnf)
_, _ = args, err
if err != nil {
if er, ok := err.(*flags.Error); ok && er.Type == flags.ErrHelp {
os.Exit(0)
}
log.Fatal(err)
}
if mycnf.InitYaml {
dumpFile(CONFIG_FILE, mycnf)
os.Exit(0)
}
if mycnf.Version {
fmt.Println(VERSION)
os.Exit(0)
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
type TaskConfig struct {
Replacer string
Command string
Timeout time.Duration
Files []string
}
type TaskResult struct {
StartTime string
TimeCost time.Duration
Command string
Filename string
Output string // console out
Source string // source code
Error error
}
func Go(f func() error) chan error {
ch := make(chan error)
go func() {
err := f()
ch <- err
}()
return ch
}
func groupKill(cmd *exec.Cmd) (err error) {
var pid, pgid int
if cmd.Process != nil {
pid = cmd.Process.Pid
c := exec.Command("/bin/ps", "-o", "pgid", "-p", strconv.Itoa(pid), "--no-header")
var out []byte
out, err = c.Output()
if err != nil {
return
}
_, err = fmt.Sscanf(string(out), "%d", &pgid)
if err != nil {
return
}
err = exec.Command("/bin/kill", "-TERM", "-"+strconv.Itoa(pgid)).Run()
}
return
}
func work(cfg *TaskConfig) (results []TaskResult) {
var err error
var failcnt = 0
results = []TaskResult{}
for i, file := range cfg.Files {
start := time.Now()
r := TaskResult{
StartTime: start.Format("15:04:05"),
Filename: file,
}
c := strings.Replace(cfg.Command, cfg.Replacer, file, -1)
r.Command = c
if mycnf.MaxFail != 0 && failcnt >= mycnf.MaxFail {
quitProgram = true
}
if quitProgram {
r.Error = errors.New("skip test")
results = append(results, r)
break
}
prefix := fmt.Sprintf("\r\033[36m>>>\033[0m %-5d", i)
format := fmt.Sprintf(prefix+"%%-14v %-24s\n", c) //file) //file)
// show current exec file
fmt.Printf(prefix+"exec %s ...", strconv.Quote(file))
if mycnf.Verbose {
fmt.Printf("\n")
}
output := bytes.NewBuffer(nil)
cmd := exec.Command("/bin/bash", "-c", c)
if mycnf.Verbose {
cmd.Stdout = io.MultiWriter(os.Stdout, output)
cmd.Stderr = io.MultiWriter(os.Stderr, output)
} else {
cmd.Stdout = output
cmd.Stderr = output
}
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Setpgid = true
// handle timeout
if err = cmd.Start(); err == nil {
select {
case <-sigC:
quitProgram = true
groupKill(cmd)
err = errors.New("signal: interrupt")
case <-time.After(cfg.Timeout):
//cmd.Process.Kill()
groupKill(cmd)
err = errors.New("signal: terminated")
case err = <-Go(cmd.Wait):
}
}
if err != nil {
r.Error = err
failcnt += 1
fmt.Printf(format, fmt.Sprintf("\033[33m"+"err: %s"+"\033[0m", err))
} else {
fmt.Printf(format, "\033[32m"+"success"+"\033[0m")
}
r.TimeCost = time.Now().Sub(start)
r.Output = string(output.Bytes())
r.Source = "unfinished(todo)"
results = append(results, r)
}
return
}
func fileExists(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.Mode().IsRegular()
}
func selfPath() string {
return filepath.Dir(os.Args[0])
}
const STATE_FILE = "state-travel.yml"
const CONFIG_FILE = ".travel.yml"
var cfgTree = map[string]*GlobalConfig{}
func main() {
var err error
var taskcfg = &TaskConfig{}
var startTime = time.Now()
cfgPath := filepath.Join(mycnf.Path, CONFIG_FILE)
loadFile(cfgPath, mycnf)
if mycnf.Reload && fileExists(STATE_FILE) {
err = loadFile(STATE_FILE, taskcfg)
if err != nil {
log.Fatal(err)
}
} else {
files, err := pathWalk(mycnf.Path, mycnf.Depth)
if err != nil {
log.Fatal(err)
}
taskcfg.Command = mycnf.Command
taskcfg.Replacer = mycnf.Replacer
timeout, err := time.ParseDuration(mycnf.Timeout)
if err != nil {
log.Fatal(err)
}
taskcfg.Timeout = timeout
taskcfg.Files = files
}
results := work(taskcfg)
errfiles := []string{}
errCnt, totCnt := 0, 0
for _, r := range results {
if r.Error != nil {
errCnt++
errfiles = append(errfiles, r.Filename)
}
totCnt++
}
defer func() {
if errCnt != 0 {
os.Exit(13) // 13 is a lucky number
}
}()
// reder html
data := map[string]interface{}{}
data["StartTime"] = startTime.Format("2006-01-02 15:04:05")
endTime := time.Now()
data["TimeCost"] = endTime.Sub(startTime)
data["Tasks"] = results
hostname, _ := os.Hostname()
data["Host"] = hostname
data["Total"] = len(taskcfg.Files)
data["FailCount"] = errCnt
//htmlTmplPath := filepath.Join(selfPath(), ".html.tmpl")
err = renderTemplateFile(mycnf.Result, defaultTemplate, data)
if err != nil {
log.Fatal(err)
}
htmlData, _ := ioutil.ReadFile(mycnf.Result)
// save to restart again
taskcfg.Files = errfiles
err = dumpFile(STATE_FILE, taskcfg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("summary: (total: %d fail: %d)\n", totCnt, errCnt)
smsTmplPath := filepath.Join(selfPath(), ".sms.tmpl")
if !quitProgram && len(mycnf.Notify) != 0 {
msg, err := renderWithDefault(smsTmplPath, defaultSMSTemplate, data)
if err != nil {
log.Fatal(err)
}
err = sendNotify(msg, htmlData, mycnf.Notify...)
if err != nil {
log.Fatal(err)
}
}
}