]>
Commit | Line | Data |
---|---|---|
714a4016 DM |
1 | package PVE::HA::Env::PVE2; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
76737af5 DM |
5 | use POSIX qw(:errno_h :fcntl_h); |
6 | use IO::File; | |
115805fd | 7 | use IO::Socket::UNIX; |
714a4016 DM |
8 | |
9 | use PVE::SafeSyslog; | |
10 | use PVE::Tools; | |
119656b9 | 11 | use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file); |
022e4e79 DM |
12 | use PVE::INotify; |
13 | use PVE::RPCEnvironment; | |
714a4016 | 14 | |
a89ff919 | 15 | use PVE::HA::Tools ':exit_codes'; |
714a4016 | 16 | use PVE::HA::Env; |
ce216792 | 17 | use PVE::HA::Config; |
c982dfee | 18 | use PVE::HA::FenceConfig; |
9e5ea8f7 DM |
19 | use PVE::HA::Resources; |
20 | use PVE::HA::Resources::PVEVM; | |
21 | use PVE::HA::Resources::PVECT; | |
714a4016 | 22 | |
9e5ea8f7 DM |
23 | PVE::HA::Resources::PVEVM->register(); |
24 | PVE::HA::Resources::PVECT->register(); | |
25 | ||
26 | PVE::HA::Resources->init(); | |
022e4e79 | 27 | |
007fcc8b DM |
28 | my $lockdir = "/etc/pve/priv/lock"; |
29 | ||
714a4016 DM |
30 | sub new { |
31 | my ($this, $nodename) = @_; | |
32 | ||
33 | die "missing nodename" if !$nodename; | |
34 | ||
35 | my $class = ref($this) || $this; | |
36 | ||
37 | my $self = bless {}, $class; | |
38 | ||
39 | $self->{nodename} = $nodename; | |
40 | ||
41 | return $self; | |
42 | } | |
43 | ||
44 | sub nodename { | |
45 | my ($self) = @_; | |
46 | ||
47 | return $self->{nodename}; | |
48 | } | |
49 | ||
dd9c0c9d TL |
50 | sub hardware { |
51 | my ($self) = @_; | |
52 | ||
53 | die "hardware is for testing and simulation only"; | |
54 | } | |
55 | ||
714a4016 DM |
56 | sub read_manager_status { |
57 | my ($self) = @_; | |
714a4016 | 58 | |
139a9b90 | 59 | return PVE::HA::Config::read_manager_status(); |
714a4016 DM |
60 | } |
61 | ||
62 | sub write_manager_status { | |
63 | my ($self, $status_obj) = @_; | |
63f6a08c | 64 | |
139a9b90 | 65 | PVE::HA::Config::write_manager_status($status_obj); |
714a4016 DM |
66 | } |
67 | ||
c4a221bc DM |
68 | sub read_lrm_status { |
69 | my ($self, $node) = @_; | |
70 | ||
71 | $node = $self->{nodename} if !defined($node); | |
72 | ||
139a9b90 | 73 | return PVE::HA::Config::read_lrm_status($node); |
c4a221bc DM |
74 | } |
75 | ||
76 | sub write_lrm_status { | |
77 | my ($self, $status_obj) = @_; | |
78 | ||
6cbcb5f7 | 79 | my $node = $self->{nodename}; |
63f6a08c | 80 | |
139a9b90 DM |
81 | PVE::HA::Config::write_lrm_status($node, $status_obj); |
82 | } | |
c4a221bc | 83 | |
cde77779 | 84 | sub is_node_shutdown { |
d42219a3 TL |
85 | my ($self) = @_; |
86 | ||
cde77779 | 87 | my $shutdown = 0; |
f65f41b9 | 88 | my $reboot = 0; |
d42219a3 TL |
89 | |
90 | my $code = sub { | |
91 | my $line = shift; | |
92 | ||
61ae38eb | 93 | # ensure we match the full unit name by matching /^JOB_ID UNIT / |
f65f41b9 TL |
94 | # see: man systemd.special |
95 | $shutdown = 1 if ($line =~ m/^\d+\s+shutdown\.target\s+/); | |
96 | $reboot = 1 if ($line =~ m/^\d+\s+reboot\.target\s+/); | |
d42219a3 TL |
97 | }; |
98 | ||
61ae38eb | 99 | my $cmd = ['/bin/systemctl', '--full', 'list-jobs']; |
d42219a3 TL |
100 | eval { PVE::Tools::run_command($cmd, outfunc => $code, noerr => 1); }; |
101 | ||
f65f41b9 | 102 | return ($shutdown, $reboot); |
d42219a3 TL |
103 | } |
104 | ||
139a9b90 DM |
105 | sub queue_crm_commands { |
106 | my ($self, $cmd) = @_; | |
c4a221bc | 107 | |
139a9b90 DM |
108 | return PVE::HA::Config::queue_crm_commands($cmd); |
109 | } | |
110 | ||
111 | sub read_crm_commands { | |
112 | my ($self) = @_; | |
113 | ||
114 | return PVE::HA::Config::read_crm_commands(); | |
c4a221bc DM |
115 | } |
116 | ||
b83b4ae8 DM |
117 | sub read_service_config { |
118 | my ($self) = @_; | |
ce216792 | 119 | |
85f6e9ca | 120 | return PVE::HA::Config::read_and_check_resources_config(); |
714a4016 DM |
121 | } |
122 | ||
c982dfee TL |
123 | sub read_fence_config { |
124 | my ($self) = @_; | |
125 | ||
126 | return PVE::HA::Config::read_fence_config(); | |
127 | } | |
128 | ||
129 | sub fencing_mode { | |
130 | my ($self) = @_; | |
131 | ||
132 | my $datacenterconfig = cfs_read_file('datacenter.cfg'); | |
133 | ||
134 | return 'watchdog' if !$datacenterconfig->{fencing}; | |
135 | ||
136 | return $datacenterconfig->{fencing}; | |
137 | } | |
138 | ||
139 | sub exec_fence_agent { | |
140 | my ($self, $agent, $node, @param) = @_; | |
141 | ||
142 | # setup execution environment | |
143 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
144 | ||
145 | my $cmd = "$agent " . PVE::HA::FenceConfig::gen_arg_str(@param); | |
146 | ||
147 | exec($cmd); | |
148 | exit -1; | |
149 | } | |
150 | ||
9da84a0d TL |
151 | # this is only allowed by the master to recover a _fenced_ service |
152 | sub steal_service { | |
6da27e23 | 153 | my ($self, $sid, $current_node, $new_node) = @_; |
8456bde2 | 154 | |
6ca2edcd | 155 | my (undef, $type, $name) = PVE::HA::Tools::parse_sid($sid); |
6da27e23 | 156 | |
303a08aa TL |
157 | if(my $plugin = PVE::HA::Resources->lookup($type)) { |
158 | my $old = $plugin->config_file($name, $current_node); | |
159 | my $new = $plugin->config_file($name, $new_node); | |
6da27e23 DM |
160 | rename($old, $new) || |
161 | die "rename '$old' to '$new' failed - $!\n"; | |
162 | } else { | |
163 | die "implement me"; | |
164 | } | |
8456bde2 DM |
165 | } |
166 | ||
abc920b4 DM |
167 | sub read_group_config { |
168 | my ($self) = @_; | |
169 | ||
139a9b90 | 170 | return PVE::HA::Config::read_group_config(); |
3b996922 DM |
171 | } |
172 | ||
714a4016 DM |
173 | # this should return a hash containing info |
174 | # what nodes are members and online. | |
175 | sub get_node_info { | |
176 | my ($self) = @_; | |
177 | ||
d706ef8b | 178 | my ($node_info, $quorate) = ({}, 0); |
63f6a08c | 179 | |
d706ef8b DM |
180 | my $nodename = $self->{nodename}; |
181 | ||
182 | $quorate = PVE::Cluster::check_cfs_quorum(1) || 0; | |
183 | ||
184 | my $members = PVE::Cluster::get_members(); | |
185 | ||
186 | foreach my $node (keys %$members) { | |
187 | my $d = $members->{$node}; | |
63f6a08c | 188 | $node_info->{$node}->{online} = $d->{online}; |
d706ef8b | 189 | } |
63f6a08c | 190 | |
d706ef8b | 191 | $node_info->{$nodename}->{online} = 1; # local node is always up |
63f6a08c | 192 | |
d706ef8b | 193 | return ($node_info, $quorate); |
714a4016 DM |
194 | } |
195 | ||
196 | sub log { | |
197 | my ($self, $level, $msg) = @_; | |
198 | ||
199 | chomp $msg; | |
200 | ||
201 | syslog($level, $msg); | |
202 | } | |
203 | ||
1b3969b6 TL |
204 | sub sendmail { |
205 | my ($self, $subject, $text) = @_; | |
206 | ||
207 | my $mailfrom = 'root@' . $self->nodename(); | |
208 | my $mailto = 'root@localhost'; | |
209 | ||
210 | PVE::Tools::sendmail($mailto, $subject, $text, undef, $mailfrom); | |
211 | } | |
212 | ||
d69a79f3 | 213 | my $last_lock_status_hash = {}; |
007fcc8b DM |
214 | |
215 | sub get_pve_lock { | |
216 | my ($self, $lockid) = @_; | |
714a4016 | 217 | |
007fcc8b | 218 | my $got_lock = 0; |
4d24e7db | 219 | |
4d24e7db DM |
220 | my $filename = "$lockdir/$lockid"; |
221 | ||
d69a79f3 DM |
222 | $last_lock_status_hash->{$lockid} //= { lock_time => 0, got_lock => 0}; |
223 | my $last = $last_lock_status_hash->{$lockid}; | |
007fcc8b DM |
224 | |
225 | my $ctime = time(); | |
d69a79f3 DM |
226 | my $last_lock_time = $last->{lock_time} // 0; |
227 | my $last_got_lock = $last->{got_lock}; | |
4d24e7db | 228 | |
5d2406c9 | 229 | my $retry_timeout = 120; # hardcoded lock lifetime limit from pmxcfs |
63f6a08c | 230 | |
4d24e7db DM |
231 | eval { |
232 | ||
233 | mkdir $lockdir; | |
234 | ||
007fcc8b DM |
235 | # pve cluster filesystem not online |
236 | die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d $lockdir; | |
237 | ||
cb0bac5e | 238 | if (($ctime - $last_lock_time) < $retry_timeout) { |
737abf2f DM |
239 | # try cfs lock update request (utime) |
240 | if (utime(0, $ctime, $filename)) { | |
241 | $got_lock = 1; | |
242 | return; | |
243 | } | |
d69a79f3 DM |
244 | die "cfs lock update failed - $!\n"; |
245 | } | |
007fcc8b | 246 | |
d69a79f3 DM |
247 | if (mkdir $filename) { |
248 | $got_lock = 1; | |
249 | return; | |
007fcc8b | 250 | } |
4d24e7db | 251 | |
d69a79f3 DM |
252 | utime 0, 0, $filename; # cfs unlock request |
253 | die "can't get cfs lock\n"; | |
4d24e7db DM |
254 | }; |
255 | ||
007fcc8b DM |
256 | my $err = $@; |
257 | ||
d69a79f3 | 258 | #$self->log('err', $err) if $err; # for debugging |
63f6a08c | 259 | |
d69a79f3 DM |
260 | $last->{got_lock} = $got_lock; |
261 | $last->{lock_time} = $ctime if $got_lock; | |
007fcc8b | 262 | |
d69a79f3 | 263 | if (!!$got_lock != !!$last_got_lock) { |
007fcc8b | 264 | if ($got_lock) { |
63f6a08c | 265 | $self->log('info', "successfully acquired lock '$lockid'"); |
007fcc8b DM |
266 | } else { |
267 | my $msg = "lost lock '$lockid"; | |
63f6a08c | 268 | $msg .= " - $err" if $err; |
007fcc8b DM |
269 | $self->log('err', $msg); |
270 | } | |
271 | } | |
272 | ||
273 | return $got_lock; | |
274 | } | |
275 | ||
276 | sub get_ha_manager_lock { | |
277 | my ($self) = @_; | |
278 | ||
007fcc8b | 279 | return $self->get_pve_lock("ha_manager_lock"); |
714a4016 DM |
280 | } |
281 | ||
de002253 TL |
282 | # release the cluster wide manager lock. |
283 | # when released another CRM may step up and get the lock, thus this should only | |
284 | # get called when shutting down/deactivating the current master | |
285 | sub release_ha_manager_lock { | |
286 | my ($self) = @_; | |
287 | ||
288 | return rmdir("$lockdir/ha_manager_lock"); | |
289 | } | |
290 | ||
714a4016 | 291 | sub get_ha_agent_lock { |
714a4016 | 292 | my ($self, $node) = @_; |
63f6a08c | 293 | |
f5c29173 | 294 | $node = $self->nodename() if !defined($node); |
714a4016 | 295 | |
f5c29173 | 296 | return $self->get_pve_lock("ha_agent_${node}_lock"); |
714a4016 DM |
297 | } |
298 | ||
ff165cd8 TL |
299 | # release the respective node agent lock. |
300 | # this should only get called if the nodes LRM gracefully shuts down with | |
301 | # all services already cleanly stopped! | |
302 | sub release_ha_agent_lock { | |
303 | my ($self) = @_; | |
304 | ||
305 | my $node = $self->nodename(); | |
306 | ||
307 | return rmdir("$lockdir/ha_agent_${node}_lock"); | |
308 | } | |
309 | ||
714a4016 DM |
310 | sub quorate { |
311 | my ($self) = @_; | |
312 | ||
4d24e7db | 313 | my $quorate = 0; |
63f6a08c TL |
314 | eval { |
315 | $quorate = PVE::Cluster::check_cfs_quorum(); | |
4d24e7db | 316 | }; |
63f6a08c | 317 | |
4d24e7db | 318 | return $quorate; |
714a4016 DM |
319 | } |
320 | ||
321 | sub get_time { | |
322 | my ($self) = @_; | |
323 | ||
324 | return time(); | |
325 | } | |
326 | ||
327 | sub sleep { | |
328 | my ($self, $delay) = @_; | |
329 | ||
330 | CORE::sleep($delay); | |
331 | } | |
332 | ||
333 | sub sleep_until { | |
334 | my ($self, $end_time) = @_; | |
335 | ||
336 | for (;;) { | |
337 | my $cur_time = time(); | |
338 | ||
339 | last if $cur_time >= $end_time; | |
340 | ||
341 | $self->sleep(1); | |
342 | } | |
343 | } | |
344 | ||
345 | sub loop_start_hook { | |
346 | my ($self) = @_; | |
347 | ||
4d24e7db | 348 | PVE::Cluster::cfs_update(); |
63f6a08c | 349 | |
714a4016 DM |
350 | $self->{loop_start} = $self->get_time(); |
351 | } | |
352 | ||
353 | sub loop_end_hook { | |
354 | my ($self) = @_; | |
355 | ||
356 | my $delay = $self->get_time() - $self->{loop_start}; | |
63f6a08c | 357 | |
714a4016 DM |
358 | warn "loop take too long ($delay seconds)\n" if $delay > 30; |
359 | } | |
360 | ||
76737af5 DM |
361 | my $watchdog_fh; |
362 | ||
714a4016 DM |
363 | sub watchdog_open { |
364 | my ($self) = @_; | |
365 | ||
76737af5 DM |
366 | die "watchdog already open\n" if defined($watchdog_fh); |
367 | ||
115805fd DM |
368 | $watchdog_fh = IO::Socket::UNIX->new( |
369 | Type => SOCK_STREAM(), | |
370 | Peer => "/run/watchdog-mux.sock") || | |
371 | die "unable to open watchdog socket - $!\n"; | |
63f6a08c | 372 | |
76737af5 | 373 | $self->log('info', "watchdog active"); |
714a4016 DM |
374 | } |
375 | ||
376 | sub watchdog_update { | |
377 | my ($self, $wfh) = @_; | |
378 | ||
76737af5 DM |
379 | my $res = $watchdog_fh->syswrite("\0", 1); |
380 | if (!defined($res)) { | |
381 | $self->log('err', "watchdog update failed - $!\n"); | |
382 | return 0; | |
383 | } | |
384 | if ($res != 1) { | |
385 | $self->log('err', "watchdog update failed - write $res bytes\n"); | |
386 | return 0; | |
387 | } | |
388 | ||
389 | return 1; | |
714a4016 DM |
390 | } |
391 | ||
392 | sub watchdog_close { | |
393 | my ($self, $wfh) = @_; | |
394 | ||
76737af5 DM |
395 | $watchdog_fh->syswrite("V", 1); # magic watchdog close |
396 | if (!$watchdog_fh->close()) { | |
397 | $self->log('err', "watchdog close failed - $!"); | |
398 | } else { | |
399 | $watchdog_fh = undef; | |
400 | $self->log('info', "watchdog closed (disabled)"); | |
401 | } | |
714a4016 DM |
402 | } |
403 | ||
a2aae08a TL |
404 | sub after_fork { |
405 | my ($self) = @_; | |
406 | ||
407 | # close inherited inotify FD from parent and reopen our own | |
408 | PVE::INotify::inotify_close(); | |
409 | PVE::INotify::inotify_init(); | |
410 | ||
411 | PVE::Cluster::cfs_update(); | |
412 | } | |
413 | ||
a28fa330 TL |
414 | sub get_max_workers { |
415 | my ($self) = @_; | |
416 | ||
417 | my $datacenterconfig = cfs_read_file('datacenter.cfg'); | |
418 | ||
419 | return $datacenterconfig->{max_workers} || 4; | |
420 | } | |
421 | ||
714a4016 | 422 | 1; |