[pve-devel] [PATCH cd-builder 2/2] fix #1999: create a tree view for qm listsnapshot
Rhonda D'Vine
rhonda at proxmox.com
Fri May 3 11:48:50 CEST 2019
The look of the tree is based on the GUI variant, so that we have a
consistent output when run multiple times, too.
Signed-off-by: Rhonda D'Vine <rhonda at proxmox.com>
---
PVE/CLI/qm.pm | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 59 insertions(+), 4 deletions(-)
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index fc06fa5..4d288df 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -922,12 +922,67 @@ our $cmddef = {
listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename },
sub {
my $res = shift;
- foreach my $e (sort { $a->{snaptime} ? ( $b->{snaptime} ? ($a->{snaptime} <=> $b->{snaptime}) : -1 ) : 1 } @$res) {
- my $headline = $e->{description} || 'no-description';
- $headline =~ s/\n.*//sg;
+
+ # "our" scope for the snaptimesort function
+ our $snapshots;
+ foreach my $e (@$res) {
+ $snapshots->{$e->{name}} = $e;
+ }
+
+ my $root;
+ foreach my $e (@$res) {
+ if ($e->{parent}) {
+ push @{$snapshots->{$e->{parent}}->{children}}, $e->{name};
+ } else {
+ $root = $e->{name};
+ }
+ }
+
+ my $prefix = '`->';
+
+ # sort the elements by snaptime - with "current" (no snaptime) highest
+ sub snaptimesort {
+ if ($snapshots->{$a}->{snaptime}) {
+ if ($snapshots->{$b}->{snaptime}) {
+ return $snapshots->{$a}->{snaptime} <=> $snapshots->{$b}->{snaptime};
+ } else {
+ return -1;
+ }
+ } else {
+ return 1;
+ }
+ }
+
+ # recursion function for displaying the tree
+ sub snapshottree {
+ my ($prefix, $root, $snapshots) = @_;
+ my $e = $snapshots->{$root};
+
+ my $description = $e->{description} || 'no-description';
+ $description =~ s/\n.*//sg;
my $parent = $e->{parent} // 'no-parent';
- printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
+
+ # create the timestamp string
+ my $timestring = "";
+ if (defined $e->{snaptime}) {
+ my ($sec, $min, $hour, $mday, $mon, $year) = localtime($e->{snaptime});
+ $year += 1900;
+ $mon++;
+ $timestring = sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year, $mon, $mday, $hour, $min, $sec);
+ }
+
+ # for aligning the description
+ my $len = 30 - length($prefix);
+ printf("%s %-${len}s %-23s %s\n", $prefix, $root, $timestring, $description);
+ if ($e->{children}) {
+ $prefix = " $prefix";
+ foreach my $child (sort snaptimesort @{$e->{children}}) {
+ snapshottree ($prefix, $child, $snapshots);
+ }
+ }
}
+
+ snapshottree ($prefix, $root, $snapshots);
}],
rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
--
2.11.0
More information about the pve-devel
mailing list