]> git.proxmox.com Git - pve-firewall.git/blob - src/PVE/Firewall/Helpers.pm
helpers: move over missing lock_vmfw_conf
[pve-firewall.git] / src / PVE / Firewall / Helpers.pm
1 package PVE::Firewall::Helpers;
2
3 use strict;
4 use warnings;
5
6 use PVE::Cluster;
7 use PVE::Tools qw(file_get_contents file_set_contents);
8
9 use base 'Exporter';
10 our @EXPORT_OK = qw(
11 lock_vmfw_conf
12 remove_vmfw_conf
13 clone_vmfw_conf
14 );
15
16 my $pvefw_conf_dir = "/etc/pve/firewall";
17
18 sub lock_vmfw_conf {
19 my ($vmid, $timeout, $code, @param) = @_;
20
21 die "can't lock VM firewall config for undefined VMID\n"
22 if !defined($vmid);
23
24 my $res = PVE::Cluster::cfs_lock_firewall("vm-$vmid", $timeout, $code, @param);
25 die $@ if $@;
26
27 return $res;
28 }
29
30 sub remove_vmfw_conf {
31 my ($vmid) = @_;
32
33 my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
34
35 unlink $vmfw_conffile;
36 }
37
38 sub clone_vmfw_conf {
39 my ($vmid, $newid) = @_;
40
41 my $sourcevm_conffile = "$pvefw_conf_dir/$vmid.fw";
42 my $clonevm_conffile = "$pvefw_conf_dir/$newid.fw";
43
44 lock_vmfw_conf($newid, 10, sub {
45 if (-f $clonevm_conffile) {
46 unlink $clonevm_conffile;
47 }
48 if (-f $sourcevm_conffile) {
49 my $data = file_get_contents($sourcevm_conffile);
50 file_set_contents($clonevm_conffile, $data);
51 }
52 });
53 }
54
55 1;