Skip to content

Commit

Permalink
Env vars set in conf.d files are expected to propagate
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Feb 13, 2024
1 parent 0ed78b3 commit e9c2cf8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
12 changes: 8 additions & 4 deletions cmd/backup/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func loadEnvVars() (*Config, error) {
}

type configFile struct {
name string
config *Config
name string
config *Config
additionalEnvVars map[string]string
}

func loadEnvFiles(directory string) ([]configFile, error) {
Expand All @@ -74,13 +75,16 @@ func loadEnvFiles(directory string) ([]configFile, error) {
}
lookup := func(key string) (string, bool) {
val, ok := envFile[key]
return val, ok
if ok {
return val, ok
}
return os.LookupEnv(key)
}
c, err := loadConfig(lookup)
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()})
cs = append(cs, configFile{config: c, name: item.Name(), additionalEnvVars: envFile})
}

return cs, nil
Expand Down
42 changes: 36 additions & 6 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *command) must(err error) {
}
}

func runScript(c *Config) (err error) {
func runScript(c *Config, envVars map[string]string) (err error) {
defer func() {
if derr := recover(); derr != nil {
err = fmt.Errorf("runScript: unexpected panic running script: %v", err)
Expand All @@ -63,6 +63,35 @@ func runScript(c *Config) (err error) {
}
}()

if envVars != nil {

Check failure on line 66 in cmd/backup/main.go

View workflow job for this annotation

GitHub Actions / lint

S1031: unnecessary nil check around range (gosimple)
for key, value := range envVars {
currentVal, currentOk := os.LookupEnv(key)
defer func(currentKey, currentVal string, currentOk bool) {
if !currentOk {
_ = os.Unsetenv(currentKey)
} else {
_ = os.Setenv(currentKey, currentVal)
}
s.logger.Info(fmt.Sprintf("unset %v: %v", currentKey, currentVal))
}(key, currentVal, currentOk)

if err := os.Setenv(key, value); err != nil {
return fmt.Errorf(
"Unexpected error overloading environment %s: %w",
s.c.BackupCronExpression,
err,
)
}
s.logger.Info(fmt.Sprintf("set %v: %v", key, value))
}
}

if s.c.BackupFilenameExpand {
s.file = os.ExpandEnv(s.file)
s.c.BackupLatestSymlink = os.ExpandEnv(s.c.BackupLatestSymlink)
s.c.BackupPruningPrefix = os.ExpandEnv(s.c.BackupPruningPrefix)
}

scriptErr := func() error {
if err := s.withLabeledCommands(lifecyclePhaseArchive, func() (err error) {
restartContainersAndServices, err := s.stopContainersAndServices()
Expand Down Expand Up @@ -132,15 +161,16 @@ func (c *command) runInForeground(profileCronExpression string) error {
),
)

addJob := func(config *Config, name string) error {
addJob := func(config *Config, name string, envVars map[string]string) error {
if _, err := cr.AddFunc(config.BackupCronExpression, func() {
c.logger.Info(
fmt.Sprintf(
"Now running script on schedule %s",
config.BackupCronExpression,
),
)
if err := runScript(config); err != nil {

if err := runScript(config, envVars); err != nil {
c.logger.Error(
fmt.Sprintf(
"Unexpected error running schedule %s: %v",
Expand Down Expand Up @@ -175,15 +205,15 @@ func (c *command) runInForeground(profileCronExpression string) error {
if err != nil {
return fmt.Errorf("runInForeground: could not load config from environment variables: %w", err)
} else {
err = addJob(c, "from environment")
err = addJob(c, "from environment", nil)
if err != nil {
return fmt.Errorf("runInForeground: error adding job from env: %w", err)
}
}
} else {
c.logger.Info("/etc/dockervolumebackup/conf.d was found, using configuration files from this directory.")
for _, config := range cs {
err = addJob(config.config, config.name)
err = addJob(config.config, config.name, config.additionalEnvVars)
if err != nil {
return fmt.Errorf("runInForeground: error adding jobs from conf files: %w", err)
}
Expand Down Expand Up @@ -227,7 +257,7 @@ func (c *command) runAsCommand() error {
if err != nil {
return fmt.Errorf("runAsCommand: error loading env vars: %w", err)
}
err = runScript(config)
err = runScript(config, nil)
if err != nil {
return fmt.Errorf("runAsCommand: error running script: %w", err)
}
Expand Down
5 changes: 0 additions & 5 deletions cmd/backup/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ func newScript(c *Config) (*script, error) {
}
s.file = bf.String()

if s.c.BackupFilenameExpand {
s.file = os.ExpandEnv(s.file)
s.c.BackupLatestSymlink = os.ExpandEnv(s.c.BackupLatestSymlink)
s.c.BackupPruningPrefix = os.ExpandEnv(s.c.BackupPruningPrefix)
}
s.file = timeutil.Strftime(&s.stats.StartTime, s.file)

_, err := os.Stat("/var/run/docker.sock")
Expand Down
2 changes: 1 addition & 1 deletion test/confd/01backup.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
BACKUP_CRON_EXPRESSION="*/1 * * * *"
NAME="conf"
BACKUP_CRON_EXPRESSION="*/1 * * * *"

0 comments on commit e9c2cf8

Please sign in to comment.