TMP directory crud
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
c2f6cd46f2
commit
76caf11636
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.
111
main.go
111
main.go
|
@ -4,6 +4,7 @@ import (
|
|||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -34,6 +35,7 @@ var (
|
|||
notifiedChanges = make(map[string]string)
|
||||
monitoredContainers = make(map[string]*MonitoredContainer)
|
||||
mu sync.Mutex
|
||||
httpClient *http.Client
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -46,6 +48,10 @@ func init() {
|
|||
if glitchtipDSN == "" {
|
||||
log.Fatal("GLITCHTIP_DSN environment variable is not set")
|
||||
}
|
||||
|
||||
httpClient = &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -57,13 +63,24 @@ func main() {
|
|||
log.Fatalf("Error creating diffs directory: %v", err)
|
||||
}
|
||||
|
||||
for {
|
||||
err := fetchAndMonitorContainers()
|
||||
if err != nil {
|
||||
log.Printf("Error in fetching and monitoring containers: %v", err)
|
||||
}
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
log.Println("Fetching and monitoring containers...")
|
||||
err := fetchAndMonitorContainers()
|
||||
if err != nil {
|
||||
log.Printf("Error in fetching and monitoring containers: %v", err)
|
||||
}
|
||||
default:
|
||||
time.Sleep(1 * time.Second)
|
||||
err := monitorContainers()
|
||||
if err != nil {
|
||||
log.Printf("Error in monitoring containers: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,14 +89,32 @@ func fetchAndMonitorContainers() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Fetched %d containers\n", len(containers))
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
// Remove containers that are no longer present
|
||||
for id := range monitoredContainers {
|
||||
found := false
|
||||
for _, container := range containers {
|
||||
if container.ID == id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
delete(monitoredContainers, id)
|
||||
log.Printf("Stopped monitoring container %s\n", id)
|
||||
}
|
||||
}
|
||||
|
||||
// Add or update containers
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -87,10 +122,16 @@ func fetchAndMonitorContainers() error {
|
|||
Info: container,
|
||||
LastChecked: time.Now().Add(-interval),
|
||||
}
|
||||
log.Printf("Started monitoring container %s (%s) with interval %s\n", container.Name, container.ID, container.Interval)
|
||||
} else {
|
||||
monitoredContainers[container.ID].Info = container
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func monitorContainers() error {
|
||||
var wg sync.WaitGroup
|
||||
for _, monitoredContainer := range monitoredContainers {
|
||||
wg.Add(1)
|
||||
|
@ -106,6 +147,7 @@ func fetchAndMonitorContainers() error {
|
|||
if time.Since(mc.LastChecked) >= interval {
|
||||
mc.LastChecked = time.Now()
|
||||
mu.Unlock()
|
||||
log.Printf("Checking container %s (%s)\n", mc.Info.Name, mc.Info.ID)
|
||||
checkAndNotify(mc.Info)
|
||||
} else {
|
||||
mu.Unlock()
|
||||
|
@ -113,23 +155,34 @@ func fetchAndMonitorContainers() error {
|
|||
}(monitoredContainer)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func fetchContainers() ([]ContainerInfo, error) {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/containers", apiAddress))
|
||||
log.Printf("Fetching containers from API: %s/containers\n", apiAddress)
|
||||
resp, err := httpClient.Get(fmt.Sprintf("%s/containers", apiAddress))
|
||||
if err != nil {
|
||||
log.Printf("Error fetching containers: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Printf("Error fetching containers: %s", resp.Status)
|
||||
return nil, fmt.Errorf("error fetching containers: %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading response body: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Printf("Response body: %s\n", body)
|
||||
|
||||
var containers []ContainerInfo
|
||||
if err := json.NewDecoder(resp.Body).Decode(&containers); err != nil {
|
||||
if err := json.Unmarshal(body, &containers); err != nil {
|
||||
log.Printf("Error unmarshaling response body: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -137,21 +190,27 @@ func fetchContainers() ([]ContainerInfo, error) {
|
|||
}
|
||||
|
||||
func fetchDiffOutput(containerID string) (string, error) {
|
||||
resp, err := http.Get(fmt.Sprintf("%s/diff?id=%s", apiAddress, containerID))
|
||||
log.Printf("Fetching diff output for container ID: %s\n", containerID)
|
||||
resp, err := httpClient.Get(fmt.Sprintf("%s/diff?id=%s", apiAddress, containerID))
|
||||
if err != nil {
|
||||
log.Printf("Error fetching diff: %v", err)
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
log.Printf("Error fetching diff: %s", resp.Status)
|
||||
return "", fmt.Errorf("error fetching diff: %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading response body: %v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Printf("Diff output: %s\n", body)
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
|
@ -211,10 +270,15 @@ func filterDiffOutput(diffOutput, cname string, ignores []string) (string, error
|
|||
return "", fmt.Errorf("error writing diff to file: %s", err)
|
||||
}
|
||||
|
||||
// Construct the /usr/local/bin/oculus_filter command
|
||||
if len(ignores) == 0 {
|
||||
log.Println("No ignore patterns provided.")
|
||||
return "", fmt.Errorf("no ignore patterns provided")
|
||||
}
|
||||
|
||||
// Construct the filter command
|
||||
args := append([]string{filename}, ignores...)
|
||||
cmd := exec.Command("/usr/local/bin/oculus_filter", args...)
|
||||
fullCommand := fmt.Sprintf("/usr/local/bin/oculus_filter %s", strings.Join(cmd.Args, " "))
|
||||
fullCommand := fmt.Sprintf("oculus_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
|
||||
|
||||
|
@ -252,6 +316,23 @@ func writeToFile(filename, content string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
dstFile, err := os.Create(dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
_, err = io.Copy(dstFile, srcFile)
|
||||
return err
|
||||
}
|
||||
|
||||
func sendNotification(content string) error {
|
||||
cmd := exec.Command("go-glitch")
|
||||
cmd.Stdin = strings.NewReader(content)
|
||||
|
|
Loading…
Reference in New Issue