]> git.proxmox.com Git - pve-firewall.git/blobdiff - src/PVE/Firewall/Helpers.pm
move clone_vmfw_conf && remove_vmfw_conf to a Helpers
[pve-firewall.git] / src / PVE / Firewall / Helpers.pm
diff --git a/src/PVE/Firewall/Helpers.pm b/src/PVE/Firewall/Helpers.pm
new file mode 100644 (file)
index 0000000..3112ebc
--- /dev/null
@@ -0,0 +1,41 @@
+package PVE::Firewall::Helpers;
+
+use strict;
+use warnings;
+
+use PVE::Tools qw(file_get_contents file_set_contents);
+
+use base 'Exporter';
+our @EXPORT_OK = qw(
+remove_vmfw_conf
+clone_vmfw_conf
+);
+
+my $pvefw_conf_dir = "/etc/pve/firewall";
+
+sub remove_vmfw_conf {
+    my ($vmid) = @_;
+
+    my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
+
+    unlink $vmfw_conffile;
+}
+
+sub clone_vmfw_conf {
+    my ($vmid, $newid) = @_;
+
+    my $sourcevm_conffile = "$pvefw_conf_dir/$vmid.fw";
+    my $clonevm_conffile = "$pvefw_conf_dir/$newid.fw";
+
+    lock_vmfw_conf($newid, 10, sub {
+       if (-f $clonevm_conffile) {
+           unlink $clonevm_conffile;
+       }
+       if (-f $sourcevm_conffile) {
+           my $data = file_get_contents($sourcevm_conffile);
+           file_set_contents($clonevm_conffile, $data);
+       }
+    });
+}
+
+1;
\ No newline at end of file