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