-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
264 lines (223 loc) · 6.82 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
package main
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/robfig/cron/v3"
)
var (
TotalMessageCount int
TotalWarningCount int
TotalErrorCount int
NotificationMessage string
)
var Cron = cron.New()
func main() {
LoadEnvVariables()
if len(os.Args) > 1 {
switch os.Args[1] {
case "healthcheck":
healthCheck()
}
}
if err := CompileRegex(); err != nil {
panic(err)
}
var ListenerWaitGroup sync.WaitGroup
ListenerWaitGroup.Add(1)
go startHttpServer(&ListenerWaitGroup)
ListenerWaitGroup.Wait()
// Cron = cron.New()
Cron.AddFunc(CronSchedule, CronProcess)
if ImportOnStartup {
var StartupWaitGroup sync.WaitGroup
StartupWaitGroup.Add(1)
go func() {
Process()
StartupWaitGroup.Done()
}()
StartupWaitGroup.Wait()
}
Cron.Start()
fmt.Printf("Next Import: %s\n", Cron.Entries()[0].Schedule.Next(time.Now()))
select {} // TODO: replace with sync.WaitGroup
// https://stackoverflow.com/questions/42752705/prevent-the-main-function-from-terminating-before-goroutines-finish-in-golang
}
func healthCheck() {
response, err := http.Get(fmt.Sprintf("http://localhost:%d/status", httpListenPort))
switch {
case err != nil:
os.Exit(1)
case response.StatusCode != 200:
os.Exit(1)
default:
os.Exit(0)
}
}
func CronProcess() {
Process()
fmt.Printf("Next Import: %s\n", Cron.Entries()[0].Schedule.Next(time.Now()))
}
func GetErrorMessage(ExitCode int) string {
switch ExitCode {
case 1:
return "Generic error or unspecified problem during import"
case 64:
return "Cannot connect to Firefly III"
case 65:
return "Invalid path provided"
case 66:
return "Path is not allowed"
case 67:
return "There are no files in the provided directory"
case 68:
return "Cannot read configuration file"
case 69:
return "Cannot parse configuration file"
case 70:
return "The importable file cannot be found"
case 71:
return "The importable file cannot be read"
case 72:
return "Too many errors processing the data in the importable file"
case 73:
return "Nothing was imported during this run"
}
return ""
}
func Process() error {
statusEndPoint.ImportRunning = true
defer func() {
statusEndPoint.ImportRunning = false
statusEndPoint.LastImport = time.Now()
}()
fmt.Printf(Blue + "Starting Import Job\n" + Reset)
fmt.Printf(Blue + "-------------------\n" + Reset)
files, err := getDockerDirContents(DockerImportDir, ".json")
if err != nil {
return err
}
JsonFileCount := len(files)
MaxFileNameLength := 0
for _, dockerFileContent := range files {
if len(dockerFileContent.FileName) > MaxFileNameLength {
MaxFileNameLength = len(dockerFileContent.FileName)
}
}
TotalMessageCount = 0
TotalWarningCount = 0
TotalErrorCount = 0
for index, dockerFileContent := range files {
// Add trailing whitespace to filename so they appear uniform length on terminal output
PaddedFileName := dockerFileContent.FileName
for {
if len(PaddedFileName) >= MaxFileNameLength {
break
}
PaddedFileName += " "
}
formatString := "%0" + fmt.Sprintf("%d", len(strconv.Itoa(JsonFileCount))) + "d"
fmt.Printf("["+formatString+"/"+formatString+"] %s ", index+1, JsonFileCount, PaddedFileName)
if ExecResult, err := ProcessJsonFile(dockerFileContent.FilePath); err != nil {
ReportError := ExecResult.ExitCode != 73
DisplayName := strings.ToUpper(strings.TrimSuffix(dockerFileContent.FileName, filepath.Ext(dockerFileContent.FileName)))
NotificationMessageLine := fmt.Sprintf("- %s: ExitCode %d (%s)", DisplayName, ExecResult.ExitCode, GetErrorMessage(ExecResult.ExitCode))
logID, err := recordLog(ExecResult.StdOut)
if err == nil {
NotificationMessageLine += fmt.Sprintf(" [View Log](%s/logs/%s)", httpBaseURL, logID)
}
if NotificationMessage != "" {
NotificationMessage += "\n"
}
if ReportError {
NotificationMessage += NotificationMessageLine
TotalErrorCount += 1
}
NotificationMessageLine = ""
fmt.Printf(Red+"X"+Reset+" | Error - log stored at /logs/%s.log\n", logID)
}
}
if TotalMessageCount+TotalWarningCount+TotalErrorCount == 0 {
fmt.Println(Green + "No new messages - no notification sent" + Reset)
} else {
notification{
Title: "Data Imported",
Message: NotificationMessage,
GotifyExtras: &gotifyExtras{
GotifyClientDisplay: &gotifyClientDisplay{
GotifyContentType: "text/markdown"}},
}.Send()
}
Response, err := http.Get(UptimeKumaUrl)
switch {
case err != nil:
fmt.Printf("Error: %s\n", err.Error())
case Response.StatusCode != 200:
fmt.Printf("UptimeKuma Error: %s\n", Response.Status)
}
fmt.Printf(Blue + "-------------------\n" + Reset)
fmt.Printf(Blue + "Import Job Finished\n" + Reset)
return nil
}
func recordLog(message string) (string, error) {
logID := uuid.NewString()
err := os.WriteFile(fmt.Sprintf("/logs/%s.log", logID), []byte(message), 0644) //Todo, add error check
return logID, err
}
func ProcessJsonFile(FilePath string) (ExecResult, error) {
FileName := filepath.Base(FilePath)
ExecResult, err := Exec(Context, DockerContainerName, "www-data", []string{
"php",
"artisan",
"importer:import",
FilePath})
if err != nil {
return ExecResult, err
}
MessageCountTextArray := MessageCountExtractRegex.FindStringSubmatch(ExecResult.StdOut)
WarningCountTextArray := WarningCountExtractRegex.FindStringSubmatch(ExecResult.StdOut)
ErrorCountTextArray := ErrorCountExtractRegex.FindStringSubmatch(ExecResult.StdOut)
if len(MessageCountTextArray) != 2 ||
len(WarningCountTextArray) != 2 ||
len(ErrorCountTextArray) != 2 {
// Notify()
return ExecResult, fmt.Errorf("TextArrays do not all contain 2 elements")
}
MessageCount, errMessageCount := strconv.Atoi(MessageCountTextArray[1])
WarningCount, errWarningCount := strconv.Atoi(WarningCountTextArray[1])
ErrorCount, errErrorCount := strconv.Atoi(ErrorCountTextArray[1])
if err := errors.Join(errMessageCount, errWarningCount, errErrorCount); err != nil {
return ExecResult, err
}
TotalMessageCount += MessageCount
TotalWarningCount += WarningCount
TotalErrorCount += ErrorCount
fmt.Printf(Green+"✓"+Reset+" | %d NEW %d WARNINGS %d ERRORS\n", MessageCount, WarningCount, ErrorCount)
if MessageCount+WarningCount+ErrorCount == 0 {
return ExecResult, nil
}
DisplayName := strings.ToUpper(strings.TrimSuffix(FileName, filepath.Ext(FileName)))
NotificationMessageLine := fmt.Sprintf("- %s:", DisplayName)
if MessageCount > 0 {
NotificationMessageLine += fmt.Sprintf(" %d NEW", MessageCount)
}
if WarningCount > 0 {
NotificationMessageLine += fmt.Sprintf(" %d WARNINGS", WarningCount)
}
if ErrorCount > 0 {
NotificationMessageLine += fmt.Sprintf(" %d ERRORS", ErrorCount)
}
if NotificationMessage != "" {
NotificationMessage += "\n"
}
NotificationMessage += NotificationMessageLine
NotificationMessageLine = ""
return ExecResult, nil
}