diff --git a/barman/cli.py b/barman/cli.py index 8879eca58..ca531f174 100644 --- a/barman/cli.py +++ b/barman/cli.py @@ -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 diff --git a/barman/cluster.py b/barman/cluster.py index 2522fba20..f4904a484 100644 --- a/barman/cluster.py +++ b/barman/cluster.py @@ -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() diff --git a/barman/config.py b/barman/config.py index 8b3d91c26..e3125b4d4 100644 --- a/barman/config.py +++ b/barman/config.py @@ -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.