]> git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/LRM.pm
lrm: factor out check fo maintenance-request
[pve-ha-manager.git] / src / PVE / HA / LRM.pm
1 package PVE::HA::LRM;
2
3 # Local Resource Manager
4
5 use strict;
6 use warnings;
7 use POSIX qw(:sys_wait_h);
8
9 use PVE::SafeSyslog;
10 use PVE::Tools;
11 use PVE::HA::Tools ':exit_codes';
12 use PVE::HA::Resources;
13
14 # Server can have several states:
15
16 my $valid_states = {
17 wait_for_agent_lock => "waiting for agent lock",
18 active => "got agent_lock",
19 maintenance => "going into maintenance",
20 lost_agent_lock => "lost agent_lock",
21 };
22
23 # we sleep ~10s per 'active' round, so if no services is available for >= 10 min we'd go in wait
24 # state giving up the watchdog and the LRM lock voluntary, ensuring the WD can do no harm
25 my $max_active_idle_rounds = 60;
26
27 sub new {
28 my ($this, $haenv) = @_;
29
30 my $class = ref($this) || $this;
31
32 my $self = bless {
33 haenv => $haenv,
34 status => { state => 'startup' },
35 workers => {},
36 results => {},
37 restart_tries => {},
38 shutdown_request => 0,
39 shutdown_errors => 0,
40 # mode can be: active, reboot, shutdown, restart
41 mode => 'active',
42 cluster_state_update => 0,
43 active_idle_rounds => 0,
44 }, $class;
45
46 $self->set_local_status({ state => 'wait_for_agent_lock' });
47
48 return $self;
49 }
50
51 sub shutdown_request {
52 my ($self) = @_;
53
54 return if $self->{shutdown_request}; # already in shutdown mode
55
56 my $haenv = $self->{haenv};
57
58 my $nodename = $haenv->nodename();
59
60 my ($shutdown, $reboot) = $haenv->is_node_shutdown();
61
62 my $dc_cfg = $haenv->get_datacenter_settings();
63 my $shutdown_policy = $dc_cfg->{ha}->{shutdown_policy} // 'conditional';
64
65 if ($shutdown) { # don't log this on service restart, only on node shutdown
66 $haenv->log('info', "got shutdown request with shutdown policy '$shutdown_policy'");
67 }
68
69 my $freeze_all;
70 my $maintenance;
71 if ($shutdown_policy eq 'conditional') {
72 $freeze_all = $reboot;
73 } elsif ($shutdown_policy eq 'freeze') {
74 $freeze_all = 1;
75 } elsif ($shutdown_policy eq 'failover') {
76 $freeze_all = 0;
77 } elsif ($shutdown_policy eq 'migrate') {
78 $maintenance = 1;
79 } else {
80 $haenv->log('err', "unknown shutdown policy '$shutdown_policy', fall back to conditional");
81 $freeze_all = $reboot;
82 }
83
84 if ($maintenance) {
85 # we get marked as unaivalable by the manager, then all services will
86 # be migrated away, we'll still have the same "can we exit" clause than
87 # a normal shutdown -> no running service on this node
88 # FIXME: after X minutes, add shutdown command for remaining services,
89 # e.g., if they have no alternative node???
90 } elsif ($shutdown) {
91 # *always* queue stop jobs for all services if the node shuts down,
92 # independent if it's a reboot or a poweroff, else we may corrupt
93 # services or hinder node shutdown
94 my $ss = $self->{service_status};
95
96 foreach my $sid (keys %$ss) {
97 my $sd = $ss->{$sid};
98 next if !$sd->{node};
99 next if $sd->{node} ne $nodename;
100 # Note: use undef uid to mark shutdown/stop jobs
101 $self->queue_resource_command($sid, undef, 'request_stop');
102 }
103 }
104
105 if ($shutdown) {
106 my $shutdown_type = $reboot ? 'reboot' : 'shutdown';
107 if ($maintenance) {
108 $haenv->log('info', "$shutdown_type LRM, doing maintenance, removing this node from active list");
109 $self->{mode} = 'maintenance';
110 } elsif ($freeze_all) {
111 $haenv->log('info', "$shutdown_type LRM, stop and freeze all services");
112 $self->{mode} = 'restart';
113 } else {
114 $haenv->log('info', "shutdown LRM, stop all services");
115 $self->{mode} = 'shutdown';
116 }
117 } else {
118 $haenv->log('info', "restart LRM, freeze all services");
119 $self->{mode} = 'restart';
120 }
121
122 $self->{shutdown_request} = $haenv->get_time();
123
124 eval { $self->update_lrm_status() or die "not quorate?\n"; };
125 if (my $err = $@) {
126 $haenv->log('err', "unable to update lrm status file - $err");
127 }
128 }
129
130 sub get_local_status {
131 my ($self) = @_;
132
133 return $self->{status};
134 }
135
136 sub set_local_status {
137 my ($self, $new) = @_;
138
139 die "invalid state '$new->{state}'" if !$valid_states->{$new->{state}};
140
141 my $haenv = $self->{haenv};
142
143 my $old = $self->{status};
144
145 # important: only update if if really changed
146 return if $old->{state} eq $new->{state};
147
148 $haenv->log('info', "status change $old->{state} => $new->{state}");
149
150 $new->{state_change_time} = $haenv->get_time();
151
152 $self->{status} = $new;
153 }
154
155 sub update_lrm_status {
156 my ($self) = @_;
157
158 my $haenv = $self->{haenv};
159
160 return 0 if !$haenv->quorate();
161
162 my $lrm_status = {
163 state => $self->{status}->{state},
164 mode => $self->{mode},
165 results => $self->{results},
166 timestamp => $haenv->get_time(),
167 };
168
169 eval { $haenv->write_lrm_status($lrm_status); };
170 if (my $err = $@) {
171 $haenv->log('err', "unable to write lrm status file - $err");
172 return 0;
173 }
174
175 return 1;
176 }
177
178 sub update_service_status {
179 my ($self) = @_;
180
181 my $haenv = $self->{haenv};
182
183 my $ms = eval { $haenv->read_manager_status(); };
184 if (my $err = $@) {
185 $haenv->log('err', "updating service status from manager failed: $err");
186 return undef;
187 } else {
188 $self->{service_status} = $ms->{service_status} || {};
189 my $nodename = $haenv->nodename();
190 $self->{node_status} = $ms->{node_status}->{$nodename} || 'unknown';
191 return 1;
192 }
193 }
194
195 sub get_protected_ha_agent_lock {
196 my ($self) = @_;
197
198 my $haenv = $self->{haenv};
199
200 my $count = 0;
201 my $starttime = $haenv->get_time();
202
203 for (;;) {
204
205 if ($haenv->get_ha_agent_lock()) {
206 if ($self->{ha_agent_wd}) {
207 $haenv->watchdog_update($self->{ha_agent_wd});
208 } else {
209 my $wfh = $haenv->watchdog_open();
210 $self->{ha_agent_wd} = $wfh;
211 }
212 return 1;
213 }
214
215 last if ++$count > 5; # try max 5 time
216
217 my $delay = $haenv->get_time() - $starttime;
218 last if $delay > 5; # for max 5 seconds
219
220 $haenv->sleep(1);
221 }
222
223 return 0;
224 }
225
226 # only cares if any service has the local node as their node, independent of which req.state it is
227 sub has_configured_service_on_local_node {
228 my ($self) = @_;
229
230 my $haenv = $self->{haenv};
231 my $nodename = $haenv->nodename();
232
233 my $ss = $self->{service_status};
234 foreach my $sid (keys %$ss) {
235 my $sd = $ss->{$sid};
236 next if !$sd->{node} || $sd->{node} ne $nodename;
237
238 return 1;
239 }
240 return 0;
241 }
242
243 sub is_fence_requested {
244 my ($self) = @_;
245
246 my $haenv = $self->{haenv};
247
248 my $nodename = $haenv->nodename();
249 my $ss = $self->{service_status};
250
251 my $fenced_services = PVE::HA::Tools::count_fenced_services($ss, $nodename);
252
253 return $fenced_services || $self->{node_status} eq 'fence';
254 }
255
256 sub is_maintenance_requested {
257 my ($self) = @_;
258
259 # shutdown maintenance or manual request
260 return $self->{mode} eq 'maintenance';
261 }
262
263 sub active_service_count {
264 my ($self) = @_;
265
266 my $haenv = $self->{haenv};
267 my $nodename = $haenv->nodename();
268
269 my $ss = $self->{service_status};
270
271 my $count = 0;
272 foreach my $sid (keys %$ss) {
273 my $sd = $ss->{$sid};
274 next if !$sd->{node};
275 next if $sd->{node} ne $nodename;
276 my $req_state = $sd->{state};
277 next if !defined($req_state);
278 next if $req_state eq 'stopped';
279 # NOTE: 'ignored' ones are already dropped by the manager from service_status
280 next if $req_state eq 'freeze';
281 # erroneous services are not managed by HA, don't count them as active
282 next if $req_state eq 'error';
283 # request_start is for (optional) better node selection for stop -> started transition
284 next if $req_state eq 'request_start';
285
286 $count++;
287 }
288
289 return $count;
290 }
291
292 my $wrote_lrm_status_at_startup = 0;
293
294 sub do_one_iteration {
295 my ($self) = @_;
296
297 my $haenv = $self->{haenv};
298
299 $haenv->loop_start_hook();
300
301 $self->{cluster_state_update} = $haenv->cluster_state_update();
302
303 my $res = $self->work();
304
305 $haenv->loop_end_hook();
306
307 return $res;
308 }
309
310 # NOTE: this is disabling the self-fence mechanism, so it must NOT be called with active services
311 # It's normally *only* OK on graceful shutdown (with no services, or all services frozen)
312 my sub give_up_watchdog_protection {
313 my ($self) = @_;
314
315 if ($self->{ha_agent_wd}) {
316 $self->{haenv}->watchdog_close($self->{ha_agent_wd});
317 delete $self->{ha_agent_wd}; # only delete after close!
318 }
319 }
320
321 sub work {
322 my ($self) = @_;
323
324 my $haenv = $self->{haenv};
325
326 if (!$wrote_lrm_status_at_startup) {
327 if ($self->update_lrm_status()) {
328 $wrote_lrm_status_at_startup = 1;
329 } else {
330 # do nothing
331 $haenv->sleep(5);
332 return $self->{shutdown_request} ? 0 : 1;
333 }
334 }
335
336 my $status = $self->get_local_status();
337 my $state = $status->{state};
338
339 $self->update_service_status();
340
341 my $fence_request = $self->is_fence_requested();
342
343 # do state changes first
344
345 my $ctime = $haenv->get_time();
346
347 if ($state eq 'wait_for_agent_lock') {
348
349 my $service_count = $self->active_service_count();
350
351 if (!$fence_request && $service_count && $haenv->quorate()) {
352 if ($self->get_protected_ha_agent_lock()) {
353 $self->set_local_status({ state => 'active' });
354 }
355 }
356
357 } elsif ($state eq 'lost_agent_lock') {
358
359 if (!$fence_request && $haenv->quorate()) {
360 if ($self->get_protected_ha_agent_lock()) {
361 $self->set_local_status({ state => 'active' });
362 }
363 }
364
365 } elsif ($state eq 'active') {
366
367 if ($fence_request) {
368 $haenv->log('err', "node need to be fenced - releasing agent_lock\n");
369 $self->set_local_status({ state => 'lost_agent_lock'});
370 } elsif (!$self->get_protected_ha_agent_lock()) {
371 $self->set_local_status({ state => 'lost_agent_lock'});
372 } elsif ($self->is_maintenance_requested()) {
373 $self->set_local_status({ state => 'maintenance'});
374 } else {
375 if (!$self->has_configured_service_on_local_node() && !$self->run_workers()) {
376 # no active service configured for this node and all (old) workers are done
377 $self->{active_idle_rounds}++;
378 if ($self->{active_idle_rounds} > $max_active_idle_rounds) {
379 $haenv->log('info', "node had no service configured for $max_active_idle_rounds rounds, going idle.\n");
380 # safety: no active service & no running worker for quite some time -> OK
381 $haenv->release_ha_agent_lock();
382 give_up_watchdog_protection($self);
383 $self->set_local_status({ state => 'wait_for_agent_lock'});
384 $self->{active_idle_rounds} = 0;
385 }
386 } elsif ($self->{active_idle_rounds}) {
387 $self->{active_idle_rounds} = 0;
388 }
389 }
390 } elsif ($state eq 'maintenance') {
391
392 if ($fence_request) {
393 $haenv->log('err', "node need to be fenced during maintenance mode - releasing agent_lock\n");
394 $self->set_local_status({ state => 'lost_agent_lock'});
395 } elsif (!$self->get_protected_ha_agent_lock()) {
396 $self->set_local_status({ state => 'lost_agent_lock'});
397 }
398 }
399
400 $status = $self->get_local_status();
401 $state = $status->{state};
402
403 # do work
404
405 if ($state eq 'wait_for_agent_lock') {
406
407 return 0 if $self->{shutdown_request};
408
409 $self->update_lrm_status();
410
411 $haenv->sleep(5);
412
413 } elsif ($state eq 'active') {
414
415 my $startime = $haenv->get_time();
416
417 my $max_time = 10;
418
419 my $shutdown = 0;
420
421 # do work (max_time seconds)
422 eval {
423 # fixme: set alert timer
424
425 # if we could not get the current service status there's no point
426 # in doing anything, try again next round.
427 return if !$self->update_service_status();
428
429 if ($self->{shutdown_request}) {
430
431 if ($self->{mode} eq 'restart') {
432 # catch exited workers to update service state
433 my $workers = $self->run_workers();
434 my $service_count = $self->active_service_count();
435
436 if ($service_count == 0 && $workers == 0) {
437 # safety: no active services or workers -> OK
438 give_up_watchdog_protection($self);
439 $shutdown = 1;
440
441 # restart with no or freezed services, release the lock
442 $haenv->release_ha_agent_lock();
443 }
444 } else {
445
446 if ($self->run_workers() == 0) {
447 if ($self->{shutdown_errors} == 0) {
448 # safety: no active services and LRM shutdown -> OK
449 give_up_watchdog_protection($self);
450
451 # shutdown with all services stopped thus release the lock
452 $haenv->release_ha_agent_lock();
453 }
454
455 $shutdown = 1;
456 }
457 }
458 } else {
459 if (!$self->{cluster_state_update}) {
460 # update failed but we could still renew our lock (cfs restart?),
461 # safely skip manage and expect to update just fine next round
462 $haenv->log('notice', "temporary inconsistent cluster state " .
463 "(cfs restart?), skip round");
464 return;
465 }
466
467 $self->manage_resources();
468
469 }
470 };
471 if (my $err = $@) {
472 $haenv->log('err', "got unexpected error - $err");
473 }
474
475 $self->update_lrm_status();
476
477 return 0 if $shutdown;
478
479 $haenv->sleep_until($startime + $max_time);
480
481 } elsif ($state eq 'lost_agent_lock') {
482
483 # NOTE: watchdog is active an will trigger soon!
484 # so we hope to get the lock back soon!
485 if ($self->{shutdown_request}) {
486
487 my $service_count = $self->active_service_count();
488
489 if ($service_count > 0) {
490 $haenv->log('err', "get shutdown request in state 'lost_agent_lock' - " .
491 "detected $service_count running services");
492
493 if ($self->{mode} eq 'restart') {
494 my $state_mt = $self->{status}->{state_change_time};
495
496 # watchdog should have already triggered, so either it's set
497 # set to noboot or it failed. As we are in restart mode, and
498 # have infinity stoptimeout -> exit now - we don't touch services
499 # or change state, so this is save, relatively speaking
500 if (($haenv->get_time() - $state_mt) > 90) {
501 $haenv->log('err', "lost agent lock and restart request for over 90 seconds - giving up!");
502 return 0;
503 }
504 }
505 } else {
506 # safety: all services are stopped, so we can close the watchdog
507 give_up_watchdog_protection($self);
508
509 return 0;
510 }
511 }
512
513 $haenv->sleep(5);
514
515 } elsif ($state eq 'maintenance') {
516
517 my $startime = $haenv->get_time();
518 return if !$self->update_service_status();
519
520 # wait until all active services moved away
521 my $service_count = $self->active_service_count();
522
523 my $exit_lrm = 0;
524
525 if ($self->{shutdown_request}) {
526 if ($service_count == 0 && $self->run_workers() == 0) {
527 # safety: going into maintenance and all active services got moved -> OK
528 give_up_watchdog_protection($self);
529
530 $exit_lrm = 1;
531
532 # restart with no or freezed services, release the lock
533 $haenv->release_ha_agent_lock();
534 }
535 }
536
537 $self->manage_resources() if !$exit_lrm;
538
539 $self->update_lrm_status();
540
541 return 0 if $exit_lrm;
542
543 $haenv->sleep_until($startime + 5);
544
545 } else {
546
547 die "got unexpected status '$state'\n";
548
549 }
550
551 return 1;
552 }
553
554 sub run_workers {
555 my ($self) = @_;
556
557 my $haenv = $self->{haenv};
558
559 my $starttime = $haenv->get_time();
560
561 # number of workers to start, if 0 we exec the command directly witouth forking
562 my $max_workers = $haenv->get_max_workers();
563 my $sc = $haenv->read_service_config();
564
565 my $worker = $self->{workers};
566 # we only got limited time but want to ensure that every queued worker is scheduled
567 # eventually, so sort by the count a worker was seen here in this loop
568 my $fair_sorter = sub {
569 $worker->{$b}->{start_tries} <=> $worker->{$a}->{start_tries} || $a cmp $b
570 };
571
572 while (($haenv->get_time() - $starttime) <= 8) {
573 my $count = $self->check_active_workers();
574
575 for my $sid (sort $fair_sorter grep { !$worker->{$_}->{pid} } keys %$worker) {
576 my $w = $worker->{$sid};
577 # higher try-count means higher priority especially compared to newly queued jobs, so
578 # count every try to avoid starvation
579 $w->{start_tries}++;
580 # FIXME: should be last and ensure that check_active_workers is called sooner
581 next if $count >= $max_workers && $max_workers > 0;
582
583 # only fork if we may, else call exec_resource_agent directly (e.g. for tests)
584 if ($max_workers > 0) {
585 my $pid = fork();
586 if (!defined($pid)) {
587 $haenv->log('err', "forking worker failed - $!");
588 $count = 0; last; # abort, try later
589 } elsif ($pid == 0) {
590 $haenv->after_fork(); # cleanup
591
592 # do work
593 my $res = -1;
594 eval {
595 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{params});
596 };
597 if (my $err = $@) {
598 $haenv->log('err', $err);
599 POSIX::_exit(-1);
600 }
601 POSIX::_exit($res);
602 } else {
603 $count++;
604 $w->{pid} = $pid;
605 }
606 } else {
607 my $res = -1;
608 eval {
609 $res = $self->exec_resource_agent($sid, $sc->{$sid}, $w->{state}, $w->{params});
610 $res = $res << 8 if $res > 0;
611 };
612 if (my $err = $@) {
613 $haenv->log('err', $err);
614 }
615 if (defined($w->{uid})) {
616 $self->resource_command_finished($sid, $w->{uid}, $res);
617 } else {
618 $self->stop_command_finished($sid, $res);
619 }
620 }
621 }
622
623 last if !$count;
624
625 $haenv->sleep(1);
626 }
627
628 return scalar(keys %{$self->{workers}});
629 }
630
631 sub manage_resources {
632 my ($self) = @_;
633
634 my $haenv = $self->{haenv};
635
636 my $nodename = $haenv->nodename();
637
638 my $ss = $self->{service_status};
639
640 foreach my $sid (keys %{$self->{restart_tries}}) {
641 delete $self->{restart_tries}->{$sid} if !$ss->{$sid};
642 }
643
644 foreach my $sid (keys %$ss) {
645 my $sd = $ss->{$sid};
646 next if !$sd->{node} || !$sd->{uid};
647 next if $sd->{node} ne $nodename;
648 my $request_state = $sd->{state};
649 next if !defined($request_state);
650 # can only happen for restricted groups where the failed node itself needs to be the
651 # reocvery target. Always let the master first do so, it will then marked as 'stopped' and
652 # we can just continue normally. But we must NOT do anything with it while still in recovery
653 next if $request_state eq 'recovery';
654 next if $request_state eq 'freeze';
655 # intermediate step for optional better node selection on stop -> start request state change
656 next if $request_state eq 'request_start';
657
658 $self->queue_resource_command($sid, $sd->{uid}, $request_state, {
659 'target' => $sd->{target},
660 'timeout' => $sd->{timeout},
661 });
662 }
663
664 return $self->run_workers();
665 }
666
667 sub queue_resource_command {
668 my ($self, $sid, $uid, $state, $params) = @_;
669
670 # do not queue the exact same command twice as this may lead to an inconsistent HA state when
671 # the first command fails but the CRM does not process its failure right away and the LRM starts
672 # a second try, without the CRM knowing of it (race condition) The 'stopped' command is an
673 # exception as we do not process its result in the CRM and we want to execute it always (even
674 # with no active CRM)
675 return if $state ne 'stopped' && $uid && defined($self->{results}->{$uid});
676
677 if (my $w = $self->{workers}->{$sid}) {
678 return if $w->{pid}; # already started
679 # else, delete and overwrite queue entry with new command
680 delete $self->{workers}->{$sid};
681 }
682
683 $self->{workers}->{$sid} = {
684 sid => $sid,
685 uid => $uid,
686 state => $state,
687 start_tries => 0,
688 };
689
690 $self->{workers}->{$sid}->{params} = $params if $params;
691 }
692
693 sub check_active_workers {
694 my ($self) = @_;
695
696 # finish/count workers
697 my $count = 0;
698 foreach my $sid (keys %{$self->{workers}}) {
699 my $w = $self->{workers}->{$sid};
700 my $pid = $w->{pid} || next;
701
702 my $waitpid = waitpid($pid, WNOHANG); # check status
703 if (defined($waitpid) && ($waitpid == $pid)) {
704 if (defined($w->{uid})) {
705 $self->resource_command_finished($sid, $w->{uid}, $?);
706 } else {
707 $self->stop_command_finished($sid, $?);
708 }
709 } else {
710 $count++; # still active
711 }
712 }
713
714 return $count;
715 }
716
717 sub stop_command_finished {
718 my ($self, $sid, $status) = @_;
719
720 my $haenv = $self->{haenv};
721
722 my $w = delete $self->{workers}->{$sid};
723 return if !$w; # should not happen
724
725 my $exit_code = -1;
726
727 if ($status == -1) {
728 $haenv->log('err', "resource agent $sid finished - failed to execute");
729 } elsif (my $sig = ($status & 127)) {
730 $haenv->log('err', "resource agent $sid finished - got signal $sig");
731 } else {
732 $exit_code = ($status >> 8);
733 }
734
735 if ($exit_code != 0) {
736 $self->{shutdown_errors}++;
737 }
738 }
739
740 sub resource_command_finished {
741 my ($self, $sid, $uid, $status) = @_;
742
743 my $haenv = $self->{haenv};
744
745 my $w = delete $self->{workers}->{$sid};
746 return if !$w; # should not happen
747
748 my $exit_code = -1;
749
750 if ($status == -1) {
751 $haenv->log('err', "resource agent $sid finished - failed to execute");
752 } elsif (my $sig = ($status & 127)) {
753 $haenv->log('err', "resource agent $sid finished - got signal $sig");
754 } else {
755 $exit_code = ($status >> 8);
756 }
757
758 $exit_code = $self->handle_service_exitcode($sid, $w->{state}, $exit_code);
759
760 return if $exit_code == ETRY_AGAIN; # tell nobody, simply retry
761
762 $self->{results}->{$uid} = {
763 sid => $w->{sid},
764 state => $w->{state},
765 exit_code => $exit_code,
766 };
767
768 my $ss = $self->{service_status};
769
770 # compute hash of valid/existing uids
771 my $valid_uids = {};
772 foreach my $sid (keys %$ss) {
773 my $sd = $ss->{$sid};
774 next if !$sd->{uid};
775 $valid_uids->{$sd->{uid}} = 1;
776 }
777
778 my $results = {};
779 foreach my $id (keys %{$self->{results}}) {
780 next if !$valid_uids->{$id};
781 $results->{$id} = $self->{results}->{$id};
782 }
783 $self->{results} = $results;
784 }
785
786 # processes the exit code from a finished resource agent, so that the CRM knows
787 # if the LRM wants to retry an action based on the current recovery policies for
788 # the failed service, or the CRM itself must try to recover from the failure.
789 sub handle_service_exitcode {
790 my ($self, $sid, $cmd, $exit_code) = @_;
791
792 my $haenv = $self->{haenv};
793 my $tries = $self->{restart_tries};
794
795 my $sc = $haenv->read_service_config();
796
797 my $max_restart = 0;
798
799 if (my $cd = $sc->{$sid}) {
800 $max_restart = $cd->{max_restart};
801 }
802
803 if ($cmd eq 'started') {
804
805 if ($exit_code == SUCCESS) {
806
807 $tries->{$sid} = 0;
808
809 return $exit_code;
810
811 } elsif ($exit_code == ERROR) {
812
813 $tries->{$sid} = 0 if !defined($tries->{$sid});
814
815 if ($tries->{$sid} >= $max_restart) {
816 $haenv->log('err', "unable to start service $sid on local node".
817 " after $tries->{$sid} retries");
818 $tries->{$sid} = 0;
819 return ERROR;
820 }
821
822 $tries->{$sid}++;
823
824 $haenv->log('warning', "restart policy: retry number $tries->{$sid}" .
825 " for service '$sid'");
826 # tell CRM that we retry the start
827 return ETRY_AGAIN;
828 }
829 }
830
831 return $exit_code;
832
833 }
834
835 sub exec_resource_agent {
836 my ($self, $sid, $service_config, $cmd, $params) = @_;
837
838 # setup execution environment
839
840 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
841
842 my $haenv = $self->{haenv};
843
844 my $nodename = $haenv->nodename();
845
846 my (undef, $service_type, $service_name) = $haenv->parse_sid($sid);
847
848 my $plugin = PVE::HA::Resources->lookup($service_type);
849 if (!$plugin) {
850 $haenv->log('err', "service type '$service_type' not implemented");
851 return EUNKNOWN_SERVICE_TYPE;
852 }
853
854 if (!$service_config) {
855 $haenv->log('err', "missing resource configuration for '$sid'");
856 return EUNKNOWN_SERVICE;
857 }
858
859 # process error state early
860 if ($cmd eq 'error') {
861 $haenv->log('err', "service $sid is in an error state and needs manual " .
862 "intervention. Look up 'ERROR RECOVERY' in the documentation.");
863
864 return SUCCESS; # error always succeeds
865 }
866
867 if ($service_config->{node} ne $nodename) {
868 $haenv->log('err', "service '$sid' not on this node");
869 return EWRONG_NODE;
870 }
871
872 my $id = $service_name;
873
874 my $running = $plugin->check_running($haenv, $id);
875
876 if ($cmd eq 'started') {
877
878 return SUCCESS if $running;
879
880 $haenv->log("info", "starting service $sid");
881
882 $plugin->start($haenv, $id);
883
884 $running = $plugin->check_running($haenv, $id);
885
886 if ($running) {
887 $haenv->log("info", "service status $sid started");
888 return SUCCESS;
889 } else {
890 $haenv->log("warning", "unable to start service $sid");
891 return ERROR;
892 }
893
894 } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') {
895
896 return SUCCESS if !$running;
897
898 if (defined($params->{timeout})) {
899 $haenv->log("info", "stopping service $sid (timeout=$params->{timeout})");
900 } else {
901 $haenv->log("info", "stopping service $sid");
902 }
903
904 $plugin->shutdown($haenv, $id, $params->{timeout});
905
906 $running = $plugin->check_running($haenv, $id);
907
908 if (!$running) {
909 $haenv->log("info", "service status $sid stopped");
910 return SUCCESS;
911 } else {
912 $haenv->log("info", "unable to stop stop service $sid (still running)");
913 return ERROR;
914 }
915
916 } elsif ($cmd eq 'migrate' || $cmd eq 'relocate' || $cmd eq 'request_start_balance') {
917
918 my $target = $params->{target};
919 if (!defined($target)) {
920 die "$cmd '$sid' failed - missing target\n" if !defined($target);
921 return EINVALID_PARAMETER;
922 }
923
924 if ($service_config->{node} eq $target) {
925 # already there
926 return SUCCESS;
927 }
928
929 my $online = ($cmd eq 'migrate') ? 1 : 0;
930
931 my $res = $plugin->migrate($haenv, $id, $target, $online);
932
933 # something went wrong if service is still on this node
934 if (!$res) {
935 $haenv->log("err", "service $sid not moved (migration error)");
936 return ERROR;
937 }
938
939 return SUCCESS;
940
941 }
942
943 $haenv->log("err", "implement me (cmd '$cmd')");
944 return EUNKNOWN_COMMAND;
945 }
946
947
948 1;