Skip to content

Commit

Permalink
feat: add support for properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 18, 2023
1 parent 437f081 commit eec3b8a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
5 changes: 5 additions & 0 deletions canary-checker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# check.disabled.http=true
# check.disabled.dns=true
# check.disabled.s3=true

# check.disabled.tcp=true
5 changes: 3 additions & 2 deletions checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package checks
import (
"database/sql"
"fmt"
"strings"
"time"

"github.com/flanksource/canary-checker/api/context"
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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",
Expand All @@ -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)
Expand All @@ -55,6 +88,10 @@ func setup() {

push.AddServers(pushServers)

if err := loadPropertiesToDB(db.Gorm, propertiesFile); err != nil {
logger.Fatalf("%v", err)
}

go push.Start()
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/utils/properties.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit eec3b8a

Please sign in to comment.