]> git.proxmox.com Git - pmg-api.git/blob - PMG/RESTEnvironment.pm
PMG/HTTPServer.pm: add safety check for master node
[pmg-api.git] / PMG / RESTEnvironment.pm
1 package PMG::RESTEnvironment;
2
3 use strict;
4 use warnings;
5
6 use PVE::INotify;
7 use PVE::RESTEnvironment;
8
9 use PMG::Cluster;
10 use PMG::ClusterConfig;
11
12 use base qw(PVE::RESTEnvironment);
13
14 my $nodename = PVE::INotify::nodename();
15
16 # initialize environment - must be called once at program startup
17 sub init {
18 my ($class, $type, %params) = @_;
19
20 $class = ref($class) || $class;
21
22 my $self = $class->SUPER::init($type, %params);
23
24 $self->{cinfo} = {};
25
26 return $self;
27 };
28
29 # init_request - must be called before each RPC request
30 sub init_request {
31 my ($self, %params) = @_;
32
33 $self->SUPER::init_request(%params);
34
35 $self->{cinfo} = PVE::INotify::read_file("cluster.conf");
36 }
37
38 sub check_node_is_master {
39 my ($self, $noerr);
40
41 my $master = PMG::Cluster::get_master_node($self->{cinfo});
42
43 return 1 if $master eq 'localhost' || $master eq $nodename;
44
45 return undef if $noerr;
46
47 die "this node ('$nodename') is not the master node\n";
48 }
49
50 1;