Skip to content

Commit

Permalink
Proof of concept config switch command
Browse files Browse the repository at this point in the history
Adds the `barman config switch <server> <model>` command. It will
apply the values from the named model into the config for the named
server.

It works but is missing:
1. Locking. We need a fairly global lock for this.
2. Validation. We don't check the model exists or is a member of the
   right cluster.
3. Supported keys should be defined somewhere other than the cli
   function.
4. Logging. We should log that the operation took place and log the
   old and new values so it is clear what has been changed.
  • Loading branch information
mikewallace1979 committed Oct 11, 2023
1 parent b013603 commit 782940c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
41 changes: 41 additions & 0 deletions barman/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,47 @@ def check_wal_archive(args):
output.close_and_exit()


@command(
[
argument(
"subcommand",
help="specifies the config command to execute - either switch or model",
),
argument(
"server_name",
completer=server_completer,
help="specifies the server name for the command",
),
argument(
"model_name",
completer=server_completer,
help="specifies the name of the model from which new values should be "
"obtained",
),
]
)
def config(args):
"""
Perform operations on the Barman configuration.
"""
server = get_server(args)
# Get the model without validating that it exists or belongs to the right cluster
model = [
s for s in barman.__config__.servers() if s.model and s.name == args.model_name
][0]
# Update server values without validating the model values are limited to sensible things
new_keys = []
# This will be defined somewhere else, probably
model_options = ('conninfo', 'streaming_conninfo')
for k in model_options:
v = getattr(model, k)
if v is not None:
setattr(server.config, k, v)
new_keys.append(k)
# Write to autoconf
server.write_autoconf(new_keys)


def pretty_args(args):
"""
Prettify the given argparse namespace to be human readable
Expand Down
38 changes: 38 additions & 0 deletions barman/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,44 @@ def update_msg_list_and_disable_server(self, msg_list):
self.msg_list.extend(msg_list)
self.disabled = True

def write_autoconf(self, keys):
"""
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"
)
# Servers have their own autoconf file
autoconf_path = os.path.join(config_files_directory, f"{self.name}.auto.conf")
autoconf_tmp_path = ".".join((autoconf_path, "new"))

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

# Make sure there's a section for this server
if not autoconf.has_section(self.name):
autoconf.add_section(self.name)

# Scan the server config for keys we care about
for option in keys:
print(option, getattr(self, option))
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
4 changes: 4 additions & 0 deletions barman/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4018,3 +4018,7 @@ def load_primary_info(self):
"Cannot open %s file for server %s: %s"
% (PRIMARY_INFO_FILE, self.config.name, e)
)

# New functions for updating config on disk
def write_autoconf(self, keys):
self.config.write_autoconf(keys)

0 comments on commit 782940c

Please sign in to comment.