7
7
import sys
8
8
from datetime import datetime
9
9
from tabulate import tabulate
10
+ import yaml
11
+ import glob
12
+ import io
13
+ import contextlib
14
+
10
15
11
16
def pgV ():
12
17
"""Return the first found PostgreSQL version (v14 thru v17)."""
@@ -432,6 +437,97 @@ def run_external_command(command, **kwargs):
432
437
except Exception as e :
433
438
util .exit_message (f"Failed:{ str (e )} " )
434
439
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 ("==============================================" )
435
531
if __name__ == "__main__" :
436
532
fire .Fire ({
437
533
"backup" : backup ,
@@ -445,4 +541,5 @@ def run_external_command(command, **kwargs):
445
541
"set_hbaconf" : modify_hba_conf ,
446
542
"set_postgresqlconf" : modify_postgresql_conf ,
447
543
"command" : run_external_command ,
544
+ "update-config" : update_config ,
448
545
})
0 commit comments