]> git.proxmox.com Git - pmg-api.git/blob - PMG/ClusterConfig.pm
bin/pmgtunnel: new service to tunnel database connections
[pmg-api.git] / PMG / ClusterConfig.pm
1 package PMG::ClusterConfig::Base;
2
3 use strict;
4 use warnings;
5 use Data::Dumper;
6
7 use PVE::Tools;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PVE::SectionConfig;
10
11 use base qw(PVE::SectionConfig);
12
13 my $defaultData = {
14 propertyList => {
15 type => { description => "Cluster node type." },
16 cid => {
17 description => "Cluster Node ID.",
18 type => 'integer',
19 minimum => 1,
20 },
21 },
22 };
23
24 sub private {
25 return $defaultData;
26 }
27
28 sub parse_section_header {
29 my ($class, $line) = @_;
30
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);
36 }
37 return undef;
38 }
39
40 package PMG::ClusterConfig::Node;
41
42 use strict;
43 use warnings;
44
45 use base qw(PMG::ClusterConfig::Base);
46
47 sub type {
48 return 'node';
49 }
50 sub properties {
51 return {
52 ip => {
53 description => "IP address.",
54 type => 'string', format => 'ip',
55 },
56 name => {
57 description => "Node name.",
58 type => 'string', format =>'pve-node',
59 },
60 hostrsapubkey => {
61 description => "Public SSH RSA key for the host.",
62 type => 'string',
63 pattern => '^[A-Za-z0-9\.\/\+]{200,}$',
64 },
65 rootrsapubkey => {
66 description => "Public SSH RSA key for the root user.",
67 type => 'string',
68 pattern => '^[A-Za-z0-9\.\/\+]{200,}$',
69 },
70 fingerprint => {
71 description => "SSL certificate fingerprint.",
72 type => 'string',
73 pattern => '^(:?[A-Z0-9][A-Z0-9]:){31}[A-Z0-9][A-Z0-9]$',
74 },
75 };
76 }
77
78 sub options {
79 return {
80 ip => { fixed => 1 },
81 name => { fixed => 1 },
82 hostrsapubkey => {},
83 rootrsapubkey => {},
84 fingerprint => {},
85 };
86 }
87
88 package PMG::ClusterConfig::Master;
89
90 use strict;
91 use warnings;
92
93 use base qw(PMG::ClusterConfig::Base);
94
95 sub type {
96 return 'master';
97 }
98
99 sub properties {
100 return {
101 maxcid => {
102 description => "Maximum used cluster node ID (used internally, do not modify).",
103 type => 'integer',
104 minimum => 1,
105 },
106 };
107 }
108
109 sub options {
110 return {
111 maxcid => { fixed => 1 },
112 ip => { fixed => 1 },
113 name => { fixed => 1 },
114 hostrsapubkey => {},
115 rootrsapubkey => {},
116 fingerprint => {},
117 };
118 }
119
120 package PMG::ClusterConfig;
121
122 use strict;
123 use warnings;
124 use Data::Dumper;
125
126 use PVE::SafeSyslog;
127 use PVE::Tools;
128 use PVE::INotify;
129
130 use PMG::Utils;
131
132 PMG::ClusterConfig::Node->register;
133 PMG::ClusterConfig::Master->register;
134 PMG::ClusterConfig::Base->init();
135
136
137 sub new {
138 my ($type) = @_;
139
140 my $class = ref($type) || $type;
141
142 my $cfg = PVE::INotify::read_file("cluster.conf");
143
144 return bless $cfg, $class;
145 }
146
147 sub write {
148 my ($self) = @_;
149
150 PVE::INotify::write_file("cluster.conf", $self);
151 }
152
153 my $lockfile = "/var/lock/pmgcluster.lck";
154
155 sub lock_config {
156 my ($code, $errmsg) = @_;
157
158 my $res = PVE::Tools::lock_file($lockfile, undef, $code);
159 if (my $err = $@) {
160 $errmsg ? die "$errmsg: $err" : die $err;
161 }
162 return $res;
163 }
164
165 sub read_cluster_conf {
166 my ($filename, $fh) = @_;
167
168 local $/ = undef; # slurp mode
169
170 my $raw = defined($fh) ? <$fh> : undef;
171
172 my $cinfo = PMG::ClusterConfig::Base->parse_config($filename, $raw);
173
174 my $localname = PVE::INotify::nodename();
175 my $localip = PMG::Utils::lookup_node_ip($localname);
176
177 $cinfo->{remnodes} = [];
178 $cinfo->{dbport}->{0} = 5432;
179
180 $cinfo->{local} = {
181 cid => 0,
182 ip => $localip,
183 name => $localname,
184 };
185
186 my $maxcid = 0;
187 my $names_hash = {};
188
189 my $errprefix = "unable to parse $filename";
190
191 foreach my $cid (keys %{$cinfo->{ids}}) {
192 my $d = $cinfo->{ids}->{$cid};
193
194 die "$errprefix: duplicate use of name '$d->{name}'\n" if $names_hash->{$d->{name}};
195 $names_hash->{$d->{name}} = 1;
196
197 $d->{cid} = $cid;
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;
202 }
203
204 if ($maxcid) {
205 die "$errprefix: cluster without master node\n"
206 if !defined($cinfo->{master});
207 $cinfo->{master}->{maxcid} = $maxcid;
208 }
209
210 my $ind = 0;
211 foreach my $cid (sort keys %{$cinfo->{ids}}) {
212 if ($cinfo->{'local'}->{cid} == $cid) { # local CID
213 $cinfo->{dbport}->{$cid} = 5432;
214 } else {
215 my $portid = $ind++;
216 $cinfo->{dbport}->{$cid} = 50100 + $portid;
217 push @{$cinfo->{remnodes}}, $cid;
218 }
219 }
220
221 return $cinfo;
222 }
223
224 sub write_cluster_conf {
225 my ($filename, $fh, $cfg) = @_;
226
227 my $raw = PMG::ClusterConfig::Base->write_config($filename, $cfg);
228
229 PVE::Tools::safe_print($filename, $fh, $raw);
230 }
231
232 PVE::INotify::register_file('cluster.conf', "/etc/pmg/cluster.conf",
233 \&read_cluster_conf,
234 \&write_cluster_conf,
235 undef,
236 always_call_parser => 1);
237
238 1;