]>
Commit | Line | Data |
---|---|---|
5f095798 DM |
1 | package PVE::HA::LRM; |
2 | ||
3 | # Local Resource Manager | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
c4a221bc DM |
7 | use Data::Dumper; |
8 | use POSIX qw(:sys_wait_h); | |
5f095798 DM |
9 | |
10 | use PVE::SafeSyslog; | |
11 | use PVE::Tools; | |
a89ff919 | 12 | use PVE::HA::Tools ':exit_codes'; |
2a045f55 | 13 | use PVE::HA::Resources; |
5f095798 DM |
14 | |
15 | # Server can have several states: | |
16 | ||
17 | my $valid_states = { | |
ec911edd | 18 | wait_for_agent_lock => "waiting for agent lock", |
0bba8f60 | 19 | active => "got agent_lock", |
5f095798 DM |
20 | lost_agent_lock => "lost agent_lock", |
21 | }; | |
22 | ||
23 | sub new { | |
24 | my ($this, $haenv) = @_; | |
25 | ||
26 | my $class = ref($this) || $this; | |
27 | ||
28 | my $self = bless { | |
29 | haenv => $haenv, | |
30 | status => { state => 'startup' }, | |
c4a221bc DM |
31 | workers => {}, |
32 | results => {}, | |
ea4443cc | 33 | restart_tries => {}, |
067cdf33 | 34 | shutdown_request => 0, |
116dea30 | 35 | shutdown_errors => 0, |
9c7d068b DM |
36 | # mode can be: active, reboot, shutdown, restart |
37 | mode => 'active', | |
5f095798 DM |
38 | }, $class; |
39 | ||
b0bf08a9 | 40 | $self->set_local_status({ state => 'wait_for_agent_lock' }); |
9c7d068b | 41 | |
5f095798 DM |
42 | return $self; |
43 | } | |
44 | ||
45 | sub shutdown_request { | |
46 | my ($self) = @_; | |
47 | ||
f1be5b3a DM |
48 | return if $self->{shutdown_request}; # already in shutdown mode |
49 | ||
499f06e3 DM |
50 | my $haenv = $self->{haenv}; |
51 | ||
116dea30 DM |
52 | my $nodename = $haenv->nodename(); |
53 | ||
f65f41b9 | 54 | my ($shutdown, $reboot) = $haenv->is_node_shutdown(); |
499f06e3 DM |
55 | |
56 | if ($shutdown) { | |
f65f41b9 TL |
57 | # *always* queue stop jobs for all services if the node shuts down, |
58 | # independent if it's a reboot or a poweroff, else we may corrupt | |
59 | # services or hinder node shutdown | |
116dea30 DM |
60 | my $ss = $self->{service_status}; |
61 | ||
62 | foreach my $sid (keys %$ss) { | |
63 | my $sd = $ss->{$sid}; | |
64 | next if !$sd->{node}; | |
65 | next if $sd->{node} ne $nodename; | |
c0edbd7e | 66 | # Note: use undef uid to mark shutdown/stop jobs |
116dea30 DM |
67 | $self->queue_resource_command($sid, undef, 'request_stop'); |
68 | } | |
f65f41b9 | 69 | } |
116dea30 | 70 | |
f65f41b9 TL |
71 | if ($shutdown) { |
72 | if ($reboot) { | |
73 | $haenv->log('info', "reboot LRM, stop and freeze all services"); | |
74 | $self->{mode} = 'restart'; | |
75 | } else { | |
76 | $haenv->log('info', "shutdown LRM, stop all services"); | |
77 | $self->{mode} = 'shutdown'; | |
78 | } | |
499f06e3 DM |
79 | } else { |
80 | $haenv->log('info', "restart LRM, freeze all services"); | |
81 | $self->{mode} = 'restart'; | |
82 | } | |
9c7d068b | 83 | |
499f06e3 | 84 | $self->{shutdown_request} = 1; |
9c7d068b DM |
85 | |
86 | eval { $self->update_lrm_status(); }; | |
87 | if (my $err = $@) { | |
5bd7aa54 | 88 | $self->log('err', "unable to update lrm status file - $err"); |
9c7d068b | 89 | } |
5f095798 DM |
90 | } |
91 | ||
92 | sub get_local_status { | |
93 | my ($self) = @_; | |
94 | ||
95 | return $self->{status}; | |
96 | } | |
97 | ||
98 | sub set_local_status { | |
99 | my ($self, $new) = @_; | |
100 | ||
101 | die "invalid state '$new->{state}'" if !$valid_states->{$new->{state}}; | |
102 | ||
103 | my $haenv = $self->{haenv}; | |
104 | ||
105 | my $old = $self->{status}; | |
106 | ||
107 | # important: only update if if really changed | |
108 | return if $old->{state} eq $new->{state}; | |
109 | ||
0bba8f60 | 110 | $haenv->log('info', "status change $old->{state} => $new->{state}"); |
5f095798 DM |
111 | |
112 | $new->{state_change_time} = $haenv->get_time(); | |
113 | ||
114 | $self->{status} = $new; | |
115 | } | |
116 | ||
9c7d068b DM |
117 | sub update_lrm_status { |
118 | my ($self) = @_; | |
119 | ||
5bd7aa54 DM |
120 | my $haenv = $self->{haenv}; |
121 | ||
79829202 DM |
122 | return 0 if !$haenv->quorate(); |
123 | ||
9c7d068b | 124 | my $lrm_status = { |
331a9f00 | 125 | state => $self->{status}->{state}, |
9c7d068b DM |
126 | mode => $self->{mode}, |
127 | results => $self->{results}, | |
aa330d1c | 128 | timestamp => $haenv->get_time(), |
9c7d068b DM |
129 | }; |
130 | ||
5bd7aa54 DM |
131 | eval { $haenv->write_lrm_status($lrm_status); }; |
132 | if (my $err = $@) { | |
133 | $haenv->log('err', "unable to write lrm status file - $err"); | |
134 | return 0; | |
135 | } | |
136 | ||
137 | return 1; | |
9c7d068b DM |
138 | } |
139 | ||
5f095798 DM |
140 | sub get_protected_ha_agent_lock { |
141 | my ($self) = @_; | |
142 | ||
143 | my $haenv = $self->{haenv}; | |
144 | ||
145 | my $count = 0; | |
146 | my $starttime = $haenv->get_time(); | |
147 | ||
148 | for (;;) { | |
149 | ||
150 | if ($haenv->get_ha_agent_lock()) { | |
151 | if ($self->{ha_agent_wd}) { | |
152 | $haenv->watchdog_update($self->{ha_agent_wd}); | |
153 | } else { | |
154 | my $wfh = $haenv->watchdog_open(); | |
155 | $self->{ha_agent_wd} = $wfh; | |
156 | } | |
157 | return 1; | |
158 | } | |
159 | ||
160 | last if ++$count > 5; # try max 5 time | |
161 | ||
162 | my $delay = $haenv->get_time() - $starttime; | |
163 | last if $delay > 5; # for max 5 seconds | |
164 | ||
165 | $haenv->sleep(1); | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
546e2f1f DM |
171 | sub active_service_count { |
172 | my ($self) = @_; | |
173 | ||
174 | my $haenv = $self->{haenv}; | |
175 | ||
176 | my $nodename = $haenv->nodename(); | |
177 | ||
178 | my $ss = $self->{service_status}; | |
179 | ||
180 | my $count = 0; | |
181 | ||
182 | foreach my $sid (keys %$ss) { | |
183 | my $sd = $ss->{$sid}; | |
184 | next if !$sd->{node}; | |
185 | next if $sd->{node} ne $nodename; | |
186 | my $req_state = $sd->{state}; | |
187 | next if !defined($req_state); | |
188 | next if $req_state eq 'stopped'; | |
9c7d068b | 189 | next if $req_state eq 'freeze'; |
38545741 TL |
190 | # erroneous services are not managed by HA, don't count them as active |
191 | next if $req_state eq 'error'; | |
546e2f1f DM |
192 | |
193 | $count++; | |
194 | } | |
195 | ||
196 | return $count; | |
197 | } | |
5bd7aa54 DM |
198 | |
199 | my $wrote_lrm_status_at_startup = 0; | |
200 | ||
5f095798 DM |
201 | sub do_one_iteration { |
202 | my ($self) = @_; | |
203 | ||
204 | my $haenv = $self->{haenv}; | |
205 | ||
c5ec095f | 206 | if (!$wrote_lrm_status_at_startup) { |
79829202 | 207 | if ($self->update_lrm_status()) { |
c5ec095f DM |
208 | $wrote_lrm_status_at_startup = 1; |
209 | } else { | |
210 | # do nothing | |
211 | $haenv->sleep(5); | |
212 | return $self->{shutdown_request} ? 0 : 1; | |
213 | } | |
5bd7aa54 DM |
214 | } |
215 | ||
5f095798 DM |
216 | my $status = $self->get_local_status(); |
217 | my $state = $status->{state}; | |
218 | ||
067cdf33 DM |
219 | my $ms = $haenv->read_manager_status(); |
220 | $self->{service_status} = $ms->{service_status} || {}; | |
221 | ||
49777d09 | 222 | my $fence_request = PVE::HA::Tools::count_fenced_services($self->{service_status}, $haenv->nodename()); |
067cdf33 | 223 | |
5f095798 DM |
224 | # do state changes first |
225 | ||
226 | my $ctime = $haenv->get_time(); | |
227 | ||
b0bf08a9 | 228 | if ($state eq 'wait_for_agent_lock') { |
5f095798 | 229 | |
546e2f1f | 230 | my $service_count = $self->active_service_count(); |
5f095798 | 231 | |
067cdf33 | 232 | if (!$fence_request && $service_count && $haenv->quorate()) { |
0bba8f60 DM |
233 | if ($self->get_protected_ha_agent_lock()) { |
234 | $self->set_local_status({ state => 'active' }); | |
5f095798 DM |
235 | } |
236 | } | |
237 | ||
238 | } elsif ($state eq 'lost_agent_lock') { | |
239 | ||
067cdf33 | 240 | if (!$fence_request && $haenv->quorate()) { |
0bba8f60 DM |
241 | if ($self->get_protected_ha_agent_lock()) { |
242 | $self->set_local_status({ state => 'active' }); | |
5f095798 DM |
243 | } |
244 | } | |
245 | ||
0bba8f60 | 246 | } elsif ($state eq 'active') { |
5f095798 | 247 | |
067cdf33 DM |
248 | if ($fence_request) { |
249 | $haenv->log('err', "node need to be fenced - releasing agent_lock\n"); | |
250 | $self->set_local_status({ state => 'lost_agent_lock'}); | |
251 | } elsif (!$self->get_protected_ha_agent_lock()) { | |
5f095798 DM |
252 | $self->set_local_status({ state => 'lost_agent_lock'}); |
253 | } | |
254 | } | |
255 | ||
256 | $status = $self->get_local_status(); | |
257 | $state = $status->{state}; | |
258 | ||
259 | # do work | |
260 | ||
261 | if ($state eq 'wait_for_agent_lock') { | |
262 | ||
263 | return 0 if $self->{shutdown_request}; | |
79829202 DM |
264 | |
265 | $self->update_lrm_status(); | |
266 | ||
5f095798 DM |
267 | $haenv->sleep(5); |
268 | ||
0bba8f60 | 269 | } elsif ($state eq 'active') { |
5f095798 DM |
270 | |
271 | my $startime = $haenv->get_time(); | |
272 | ||
273 | my $max_time = 10; | |
274 | ||
275 | my $shutdown = 0; | |
276 | ||
277 | # do work (max_time seconds) | |
278 | eval { | |
279 | # fixme: set alert timer | |
280 | ||
281 | if ($self->{shutdown_request}) { | |
282 | ||
499f06e3 | 283 | if ($self->{mode} eq 'restart') { |
5f095798 | 284 | |
499f06e3 | 285 | my $service_count = $self->active_service_count(); |
5f095798 | 286 | |
499f06e3 | 287 | if ($service_count == 0) { |
5f095798 | 288 | |
116dea30 DM |
289 | if ($self->run_workers() == 0) { |
290 | if ($self->{ha_agent_wd}) { | |
291 | $haenv->watchdog_close($self->{ha_agent_wd}); | |
292 | delete $self->{ha_agent_wd}; | |
293 | } | |
294 | ||
295 | $shutdown = 1; | |
e23f674c TL |
296 | |
297 | # restart with no or freezed services, release the lock | |
298 | $haenv->release_ha_agent_lock(); | |
116dea30 DM |
299 | } |
300 | } | |
301 | } else { | |
302 | ||
303 | if ($self->run_workers() == 0) { | |
304 | if ($self->{shutdown_errors} == 0) { | |
305 | if ($self->{ha_agent_wd}) { | |
306 | $haenv->watchdog_close($self->{ha_agent_wd}); | |
307 | delete $self->{ha_agent_wd}; | |
308 | } | |
0e5b1a43 TL |
309 | |
310 | # shutdown with all services stopped thus release the lock | |
311 | $haenv->release_ha_agent_lock(); | |
499f06e3 | 312 | } |
5f095798 | 313 | |
499f06e3 DM |
314 | $shutdown = 1; |
315 | } | |
5f095798 | 316 | } |
c4a221bc | 317 | } else { |
c4a221bc DM |
318 | |
319 | $self->manage_resources(); | |
067cdf33 | 320 | |
5f095798 DM |
321 | } |
322 | }; | |
323 | if (my $err = $@) { | |
324 | $haenv->log('err', "got unexpected error - $err"); | |
325 | } | |
326 | ||
79829202 DM |
327 | $self->update_lrm_status(); |
328 | ||
5f095798 DM |
329 | return 0 if $shutdown; |
330 | ||
331 | $haenv->sleep_until($startime + $max_time); | |
332 | ||
333 | } elsif ($state eq 'lost_agent_lock') { | |
334 | ||
335 | # Note: watchdog is active an will triger soon! | |
336 | ||
337 | # so we hope to get the lock back soon! | |
338 | ||
339 | if ($self->{shutdown_request}) { | |
340 | ||
546e2f1f | 341 | my $service_count = $self->active_service_count(); |
5f095798 | 342 | |
546e2f1f | 343 | if ($service_count > 0) { |
5f095798 | 344 | $haenv->log('err', "get shutdown request in state 'lost_agent_lock' - " . |
546e2f1f | 345 | "detected $service_count running services"); |
5f095798 | 346 | |
546e2f1f | 347 | } else { |
5f095798 | 348 | |
546e2f1f | 349 | # all services are stopped, so we can close the watchdog |
5f095798 | 350 | |
546e2f1f DM |
351 | if ($self->{ha_agent_wd}) { |
352 | $haenv->watchdog_close($self->{ha_agent_wd}); | |
353 | delete $self->{ha_agent_wd}; | |
354 | } | |
355 | ||
356 | return 0; | |
5f095798 | 357 | } |
5f095798 DM |
358 | } |
359 | ||
b0bf08a9 DM |
360 | $haenv->sleep(5); |
361 | ||
5f095798 DM |
362 | } else { |
363 | ||
364 | die "got unexpected status '$state'\n"; | |
365 | ||
366 | } | |
367 | ||
368 | return 1; | |
369 | } | |
370 | ||
116dea30 | 371 | sub run_workers { |
c4a221bc DM |
372 | my ($self) = @_; |
373 | ||
374 | my $haenv = $self->{haenv}; | |
375 | ||
f31b7e94 | 376 | my $starttime = $haenv->get_time(); |
c4a221bc | 377 | |
a28fa330 TL |
378 | # number of workers to start, if 0 we exec the command directly witouth forking |
379 | my $max_workers = $haenv->get_max_workers(); | |
c4a221bc | 380 | |
6dbf93a0 | 381 | my $sc = $haenv->read_service_config(); |
f31b7e94 DM |
382 | |
383 | while (($haenv->get_time() - $starttime) < 5) { | |
c4a221bc DM |
384 | my $count = $self->check_active_workers(); |
385 | ||
a5e4bef4 | 386 | foreach my $sid (sort keys %{$self->{workers}}) { |
a28fa330 TL |
387 | last if $count >= $max_workers && $max_workers > 0; |
388 | ||
c4a221bc DM |
389 | my $w = $self->{workers}->{$sid}; |
390 | if (!$w->{pid}) { | |
a28fa330 TL |
391 | # only fork if we may else call exec_resource_agent |
392 | # directly (e.g. for regression tests) | |
393 | if ($max_workers > 0) { | |
f31b7e94 DM |
394 | my $pid = fork(); |
395 | if (!defined($pid)) { | |
396 | $haenv->log('err', "fork worker failed"); | |
397 | $count = 0; last; # abort, try later | |
398 | } elsif ($pid == 0) { | |
a2aae08a TL |
399 | $haenv->after_fork(); # cleanup |
400 | ||
f31b7e94 DM |
401 | # do work |
402 | my $res = -1; | |
403 | eval { | |
aaabde6a | 404 | $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{target}); |
f31b7e94 DM |
405 | }; |
406 | if (my $err = $@) { | |
407 | $haenv->log('err', $err); | |
408 | POSIX::_exit(-1); | |
409 | } | |
410 | POSIX::_exit($res); | |
411 | } else { | |
412 | $count++; | |
413 | $w->{pid} = $pid; | |
414 | } | |
415 | } else { | |
c4a221bc DM |
416 | my $res = -1; |
417 | eval { | |
aaabde6a | 418 | $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{target}); |
b33b5743 | 419 | $res = $res << 8 if $res > 0; |
c4a221bc DM |
420 | }; |
421 | if (my $err = $@) { | |
f31b7e94 | 422 | $haenv->log('err', $err); |
116dea30 DM |
423 | } |
424 | if (defined($w->{uid})) { | |
425 | $self->resource_command_finished($sid, $w->{uid}, $res); | |
426 | } else { | |
427 | $self->stop_command_finished($sid, $res); | |
428 | } | |
c4a221bc DM |
429 | } |
430 | } | |
431 | } | |
432 | ||
433 | last if !$count; | |
434 | ||
f31b7e94 | 435 | $haenv->sleep(1); |
c4a221bc | 436 | } |
116dea30 DM |
437 | |
438 | return scalar(keys %{$self->{workers}}); | |
439 | } | |
440 | ||
441 | sub manage_resources { | |
442 | my ($self) = @_; | |
443 | ||
444 | my $haenv = $self->{haenv}; | |
445 | ||
446 | my $nodename = $haenv->nodename(); | |
447 | ||
448 | my $ss = $self->{service_status}; | |
449 | ||
5a28da91 TL |
450 | foreach my $sid (keys %{$self->{restart_tries}}) { |
451 | delete $self->{restart_tries}->{$sid} if !$ss->{$sid}; | |
452 | } | |
453 | ||
116dea30 DM |
454 | foreach my $sid (keys %$ss) { |
455 | my $sd = $ss->{$sid}; | |
456 | next if !$sd->{node}; | |
457 | next if !$sd->{uid}; | |
458 | next if $sd->{node} ne $nodename; | |
459 | my $req_state = $sd->{state}; | |
460 | next if !defined($req_state); | |
461 | next if $req_state eq 'freeze'; | |
462 | $self->queue_resource_command($sid, $sd->{uid}, $req_state, $sd->{target}); | |
463 | } | |
464 | ||
465 | return $self->run_workers(); | |
c4a221bc DM |
466 | } |
467 | ||
c4a221bc | 468 | sub queue_resource_command { |
e88469ba | 469 | my ($self, $sid, $uid, $state, $target) = @_; |
c4a221bc | 470 | |
35cbb764 TL |
471 | # do not queue the excatly same command twice as this may lead to |
472 | # an inconsistent HA state when the first command fails but the CRM | |
473 | # does not process its failure right away and the LRM starts a second | |
474 | # try, without the CRM knowing of it (race condition) | |
475 | # The 'stopped' command is an exception as we do not process its result | |
476 | # in the CRM and we want to execute it always (even with no active CRM) | |
477 | return if $state ne 'stopped' && $uid && defined($self->{results}->{$uid}); | |
478 | ||
c4a221bc DM |
479 | if (my $w = $self->{workers}->{$sid}) { |
480 | return if $w->{pid}; # already started | |
481 | # else, delete and overwrite queue entry with new command | |
482 | delete $self->{workers}->{$sid}; | |
483 | } | |
484 | ||
485 | $self->{workers}->{$sid} = { | |
486 | sid => $sid, | |
487 | uid => $uid, | |
488 | state => $state, | |
489 | }; | |
e88469ba DM |
490 | |
491 | $self->{workers}->{$sid}->{target} = $target if $target; | |
c4a221bc DM |
492 | } |
493 | ||
494 | sub check_active_workers { | |
495 | my ($self) = @_; | |
496 | ||
497 | # finish/count workers | |
498 | my $count = 0; | |
499 | foreach my $sid (keys %{$self->{workers}}) { | |
500 | my $w = $self->{workers}->{$sid}; | |
501 | if (my $pid = $w->{pid}) { | |
502 | # check status | |
503 | my $waitpid = waitpid($pid, WNOHANG); | |
504 | if (defined($waitpid) && ($waitpid == $pid)) { | |
c0edbd7e | 505 | if (defined($w->{uid})) { |
116dea30 DM |
506 | $self->resource_command_finished($sid, $w->{uid}, $?); |
507 | } else { | |
508 | $self->stop_command_finished($sid, $?); | |
509 | } | |
c4a221bc DM |
510 | } else { |
511 | $count++; | |
512 | } | |
513 | } | |
514 | } | |
515 | ||
516 | return $count; | |
517 | } | |
518 | ||
116dea30 DM |
519 | sub stop_command_finished { |
520 | my ($self, $sid, $status) = @_; | |
521 | ||
522 | my $haenv = $self->{haenv}; | |
523 | ||
524 | my $w = delete $self->{workers}->{$sid}; | |
525 | return if !$w; # should not happen | |
526 | ||
527 | my $exit_code = -1; | |
528 | ||
529 | if ($status == -1) { | |
530 | $haenv->log('err', "resource agent $sid finished - failed to execute"); | |
531 | } elsif (my $sig = ($status & 127)) { | |
532 | $haenv->log('err', "resource agent $sid finished - got signal $sig"); | |
533 | } else { | |
534 | $exit_code = ($status >> 8); | |
535 | } | |
536 | ||
537 | if ($exit_code != 0) { | |
538 | $self->{shutdown_errors}++; | |
539 | } | |
540 | } | |
541 | ||
c4a221bc DM |
542 | sub resource_command_finished { |
543 | my ($self, $sid, $uid, $status) = @_; | |
544 | ||
545 | my $haenv = $self->{haenv}; | |
546 | ||
547 | my $w = delete $self->{workers}->{$sid}; | |
548 | return if !$w; # should not happen | |
549 | ||
550 | my $exit_code = -1; | |
551 | ||
552 | if ($status == -1) { | |
0f70400d | 553 | $haenv->log('err', "resource agent $sid finished - failed to execute"); |
c4a221bc | 554 | } elsif (my $sig = ($status & 127)) { |
0f70400d | 555 | $haenv->log('err', "resource agent $sid finished - got signal $sig"); |
c4a221bc DM |
556 | } else { |
557 | $exit_code = ($status >> 8); | |
c4a221bc DM |
558 | } |
559 | ||
ea4443cc TL |
560 | $exit_code = $self->handle_service_exitcode($sid, $w->{state}, $exit_code); |
561 | ||
280ee5d5 DM |
562 | return if $exit_code == ETRY_AGAIN; # tell nobody, simply retry |
563 | ||
c4a221bc DM |
564 | $self->{results}->{$uid} = { |
565 | sid => $w->{sid}, | |
566 | state => $w->{state}, | |
567 | exit_code => $exit_code, | |
568 | }; | |
569 | ||
570 | my $ss = $self->{service_status}; | |
571 | ||
572 | # compute hash of valid/existing uids | |
573 | my $valid_uids = {}; | |
574 | foreach my $sid (keys %$ss) { | |
575 | my $sd = $ss->{$sid}; | |
576 | next if !$sd->{uid}; | |
577 | $valid_uids->{$sd->{uid}} = 1; | |
578 | } | |
579 | ||
580 | my $results = {}; | |
581 | foreach my $id (keys %{$self->{results}}) { | |
582 | next if !$valid_uids->{$id}; | |
583 | $results->{$id} = $self->{results}->{$id}; | |
584 | } | |
585 | $self->{results} = $results; | |
c4a221bc DM |
586 | } |
587 | ||
ea4443cc TL |
588 | # processes the exit code from a finished resource agent, so that the CRM knows |
589 | # if the LRM wants to retry an action based on the current recovery policies for | |
590 | # the failed service, or the CRM itself must try to recover from the failure. | |
591 | sub handle_service_exitcode { | |
592 | my ($self, $sid, $cmd, $exit_code) = @_; | |
593 | ||
594 | my $haenv = $self->{haenv}; | |
595 | my $tries = $self->{restart_tries}; | |
596 | ||
597 | my $sc = $haenv->read_service_config(); | |
aaabde6a DM |
598 | |
599 | my $max_restart = 0; | |
600 | ||
601 | if (my $cd = $sc->{$sid}) { | |
602 | $max_restart = $cd->{max_restart}; | |
603 | } | |
ea4443cc TL |
604 | |
605 | if ($cmd eq 'started') { | |
606 | ||
a89ff919 | 607 | if ($exit_code == SUCCESS) { |
ea4443cc TL |
608 | |
609 | $tries->{$sid} = 0; | |
610 | ||
611 | return $exit_code; | |
612 | ||
a89ff919 | 613 | } elsif ($exit_code == ERROR) { |
ea4443cc TL |
614 | |
615 | $tries->{$sid} = 0 if !defined($tries->{$sid}); | |
616 | ||
aaabde6a | 617 | if ($tries->{$sid} >= $max_restart) { |
ea4443cc TL |
618 | $haenv->log('err', "unable to start service $sid on local node". |
619 | " after $tries->{$sid} retries"); | |
620 | $tries->{$sid} = 0; | |
a89ff919 | 621 | return ERROR; |
ea4443cc TL |
622 | } |
623 | ||
e9e1cd68 TL |
624 | $tries->{$sid}++; |
625 | ||
626 | $haenv->log('warning', "restart policy: retry number $tries->{$sid}" . | |
627 | " for service '$sid'"); | |
a89ff919 TL |
628 | # tell CRM that we retry the start |
629 | return ETRY_AGAIN; | |
ea4443cc TL |
630 | } |
631 | } | |
632 | ||
633 | return $exit_code; | |
634 | ||
635 | } | |
636 | ||
2a045f55 TL |
637 | sub exec_resource_agent { |
638 | my ($self, $sid, $service_config, $cmd, @params) = @_; | |
639 | ||
640 | # setup execution environment | |
641 | ||
642 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; | |
643 | ||
2a045f55 TL |
644 | my $haenv = $self->{haenv}; |
645 | ||
646 | my $nodename = $haenv->nodename(); | |
647 | ||
648 | my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid); | |
649 | ||
650 | my $plugin = PVE::HA::Resources->lookup($service_type); | |
651 | if (!$plugin) { | |
652 | $haenv->log('err', "service type '$service_type' not implemented"); | |
653 | return EUNKNOWN_SERVICE_TYPE; | |
654 | } | |
655 | ||
aaabde6a DM |
656 | if (!$service_config) { |
657 | $haenv->log('err', "missing resource configuration for '$sid'"); | |
658 | return EUNKNOWN_SERVICE; | |
659 | } | |
660 | ||
d338a56f TL |
661 | # process error state early |
662 | if ($cmd eq 'error') { | |
663 | ||
664 | $haenv->log('err', "service $sid is in an error state and needs manual " . | |
665 | "intervention. Look up 'ERROR RECOVERY' in the documentation."); | |
666 | ||
667 | return SUCCESS; # error always succeeds | |
668 | } | |
669 | ||
2a045f55 TL |
670 | if ($service_config->{node} ne $nodename) { |
671 | $haenv->log('err', "service '$sid' not on this node"); | |
672 | return EWRONG_NODE; | |
673 | } | |
674 | ||
675 | my $id = $service_name; | |
676 | ||
677 | my $running = $plugin->check_running($haenv, $id); | |
678 | ||
679 | if ($cmd eq 'started') { | |
680 | ||
681 | return SUCCESS if $running; | |
682 | ||
683 | $haenv->log("info", "starting service $sid"); | |
684 | ||
685 | $plugin->start($haenv, $id); | |
686 | ||
687 | $running = $plugin->check_running($haenv, $id); | |
688 | ||
689 | if ($running) { | |
690 | $haenv->log("info", "service status $sid started"); | |
691 | return SUCCESS; | |
692 | } else { | |
693 | $haenv->log("warning", "unable to start service $sid"); | |
694 | return ERROR; | |
695 | } | |
696 | ||
697 | } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') { | |
698 | ||
699 | return SUCCESS if !$running; | |
700 | ||
701 | $haenv->log("info", "stopping service $sid"); | |
702 | ||
703 | $plugin->shutdown($haenv, $id); | |
704 | ||
705 | $running = $plugin->check_running($haenv, $id); | |
706 | ||
707 | if (!$running) { | |
708 | $haenv->log("info", "service status $sid stopped"); | |
709 | return SUCCESS; | |
710 | } else { | |
711 | $haenv->log("info", "unable to stop stop service $sid (still running)"); | |
712 | return ERROR; | |
713 | } | |
714 | ||
715 | } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') { | |
716 | ||
717 | my $target = $params[0]; | |
718 | if (!defined($target)) { | |
719 | die "$cmd '$sid' failed - missing target\n" if !defined($target); | |
720 | return EINVALID_PARAMETER; | |
721 | } | |
722 | ||
723 | if ($service_config->{node} eq $target) { | |
724 | # already there | |
725 | return SUCCESS; | |
726 | } | |
727 | ||
728 | my $online = ($cmd eq 'migrate') ? 1 : 0; | |
729 | ||
ea28f873 | 730 | my $res = $plugin->migrate($haenv, $id, $target, $online); |
2a045f55 TL |
731 | |
732 | # something went wrong if service is still on this node | |
ea28f873 | 733 | if (!$res) { |
2a045f55 TL |
734 | $haenv->log("err", "service $sid not moved (migration error)"); |
735 | return ERROR; | |
736 | } | |
737 | ||
738 | return SUCCESS; | |
739 | ||
2a045f55 TL |
740 | } |
741 | ||
742 | $haenv->log("err", "implement me (cmd '$cmd')"); | |
743 | return EUNKNOWN_COMMAND; | |
744 | } | |
745 | ||
746 | ||
5f095798 | 747 | 1; |