Skip to content

Commit

Permalink
Refactor handling of runtime configuration to prepare for reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Feb 16, 2024
1 parent c4e480d commit 29fd38d
Show file tree
Hide file tree
Showing 14 changed files with 665 additions and 520 deletions.
155 changes: 155 additions & 0 deletions cmd/backup/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2024 - Offen Authors <[email protected]>
// SPDX-License-Identifier: MPL-2.0

package main

import (
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/robfig/cron/v3"
)

type command struct {
logger *slog.Logger
schedules []cron.EntryID
cr *cron.Cron
reload chan struct{}
}

func newCommand() *command {
return &command{
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
}
}

// runAsCommand executes a backup run for each configuration that is available
// and then returns
func (c *command) runAsCommand() error {
configurations, err := sourceConfiguration(configStrategyEnv)
if err != nil {
return fmt.Errorf("runAsCommand: error loading env vars: %w", err)
}

for _, config := range configurations {
if err := runScript(config); err != nil {
return fmt.Errorf("runAsCommand: error running script: %w", err)
}
}

return nil
}

type foregroundOpts struct {
profileCronExpression string
}

// runInForeground starts the program as a long running process, scheduling
// a job for each configuration that is available.
func (c *command) runInForeground(opts foregroundOpts) error {
c.cr = cron.New(
cron.WithParser(
cron.NewParser(
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor,
),
),
)

if err := c.schedule(configStrategyConfd); err != nil {
return fmt.Errorf("runInForeground: error scheduling: %w", err)
}

if opts.profileCronExpression != "" {
if _, err := c.cr.AddFunc(opts.profileCronExpression, c.profile); err != nil {
return fmt.Errorf("runInForeground: error adding profiling job: %w", err)
}
}

var quit = make(chan os.Signal, 1)
c.reload = make(chan struct{}, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
c.cr.Start()

for {
select {
case <-quit:
ctx := c.cr.Stop()
<-ctx.Done()
return nil
case <-c.reload:
if err := c.schedule(configStrategyConfd); err != nil {
return fmt.Errorf("runInForeground: error reloading configuration: %w", err)
}
}
}
}

// schedule wipes all existing schedules and enqueues all schedules available
// using the given configuration strategy
func (c *command) schedule(strategy configStrategy) error {
for _, id := range c.schedules {
c.cr.Remove(id)
}

configurations, err := sourceConfiguration(strategy)
if err != nil {
return fmt.Errorf("schedule: error sourcing configuration: %w", err)
}

for _, cfg := range configurations {
config := cfg
id, err := c.cr.AddFunc(config.BackupCronExpression, func() {
c.logger.Info(
fmt.Sprintf(
"Now running script on schedule %s",
config.BackupCronExpression,
),
)

if err := runScript(config); err != nil {
c.logger.Error(
fmt.Sprintf(
"Unexpected error running schedule %s: %v",
config.BackupCronExpression,
err,
),
"error",
err,
)
}
})

if err != nil {
return fmt.Errorf("addJob: error adding schedule %s: %w", config.BackupCronExpression, err)
}
c.logger.Info(fmt.Sprintf("Successfully scheduled backup %s with expression %s", config.source, config.BackupCronExpression))
if ok := checkCronSchedule(config.BackupCronExpression); !ok {
c.logger.Warn(
fmt.Sprintf("Scheduled cron expression %s will never run, is this intentional?", config.BackupCronExpression),
)

if err != nil {
return fmt.Errorf("schedule: error scheduling: %w", err)
}
c.schedules = append(c.schedules, id)
}
}

return nil
}

// must exits the program when passed an error. It should be the only
// place where the application exits forcefully.
func (c *command) must(err error) {
if err != nil {
c.logger.Error(
fmt.Sprintf("Fatal error running command: %v", err),
"error",
err,
)
os.Exit(1)
}
}
39 changes: 39 additions & 0 deletions cmd/backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type Config struct {
DropboxAppSecret string `split_words:"true"`
DropboxRemotePath string `split_words:"true"`
DropboxConcurrencyLevel NaturalNumber `split_words:"true" default:"6"`
source string
additionalEnvVars map[string]string
}

type CompressionType string
Expand Down Expand Up @@ -172,3 +174,40 @@ func (n *WholeNumber) Decode(v string) error {
func (n *WholeNumber) Int() int {
return int(*n)
}

type envVarLookup struct {
ok bool
key string
value string
}

// applyEnv sets the values in `additionalEnvVars` as environment variables.
// It returns a function that reverts all values that have been set to its
// previous state.
func (c *Config) applyEnv() (func() error, error) {
lookups := []envVarLookup{}

unset := func() error {
for _, lookup := range lookups {
if !lookup.ok {
if err := os.Unsetenv(lookup.key); err != nil {
return fmt.Errorf("(*Config).applyEnv: error unsetting env var %s: %w", lookup.key, err)
}
continue
}
if err := os.Setenv(lookup.key, lookup.value); err != nil {
return fmt.Errorf("(*Config).applyEnv: error setting back env var %s: %w", lookup.key, err)
}
}
return nil
}

for key, value := range c.additionalEnvVars {
current, ok := os.LookupEnv(key)
lookups = append(lookups, envVarLookup{ok: ok, key: key, value: current})
if err := os.Setenv(key, value); err != nil {
return unset, fmt.Errorf("(*Config).applyEnv: error setting env var: %w", err)
}
}
return unset, nil
}
55 changes: 43 additions & 12 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,39 @@ import (
"github.com/offen/envconfig"
)

type configStrategy string

const (
configStrategyEnv configStrategy = "env"
configStrategyConfd configStrategy = "confd"
)

// sourceConfiguration returns a list of config objects using the given
// strategy. It should be the single entrypoint for retrieving configuration
// for all consumers.
func sourceConfiguration(strategy configStrategy) ([]*Config, error) {
switch strategy {
case configStrategyEnv:
c, err := loadConfigFromEnvVars()
return []*Config{c}, err
case configStrategyConfd:
cs, err := loadConfigsFromEnvFiles("/etc/dockervolumebackup/conf.d")
if err != nil {
if os.IsNotExist(err) {
return sourceConfiguration(configStrategyEnv)
}
return nil, fmt.Errorf("sourceConfiguration: error loading config files: %w", err)
}
return cs, nil
default:
return nil, fmt.Errorf("sourceConfiguration: received unknown config strategy: %v", strategy)
}
}

// envProxy is a function that mimics os.LookupEnv but can read values from any other source
type envProxy func(string) (string, bool)

// loadConfig creates a config object using the given lookup function
func loadConfig(lookup envProxy) (*Config, error) {
envconfig.Lookup = func(key string) (string, bool) {
value, okValue := lookup(key)
Expand Down Expand Up @@ -44,17 +74,16 @@ func loadConfig(lookup envProxy) (*Config, error) {
return c, nil
}

func loadEnvVars() (*Config, error) {
return loadConfig(os.LookupEnv)
}

type configFile struct {
name string
config *Config
additionalEnvVars map[string]string
func loadConfigFromEnvVars() (*Config, error) {
c, err := loadConfig(os.LookupEnv)
if err != nil {
return nil, fmt.Errorf("loadEnvVars: error loading config from environment: %w", err)
}
c.source = "from environment"
return c, nil
}

func loadEnvFiles(directory string) ([]configFile, error) {
func loadConfigsFromEnvFiles(directory string) ([]*Config, error) {
items, err := os.ReadDir(directory)
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -63,7 +92,7 @@ func loadEnvFiles(directory string) ([]configFile, error) {
return nil, fmt.Errorf("loadEnvFiles: failed to read files from env directory: %w", err)
}

cs := []configFile{}
configs := []*Config{}
for _, item := range items {
if item.IsDir() {
continue
Expand All @@ -88,8 +117,10 @@ func loadEnvFiles(directory string) ([]configFile, error) {
if err != nil {
return nil, fmt.Errorf("loadEnvFiles: error loading config from file %s: %w", p, err)
}
cs = append(cs, configFile{config: c, name: item.Name(), additionalEnvVars: envFile})
c.source = item.Name()
c.additionalEnvVars = envFile
configs = append(configs, c)
}

return cs, nil
return configs, nil
}
41 changes: 41 additions & 0 deletions cmd/backup/copy_archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 - Offen Authors <[email protected]>
// SPDX-License-Identifier: MPL-2.0

package main

import (
"fmt"
"os"
"path"

"golang.org/x/sync/errgroup"
)

// copyArchive makes sure the backup file is copied to both local and remote locations
// as per the given configuration.
func (s *script) copyArchive() error {
_, name := path.Split(s.file)
if stat, err := os.Stat(s.file); err != nil {
return fmt.Errorf("copyArchive: unable to stat backup file: %w", err)
} else {
size := stat.Size()
s.stats.BackupFile = BackupFileStats{
Size: uint64(size),
Name: name,
FullPath: s.file,
}
}

eg := errgroup.Group{}
for _, backend := range s.storages {
b := backend
eg.Go(func() error {
return b.Copy(s.file)
})
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("copyArchive: error copying archive: %w", err)
}

return nil
}
Loading

0 comments on commit 29fd38d

Please sign in to comment.