Skip to content

Commit 3c70598

Browse files
committed
backrest config updated by using ./pgedge backrest update-config
1 parent 279acfe commit 3c70598

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

cli/scripts/cluster.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ def init(cluster_name, install=True):
15461546
# (f) Set BACKUP pg1-port to the node's port value
15471547
cmd_set_pg1_port = f"cd {node['path']}/pgedge && ./pgedge set BACKUP repo1-path {repo1_path}"
15481548
run_cmd(cmd_set_pg1_port, node=node, message=f"Setting BACKUP repo1-path to {repo1_path} on node '{node['name']}'", verbose=verbose)
1549-
1549+
capture_backrest_config(cluster_name, verbose=True)
15501550
# 7. If it's an HA cluster, handle Patroni/etcd, etc.
15511551
if is_ha_cluster:
15521552
pg_ver = db_settings["pg_version"]
@@ -1563,6 +1563,7 @@ def init(cluster_name, install=True):
15631563
# Configure etcd and Patroni
15641564
etcd.configure_etcd(node, sub_nodes)
15651565
ha_patroni.configure_patroni(node, sub_nodes, db[0], db_settings)
1566+
15661567
def check_source_backrest_config(source_node_data):
15671568
"""
15681569
Check the source node's JSON data for a BackRest configuration.

src/backrest/backrest.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import sys
88
from datetime import datetime
99
from tabulate import tabulate
10+
import yaml
11+
import glob
12+
import io
13+
import contextlib
14+
1015

1116
def pgV():
1217
"""Return the first found PostgreSQL version (v14 thru v17)."""
@@ -432,6 +437,97 @@ def run_external_command(command, **kwargs):
432437
except Exception as e:
433438
util.exit_message(f"Failed:{str(e)}")
434439

440+
def update_config(verbose=False):
441+
"""
442+
Update the BACKUP configuration using a node-specific YAML file.
443+
444+
./pgedge backrest update-config <flag>
445+
In non-verbose mode (default), all intermediate output is suppressed.
446+
With verbose mode enabled, detailed logs and error messages are displayed.
447+
448+
Parameters:
449+
verbose (bool): Set to True to display detailed logs (default is False).
450+
"""
451+
452+
453+
# Internal function that runs the update and prints messages if local_verbose is True.
454+
def run_update_config(local_verbose):
455+
overall_success = True
456+
457+
script_dir = os.path.dirname(os.path.realpath(__file__))
458+
pgedge_dir = os.path.dirname(script_dir)
459+
node_name = os.path.basename(os.path.dirname(pgedge_dir))
460+
461+
# Construct the path to the YAML configuration file.
462+
yaml_file = os.path.join(pgedge_dir, "backrest", f"backrest_{node_name}.yaml")
463+
if not os.path.isfile(yaml_file):
464+
if local_verbose:
465+
print(f"Error: YAML file '{yaml_file}' does not exist.")
466+
return False
467+
468+
# Read the YAML file.
469+
try:
470+
with open(yaml_file, 'r') as f:
471+
yaml_content = f.read()
472+
except Exception as e:
473+
if local_verbose:
474+
print(f"Error reading {yaml_file}: {e}")
475+
return False
476+
477+
# Parse the YAML content into a dictionary.
478+
try:
479+
config_data = yaml.safe_load(yaml_content)
480+
if not isinstance(config_data, dict):
481+
if local_verbose:
482+
print("Error: YAML file must contain a key-value mapping.")
483+
return False
484+
except Exception as e:
485+
if local_verbose:
486+
print(f"Error parsing YAML file: {e}")
487+
return False
488+
489+
if local_verbose:
490+
print("==============================================")
491+
print(" Executing Backrest Configuration Update")
492+
print("==============================================")
493+
494+
# Execute the update command for each configuration key-value pair.
495+
for key, value in config_data.items():
496+
command = ["./pgedge", "set", "BACKUP", str(key), str(value)]
497+
if local_verbose:
498+
print(f"\n-> Executing command: {' '.join(command)}")
499+
try:
500+
result = subprocess.run(command, cwd=pgedge_dir, capture_output=True, text=True)
501+
if result.returncode != 0:
502+
overall_success = False
503+
if local_verbose:
504+
print(f" Error for key '{key}': {result.stderr.strip()}")
505+
else:
506+
if local_verbose:
507+
out = result.stdout.strip()
508+
if out:
509+
print(f" Output: {out}")
510+
else:
511+
print(" Command executed successfully.")
512+
except Exception as e:
513+
overall_success = False
514+
if local_verbose:
515+
print(f" Exception for key '{key}': {str(e)}")
516+
return overall_success
517+
518+
# In non-verbose mode, redirect all output to devnull.
519+
if not verbose:
520+
dummy = io.StringIO()
521+
with contextlib.redirect_stdout(dummy):
522+
success = run_update_config(False)
523+
# Do not print any final message in non-verbose mode.
524+
else:
525+
# Verbose mode: show detailed logs.
526+
success = run_update_config(True)
527+
if success:
528+
print("\n==============================================")
529+
print(" Backrest configuration updated successfully.")
530+
print("==============================================")
435531
if __name__ == "__main__":
436532
fire.Fire({
437533
"backup": backup,
@@ -445,4 +541,5 @@ def run_external_command(command, **kwargs):
445541
"set_hbaconf": modify_hba_conf,
446542
"set_postgresqlconf": modify_postgresql_conf,
447543
"command": run_external_command,
544+
"update-config": update_config,
448545
})

0 commit comments

Comments
 (0)