Skip to content

Commit e0c25d4

Browse files
committed
Move handling of deprecated config into resolve method
1 parent 3add246 commit e0c25d4

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

cmd/backup/config.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ func (c *Config) applyEnv() (func() error, error) {
226226
return unset, nil
227227
}
228228

229-
func (c *Config) resolve() (func() error, error) {
229+
func (c *Config) resolve() (func() error, []string, error) {
230+
warnings := []string{}
230231
resetEnv, err := c.applyEnv()
231232
if err != nil {
232-
return resetEnv, errwrap.Wrap(err, "error applying env")
233+
return resetEnv, warnings, errwrap.Wrap(err, "error applying env")
233234
}
234235

235236
if c.BackupFilenameExpand {
@@ -238,5 +239,40 @@ func (c *Config) resolve() (func() error, error) {
238239
c.BackupPruningPrefix = os.ExpandEnv(c.BackupPruningPrefix)
239240
}
240241

241-
return resetEnv, nil
242+
if c.EmailNotificationRecipient != "" {
243+
emailURL := fmt.Sprintf(
244+
"smtp://%s:%s@%s:%d/?from=%s&to=%s",
245+
c.EmailSMTPUsername,
246+
c.EmailSMTPPassword,
247+
c.EmailSMTPHost,
248+
c.EmailSMTPPort,
249+
c.EmailNotificationSender,
250+
c.EmailNotificationRecipient,
251+
)
252+
c.NotificationURLs = append(c.NotificationURLs, emailURL)
253+
warnings = append(warnings,
254+
"Using EMAIL_* keys for providing notification configuration has been deprecated and will be removed in the next major version.",
255+
"Please use NOTIFICATION_URLS instead. Refer to the README for an upgrade guide.",
256+
)
257+
}
258+
259+
if c.BackupFromSnapshot {
260+
warnings = append(warnings,
261+
"Using BACKUP_FROM_SNAPSHOT has been deprecated and will be removed in the next major version.",
262+
"Please use `archive-pre` and `archive-post` commands to prepare your backup sources. Refer to the documentation for an upgrade guide.",
263+
)
264+
}
265+
266+
if c.BackupStopDuringBackupLabel != "" && c.BackupStopContainerLabel != "" {
267+
return resetEnv, warnings, errwrap.Wrap(nil, "both BACKUP_STOP_DURING_BACKUP_LABEL and BACKUP_STOP_CONTAINER_LABEL have been set, cannot continue")
268+
}
269+
if c.BackupStopContainerLabel != "" {
270+
warnings = append(warnings,
271+
"Using BACKUP_STOP_CONTAINER_LABEL has been deprecated and will be removed in the next major version.",
272+
"Please use BACKUP_STOP_DURING_BACKUP_LABEL instead. Refer to the docs for an upgrade guide.",
273+
)
274+
c.BackupStopDuringBackupLabel = c.BackupStopContainerLabel
275+
}
276+
277+
return resetEnv, warnings, nil
242278
}

cmd/backup/create_archive.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ func (s *script) createArchive() error {
1818
backupSources := s.c.BackupSources
1919

2020
if s.c.BackupFromSnapshot {
21-
s.logger.Warn(
22-
"Using BACKUP_FROM_SNAPSHOT has been deprecated and will be removed in the next major version.",
23-
)
24-
s.logger.Warn(
25-
"Please use `archive-pre` and `archive-post` commands to prepare your backup sources. Refer to the documentation for an upgrade guide.",
26-
)
2721
backupSources = filepath.Join("/tmp", s.c.BackupSources)
2822
// copy before compressing guard against a situation where backup folder's content are still growing.
2923
s.registerHook(hookLevelPlumbing, func(error) error {

cmd/backup/run_script.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func runScript(c *Config) (err error) {
4343
}
4444
}()
4545

46-
unset, err := s.c.resolve()
46+
unset, warnings, err := s.c.resolve()
4747
if err != nil {
4848
return errwrap.Wrap(err, "error applying env")
4949
}
@@ -52,6 +52,9 @@ func runScript(c *Config) (err error) {
5252
err = errors.Join(err, errwrap.Wrap(derr, "error unsetting environment variables"))
5353
}
5454
}()
55+
for _, w := range warnings {
56+
s.logger.Warn(w)
57+
}
5558

5659
if s.c != nil && s.c.BackupJitter > 0 {
5760
max := s.c.BackupJitter

cmd/backup/script.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,6 @@ func (s *script) init() error {
7979
return nil
8080
})
8181
// Register notifications first so they can fire in case of other init errors.
82-
if s.c.EmailNotificationRecipient != "" {
83-
emailURL := fmt.Sprintf(
84-
"smtp://%s:%s@%s:%d/?from=%s&to=%s",
85-
s.c.EmailSMTPUsername,
86-
s.c.EmailSMTPPassword,
87-
s.c.EmailSMTPHost,
88-
s.c.EmailSMTPPort,
89-
s.c.EmailNotificationSender,
90-
s.c.EmailNotificationRecipient,
91-
)
92-
s.c.NotificationURLs = append(s.c.NotificationURLs, emailURL)
93-
s.logger.Warn(
94-
"Using EMAIL_* keys for providing notification configuration has been deprecated and will be removed in the next major version.",
95-
)
96-
s.logger.Warn(
97-
"Please use NOTIFICATION_URLS instead. Refer to the README for an upgrade guide.",
98-
)
99-
}
10082

10183
hookLevel, ok := hookLevels[s.c.NotificationLevel]
10284
if !ok {

cmd/backup/stop_restart.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"os"
1211
"sync"
1312
"time"
1413

@@ -113,23 +112,9 @@ func (s *script) stopContainersAndServices() (func() error, error) {
113112
return noop, errwrap.Wrap(err, "error determining swarm state")
114113
}
115114

116-
labelValue := s.c.BackupStopDuringBackupLabel
117-
if s.c.BackupStopContainerLabel != "" {
118-
s.logger.Warn(
119-
"Using BACKUP_STOP_CONTAINER_LABEL has been deprecated and will be removed in the next major version.",
120-
)
121-
s.logger.Warn(
122-
"Please use BACKUP_STOP_DURING_BACKUP_LABEL instead. Refer to the docs for an upgrade guide.",
123-
)
124-
if _, ok := os.LookupEnv("BACKUP_STOP_DURING_BACKUP_LABEL"); ok {
125-
return noop, errwrap.Wrap(nil, "both BACKUP_STOP_DURING_BACKUP_LABEL and BACKUP_STOP_CONTAINER_LABEL have been set, cannot continue")
126-
}
127-
labelValue = s.c.BackupStopContainerLabel
128-
}
129-
130115
stopDuringBackupLabel := fmt.Sprintf(
131116
"docker-volume-backup.stop-during-backup=%s",
132-
labelValue,
117+
s.c.BackupStopDuringBackupLabel,
133118
)
134119

135120
stopDuringBackupNoRestartLabel := fmt.Sprintf(
@@ -144,7 +129,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
144129

145130
var containersToStop []handledContainer
146131
for _, c := range allContainers.Items {
147-
hasStopDuringBackupLabel, hasStopDuringBackupNoRestartLabel, err := checkStopLabels(c.Labels, labelValue, s.c.BackupStopDuringBackupNoRestartLabel)
132+
hasStopDuringBackupLabel, hasStopDuringBackupNoRestartLabel, err := checkStopLabels(c.Labels, s.c.BackupStopDuringBackupLabel, s.c.BackupStopDuringBackupNoRestartLabel)
148133
if err != nil {
149134
return noop, errwrap.Wrap(err, "error querying for containers to stop")
150135
}
@@ -169,7 +154,7 @@ func (s *script) stopContainersAndServices() (func() error, error) {
169154
}
170155

171156
for _, service := range allServices {
172-
hasStopDuringBackupLabel, hasStopDuringBackupNoRestartLabel, err := checkStopLabels(service.Spec.Labels, labelValue, s.c.BackupStopDuringBackupNoRestartLabel)
157+
hasStopDuringBackupLabel, hasStopDuringBackupNoRestartLabel, err := checkStopLabels(service.Spec.Labels, s.c.BackupStopDuringBackupLabel, s.c.BackupStopDuringBackupNoRestartLabel)
173158
if err != nil {
174159
return noop, errwrap.Wrap(err, "error querying for services to scale down")
175160
}

0 commit comments

Comments
 (0)