Fixup notification logging to stdout and notifications fixes.
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Colin 2024-06-13 13:46:35 -04:00
parent 895c79f493
commit 44d2ac7d70
8 changed files with 16 additions and 0 deletions

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.

16
main.go
View File

@ -16,6 +16,7 @@ import (
"time"
)
// ContainerInfo represents the information about a container.
type ContainerInfo struct {
ID string
Name string
@ -24,6 +25,7 @@ type ContainerInfo struct {
Ignores []string
}
// MonitoredContainer represents a container being monitored.
type MonitoredContainer struct {
Info ContainerInfo
LastChecked time.Time
@ -41,6 +43,7 @@ var (
)
func init() {
// Initialize environment variables
apiAddress = os.Getenv("API_ADDRESS")
if apiAddress == "" {
apiAddress = "http://localhost:8080"
@ -51,9 +54,11 @@ func init() {
log.Fatal("GLITCHTIP_DSN environment variable is not set")
}
// Set the paths for go-glitch and oculus_filter binaries
goGlitchPath = "/usr/local/bin/go-glitch"
oculusFilterPath = "/usr/local/bin/oculus_filter"
// Initialize HTTP client with a timeout
httpClient = &http.Client{
Timeout: 30 * time.Second,
}
@ -68,6 +73,7 @@ func main() {
log.Fatalf("Error creating diffs directory: %v", err)
}
// Set up a ticker to fetch and monitor containers every minute
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
@ -91,6 +97,7 @@ func main() {
}
}
// fetchAndMonitorContainers fetches the list of containers and updates the monitoring status.
func fetchAndMonitorContainers() error {
containers, err := fetchContainers()
if err != nil {
@ -138,6 +145,7 @@ func fetchAndMonitorContainers() error {
return nil
}
// monitorContainers checks each monitored container and triggers notification if there are new changes.
func monitorContainers() error {
var wg sync.WaitGroup
for _, monitoredContainer := range monitoredContainers {
@ -166,6 +174,7 @@ func monitorContainers() error {
return nil
}
// fetchContainers fetches the list of containers from the API.
func fetchContainers() ([]ContainerInfo, error) {
log.Printf("Fetching containers from API: %s/containers\n", apiAddress)
resp, err := httpClient.Get(fmt.Sprintf("%s/containers", apiAddress))
@ -197,6 +206,7 @@ func fetchContainers() ([]ContainerInfo, error) {
return containers, nil
}
// fetchDiffOutput fetches the diff output for a given container ID from the API.
func fetchDiffOutput(containerID string) (string, error) {
log.Printf("Fetching diff output for container ID: %s\n", containerID)
resp, err := httpClient.Get(fmt.Sprintf("%s/diff?id=%s", apiAddress, containerID))
@ -222,6 +232,7 @@ func fetchDiffOutput(containerID string) (string, error) {
return string(body), nil
}
// checkAndNotify checks the diff output for changes and sends a notification if new changes are detected.
func checkAndNotify(container ContainerInfo) {
diffOutput, err := fetchDiffOutput(container.ID)
if err != nil {
@ -270,6 +281,7 @@ func checkAndNotify(container ContainerInfo) {
}
}
// filterDiffOutput filters the diff output using the ignore patterns.
func filterDiffOutput(diffOutput, cname string, ignores []string) (string, error) {
filename := filepath.Join("diffs", fmt.Sprintf("%s.diff", cname))
@ -305,6 +317,7 @@ func filterDiffOutput(diffOutput, cname string, ignores []string) (string, error
return string(filteredOutput), nil
}
// writeToFile writes content to a file, using a temporary file to avoid cross-device link errors.
func writeToFile(filename, content string) error {
tempFile, err := os.CreateTemp("", "filtered_output")
if err != nil {
@ -325,6 +338,7 @@ func writeToFile(filename, content string) error {
return nil
}
// copyFile copies the content from the source file to the destination file.
func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
@ -342,6 +356,7 @@ func copyFile(src, dst string) error {
return err
}
// sendNotification sends a notification using the go-glitch binary.
func sendNotification(content string) error {
cmd := exec.Command(goGlitchPath)
cmd.Stdin = strings.NewReader(content)
@ -354,6 +369,7 @@ func sendNotification(content string) error {
return nil
}
// cleanupTempFiles removes temporary files matching the pattern /tmp/filtered_output*.
func cleanupTempFiles() {
files, err := filepath.Glob("/tmp/filtered_output*")
if err != nil {