Skip to content

Commit

Permalink
Use barman.auto.conf for backup source config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewallace1979 committed Sep 14, 2023
1 parent 17efc14 commit 8dc840c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
25 changes: 25 additions & 0 deletions barman/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,31 @@ def check_wal_archive(args):
output.close_and_exit()


@command(
[
argument(
"server_name",
completer=server_completer,
help="specifies the server name for the command",
),
argument(
"node_alias",
help="the alias of the node from which backups should be taken",
),
]
)
def set_backup_source(args):
"""
Set the source node for backups.
"""
server = get_server(args)
# TODO
# - check if already set to desired value
# - check for ongoing backups using ServerBackupLock and handle --force option
server.config.cluster_backup_source = args.node_alias
server.write_autoconf()


def pretty_args(args):
"""
Prettify the given argparse namespace to be human readable
Expand Down
4 changes: 4 additions & 0 deletions barman/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,7 @@ def _check(self, check_strategy):
hint="barman check command timed out",
check="check timeout",
)

# New functions for updating config on disk
def write_autoconf(self):
self.config.write_autoconf()
32 changes: 32 additions & 0 deletions barman/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,38 @@ def update_msg_list_and_disable_server(self, msg_list):
self.msg_list.extend(msg_list)
self.disabled = True

def write_autoconf(self):
"""
Write the config variable overrides which Barman uses.
"""
# The autoconf file lives in the configuration_files_directory
config_files_directory = self.config.get(
"barman", "configuration_files_directory"
)
autoconf_path = os.path.join(config_files_directory, "barman.auto.conf")
autoconf_tmp_path = ".".join((autoconf_path, "new"))

# Only maintain autoconf for these keys
autoconf_keys = ("cluster_primary", "cluster_backup_source")

# Read current autoconf
autoconf = ConfigParser()
with open(autoconf_path, "r") as autoconf_file:
autoconf.read_file(autoconf_file)

# Scan the server config for keys we care about
for option in autoconf_keys:
autoconf.set(self.name, option, getattr(self, option))

# Write a new version of the file safely
# This is not safe at all though - we need to use locking to prevent writes
# from other places since we read the file.
with open(autoconf_tmp_path, "w") as new_autoconf:
autoconf.write(new_autoconf)

# Rename new file into place
os.rename(autoconf_tmp_path, autoconf_path)


class Config(object):
"""This class represents the barman configuration.
Expand Down

0 comments on commit 8dc840c

Please sign in to comment.