diff --git a/canary-checker.properties b/canary-checker.properties new file mode 100644 index 000000000..e69c1c6da --- /dev/null +++ b/canary-checker.properties @@ -0,0 +1,5 @@ +# check.disabled.http=true +# check.disabled.dns=true +# check.disabled.s3=true + +# check.disabled.tcp=true \ No newline at end of file diff --git a/checks/runchecks.go b/checks/runchecks.go index 131813f22..1c388f929 100644 --- a/checks/runchecks.go +++ b/checks/runchecks.go @@ -3,6 +3,7 @@ package checks import ( "database/sql" "fmt" + "strings" "time" "github.com/flanksource/canary-checker/api/context" @@ -25,7 +26,7 @@ func getDisabledChecks(ctx *context.Context) (map[string]struct{}, error) { return result, nil } - rows, err := ctx.DB().Raw("SELECT value FROM properties WHERE name = 'check'").Rows() + rows, err := ctx.DB().Raw("SELECT name FROM properties WHERE name LIKE 'check.disabled.%'").Rows() if err != nil { return nil, err } @@ -37,7 +38,7 @@ func getDisabledChecks(ctx *context.Context) (map[string]struct{}, error) { return nil, err } - result[name] = struct{}{} + result[strings.TrimPrefix(name, "check.disabled.")] = struct{}{} } if rows.Err() != nil { diff --git a/cmd/serve.go b/cmd/serve.go index d8c335aa4..46383b47b 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -8,15 +8,18 @@ import ( "net/url" "os" "os/signal" + "strings" "time" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" echopprof "github.com/sevennt/echo-pprof" + "gorm.io/gorm" "github.com/flanksource/canary-checker/pkg/db" "github.com/flanksource/canary-checker/pkg/jobs" canaryJobs "github.com/flanksource/canary-checker/pkg/jobs/canary" + "github.com/flanksource/canary-checker/pkg/utils" "github.com/flanksource/canary-checker/pkg/runner" @@ -33,6 +36,7 @@ import ( var schedule, configFile string var executor, debug bool +var propertiesFile = "canary-checker.properties" var Serve = &cobra.Command{ Use: "serve config.yaml", @@ -47,6 +51,35 @@ var Serve = &cobra.Command{ }, } +func loadPropertiesToDB(db *gorm.DB, filename string) error { + if err := db.Exec("DELETE FROM properties WHERE name LIKE 'check.disabled.%'").Error; err != nil { + return err + } + + if _, err := os.Stat(filename); os.IsNotExist(err) { + return nil + } + + props, err := utils.ParsePropertiesFile(filename) + if err != nil { + return err + } + + var values []string + for k, v := range props { + values = append(values, fmt.Sprintf("('%s', '%s')", k, v)) + } + + if len(values) > 0 { + query := fmt.Sprintf("INSERT INTO properties (name, value) VALUES %s", strings.Join(values, ",")) + if err := db.Exec(query).Error; err != nil { + return fmt.Errorf("failed to insert properties into DB: %w", err) + } + } + + return nil +} + func setup() { if err := db.Init(); err != nil { logger.Fatalf("error connecting to db %v", err) @@ -55,6 +88,10 @@ func setup() { push.AddServers(pushServers) + if err := loadPropertiesToDB(db.Gorm, propertiesFile); err != nil { + logger.Fatalf("%v", err) + } + go push.Start() } diff --git a/pkg/utils/properties.go b/pkg/utils/properties.go new file mode 100644 index 000000000..b15520051 --- /dev/null +++ b/pkg/utils/properties.go @@ -0,0 +1,44 @@ +package utils + +import ( + "bufio" + "os" + "strings" + + "github.com/flanksource/commons/logger" +) + +type Properties map[string]string + +func ParsePropertiesFile(filename string) (Properties, error) { + file, err := os.Open(filename) + if err != nil { + return nil, err + } + defer file.Close() + + props := Properties{} + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") { + continue + } + + tokens := strings.SplitN(line, "=", 2) + if len(tokens) != 2 { + logger.Warnf("invalid line: %s", line) + continue + } + + key := strings.TrimSpace(tokens[0]) + value := strings.TrimSpace(tokens[1]) + props[key] = value + } + + if scanner.Err() != nil { + return nil, scanner.Err() + } + + return props, nil +}