]> git.proxmox.com Git - pve-guest-common.git/blobdiff - PVE/AbstractConfig.pm
refactor/cleanup add_to_pending_delete sligthly
[pve-guest-common.git] / PVE / AbstractConfig.pm
index e0d0f10af44c890d84df3d019222d1d3040439aa..b07ef5936131c95dbf517ac9c683b9c6a0c6b69b 100644 (file)
@@ -68,6 +68,139 @@ sub write_config {
     PVE::Cluster::cfs_write_file($cfspath, $conf);
 }
 
+# Pending changes related
+
+sub parse_pending_delete {
+    my ($class, $data) = @_;
+
+    return {} if !$data;
+
+    $data =~ s/[,;]/ /g;
+    $data =~ s/^\s+//;
+
+    my $pending_deletions = {};
+    for my $entry (split(/\s+/, $data)) {
+       my ($force, $key) = $entry =~ /^(!?)(.*)$/;
+
+       $pending_deletions->{$key} = {
+           force => $force ? 1 : 0,
+       };
+    }
+
+    return $pending_deletions;
+}
+
+sub print_pending_delete {
+    my ($class, $delete_hash) = @_;
+
+    my $render_key = sub {
+       my $key = shift;
+       $key = "!$key" if $delete_hash->{$key}->{force};
+       return $key;
+    };
+
+    join (',', map { $render_key->($_) } sort keys %$delete_hash);
+}
+
+sub add_to_pending_delete {
+    my ($class, $conf, $key, $force) = @_;
+
+    $conf->{pending} //= {};
+    my $pending = $conf->{pending};
+    my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
+
+    $pending_delete_hash->{$key} = { force => $force };
+
+    $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
+
+    return $conf;
+}
+
+sub remove_from_pending_delete {
+    my ($class, $conf, $key) = @_;
+
+    my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
+    delete $pending_delete_hash->{$key};
+
+    if (%$pending_delete_hash) {
+       $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
+    } else {
+       delete $conf->{pending}->{delete};
+    }
+}
+
+sub cleanup_pending {
+    my ($class, $conf) = @_;
+
+    # remove pending changes when nothing changed
+    my $changes;
+    foreach my $opt (keys %{$conf->{pending}}) {
+       next if $opt eq 'delete'; # just to be sure
+       if (defined($conf->{$opt}) && ($conf->{pending}->{$opt} eq  $conf->{$opt})) {
+           $changes = 1;
+           delete $conf->{pending}->{$opt};
+       }
+    }
+
+    my $current_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
+    my $pending_delete_hash = {};
+    while (my ($opt, $force) = each %$current_delete_hash) {
+       if (defined($conf->{$opt})) {
+           $pending_delete_hash->{$opt} = $force;
+       } else {
+           $changes = 1;
+       }
+    }
+
+    if (%$pending_delete_hash) {
+       $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
+    } else {
+       delete $conf->{pending}->{delete};
+    }
+
+    return $changes;
+}
+
+sub load_snapshot_config {
+    my ($class, $vmid, $snapname) = @_;
+
+    my $conf = $class->load_config($vmid);
+
+    my $snapshot = $conf->{snapshots}->{$snapname};
+    die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
+
+    $snapshot->{digest} = $conf->{digest};
+
+    return $snapshot;
+
+}
+
+sub load_current_config {
+    my ($class, $vmid, $current) = @_;
+
+    my $conf = $class->load_config($vmid);
+
+    # take pending changes in
+    if (!$current) {
+       foreach my $opt (keys %{$conf->{pending}}) {
+           next if $opt eq 'delete';
+           my $value = $conf->{pending}->{$opt};
+           next if ref($value); # just to be sure
+           $conf->{$opt} = $value;
+       }
+       my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
+       foreach my $opt (keys %$pending_delete_hash) {
+           delete $conf->{$opt} if $conf->{$opt};
+       }
+    }
+
+    delete $conf->{snapshots};
+    delete $conf->{pending};
+
+    return $conf;
+}
+
+
 # Lock config file using flock, run $code with @param, unlock config file.
 # $timeout is the maximum time to aquire the flock
 sub lock_config_full {
@@ -95,6 +228,15 @@ sub create_and_lock_config {
     });
 }
 
+# destroys configuration, only applyable for configs owned by the callers node.
+# dies if removal fails, e.g., when inquorate.
+sub destroy_config {
+    my ($class, $vmid) = @_;
+
+    my $config_fn = $class->config_file($vmid, $nodename);
+    unlink $config_fn or die "failed to remove config file: $!\n";
+}
+
 # Lock config file using flock, run $code with @param, unlock config file.
 # $timeout is the maximum time to aquire the flock
 # $shared eq 1 creates a non-exclusive ("read") flock