]>
Commit | Line | Data |
---|---|---|
cc32b737 DM |
1 | package PVE::HA::Config; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
139a9b90 | 5 | use JSON; |
cc32b737 | 6 | |
139a9b90 | 7 | use PVE::HA::Tools; |
cc32b737 | 8 | use PVE::HA::Groups; |
139a9b90 | 9 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); |
d6edb3ea | 10 | use PVE::HA::Resources; |
cc32b737 DM |
11 | |
12 | PVE::HA::Groups->register(); | |
13 | ||
14 | PVE::HA::Groups->init(); | |
15 | ||
139a9b90 DM |
16 | my $manager_status_filename = "ha/manager_status"; |
17 | my $ha_groups_config = "ha/groups.cfg"; | |
18 | my $ha_resources_config = "ha/resources.cfg"; | |
19 | my $crm_commands_filename = "ha/crm_commands"; | |
f83e7e6e | 20 | my $ha_fence_config = "ha/fence.cfg"; |
139a9b90 DM |
21 | |
22 | cfs_register_file($crm_commands_filename, | |
23 | sub { my ($fn, $raw) = @_; return defined($raw) ? $raw : ''; }, | |
24 | sub { my ($fn, $raw) = @_; return $raw; }); | |
25 | cfs_register_file($ha_groups_config, | |
26 | sub { PVE::HA::Groups->parse_config(@_); }, | |
27 | sub { PVE::HA::Groups->write_config(@_); }); | |
28 | cfs_register_file($ha_resources_config, | |
29 | sub { PVE::HA::Resources->parse_config(@_); }, | |
30 | sub { PVE::HA::Resources->write_config(@_); }); | |
31 | cfs_register_file($manager_status_filename, | |
32 | \&json_reader, | |
33 | \&json_writer); | |
f83e7e6e TL |
34 | cfs_register_file($ha_fence_config, |
35 | \&PVE::HA::FenceConfig::parse_config, | |
36 | \&PVE::HA::FenceConfig::write_config); | |
139a9b90 DM |
37 | |
38 | sub json_reader { | |
39 | my ($filename, $data) = @_; | |
40 | ||
f1817b68 | 41 | return defined($data) && length($data) > 0 ? decode_json($data) : {}; |
139a9b90 DM |
42 | } |
43 | ||
44 | sub json_writer { | |
45 | my ($filename, $data) = @_; | |
46 | ||
47 | return encode_json($data); | |
48 | } | |
49 | ||
50 | sub 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 | ||
60 | sub 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 |
70 | sub parse_groups_config { |
71 | my ($filename, $raw) = @_; | |
72 | ||
139a9b90 | 73 | return PVE::HA::Groups->parse_config($filename, $raw); |
cc32b737 DM |
74 | } |
75 | ||
ce216792 | 76 | sub parse_resources_config { |
cc32b737 DM |
77 | my ($filename, $raw) = @_; |
78 | ||
79 | return PVE::HA::Resources->parse_config($filename, $raw); | |
80 | } | |
81 | ||
139a9b90 DM |
82 | sub 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 |
88 | sub 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 | 123 | sub read_group_config { |
139a9b90 DM |
124 | |
125 | return cfs_read_file($ha_groups_config); | |
126 | } | |
127 | ||
9eb0f126 DM |
128 | sub write_group_config { |
129 | my ($cfg) = @_; | |
130 | ||
131 | cfs_write_file($ha_groups_config, $cfg); | |
132 | } | |
133 | ||
139a9b90 DM |
134 | sub write_resources_config { |
135 | my ($cfg) = @_; | |
136 | ||
137 | cfs_write_file($ha_resources_config, $cfg); | |
138 | } | |
139 | ||
140 | sub read_manager_status { | |
141 | my () = @_; | |
142 | ||
143 | return cfs_read_file($manager_status_filename); | |
144 | } | |
145 | ||
146 | sub write_manager_status { | |
147 | my ($status_obj) = @_; | |
f83e7e6e | 148 | |
139a9b90 DM |
149 | cfs_write_file($manager_status_filename, $status_obj); |
150 | } | |
151 | ||
f83e7e6e TL |
152 | sub read_fence_config { |
153 | my () = @_; | |
154 | ||
155 | cfs_read_file($ha_fence_config); | |
156 | } | |
157 | ||
b3c07baa TL |
158 | sub write_fence_config { |
159 | my ($cfg) = @_; | |
160 | ||
161 | cfs_write_file($ha_fence_config, $cfg); | |
162 | } | |
163 | ||
66c7e7ef | 164 | sub 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 | ||
175 | sub 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 | ||
189 | sub 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 | 200 | my $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 | 218 | sub 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 |
231 | sub 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 | |
243 | sub 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 | 261 | 1; |