[pve-devel] [PATCH/RFC v2 manager 3/3] vzdump: notes-template: replace unknown variable with error string

Fabian Ebner f.ebner at proxmox.com
Tue May 3 13:18:00 CEST 2022


rather than not setting the notes at all. They still can contain
useful information, and it likely is surprising to users to lose all
of the note when an unkown variable (or simply mistyped) is provided.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/VZDump.pm | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index 05cc1bec..79dcd62b 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -71,7 +71,7 @@ sub run_command {
 }
 
 my $generate_notes = sub {
-    my ($notes_template, $task) = @_;
+    my ($notes_template, $task, $logfd) = @_;
 
     my $info = {
 	cluster => PVE::Cluster::get_clinfo()->{cluster}->{name},
@@ -89,10 +89,14 @@ my $generate_notes = sub {
 
     $notes_template =~ s/\\(.)/$unescape->($1)/eg;
 
-    my $vars = join('|', keys $info->%*);
-    $notes_template =~ s/\{\{($vars)\}\}/$info->{$1}/g;
+    my $replace_var = sub {
+	my ($var) = @_;
+	return $info->{$var} if exists($info->{$var});
+	debugmsg('warn', "notes-template: unknown variable '$var'", $logfd);
+	return "<unknown variable '$var'>";
+    };
 
-    die "unexpected variable name '$1'" if $notes_template =~ m/\{\{([^\s}]+)\}\}/;
+    $notes_template =~ s/\{\{([^\s}]+)\}\}/$replace_var->($1)/eg;
 
     return $notes_template;
 };
@@ -1045,7 +1049,7 @@ sub exec_backup_task {
 
 	    if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') {
 		debugmsg('info', "adding notes to backup", $logfd);
-		my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task); };
+		my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task, $logfd); };
 		if (my $err = $@) {
 		    debugmsg('warn', "unable to add notes - $err", $logfd);
 		} else {
-- 
2.30.2





More information about the pve-devel mailing list