-
-
Notifications
You must be signed in to change notification settings - Fork 122
Add "print-config" subcommand #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
le-lenn
wants to merge
7
commits into
offen:main
Choose a base branch
from
le-lenn:debug-subcommand
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ae68a33
Add "show-config" subcommand
le-lenn 16bca64
Add integration tests
le-lenn ff51740
Handle wrong additional args
le-lenn eda807a
Support "BackupFilenameExpand" option
le-lenn b46dcda
Rename to print-config
le-lenn 766a0ce
Remove config nil check as it will never be nil
le-lenn 4875396
Rework print config to work with config.resolve()
le-lenn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2025 - offen.software <[email protected]> | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
|
|
||
| "github.com/offen/docker-volume-backup/internal/errwrap" | ||
| ) | ||
|
|
||
| func runPrintConfig() error { | ||
| configurations, err := sourceConfiguration(configStrategyConfd) | ||
| if err != nil { | ||
| return errwrap.Wrap(err, "error sourcing configuration") | ||
| } | ||
|
|
||
| formatter := regexp.MustCompile(`\s([A-Z])`) | ||
| for _, config := range configurations { | ||
| if err := func() error { | ||
| unset, warnings, err := config.resolve() | ||
| if err != nil { | ||
| if unset != nil { | ||
| _ = unset() | ||
| } | ||
| return errwrap.Wrap(err, "error resolving configuration") | ||
| } | ||
| fmt.Printf("source=%s\n", config.source) | ||
| for _, warning := range warnings { | ||
| fmt.Printf("warning:%s\n", warning) | ||
| } | ||
| // insert line breaks before each field name, assuming field names start with uppercase letters | ||
| formatted := formatter.ReplaceAllString(fmt.Sprintf("%+v", *config), "\n$1") | ||
| fmt.Printf("%s\n", formatted) | ||
| if err := unset(); err != nil { | ||
| return errwrap.Wrap(err, "error unsetting environment variables") | ||
| } | ||
| return nil | ||
| }(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| --- | ||
| title: Show loaded configuration | ||
| layout: default | ||
| parent: How Tos | ||
| nav_order: 8 | ||
| --- | ||
|
|
||
| # Print loaded configuration | ||
|
|
||
| You can print the configuration that `docker-volume-backup` has picked up without running a backup: | ||
|
|
||
| ```console | ||
| docker exec <container_ref> backup print-config | ||
| ``` | ||
|
|
||
| If configuration sourcing fails, the error is printed to stdout to aid debugging. | ||
|
|
||
| If you want to test a one-off value, pass it directly: | ||
|
|
||
| ```console | ||
| docker exec -e BACKUP_SOURCES=/backup -e NOTIFICATION_URLS=stdout:// <container_ref> backup print-config | ||
| ``` | ||
|
|
||
| Note: output includes secrets exactly as loaded. | ||
le-lenn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| BACKUP_SOURCES=/conf-backup | ||
| NOTIFICATION_URLS_FILE=/run/secrets/notification_urls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| services: | ||
| backup: | ||
| image: offen/docker-volume-backup:${TEST_VERSION:-canary} | ||
| volumes: | ||
| - ${CONF_DIR}:/etc/dockervolumebackup/conf.d:ro | ||
| - ${SECRET_FILE}:/run/secrets/notification_urls:ro |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| services: | ||
| backup: | ||
| image: offen/docker-volume-backup:${TEST_VERSION:-canary} | ||
| environment: | ||
| BACKUP_FILENAME: "backup-$${BACKUP_TAG}.tar" | ||
| BACKUP_FILENAME_EXPAND: true | ||
| BACKUP_TAG: expanded | ||
| BACKUP_SOURCES: /backup | ||
| NOTIFICATION_URLS: "stdout://" | ||
| AWS_S3_BUCKET_NAME: example-bucket |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #!/bin/sh | ||
|
|
||
| set -e | ||
|
|
||
| cd "$(dirname "$0")" | ||
| . ../util.sh | ||
| current_test=$(basename $(pwd)) | ||
|
|
||
| info "print-config with environment variables" | ||
| docker compose up -d --quiet-pull | ||
| logs=$(docker compose exec -T backup backup print-config) | ||
|
|
||
| echo "$logs" | ||
|
|
||
| if ! echo "$logs" | grep -q "source=from environment"; then | ||
| fail "Missing source line." | ||
| fi | ||
| pass "Source line present." | ||
|
|
||
| if ! echo "$logs" | grep -q "BackupSources:/backup"; then | ||
| fail "Missing BACKUP_SOURCES in output." | ||
| fi | ||
| pass "BACKUP_SOURCES present." | ||
|
|
||
| if ! echo "$logs" | grep -q "BackupFilename:backup-expanded.tar"; then | ||
| fail "Missing expanded BACKUP_FILENAME in output." | ||
| fi | ||
| pass "Expanded BACKUP_FILENAME present." | ||
|
|
||
| if ! echo "$logs" | grep -q "NotificationURLs:\[stdout://\]"; then | ||
| fail "Missing NOTIFICATION_URLS in output." | ||
| fi | ||
| pass "NOTIFICATION_URLS present." | ||
|
|
||
| if ! echo "$logs" | grep -q "AwsS3BucketName:example-bucket"; then | ||
| fail "Missing AWS_S3_BUCKET_NAME in output." | ||
| fi | ||
| pass "AWS_S3_BUCKET_NAME present." | ||
|
|
||
| docker compose down | ||
|
|
||
| info "print-config with conf.d and _FILE" | ||
| export CONF_DIR=$(pwd)/conf.d | ||
| export SECRET_FILE=$(mktemp) | ||
| printf "stdout://\n" > "$SECRET_FILE" | ||
|
|
||
| docker compose -f docker-compose.confd.yml up -d --quiet-pull | ||
| logs=$(docker compose -f docker-compose.confd.yml exec -T backup backup print-config) | ||
|
|
||
| echo "$logs" | ||
|
|
||
| if ! echo "$logs" | grep -q "source=01print-config.env"; then | ||
| fail "Missing conf.d source line." | ||
| fi | ||
| pass "conf.d source line present." | ||
|
|
||
| if ! echo "$logs" | grep -q "BackupSources:/conf-backup"; then | ||
| fail "Missing conf.d BACKUP_SOURCES in output." | ||
| fi | ||
| pass "conf.d BACKUP_SOURCES present." | ||
|
|
||
| if ! echo "$logs" | grep -q "NotificationURLs:\\[stdout://"; then | ||
| fail "Missing conf.d NOTIFICATION_URLS in output." | ||
| fi | ||
| pass "conf.d NOTIFICATION_URLS present." | ||
|
|
||
| docker compose -f docker-compose.confd.yml down | ||
| rm -f "$SECRET_FILE" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when no configuration directory is present and configuration is provided via env vars only? Does it fall back to env? Sorry, it's been a while since I have written this 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understand this comment. Is
test/print-config/docker-compose.ymlusing only env variables?