-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
130 lines (113 loc) · 2.76 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
package main
import (
"encoding/json"
"fmt"
"github.com/docker/docker/client"
"github.com/gorhill/cronexpr"
"github.com/palicao/docker-executor/lib"
"log"
"net/http"
"strings"
"sync"
"time"
"unicode"
"flag"
)
type ApiResponse struct {
JobName string
StartTime time.Time
EndTime time.Time
Output []string
}
func main() {
configFile := flag.String("config", "./config.yaml", "specify the yaml config file location")
flag.Parse()
config, err := lib.GetConfigFromFile(*configFile)
if err != nil {
log.Fatalf("error reading config: %v", err)
}
cli, err := client.NewEnvClient()
if err != nil {
log.Fatalf("error creating client: %v", err)
}
api := lib.NewDockerApi(cli)
done := make(chan bool)
go startServer(config, api, done)
go scheduleJobs(config, api, done)
<-done
}
func scheduleJobs(config *lib.Config, api *lib.DockerApi, done chan bool) {
var wg sync.WaitGroup
for _, job := range config.Jobs {
if job.Schedule != "" {
scheduleJob(job, api, &wg)
}
}
wg.Wait()
done <- true
}
func startServer(config *lib.Config, api *lib.DockerApi, done chan bool) {
for jobName, job := range config.Jobs {
if job.ApiExpose == true {
http.HandleFunc("/jobs/run/"+jobName, func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
response := runJob(job, api)
endTime := time.Now()
res := ApiResponse{
JobName: jobName,
StartTime: startTime,
EndTime: endTime,
Output: prepareOutput(response),
}
js, err := json.Marshal(res)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
})
}
}
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalf("error starting http server: %v", err)
}
done <- true
}
func prepareOutput(in []byte) []string {
s := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) || unicode.IsSpace(r) {
return r
}
return -1
}, string(in))
return strings.Split(strings.Trim(s, "\n"), "\n")
}
func runJob(job lib.Job, api *lib.DockerApi) []byte {
fmt.Printf("running job %s\n", time.Now().Format("15:04:05"))
if job.Type == lib.JobTypeRun {
response, err := api.RunJobAsContainer(job)
if err != nil {
log.Fatalf("error running container: %v", err)
}
return response
} else {
response, err := api.RunJobAsService(job)
if err != nil {
log.Fatalf("error running service: %v", err)
}
return response
}
}
func scheduleJob(job lib.Job, api *lib.DockerApi, wg *sync.WaitGroup) {
nextTime := cronexpr.MustParse(job.Schedule).Next(time.Now())
wg.Add(1)
go func() {
time.Sleep(nextTime.Sub(time.Now()))
scheduleJob(job, api, wg)
response := runJob(job, api)
fmt.Println(string(response))
wg.Done()
}()
}