This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
182 lines (160 loc) · 4.39 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
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"time"
"github.com/fsnotify/fsnotify"
"github.com/galleybytes/monitor/pkg/handlers"
gocache "github.com/patrickmn/go-cache"
)
var (
err error
watcher *fsnotify.Watcher
clusterName string
resourceUUID string
resourceName string
resourceNamespace string
resourceGeneration string
generationsDir string
managerServiceHost string
)
// addWatcher adds files under the generation dir
func addWatcher(fileInfo fs.FileInfo, path string) {
file := filepath.Join(path, fileInfo.Name())
if _, err := os.Stat(file); err == nil {
if filepath.Ext(file) == ".out" {
watcher.Add(file)
}
}
}
func init() {
managerServiceHost = os.Getenv("MONITOR_MANAGER_SERVICE_HOST")
if managerServiceHost == "" {
log.Fatal("MONITOR_MANAGER_SERVICE_HOST cannot be empty")
}
clusterName = os.Getenv("CLUSTER_NAME")
if clusterName == "" {
log.Fatal("CLUSTER_NAME cannot be empty")
}
resourceUUID = os.Getenv("TFO_RESOURCE_UUID")
if resourceUUID == "" {
log.Fatal("TFO_RESOURCE_UUID cannot be empty")
}
resourceNamespace = os.Getenv("TFO_NAMESPACE")
if resourceNamespace == "" {
log.Fatal("TFO_NAMESPACE cannot be empty")
}
resourceName = os.Getenv("TFO_RESOURCE")
if resourceName == "" {
log.Fatal("TFO_RESOURCE cannot be empty")
}
resourceGeneration = os.Getenv("TFO_GENERATION")
if resourceGeneration == "" {
log.Fatal("TFO_GENERATION cannot be empty")
}
generationsRootDir := os.Getenv("TFO_ROOT_PATH")
if generationsRootDir == "" {
log.Fatal("TFO_ROOT_PATH cannot be empty")
}
generationsDir = fmt.Sprintf("%s/generations/%s", generationsRootDir, resourceGeneration)
}
func main() {
// cancelChan := make(chan os.Signal, 1)
// // catch SIGETRM or SIGINTERRUPT
// signal.Notify(cancelChan, syscall.SIGTERM, syscall.SIGINT)
// go func() {
// for {
// time.Sleep(time.Second)
// }
// }()
cache := gocache.New(gocache.NoExpiration, gocache.NoExpiration)
requestHandler := handlers.New(managerServiceHost+"/api-token-please", cache)
cluster := requestHandler.GetOrSetCluster(clusterName)
tfoResource := requestHandler.GetOrSetTFOResource(resourceUUID, resourceNamespace, resourceName, resourceGeneration, *cluster)
log.Print("TFO Resource is ", tfoResource.Namespace, "/", tfoResource.Name, ", UUID:", tfoResource.UUID)
watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
log.Print("Finding files")
for {
fileInfo, err := os.Stat(generationsDir)
if err == nil {
if fileInfo.IsDir() {
break
}
}
time.Sleep(50 * time.Millisecond)
}
watcher.Add(generationsDir)
// List the directory of the generation which is where logs will be
fileInfos, err := ioutil.ReadDir(generationsDir)
if err != nil {
log.Fatal(err)
}
// Read in all files on init
for _, fileInfo := range fileInfos {
file := filepath.Join(generationsDir, fileInfo.Name())
isLog, taskType, rerun, generation, uid := handlers.ParseFile(file)
if !isLog {
continue
}
requestHandler.EventWriter(file, tfoResource, taskType, generation, rerun, uid)
}
log.Print("Starting log watcher")
// done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Printf("event: '%s': %s", event.Name, event.Op)
if event.Op == fsnotify.Create {
file, err := os.Stat(event.Name)
if err == nil {
if file.IsDir() {
// I don't think we need any more watchers?
// addWatcher(file, generationsDir)
continue
}
}
}
if event.Op == fsnotify.Create || event.Op == fsnotify.Write {
isLog, taskType, rerun, generation, uid := handlers.ParseFile(event.Name)
if !isLog {
continue
}
requestHandler.EventWriter(event.Name, tfoResource, taskType, generation, rerun, uid)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
// case sig := <-cancelChan:
// log.Printf("Caught SIGTERM %v", sig)
// done <- true
}
}
}()
// Start a poll for messages on the approvals model
log.Println("Starting approval watcher")
for {
items := cache.Items()
uids := []string{}
for key, _ := range items {
uids = append(uids, key)
}
requestHandler.FindApprovals(uids, generationsDir)
time.Sleep(15 * time.Second)
}
// // Wait until done is returned.
// <-done
}