public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings
@ 2022-01-17 11:35 Fabian Ebner
  2022-01-17 11:35 ` [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook Fabian Ebner
  2022-01-17 15:32 ` [pve-devel] applied: [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings Fabian Grünbichler
  0 siblings, 2 replies; 5+ messages in thread
From: Fabian Ebner @ 2022-01-17 11:35 UTC (permalink / raw)
  To: pve-devel

Some environment variables are undef in certain scenarios.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---

New in v2.

 vzdump-hook-script.pl | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/vzdump-hook-script.pl b/vzdump-hook-script.pl
index 1be440b3..60edbc04 100755
--- a/vzdump-hook-script.pl
+++ b/vzdump-hook-script.pl
@@ -15,11 +15,16 @@ if ($phase eq 'job-start' ||
     $phase eq 'job-end'  ||
     $phase eq 'job-abort') {
 
+    # undef for Proxmox Backup Server storages
     my $dumpdir = $ENV{DUMPDIR};
 
+    # undef when --dumpdir is used directly
     my $storeid = $ENV{STOREID};
 
-    print "HOOK-ENV: dumpdir=$dumpdir;storeid=$storeid\n";
+    print "HOOK-ENV: ";
+    print "dumpdir=$dumpdir;" if defined($dumpdir);
+    print "storeid=$storeid;" if defined($storeid);
+    print "\n";
 
     # do what you want
 
@@ -37,8 +42,10 @@ if ($phase eq 'job-start' ||
 
     my $vmtype = $ENV{VMTYPE}; # lxc/qemu
 
+    # undef for Proxmox Backup Server storages
     my $dumpdir = $ENV{DUMPDIR};
 
+    # undef when --dumpdir is used directly
     my $storeid = $ENV{STOREID};
 
     my $hostname = $ENV{HOSTNAME};
@@ -47,9 +54,14 @@ if ($phase eq 'job-start' ||
     my $target = $ENV{TARGET};
 
     # logfile is only available in phase 'log-end'
+    # undef for Proxmox Backup Server storages
     my $logfile = $ENV{LOGFILE};
 
-    print "HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;storeid=$storeid;hostname=$hostname;target=$target;logfile=$logfile\n";
+    print "HOOK-ENV: ";
+    for my $var (qw(vmtype dumpdir storeid hostname target logfile)) {
+	print "$var=$ENV{uc($var)};" if defined($ENV{uc($var)});
+    }
+    print "\n";
 
     # example: copy resulting backup file to another host using scp
     if ($phase eq 'backup-end') {
-- 
2.30.2





^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook
  2022-01-17 11:35 [pve-devel] [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings Fabian Ebner
@ 2022-01-17 11:35 ` Fabian Ebner
  2022-01-17 15:37   ` Fabian Grünbichler
  2022-01-17 15:32 ` [pve-devel] applied: [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings Fabian Grünbichler
  1 sibling, 1 reply; 5+ messages in thread
From: Fabian Ebner @ 2022-01-17 11:35 UTC (permalink / raw)
  To: pve-devel

which allows users to prepare the backup storage for activation (e.g.
by waking up a remote node).

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---

Changes from v1:
    * Different approach: add a new hook rather than trying to re-use
      job-start.
    * Add example to the example hook script to clarify when job-init
      should/can be used.

 PVE/VZDump.pm         | 17 ++++++++++++++++-
 vzdump-hook-script.pl | 12 +++++++++++-
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index b5a5fadd..e526eef8 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -497,6 +497,13 @@ sub new {
 	$opts->{storage} = 'local';
     }
 
+    $self->{job_init_log} = '';
+    open my $job_init_fd, '>', \$self->{job_init_log};
+    $self->run_hook_script('job-init', undef, $job_init_fd);
+    close $job_init_fd;
+
+    PVE::Cluster::cfs_update(); # Pick up eventual changes made by the hook script.
+
     my $errors = '';
 
     if ($opts->{storage}) {
@@ -1194,7 +1201,15 @@ sub exec_backup {
 
     my $totaltime = time() - $starttime;
 
-    eval { $self->sendmail ($tasklist, $totaltime, undef, $job_start_log, $job_end_log); };
+    eval {
+	$self->sendmail(
+	    $tasklist,
+	    $totaltime,
+	    undef,
+	    $self->{job_init_log} . $job_start_log,
+	    $job_end_log,
+	);
+    };
     debugmsg ('err', $@) if $@;
 
     die $err if $err;
diff --git a/vzdump-hook-script.pl b/vzdump-hook-script.pl
index 60edbc04..81b9b10f 100755
--- a/vzdump-hook-script.pl
+++ b/vzdump-hook-script.pl
@@ -11,11 +11,13 @@ print "HOOK: " . join (' ', @ARGV) . "\n";
 
 my $phase = shift;
 
-if ($phase eq 'job-start' ||
+if ($phase eq 'job-init' ||
+    $phase eq 'job-start' ||
     $phase eq 'job-end'  ||
     $phase eq 'job-abort') {
 
     # undef for Proxmox Backup Server storages
+    # undef in phase 'job-init' except when --dumpdir is used directly
     my $dumpdir = $ENV{DUMPDIR};
 
     # undef when --dumpdir is used directly
@@ -26,6 +28,14 @@ if ($phase eq 'job-start' ||
     print "storeid=$storeid;" if defined($storeid);
     print "\n";
 
+    # example: wake up remote storage node and enable storage
+    if ($phase eq 'job-init') {
+        #system ("") == 0 ||
+        #    die "waking up remote node failed";
+        #system ("/sbin/pvesm set $storeid --disable 0") == 0 ||
+        #    die "enabling storage $storeid failed";
+    }
+
     # do what you want
 
 } elsif ($phase eq 'backup-start' ||
-- 
2.30.2





^ permalink raw reply	[flat|nested] 5+ messages in thread

* [pve-devel] applied: [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings
  2022-01-17 11:35 [pve-devel] [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings Fabian Ebner
  2022-01-17 11:35 ` [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook Fabian Ebner
@ 2022-01-17 15:32 ` Fabian Grünbichler
  1 sibling, 0 replies; 5+ messages in thread
From: Fabian Grünbichler @ 2022-01-17 15:32 UTC (permalink / raw)
  To: Proxmox VE development discussion

On January 17, 2022 12:35 pm, Fabian Ebner wrote:
> Some environment variables are undef in certain scenarios.
> 
> Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
> ---
> 
> New in v2.
> 
>  vzdump-hook-script.pl | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/vzdump-hook-script.pl b/vzdump-hook-script.pl
> index 1be440b3..60edbc04 100755
> --- a/vzdump-hook-script.pl
> +++ b/vzdump-hook-script.pl
> @@ -15,11 +15,16 @@ if ($phase eq 'job-start' ||
>      $phase eq 'job-end'  ||
>      $phase eq 'job-abort') {
>  
> +    # undef for Proxmox Backup Server storages
>      my $dumpdir = $ENV{DUMPDIR};
>  
> +    # undef when --dumpdir is used directly
>      my $storeid = $ENV{STOREID};
>  
> -    print "HOOK-ENV: dumpdir=$dumpdir;storeid=$storeid\n";
> +    print "HOOK-ENV: ";
> +    print "dumpdir=$dumpdir;" if defined($dumpdir);
> +    print "storeid=$storeid;" if defined($storeid);
> +    print "\n";
>  
>      # do what you want
>  
> @@ -37,8 +42,10 @@ if ($phase eq 'job-start' ||
>  
>      my $vmtype = $ENV{VMTYPE}; # lxc/qemu
>  
> +    # undef for Proxmox Backup Server storages
>      my $dumpdir = $ENV{DUMPDIR};
>  
> +    # undef when --dumpdir is used directly
>      my $storeid = $ENV{STOREID};
>  
>      my $hostname = $ENV{HOSTNAME};
> @@ -47,9 +54,14 @@ if ($phase eq 'job-start' ||
>      my $target = $ENV{TARGET};
>  
>      # logfile is only available in phase 'log-end'
> +    # undef for Proxmox Backup Server storages
>      my $logfile = $ENV{LOGFILE};
>  
> -    print "HOOK-ENV: vmtype=$vmtype;dumpdir=$dumpdir;storeid=$storeid;hostname=$hostname;target=$target;logfile=$logfile\n";
> +    print "HOOK-ENV: ";
> +    for my $var (qw(vmtype dumpdir storeid hostname target logfile)) {
> +	print "$var=$ENV{uc($var)};" if defined($ENV{uc($var)});
> +    }
> +    print "\n";
>  
>      # example: copy resulting backup file to another host using scp
>      if ($phase eq 'backup-end') {
> -- 
> 2.30.2
> 
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 
> 
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook
  2022-01-17 11:35 ` [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook Fabian Ebner
@ 2022-01-17 15:37   ` Fabian Grünbichler
  2022-01-17 15:54     ` Thomas Lamprecht
  0 siblings, 1 reply; 5+ messages in thread
From: Fabian Grünbichler @ 2022-01-17 15:37 UTC (permalink / raw)
  To: Proxmox VE development discussion

On January 17, 2022 12:35 pm, Fabian Ebner wrote:
> which allows users to prepare the backup storage for activation (e.g.
> by waking up a remote node).
> 
> Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
> Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>

Reviewed-by: Fabian Grünbichler <f.gruenbichler@proxmox.com> 

but giving others a chance to object to the new phase before applying ;)

> ---
> 
> Changes from v1:
>     * Different approach: add a new hook rather than trying to re-use
>       job-start.
>     * Add example to the example hook script to clarify when job-init
>       should/can be used.
> 
>  PVE/VZDump.pm         | 17 ++++++++++++++++-
>  vzdump-hook-script.pl | 12 +++++++++++-
>  2 files changed, 27 insertions(+), 2 deletions(-)
> 
> diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
> index b5a5fadd..e526eef8 100644
> --- a/PVE/VZDump.pm
> +++ b/PVE/VZDump.pm
> @@ -497,6 +497,13 @@ sub new {
>  	$opts->{storage} = 'local';
>      }
>  
> +    $self->{job_init_log} = '';
> +    open my $job_init_fd, '>', \$self->{job_init_log};
> +    $self->run_hook_script('job-init', undef, $job_init_fd);
> +    close $job_init_fd;
> +
> +    PVE::Cluster::cfs_update(); # Pick up eventual changes made by the hook script.

small nit for when this gets applied: s/eventual/possible

eventual means 'final' or something else happening 'later'[0] (not 
'eventuell' ;))

0: https://www.merriam-webster.com/thesaurus/eventual

> +
>      my $errors = '';
>  
>      if ($opts->{storage}) {
> @@ -1194,7 +1201,15 @@ sub exec_backup {
>  
>      my $totaltime = time() - $starttime;
>  
> -    eval { $self->sendmail ($tasklist, $totaltime, undef, $job_start_log, $job_end_log); };
> +    eval {
> +	$self->sendmail(
> +	    $tasklist,
> +	    $totaltime,
> +	    undef,
> +	    $self->{job_init_log} . $job_start_log,
> +	    $job_end_log,
> +	);
> +    };
>      debugmsg ('err', $@) if $@;
>  
>      die $err if $err;
> diff --git a/vzdump-hook-script.pl b/vzdump-hook-script.pl
> index 60edbc04..81b9b10f 100755
> --- a/vzdump-hook-script.pl
> +++ b/vzdump-hook-script.pl
> @@ -11,11 +11,13 @@ print "HOOK: " . join (' ', @ARGV) . "\n";
>  
>  my $phase = shift;
>  
> -if ($phase eq 'job-start' ||
> +if ($phase eq 'job-init' ||
> +    $phase eq 'job-start' ||
>      $phase eq 'job-end'  ||
>      $phase eq 'job-abort') {
>  
>      # undef for Proxmox Backup Server storages
> +    # undef in phase 'job-init' except when --dumpdir is used directly
>      my $dumpdir = $ENV{DUMPDIR};
>  
>      # undef when --dumpdir is used directly
> @@ -26,6 +28,14 @@ if ($phase eq 'job-start' ||
>      print "storeid=$storeid;" if defined($storeid);
>      print "\n";
>  
> +    # example: wake up remote storage node and enable storage
> +    if ($phase eq 'job-init') {
> +        #system ("") == 0 ||
> +        #    die "waking up remote node failed";
> +        #system ("/sbin/pvesm set $storeid --disable 0") == 0 ||
> +        #    die "enabling storage $storeid failed";
> +    }
> +
>      # do what you want
>  
>  } elsif ($phase eq 'backup-start' ||
> -- 
> 2.30.2
> 
> 
> 
> _______________________________________________
> pve-devel mailing list
> pve-devel@lists.proxmox.com
> https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
> 




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook
  2022-01-17 15:37   ` Fabian Grünbichler
@ 2022-01-17 15:54     ` Thomas Lamprecht
  0 siblings, 0 replies; 5+ messages in thread
From: Thomas Lamprecht @ 2022-01-17 15:54 UTC (permalink / raw)
  To: Proxmox VE development discussion, Fabian Grünbichler

On 17.01.22 16:37, Fabian Grünbichler wrote:
> On January 17, 2022 12:35 pm, Fabian Ebner wrote:
>> which allows users to prepare the backup storage for activation (e.g.
>> by waking up a remote node).
>>
>> Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
>> Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
> 
> Reviewed-by: Fabian Grünbichler <f.gruenbichler@proxmox.com> 
> 
> but giving others a chance to object to the new phase before applying ;)

For the general approach of adding this hook

Acked-by: Thomas Lamprecht <t.lamprecht@proxmox.com>


>> @@ -26,6 +28,14 @@ if ($phase eq 'job-start' ||
>>      print "storeid=$storeid;" if defined($storeid);
>>      print "\n";
>>  
>> +    # example: wake up remote storage node and enable storage
>> +    if ($phase eq 'job-init') {
>> +        #system ("") == 0 ||

nit: maybe add a command there so that it's easier to adapt for non-perl people, whom
may not know that above is not some required magic incantation that they must keep as
is. So, e.g.:

#system("wakeonlan AA:BB:CC:DD:EE:FF")
#sleep(30);

but as said, just a nit that caught my eyes, no hard feelings on that whatsoever.




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-01-17 15:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17 11:35 [pve-devel] [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings Fabian Ebner
2022-01-17 11:35 ` [pve-devel] [PATCH v2 manager 2/2] close #3476: vzdump: add job-init hook Fabian Ebner
2022-01-17 15:37   ` Fabian Grünbichler
2022-01-17 15:54     ` Thomas Lamprecht
2022-01-17 15:32 ` [pve-devel] applied: [PATCH v2 manager 1/2] vzdump: example hook script: avoid undef warnings Fabian Grünbichler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal