]> git.proxmox.com Git - pve-ha-manager.git/blame - src/PVE/HA/Config.pm
clean up 'Data::Dumper' usage tree wide
[pve-ha-manager.git] / src / PVE / HA / Config.pm
CommitLineData
cc32b737
DM
1package PVE::HA::Config;
2
3use strict;
4use warnings;
139a9b90 5use JSON;
cc32b737 6
139a9b90 7use PVE::HA::Tools;
cc32b737 8use PVE::HA::Groups;
139a9b90 9use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
d6edb3ea 10use PVE::HA::Resources;
cc32b737
DM
11
12PVE::HA::Groups->register();
13
14PVE::HA::Groups->init();
15
139a9b90
DM
16my $manager_status_filename = "ha/manager_status";
17my $ha_groups_config = "ha/groups.cfg";
18my $ha_resources_config = "ha/resources.cfg";
19my $crm_commands_filename = "ha/crm_commands";
f83e7e6e 20my $ha_fence_config = "ha/fence.cfg";
139a9b90
DM
21
22cfs_register_file($crm_commands_filename,
23 sub { my ($fn, $raw) = @_; return defined($raw) ? $raw : ''; },
24 sub { my ($fn, $raw) = @_; return $raw; });
25cfs_register_file($ha_groups_config,
26 sub { PVE::HA::Groups->parse_config(@_); },
27 sub { PVE::HA::Groups->write_config(@_); });
28cfs_register_file($ha_resources_config,
29 sub { PVE::HA::Resources->parse_config(@_); },
30 sub { PVE::HA::Resources->write_config(@_); });
31cfs_register_file($manager_status_filename,
32 \&json_reader,
33 \&json_writer);
f83e7e6e
TL
34cfs_register_file($ha_fence_config,
35 \&PVE::HA::FenceConfig::parse_config,
36 \&PVE::HA::FenceConfig::write_config);
139a9b90
DM
37
38sub json_reader {
39 my ($filename, $data) = @_;
40
f1817b68 41 return defined($data) && length($data) > 0 ? decode_json($data) : {};
139a9b90
DM
42}
43
44sub json_writer {
45 my ($filename, $data) = @_;
46
47 return encode_json($data);
48}
49
50sub read_lrm_status {
51 my ($node) = @_;
52
53 die "undefined node" if !defined($node);
54
55 my $filename = "/etc/pve/nodes/$node/lrm_status";
56
57 return PVE::HA::Tools::read_json_from_file($filename, {});
58}
59
60sub write_lrm_status {
61 my ($node, $status_obj) = @_;
62
63 die "undefined node" if !defined($node);
64
65 my $filename = "/etc/pve/nodes/$node/lrm_status";
66
67 PVE::HA::Tools::write_json_to_file($filename, $status_obj);
68}
69
cc32b737
DM
70sub parse_groups_config {
71 my ($filename, $raw) = @_;
72
139a9b90 73 return PVE::HA::Groups->parse_config($filename, $raw);
cc32b737
DM
74}
75
ce216792 76sub parse_resources_config {
cc32b737
DM
77 my ($filename, $raw) = @_;
78
79 return PVE::HA::Resources->parse_config($filename, $raw);
80}
81
139a9b90
DM
82sub read_resources_config {
83
84 return cfs_read_file($ha_resources_config);
85}
86
85f6e9ca
TL
87# checks if resource exists and sets defaults for unset values
88sub read_and_check_resources_config {
89
90 my $res = cfs_read_file($ha_resources_config);
91
92 my $vmlist = PVE::Cluster::get_vmlist();
93 my $conf = {};
94
95 foreach my $sid (keys %{$res->{ids}}) {
96 my $d = $res->{ids}->{$sid};
97 my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid);
bb07bd2c
TL
98 $d->{state} = 'started' if !defined($d->{state});
99 $d->{state} = 'started' if $d->{state} eq 'enabled'; # backward compatibility
85f6e9ca
TL
100 $d->{max_restart} = 1 if !defined($d->{max_restart});
101 $d->{max_relocate} = 1 if !defined($d->{max_relocate});
102 if (PVE::HA::Resources->lookup($d->{type})) {
103 if (my $vmd = $vmlist->{ids}->{$name}) {
104 if (!$vmd) {
105 warn "no such VM '$name'\n";
106 } else {
107 $d->{node} = $vmd->{node};
108 $conf->{$sid} = $d;
109 }
110 } else {
111 if (defined($d->{node})) {
112 $conf->{$sid} = $d;
113 } else {
114 warn "service '$sid' without node\n";
115 }
116 }
117 }
118 }
119
120 return $conf;
121}
122
139a9b90 123sub read_group_config {
139a9b90
DM
124
125 return cfs_read_file($ha_groups_config);
126}
127
9eb0f126
DM
128sub write_group_config {
129 my ($cfg) = @_;
130
131 cfs_write_file($ha_groups_config, $cfg);
132}
133
139a9b90
DM
134sub write_resources_config {
135 my ($cfg) = @_;
136
137 cfs_write_file($ha_resources_config, $cfg);
138}
139
140sub read_manager_status {
141 my () = @_;
142
143 return cfs_read_file($manager_status_filename);
144}
145
146sub write_manager_status {
147 my ($status_obj) = @_;
f83e7e6e 148
139a9b90
DM
149 cfs_write_file($manager_status_filename, $status_obj);
150}
151
f83e7e6e
TL
152sub read_fence_config {
153 my () = @_;
154
155 cfs_read_file($ha_fence_config);
156}
157
b3c07baa
TL
158sub write_fence_config {
159 my ($cfg) = @_;
160
161 cfs_write_file($ha_fence_config, $cfg);
162}
163
66c7e7ef 164sub lock_ha_domain {
139a9b90
DM
165 my ($code, $errmsg) = @_;
166
66c7e7ef 167 my $res = PVE::Cluster::cfs_lock_domain("ha", undef, $code);
139a9b90
DM
168 my $err = $@;
169 if ($err) {
170 $errmsg ? die "$errmsg: $err" : die $err;
171 }
172 return $res;
173}
174
175sub queue_crm_commands {
176 my ($cmd) = @_;
177
178 chomp $cmd;
179
180 my $code = sub {
181 my $data = cfs_read_file($crm_commands_filename);
182 $data .= "$cmd\n";
183 cfs_write_file($crm_commands_filename, $data);
184 };
185
66c7e7ef 186 return lock_ha_domain($code);
139a9b90
DM
187}
188
189sub read_crm_commands {
190
191 my $code = sub {
192 my $data = cfs_read_file($crm_commands_filename);
193 cfs_write_file($crm_commands_filename, '');
194 return $data;
195 };
196
66c7e7ef 197 return lock_ha_domain($code);
139a9b90
DM
198}
199
0e703a14 200my $service_check_ha_state = sub {
e709d3d0
DM
201 my ($conf, $sid, $has_state) = @_;
202
203 if (my $d = $conf->{ids}->{$sid}) {
204 return 1 if !defined($has_state);
205
54f19934
DM
206 # backward compatibility
207 $has_state = 'started' if $has_state eq 'enabled';
208
209 $d->{state} = 'started' if !defined($d->{state}) ||
210 ($d->{state} eq 'enabled');
211
e709d3d0
DM
212 return 1 if $d->{state} eq $has_state;
213 }
214
215 return undef;
216};
217
139a9b90 218sub vm_is_ha_managed {
55c82a65 219 my ($vmid, $has_state) = @_;
139a9b90
DM
220
221 my $conf = cfs_read_file($ha_resources_config);
222
8facb786 223 my $types = PVE::HA::Resources->lookup_types();
e709d3d0 224 foreach my $type ('vm', 'ct') {
0e703a14 225 return 1 if &$service_check_ha_state($conf, "$type:$vmid", $has_state);
e709d3d0 226 }
8facb786
TL
227
228 return undef;
139a9b90
DM
229}
230
9e03f787
TL
231sub service_is_ha_managed {
232 my ($sid, $has_state, $noerr) = @_;
233
234 my $conf = cfs_read_file($ha_resources_config);
235
0e703a14 236 return 1 if &$service_check_ha_state($conf, $sid, $has_state);
9e03f787
TL
237
238 die "resource '$sid' is not HA managed\n" if !$noerr;
239
240 return undef;
241}
917ea00c
TL
242
243sub get_service_status {
244 my ($sid) = @_;
245
246 my $status = { managed => 0 };
247
248 my $conf = cfs_read_file($ha_resources_config);
249
0e703a14 250 if (&$service_check_ha_state($conf, $sid)) {
917ea00c
TL
251 my $manager_status = cfs_read_file($manager_status_filename);
252
253 $status->{managed} = 1;
254 $status->{group} = $conf->{ids}->{$sid}->{group};
255 $status->{state} = $manager_status->{service_status}->{$sid}->{state};
256 }
257
258 return $status;
259}
260
cc32b737 2611;