Skip to content

Commit

Permalink
Merge updater into the config package (#426)
Browse files Browse the repository at this point in the history
* Merge updater into the config package

* Rename config.NewUpdaterOptionsWithFlags() for clarity; update deps
  • Loading branch information
jdoe7865623 authored Mar 25, 2022
1 parent 8dae06b commit 750f6bd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 65 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/corpix/uarand v0.1.1
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.3.0
github.com/miekg/dns v1.1.46
github.com/miekg/dns v1.1.47
github.com/mitchellh/mapstructure v1.4.3
github.com/mjpitz/go-ga v0.0.7
github.com/prometheus/client_golang v1.12.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.1.46 h1:uzwpxRtSVxtcIZmz/4Uz6/Rn7G11DvsaslXoy5LxQio=
github.com/miekg/dns v1.1.46/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/miekg/dns v1.1.47 h1:J9bWiXbqMbnZPcY8Qi2E3EWIBsIm6MZzzJB9VRg5gL8=
github.com/miekg/dns v1.1.47/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ import (

"github.com/Arriven/db1000n/src/jobs"
"github.com/Arriven/db1000n/src/runner"
"github.com/Arriven/db1000n/src/runner/config"
"github.com/Arriven/db1000n/src/utils"
"github.com/Arriven/db1000n/src/utils/metrics"
"github.com/Arriven/db1000n/src/utils/ota"
"github.com/Arriven/db1000n/src/utils/updater"
)

func main() {
Expand All @@ -54,7 +54,7 @@ func main() {
jobsGlobalConfig := jobs.NewGlobalConfigWithFlags()
otaConfig := ota.NewConfigWithFlags()
countryCheckerConfig := utils.NewCountryCheckerConfigWithFlags()
updaterMode, destinationConfig := updater.NewOptionsWithFlags()
updaterMode, destinationPath := config.NewUpdaterOptionsWithFlags()
prometheusOn, prometheusPushGateways := metrics.NewOptionsWithFlags()
pprof := flag.String("pprof", utils.GetEnvStringDefault("GO_PPROF_ENDPOINT", ""), "enable pprof")
help := flag.Bool("h", false, "print help message and exit")
Expand All @@ -68,7 +68,7 @@ func main() {

return
case *updaterMode:
updater.Run(*destinationConfig, strings.Split(runnerConfigOptions.PathsCSV, ","), []byte(runnerConfigOptions.BackupConfig))
config.UpdateLocal(*destinationPath, strings.Split(runnerConfigOptions.PathsCSV, ","), []byte(runnerConfigOptions.BackupConfig))

return
}
Expand Down
3 changes: 2 additions & 1 deletion src/runner/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func fetchSingle(path string, lastKnownConfig *RawConfig) (*RawConfig, error) {
return &RawConfig{Body: res, etag: etag, lastModified: lastModified}, nil
}

// FetchRawConfig retrieves the current config using a list of paths. Falls back to the last known config in case of errors.
func FetchRawConfig(paths []string, lastKnownConfig *RawConfig) *RawConfig {
newConfig := fetch(paths, lastKnownConfig)

Expand All @@ -140,7 +141,7 @@ func FetchRawConfig(paths []string, lastKnownConfig *RawConfig) *RawConfig {
return newConfig
}

// Update the job config from a list of paths or the built-in backup. Returns nil, nil in case of no changes.
// Unmarshal config encoded with the given format.
func Unmarshal(body []byte, format string) *Config {
if body == nil {
return nil
Expand Down
52 changes: 52 additions & 0 deletions src/runner/config/updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package config

import (
"bytes"
"flag"
"log"
"os"
"time"

"github.com/Arriven/db1000n/src/utils"
)

// NewUpdaterOptionsWithFlags returns updater options initialized with command line flags.
func NewUpdaterOptionsWithFlags() (updaterMode *bool, destinationPath *string) {
return flag.Bool("updater-mode", utils.GetEnvBoolDefault("UPDATER_MODE", false), "Only run config updater"),
flag.String("updater-destination-config", utils.GetEnvStringDefault("UPDATER_DESTINATION_CONFIG", "config/config.json"),
"Destination config file to write (only applies if updater-mode is enabled")
}

func UpdateLocal(destinationPath string, configPaths []string, backupConfig []byte) {
lastKnownConfig := &RawConfig{Body: backupConfig}

for {
if rawConfig := FetchRawConfig(configPaths, lastKnownConfig); !bytes.Equal(lastKnownConfig.Body, rawConfig.Body) {
if err := writeConfig(rawConfig.Body, destinationPath); err != nil {
log.Printf("Error writing config: %v", err)
}

return
}

time.Sleep(1 * time.Minute)
}
}

func writeConfig(body []byte, destinationPath string) error {
file, err := os.Create(destinationPath)
if err != nil {
return err
}

defer file.Close()

size, err := file.Write(body)
if err != nil {
return err
}

log.Printf("Saved %s with size %d", destinationPath, size)

return nil
}
58 changes: 0 additions & 58 deletions src/utils/updater/updater.go

This file was deleted.

0 comments on commit 750f6bd

Please sign in to comment.