]> git.proxmox.com Git - pve-ha-manager.git/blame - src/PVE/HA/Config.pm
wrap possible problematic cfs_read_file calls in eval
[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
bf7febe3 84 return eval { cfs_read_file($ha_resources_config) };
139a9b90
DM
85}
86
85f6e9ca
TL
87# checks if resource exists and sets defaults for unset values
88sub read_and_check_resources_config {
89
bf7febe3 90 my $res = eval { cfs_read_file($ha_resources_config) };
85f6e9ca
TL
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 124
bf7febe3 125 return eval { cfs_read_file($ha_groups_config) };
139a9b90
DM
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
bf7febe3 143 return eval { cfs_read_file($manager_status_filename) };
139a9b90
DM
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
bf7febe3 155 return eval { cfs_read_file($ha_fence_config) };
f83e7e6e
TL
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 {
bf7febe3
TL
192 my $data = eval { cfs_read_file($crm_commands_filename) };
193 return undef if !$data;
139a9b90
DM
194 cfs_write_file($crm_commands_filename, '');
195 return $data;
196 };
197
66c7e7ef 198 return lock_ha_domain($code);
139a9b90
DM
199}
200
0e703a14 201my $service_check_ha_state = sub {
e709d3d0
DM
202 my ($conf, $sid, $has_state) = @_;
203
204 if (my $d = $conf->{ids}->{$sid}) {
667670b2
TL
205 if (!defined($has_state)) {
206 # ignored service behave as if they were not managed by HA
207 return 0 if defined($d->{state}) && $d->{state} eq 'ignored';
208 return 1;
209 }
e709d3d0 210
54f19934
DM
211 # backward compatibility
212 $has_state = 'started' if $has_state eq 'enabled';
213
214 $d->{state} = 'started' if !defined($d->{state}) ||
215 ($d->{state} eq 'enabled');
216
e709d3d0
DM
217 return 1 if $d->{state} eq $has_state;
218 }
219
220 return undef;
221};
222
139a9b90 223sub vm_is_ha_managed {
55c82a65 224 my ($vmid, $has_state) = @_;
139a9b90
DM
225
226 my $conf = cfs_read_file($ha_resources_config);
227
8facb786 228 my $types = PVE::HA::Resources->lookup_types();
e709d3d0 229 foreach my $type ('vm', 'ct') {
0e703a14 230 return 1 if &$service_check_ha_state($conf, "$type:$vmid", $has_state);
e709d3d0 231 }
8facb786
TL
232
233 return undef;
139a9b90
DM
234}
235
9e03f787
TL
236sub service_is_ha_managed {
237 my ($sid, $has_state, $noerr) = @_;
238
239 my $conf = cfs_read_file($ha_resources_config);
240
0e703a14 241 return 1 if &$service_check_ha_state($conf, $sid, $has_state);
9e03f787
TL
242
243 die "resource '$sid' is not HA managed\n" if !$noerr;
244
245 return undef;
246}
917ea00c
TL
247
248sub get_service_status {
249 my ($sid) = @_;
250
251 my $status = { managed => 0 };
252
253 my $conf = cfs_read_file($ha_resources_config);
254
0e703a14 255 if (&$service_check_ha_state($conf, $sid)) {
917ea00c
TL
256 my $manager_status = cfs_read_file($manager_status_filename);
257
258 $status->{managed} = 1;
259 $status->{group} = $conf->{ids}->{$sid}->{group};
260 $status->{state} = $manager_status->{service_status}->{$sid}->{state};
261 }
262
263 return $status;
264}
265
cc32b737 2661;