This seems to work now.
ci/woodpecker/push/woodpecker Pipeline failed
Details
ci/woodpecker/push/woodpecker Pipeline failed
Details
This commit is contained in:
parent
a6b6f5cbf6
commit
789f94cbaf
|
|
@ -1 +1,2 @@
|
||||||
A /newfile.txt
|
A /newfile.txt
|
||||||
|
A /detectme.txt
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1 @@
|
||||||
D /file1.txt
|
A /detectme.yml
|
||||||
A /file2.txt
|
|
||||||
C /do/match/file3.txt
|
|
||||||
A /dontmatch.yml
|
|
||||||
D /dont/match.md
|
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
174
main.go
174
main.go
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -24,60 +23,100 @@ type ContainerInfo struct {
|
||||||
Ignores []string
|
Ignores []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var notifiedChanges = struct {
|
type MonitoredContainer struct {
|
||||||
sync.RWMutex
|
Info ContainerInfo
|
||||||
changes map[string]string
|
LastChecked time.Time
|
||||||
}{changes: make(map[string]string)}
|
}
|
||||||
|
|
||||||
var apiAddress string
|
var (
|
||||||
|
apiAddress string
|
||||||
|
glitchtipDSN string
|
||||||
|
notifiedChanges = make(map[string]string)
|
||||||
|
monitoredContainers = make(map[string]*MonitoredContainer)
|
||||||
|
mu sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
apiAddress = os.Getenv("API_ADDRESS")
|
apiAddress = os.Getenv("API_ADDRESS")
|
||||||
if apiAddress == "" {
|
if apiAddress == "" {
|
||||||
apiAddress = "http://localhost:8080"
|
apiAddress = "http://localhost:8080"
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
glitchtipDSN = os.Getenv("GLITCHTIP_DSN")
|
||||||
// Check if GLITCHTIP_DSN environment variable is set
|
|
||||||
glitchtipDSN := os.Getenv("GLITCHTIP_DSN")
|
|
||||||
if glitchtipDSN == "" {
|
if glitchtipDSN == "" {
|
||||||
log.Fatal("GLITCHTIP_DSN environment variable is not set")
|
log.Fatal("GLITCHTIP_DSN environment variable is not set")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
log.Println("Starting Oculus...")
|
log.Println("Starting Oculus...")
|
||||||
|
|
||||||
// Ensure the diffs directory exists
|
// Ensure the diffs directory exists
|
||||||
err := ensureDiffsDirectory()
|
err := os.MkdirAll("./diffs", os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error creating diffs directory: %v", err)
|
log.Fatalf("Error creating diffs directory: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
containers, err := fetchContainers()
|
err := fetchAndMonitorContainers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching containers: %v", err)
|
log.Printf("Error in fetching and monitoring containers: %v", err)
|
||||||
time.Sleep(1 * time.Minute)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
time.Sleep(1 * time.Second)
|
||||||
for _, container := range containers {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(container ContainerInfo) {
|
|
||||||
defer wg.Done()
|
|
||||||
checkAndNotify(container)
|
|
||||||
}(container)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
cleanupHashes(containers)
|
|
||||||
|
|
||||||
// Sleep for 1 minute before checking for new containers again
|
|
||||||
time.Sleep(1 * time.Minute)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fetchAndMonitorContainers() error {
|
||||||
|
containers, err := fetchContainers()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, container := range containers {
|
||||||
|
mu.Lock()
|
||||||
|
if _, exists := monitoredContainers[container.ID]; !exists {
|
||||||
|
interval, err := time.ParseDuration(container.Interval)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Invalid interval for container %s (%s): %v", container.Name, container.ID, err)
|
||||||
|
mu.Unlock()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
monitoredContainers[container.ID] = &MonitoredContainer{
|
||||||
|
Info: container,
|
||||||
|
LastChecked: time.Now().Add(-interval),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for _, monitoredContainer := range monitoredContainers {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(mc *MonitoredContainer) {
|
||||||
|
defer wg.Done()
|
||||||
|
interval, err := time.ParseDuration(mc.Info.Interval)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Invalid interval for container %s (%s): %v", mc.Info.Name, mc.Info.ID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
if time.Since(mc.LastChecked) >= interval {
|
||||||
|
mc.LastChecked = time.Now()
|
||||||
|
mu.Unlock()
|
||||||
|
checkAndNotify(mc.Info)
|
||||||
|
} else {
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
}(monitoredContainer)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func fetchContainers() ([]ContainerInfo, error) {
|
func fetchContainers() ([]ContainerInfo, error) {
|
||||||
resp, err := http.Get(fmt.Sprintf("%s/containers", apiAddress))
|
resp, err := http.Get(fmt.Sprintf("%s/containers", apiAddress))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -134,9 +173,9 @@ func checkAndNotify(container ContainerInfo) {
|
||||||
diffHash := fmt.Sprintf("%x", md5.Sum([]byte(filteredOutput)))
|
diffHash := fmt.Sprintf("%x", md5.Sum([]byte(filteredOutput)))
|
||||||
log.Printf("Diff hash for container %s: %s", container.Name, diffHash)
|
log.Printf("Diff hash for container %s: %s", container.Name, diffHash)
|
||||||
|
|
||||||
notifiedChanges.RLock()
|
mu.Lock()
|
||||||
lastNotifiedHash, notified := notifiedChanges.changes[container.ID]
|
lastNotifiedHash, notified := notifiedChanges[container.ID]
|
||||||
notifiedChanges.RUnlock()
|
mu.Unlock()
|
||||||
|
|
||||||
if !notified || lastNotifiedHash != diffHash {
|
if !notified || lastNotifiedHash != diffHash {
|
||||||
filename := filepath.Join("diffs", fmt.Sprintf("%s.diff", container.CName))
|
filename := filepath.Join("diffs", fmt.Sprintf("%s.diff", container.CName))
|
||||||
|
|
@ -150,9 +189,9 @@ func checkAndNotify(container ContainerInfo) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error sending notification for container %s: %v", container.ID, err)
|
log.Printf("Error sending notification for container %s: %v", container.ID, err)
|
||||||
} else {
|
} else {
|
||||||
notifiedChanges.Lock()
|
mu.Lock()
|
||||||
notifiedChanges.changes[container.ID] = diffHash
|
notifiedChanges[container.ID] = diffHash
|
||||||
notifiedChanges.Unlock()
|
mu.Unlock()
|
||||||
log.Printf("Notification sent and hash updated for container: %s (%s)", container.Name, container.ID)
|
log.Printf("Notification sent and hash updated for container: %s (%s)", container.Name, container.ID)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -172,44 +211,25 @@ func filterDiffOutput(diffOutput, cname string, ignores []string) (string, error
|
||||||
return "", fmt.Errorf("error writing diff to file: %s", err)
|
return "", fmt.Errorf("error writing diff to file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and filter the diff output
|
// Construct the filter command
|
||||||
var filteredLines []string
|
args := append([]string{filename}, ignores...)
|
||||||
file, err := os.Open(filename)
|
cmd := exec.Command("filter", args...)
|
||||||
|
fullCommand := fmt.Sprintf("filter %s", strings.Join(cmd.Args, " "))
|
||||||
|
log.Printf("Running command: %s", fullCommand)
|
||||||
|
fmt.Printf("Running command: %s\n", fullCommand) // Print the command to stdout for debugging
|
||||||
|
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error opening diff file: %s", err)
|
return "", fmt.Errorf("error running filter command: %s, output: %s", err, output)
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
ignore := false
|
|
||||||
for _, pattern := range ignores {
|
|
||||||
match, err := filepath.Match(pattern, line)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("error matching pattern: %s", err)
|
|
||||||
}
|
|
||||||
if match {
|
|
||||||
ignore = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !ignore {
|
|
||||||
filteredLines = append(filteredLines, line)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
// Read the filtered output
|
||||||
return "", fmt.Errorf("error reading diff file: %s", err)
|
filteredOutput, err := ioutil.ReadFile(filename)
|
||||||
}
|
|
||||||
|
|
||||||
filteredOutput := strings.Join(filteredLines, "\n")
|
|
||||||
err = ioutil.WriteFile(filename, []byte(filteredOutput), 0644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error writing filtered diff to file: %s", err)
|
return "", fmt.Errorf("error reading filtered diff file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filteredOutput, nil
|
return string(filteredOutput), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeToFile(filename, content string) error {
|
func writeToFile(filename, content string) error {
|
||||||
|
|
@ -233,23 +253,3 @@ func sendNotification(content string) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureDiffsDirectory() error {
|
|
||||||
return os.MkdirAll("./diffs", os.ModePerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanupHashes(containers []ContainerInfo) {
|
|
||||||
notifiedChanges.Lock()
|
|
||||||
defer notifiedChanges.Unlock()
|
|
||||||
|
|
||||||
activeContainers := make(map[string]bool)
|
|
||||||
for _, container := range containers {
|
|
||||||
activeContainers[container.ID] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
for id := range notifiedChanges.changes {
|
|
||||||
if !activeContainers[id] {
|
|
||||||
delete(notifiedChanges.changes, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue