]> git.proxmox.com Git - pve-firewall.git/blame - src/PVE/Firewall/Helpers.pm
helpers: move over missing lock_vmfw_conf
[pve-firewall.git] / src / PVE / Firewall / Helpers.pm
CommitLineData
5bdc31fb
AD
1package PVE::Firewall::Helpers;
2
3use strict;
4use warnings;
5
21d5ba9c 6use PVE::Cluster;
5bdc31fb
AD
7use PVE::Tools qw(file_get_contents file_set_contents);
8
9use base 'Exporter';
10our @EXPORT_OK = qw(
21d5ba9c 11lock_vmfw_conf
5bdc31fb
AD
12remove_vmfw_conf
13clone_vmfw_conf
14);
15
16my $pvefw_conf_dir = "/etc/pve/firewall";
17
21d5ba9c
TL
18sub 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
5bdc31fb
AD
30sub remove_vmfw_conf {
31 my ($vmid) = @_;
32
33 my $vmfw_conffile = "$pvefw_conf_dir/$vmid.fw";
34
35 unlink $vmfw_conffile;
36}
37
38sub 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
21d5ba9c 551;