-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
186 lines (165 loc) · 3.89 KB
/
cron.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
package dksync
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
)
type CronSync struct {
// Specifies Concurrency with
// Available options
// "allow"
// "forbid"
Concurrency string
// On create allows to enable/disable Job synchronization
DisableJob bool
// Specifies job Retries on fail
Retries int
// Specifies Tags
Tags string
// Specifies Executor type
// Available options
// "shell"
// "http"
// "rabbitmq"
Executor string
// Specifies Processor type
// "mail": {...}
// "log": {...}
// "redis": {...}
Processor map[string]map[string]string
// Optional field
// Specifies Job DisplayName
// if not empty will be used instead of OS Hostname
DisplayName string
// test before sync
DryRun bool
// Unexported fields
// will be assigned only when fetch crontab file
cmd string
internalName string
env string
}
var (
hostBuilder strings.Builder
hostname string
err error
)
// Processor retrieves jobs from a crontab file
// then sync to API
func (s *CronSync) JobsProcessor(path string) {
checkHost()
source, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer source.Close()
content, err := ioutil.ReadAll(source)
if err != nil {
log.Error(err)
}
if s.DryRun {
log.Infoln("Run simulation...")
}
// parse crontab file
crontab := strings.Split(parser(content), "\n")
for rawCmd := range crontab {
if crontab[rawCmd] != "" {
cmd := strings.Fields(crontab[rawCmd])
if len(cmd) > 4 {
if s.env = parseEnv(cmd[5]); s.env != "" {
s.cmd = strings.Join(cmd[6:], " ")
} else {
s.cmd = strings.Join(cmd[5:], " ")
}
s.scheduler(cmd[:5], rawCmd)
}
}
}
log.Infoln("Crontab file synchronized")
}
// scheduler build payload
// check if newly defined job already exists
// if not create job
func (s *CronSync) scheduler(cmd []string, id int) {
s.getHostname(id)
payload := &job {
Name: s.internalName,
DisplayName: fmt.Sprintf("%s-%s", s.internalName, parseJobName(s.cmd)),
Timezone: "Europe/Lisbon",
Schedule: fmt.Sprintf("0 %s", strings.Join(cmd, " ")),
Owner: "KK",
OwnerEmail: "tecnica@kk.pt",
Disabled: s.DisableJob,
Metadata: nil,
Processors: s.Processor,
Retries: s.Retries,
Concurrency: s.Concurrency,
Executor: s.Executor,
}
payload.ExecutorConfig.Command = s.cmd
payload.ExecutorConfig.Env = s.env
if err := json.Unmarshal([]byte(s.Tags), &payload.Tags); err != nil {
log.Fatal(err)
}
if s.Executor == "shell" {
payload.ExecutorConfig.Shell = "true"
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
log.Errorln(err)
}
if status := payload.checkJob(); status == 200 {
if s.DryRun {
log.Infoln(string(jsonPayload))
}
return
}
// simulate sync
if s.DryRun {
log.Infoln(string(jsonPayload))
return
}
payload.create()
}
// getHostname get current node hostname
// then replace hostname letter "." with "_"
func (s *CronSync) getHostname(id int) {
hostname, err = os.Hostname()
if err != nil {
if s.DisplayName != "" {
hostBuilder.WriteString(s.DisplayName)
} else {
hostBuilder.WriteString("KK-Node-")
}
hostBuilder.WriteString(strconv.Itoa(id))
} else {
if s.DisplayName != "" {
hostBuilder.WriteString(s.DisplayName)
} else {
hostBuilder.WriteString(hostname)
}
hostBuilder.WriteString("-")
hostBuilder.WriteString(strconv.Itoa(id))
}
s.internalName = strings.ToLower(
strings.ReplaceAll(hostBuilder.String(), ".", "_"))
}
// parseJobName parses job name from executable file
func parseJobName(job string) string {
split := regexp.MustCompile("([.][a-z]* )").Split(job, -1)
rawSplit := strings.Split(split[0], "/")
return rawSplit[len(rawSplit) - 1]
}
// parseEnv parses environment variables
func parseEnv(command string) string {
split := strings.Split(command, "=")
if len(split) > 1 {
return command
}
return ""
}