]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/ClusterConfig.pm
1 package PMG
::ClusterConfig
::Base
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use PVE
::SectionConfig
;
12 use base
qw(PVE::SectionConfig);
16 type
=> { description
=> "Cluster node type." },
18 description
=> "Cluster Node ID.",
29 sub parse_section_header
{
30 my ($class, $line) = @_;
32 if ($line =~ m/^(node|master):\s*(\d+)\s*$/) {
33 my ($type, $sectionId) = ($1, $2);
34 my $errmsg = undef; # set if you want to skip whole section
35 my $config = {}; # to return additional attributes
36 return ($type, $sectionId, $errmsg, $config);
41 package PMG
::ClusterConfig
::Node
;
46 use base
qw(PMG::ClusterConfig::Base);
54 description
=> "IP address.",
55 type
=> 'string', format
=> 'ip',
58 description
=> "Node name.",
59 type
=> 'string', format
=>'pve-node',
62 description
=> "Public SSH RSA key for the host.",
64 pattern
=> '^[A-Za-z0-9\.\/\+]{200,}$',
67 description
=> "Public SSH RSA key for the root user.",
69 pattern
=> '^[A-Za-z0-9\.\/\+]{200,}$',
72 description
=> "SSL certificate fingerprint.",
74 pattern
=> '^(:?[A-Z0-9][A-Z0-9]:){31}[A-Z0-9][A-Z0-9]$',
82 name
=> { fixed
=> 1 },
89 package PMG
::ClusterConfig
::Master
;
94 use base
qw(PMG::ClusterConfig::Base);
103 description
=> "Maximum used cluster node ID (used internally, do not modify).",
112 maxcid
=> { fixed
=> 1 },
113 ip
=> { fixed
=> 1 },
114 name
=> { fixed
=> 1 },
121 package PMG
::ClusterConfig
;
133 PMG
::ClusterConfig
::Node-
>register;
134 PMG
::ClusterConfig
::Master-
>register;
135 PMG
::ClusterConfig
::Base-
>init();
141 my $class = ref($type) || $type;
143 my $cfg = PVE
::INotify
::read_file
("cluster.conf");
145 return bless $cfg, $class;
151 PVE
::INotify
::write_file
("cluster.conf", $self);
154 my $lockfile = "/var/lock/pmgcluster.lck";
157 my ($code, $errmsg) = @_;
159 my $res = PVE
::Tools
::lock_file
($lockfile, undef, $code);
161 $errmsg ?
die "$errmsg: $err" : die $err;
166 sub read_cluster_conf
{
167 my ($filename, $fh) = @_;
169 local $/ = undef; # slurp mode
171 my $raw = defined($fh) ?
<$fh> : undef;
173 my $cinfo = PMG
::ClusterConfig
::Base-
>parse_config($filename, $raw);
175 my $localname = PVE
::INotify
::nodename
();
176 my $localip = PVE
::Network
::get_ip_from_hostname
($localname);
178 $cinfo->{remnodes
} = [];
189 my $errprefix = "unable to parse $filename";
191 foreach my $cid (keys %{$cinfo->{ids
}}) {
192 my $d = $cinfo->{ids
}->{$cid};
194 die "$errprefix: duplicate use of name '$d->{name}'\n" if $names_hash->{$d->{name
}};
195 $names_hash->{$d->{name
}} = 1;
198 $maxcid = $cid > $maxcid ?
$cid : $maxcid;
199 $maxcid = $d->{maxcid
} if defined($d->{maxcid
}) && $d->{maxcid
} > $maxcid;
200 $cinfo->{master
} = $d if $d->{type
} eq 'master';
201 $cinfo->{'local'} = $d if $d->{name
} eq $localname;
205 die "$errprefix: cluster without master node\n"
206 if !defined($cinfo->{master
});
207 $cinfo->{master
}->{maxcid
} = $maxcid;
210 my $local_cid = $cinfo->{local}->{cid
};
211 foreach my $cid (sort keys %{$cinfo->{ids
}}) {
212 if ($local_cid != $cid) {
213 push @{$cinfo->{remnodes
}}, $cid;
220 sub write_cluster_conf
{
221 my ($filename, $fh, $cfg) = @_;
223 my $raw = PMG
::ClusterConfig
::Base-
>write_config($filename, $cfg);
225 PVE
::Tools
::safe_print
($filename, $fh, $raw);
228 PVE
::INotify
::register_file
('cluster.conf', "/etc/pmg/cluster.conf",
230 \
&write_cluster_conf
,
232 always_call_parser
=> 1);