]>
git.proxmox.com Git - pmg-api.git/blob - PMG/ClusterConfig.pm
1 package PMG
::ClusterConfig
::Base
;
8 use PVE
::JSONSchema
qw(get_standard_option);
9 use PVE
::SectionConfig
;
11 use base
qw(PVE::SectionConfig);
15 type
=> { description
=> "Cluster node type." },
17 description
=> "Cluster Node ID.",
28 sub parse_section_header
{
29 my ($class, $line) = @_;
31 if ($line =~ m/^(node|master):\s*(\d+)\s*$/) {
32 my ($type, $sectionId) = ($1, $2);
33 my $errmsg = undef; # set if you want to skip whole section
34 my $config = {}; # to return additional attributes
35 return ($type, $sectionId, $errmsg, $config);
40 package PMG
::ClusterConfig
::Node
;
45 use base
qw(PMG::ClusterConfig::Base);
53 description
=> "IP address.",
54 type
=> 'string', format
=> 'ip',
57 description
=> "Node name.",
58 type
=> 'string', format
=>'pve-node',
61 description
=> "Public SSH RSA key for the host.",
65 description
=> "Public SSH RSA key for the root user.",
74 name
=> { fixed
=> 1 },
80 package PMG
::ClusterConfig
::Master
;
85 use base
qw(PMG::ClusterConfig::Base);
94 description
=> "Maximum used cluster node ID (used internally, do not modify).",
103 maxcid
=> { fixed
=> 1 },
104 ip
=> { fixed
=> 1 },
105 name
=> { fixed
=> 1 },
111 package PMG
::ClusterConfig
;
123 PMG
::ClusterConfig
::Node-
>register;
124 PMG
::ClusterConfig
::Master-
>register;
125 PMG
::ClusterConfig
::Base-
>init();
131 my $class = ref($type) || $type;
133 my $cfg = PVE
::INotify
::read_file
("cluster.conf");
135 return bless $cfg, $class;
141 PVE
::INotify
::write_file
("cluster.conf", $self);
144 my $lockfile = "/var/lock/pmgcluster.lck";
147 my ($code, $errmsg) = @_;
149 my $p = PVE
::Tools
::lock_file
($lockfile, undef, $code);
151 $errmsg ?
die "$errmsg: $err" : die $err;
155 sub read_cluster_conf
{
156 my ($filename, $fh) = @_;
158 local $/ = undef; # slurp mode
160 my $raw = defined($fh) ?
<$fh> : undef;
162 my $cinfo = PMG
::ClusterConfig
::Base-
>parse_config($filename, $raw);
164 my $localname = PVE
::INotify
::nodename
();
165 my $localip = PMG
::Utils
::lookup_node_ip
($localname);
167 $cinfo->{remnodes
} = [];
168 $cinfo->{configport
}->{0} = 83;
169 $cinfo->{dbport
}->{0} = 5432;
180 my $errprefix = "unable to parse $filename";
182 foreach my $cid (keys %{$cinfo->{ids
}}) {
183 my $d = $cinfo->{ids
}->{$cid};
185 die "$errprefix: duplicate use of name '$d->{name}'\n" if $names_hash->{$d->{name
}};
186 $names_hash->{$d->{name
}} = 1;
189 $maxcid = $cid > $maxcid ?
$cid : $maxcid;
190 $maxcid = $d->{maxcid
} if defined($d->{maxcid
}) && $d->{maxcid
} > $maxcid;
191 $cinfo->{master
} = $d if $d->{type
} eq 'master';
192 $cinfo->{'local'} = $d if $d->{name
} eq $localname;
196 die "$errprefix: cluster without master node\n"
197 if !defined($cinfo->{master
});
198 $cinfo->{master
}->{maxcid
} = $maxcid;
202 foreach my $cid (sort keys %{$cinfo->{ids
}}) {
203 if ($cinfo->{'local'}->{cid
} == $cid) { # local CID
204 $cinfo->{configport
}->{$cid} = 83;
205 $cinfo->{dbport
}->{$cid} = 5432;
208 $cinfo->{configport
}->{$cid} = 50000 + $portid;
209 $cinfo->{dbport
}->{$cid} = 50100 + $portid;
210 push @{$cinfo->{remnodes
}}, $cid;
217 sub write_cluster_conf
{
218 my ($filename, $fh, $cfg) = @_;
220 my $raw = PMG
::ClusterConfig
::Base-
>write_config($filename, $cfg);
222 PVE
::Tools
::safe_print
($filename, $fh, $raw);
225 PVE
::INotify
::register_file
('cluster.conf', "/etc/pmg/cluster.conf",
227 \
&write_cluster_conf
,
229 always_call_parser
=> 1);