]>
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; |
714a4016 | 18 | |
022e4e79 | 19 | |
007fcc8b DM |
20 | my $lockdir = "/etc/pve/priv/lock"; |
21 | ||
714a4016 DM |
22 | sub new { |
23 | my ($this, $nodename) = @_; | |
24 | ||
25 | die "missing nodename" if !$nodename; | |
26 | ||
27 | my $class = ref($this) || $this; | |
28 | ||
29 | my $self = bless {}, $class; | |
30 | ||
31 | $self->{nodename} = $nodename; | |
32 | ||
33 | return $self; | |
34 | } | |
35 | ||
36 | sub nodename { | |
37 | my ($self) = @_; | |
38 | ||
39 | return $self->{nodename}; | |
40 | } | |
41 | ||
42 | sub read_manager_status { | |
43 | my ($self) = @_; | |
714a4016 | 44 | |
139a9b90 | 45 | return PVE::HA::Config::read_manager_status(); |
714a4016 DM |
46 | } |
47 | ||
48 | sub write_manager_status { | |
49 | my ($self, $status_obj) = @_; | |
63f6a08c | 50 | |
139a9b90 | 51 | PVE::HA::Config::write_manager_status($status_obj); |
714a4016 DM |
52 | } |
53 | ||
c4a221bc DM |
54 | sub read_lrm_status { |
55 | my ($self, $node) = @_; | |
56 | ||
57 | $node = $self->{nodename} if !defined($node); | |
58 | ||
139a9b90 | 59 | return PVE::HA::Config::read_lrm_status($node); |
c4a221bc DM |
60 | } |
61 | ||
62 | sub write_lrm_status { | |
63 | my ($self, $status_obj) = @_; | |
64 | ||
6cbcb5f7 | 65 | my $node = $self->{nodename}; |
63f6a08c | 66 | |
139a9b90 DM |
67 | PVE::HA::Config::write_lrm_status($node, $status_obj); |
68 | } | |
c4a221bc | 69 | |
139a9b90 DM |
70 | sub queue_crm_commands { |
71 | my ($self, $cmd) = @_; | |
c4a221bc | 72 | |
139a9b90 DM |
73 | return PVE::HA::Config::queue_crm_commands($cmd); |
74 | } | |
75 | ||
76 | sub read_crm_commands { | |
77 | my ($self) = @_; | |
78 | ||
79 | return PVE::HA::Config::read_crm_commands(); | |
c4a221bc DM |
80 | } |
81 | ||
b83b4ae8 | 82 | sub service_config_exists { |
714a4016 | 83 | my ($self) = @_; |
63f6a08c | 84 | |
b83b4ae8 DM |
85 | return PVE::HA::Config::resources_config_exists(); |
86 | } | |
714a4016 | 87 | |
b83b4ae8 DM |
88 | sub read_service_config { |
89 | my ($self) = @_; | |
ce216792 | 90 | |
b83b4ae8 | 91 | my $res = PVE::HA::Config::read_resources_config(); |
63f6a08c | 92 | |
ce216792 DM |
93 | my $vmlist = PVE::Cluster::get_vmlist(); |
94 | my $conf = {}; | |
95 | ||
96 | foreach my $sid (keys %{$res->{ids}}) { | |
97 | my $d = $res->{ids}->{$sid}; | |
6ca2edcd | 98 | my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid); |
7a19642e | 99 | $d->{state} = 'enabled' if !defined($d->{state}); |
ea4443cc TL |
100 | $d->{max_restart} = 1 if !defined($d->{max_restart}); |
101 | $d->{max_relocate} = 1 if !defined($d->{max_relocate}); | |
303a08aa | 102 | if (PVE::HA::Resources->lookup($d->{type})) { |
b47a7a1b | 103 | if (my $vmd = $vmlist->{ids}->{$name}) { |
ce216792 | 104 | if (!$vmd) { |
b47a7a1b | 105 | warn "no such VM '$name'\n"; |
ce216792 DM |
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 | } | |
63f6a08c | 119 | |
ce216792 | 120 | return $conf; |
714a4016 DM |
121 | } |
122 | ||
8456bde2 | 123 | sub change_service_location { |
6da27e23 | 124 | my ($self, $sid, $current_node, $new_node) = @_; |
8456bde2 | 125 | |
6ca2edcd | 126 | my (undef, $type, $name) = PVE::HA::Tools::parse_sid($sid); |
6da27e23 | 127 | |
303a08aa TL |
128 | if(my $plugin = PVE::HA::Resources->lookup($type)) { |
129 | my $old = $plugin->config_file($name, $current_node); | |
130 | my $new = $plugin->config_file($name, $new_node); | |
6da27e23 DM |
131 | rename($old, $new) || |
132 | die "rename '$old' to '$new' failed - $!\n"; | |
133 | } else { | |
134 | die "implement me"; | |
135 | } | |
8456bde2 DM |
136 | } |
137 | ||
abc920b4 DM |
138 | sub read_group_config { |
139 | my ($self) = @_; | |
140 | ||
139a9b90 | 141 | return PVE::HA::Config::read_group_config(); |
3b996922 DM |
142 | } |
143 | ||
714a4016 DM |
144 | # this should return a hash containing info |
145 | # what nodes are members and online. | |
146 | sub get_node_info { | |
147 | my ($self) = @_; | |
148 | ||
d706ef8b | 149 | my ($node_info, $quorate) = ({}, 0); |
63f6a08c | 150 | |
d706ef8b DM |
151 | my $nodename = $self->{nodename}; |
152 | ||
153 | $quorate = PVE::Cluster::check_cfs_quorum(1) || 0; | |
154 | ||
155 | my $members = PVE::Cluster::get_members(); | |
156 | ||
157 | foreach my $node (keys %$members) { | |
158 | my $d = $members->{$node}; | |
63f6a08c | 159 | $node_info->{$node}->{online} = $d->{online}; |
d706ef8b | 160 | } |
63f6a08c | 161 | |
d706ef8b | 162 | $node_info->{$nodename}->{online} = 1; # local node is always up |
63f6a08c | 163 | |
d706ef8b | 164 | return ($node_info, $quorate); |
714a4016 DM |
165 | } |
166 | ||
167 | sub log { | |
168 | my ($self, $level, $msg) = @_; | |
169 | ||
170 | chomp $msg; | |
171 | ||
172 | syslog($level, $msg); | |
173 | } | |
174 | ||
007fcc8b DM |
175 | my $last_lock_status = {}; |
176 | ||
177 | sub get_pve_lock { | |
178 | my ($self, $lockid) = @_; | |
714a4016 | 179 | |
007fcc8b | 180 | my $got_lock = 0; |
4d24e7db | 181 | |
4d24e7db DM |
182 | my $filename = "$lockdir/$lockid"; |
183 | ||
007fcc8b DM |
184 | my $last = $last_lock_status->{$lockid} || 0; |
185 | ||
186 | my $ctime = time(); | |
4d24e7db | 187 | |
75aca181 DM |
188 | my $retry = 0; |
189 | my $retry_timeout = 100; # fixme: what timeout | |
63f6a08c | 190 | |
4d24e7db DM |
191 | eval { |
192 | ||
193 | mkdir $lockdir; | |
194 | ||
007fcc8b DM |
195 | # pve cluster filesystem not online |
196 | die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d $lockdir; | |
197 | ||
75aca181 DM |
198 | if ($last && (($ctime - $last) < $retry_timeout)) { |
199 | # send cfs lock update request (utime) | |
200 | if (!utime(0, $ctime, $filename)) { | |
201 | $retry = 1; | |
007fcc8b | 202 | die "cfs lock update failed - $!\n"; |
75aca181 | 203 | } |
007fcc8b DM |
204 | } else { |
205 | ||
206 | # fixme: wait some time? | |
207 | if (!(mkdir $filename)) { | |
208 | utime 0, 0, $filename; # cfs unlock request | |
209 | die "can't get cfs lock\n"; | |
210 | } | |
211 | } | |
4d24e7db | 212 | |
007fcc8b | 213 | $got_lock = 1; |
4d24e7db DM |
214 | }; |
215 | ||
007fcc8b DM |
216 | my $err = $@; |
217 | ||
75aca181 DM |
218 | if ($retry) { |
219 | # $self->log('err', $err) if $err; # for debugging | |
220 | return 0; | |
221 | } | |
63f6a08c | 222 | |
007fcc8b DM |
223 | $last_lock_status->{$lockid} = $got_lock ? $ctime : 0; |
224 | ||
17e90af6 | 225 | if (!!$got_lock != !!$last) { |
007fcc8b | 226 | if ($got_lock) { |
63f6a08c | 227 | $self->log('info', "successfully acquired lock '$lockid'"); |
007fcc8b DM |
228 | } else { |
229 | my $msg = "lost lock '$lockid"; | |
63f6a08c | 230 | $msg .= " - $err" if $err; |
007fcc8b DM |
231 | $self->log('err', $msg); |
232 | } | |
75aca181 DM |
233 | } else { |
234 | # $self->log('err', $err) if $err; # for debugging | |
007fcc8b DM |
235 | } |
236 | ||
237 | return $got_lock; | |
238 | } | |
239 | ||
240 | sub get_ha_manager_lock { | |
241 | my ($self) = @_; | |
242 | ||
007fcc8b | 243 | return $self->get_pve_lock("ha_manager_lock"); |
714a4016 DM |
244 | } |
245 | ||
246 | sub get_ha_agent_lock { | |
714a4016 | 247 | my ($self, $node) = @_; |
63f6a08c | 248 | |
f5c29173 | 249 | $node = $self->nodename() if !defined($node); |
714a4016 | 250 | |
f5c29173 | 251 | return $self->get_pve_lock("ha_agent_${node}_lock"); |
714a4016 DM |
252 | } |
253 | ||
254 | sub quorate { | |
255 | my ($self) = @_; | |
256 | ||
4d24e7db | 257 | my $quorate = 0; |
63f6a08c TL |
258 | eval { |
259 | $quorate = PVE::Cluster::check_cfs_quorum(); | |
4d24e7db | 260 | }; |
63f6a08c | 261 | |
4d24e7db | 262 | return $quorate; |
714a4016 DM |
263 | } |
264 | ||
265 | sub get_time { | |
266 | my ($self) = @_; | |
267 | ||
268 | return time(); | |
269 | } | |
270 | ||
271 | sub sleep { | |
272 | my ($self, $delay) = @_; | |
273 | ||
274 | CORE::sleep($delay); | |
275 | } | |
276 | ||
277 | sub sleep_until { | |
278 | my ($self, $end_time) = @_; | |
279 | ||
280 | for (;;) { | |
281 | my $cur_time = time(); | |
282 | ||
283 | last if $cur_time >= $end_time; | |
284 | ||
285 | $self->sleep(1); | |
286 | } | |
287 | } | |
288 | ||
289 | sub loop_start_hook { | |
290 | my ($self) = @_; | |
291 | ||
4d24e7db | 292 | PVE::Cluster::cfs_update(); |
63f6a08c | 293 | |
714a4016 DM |
294 | $self->{loop_start} = $self->get_time(); |
295 | } | |
296 | ||
297 | sub loop_end_hook { | |
298 | my ($self) = @_; | |
299 | ||
300 | my $delay = $self->get_time() - $self->{loop_start}; | |
63f6a08c | 301 | |
714a4016 DM |
302 | warn "loop take too long ($delay seconds)\n" if $delay > 30; |
303 | } | |
304 | ||
76737af5 DM |
305 | my $watchdog_fh; |
306 | ||
714a4016 DM |
307 | sub watchdog_open { |
308 | my ($self) = @_; | |
309 | ||
76737af5 DM |
310 | die "watchdog already open\n" if defined($watchdog_fh); |
311 | ||
115805fd DM |
312 | $watchdog_fh = IO::Socket::UNIX->new( |
313 | Type => SOCK_STREAM(), | |
314 | Peer => "/run/watchdog-mux.sock") || | |
315 | die "unable to open watchdog socket - $!\n"; | |
63f6a08c | 316 | |
76737af5 | 317 | $self->log('info', "watchdog active"); |
714a4016 DM |
318 | } |
319 | ||
320 | sub watchdog_update { | |
321 | my ($self, $wfh) = @_; | |
322 | ||
76737af5 DM |
323 | my $res = $watchdog_fh->syswrite("\0", 1); |
324 | if (!defined($res)) { | |
325 | $self->log('err', "watchdog update failed - $!\n"); | |
326 | return 0; | |
327 | } | |
328 | if ($res != 1) { | |
329 | $self->log('err', "watchdog update failed - write $res bytes\n"); | |
330 | return 0; | |
331 | } | |
332 | ||
333 | return 1; | |
714a4016 DM |
334 | } |
335 | ||
336 | sub watchdog_close { | |
337 | my ($self, $wfh) = @_; | |
338 | ||
76737af5 DM |
339 | $watchdog_fh->syswrite("V", 1); # magic watchdog close |
340 | if (!$watchdog_fh->close()) { | |
341 | $self->log('err', "watchdog close failed - $!"); | |
342 | } else { | |
343 | $watchdog_fh = undef; | |
344 | $self->log('info', "watchdog closed (disabled)"); | |
345 | } | |
714a4016 DM |
346 | } |
347 | ||
022e4e79 DM |
348 | sub upid_wait { |
349 | my ($self, $upid) = @_; | |
350 | ||
351 | my $task = PVE::Tools::upid_decode($upid); | |
352 | ||
353 | CORE::sleep(1); | |
354 | while (PVE::ProcFSTools::check_process_running($task->{pid}, $task->{pstart})) { | |
355 | $self->log('debug', "Task still active, waiting"); | |
356 | CORE::sleep(1); | |
357 | } | |
358 | } | |
359 | ||
0d1d32fb DM |
360 | sub can_fork { |
361 | my ($self) = @_; | |
362 | ||
363 | return 1; | |
364 | } | |
365 | ||
c4a221bc | 366 | sub exec_resource_agent { |
6dbf93a0 | 367 | my ($self, $sid, $service_config, $cmd, @params) = @_; |
c4a221bc | 368 | |
022e4e79 | 369 | # setup execution environment |
63f6a08c | 370 | |
022e4e79 DM |
371 | $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin'; |
372 | ||
373 | PVE::INotify::inotify_close(); | |
63f6a08c | 374 | |
022e4e79 DM |
375 | PVE::INotify::inotify_init(); |
376 | ||
377 | PVE::Cluster::cfs_update(); | |
63f6a08c | 378 | |
022e4e79 DM |
379 | my $nodename = $self->{nodename}; |
380 | ||
6ca2edcd | 381 | my (undef, $service_type, $service_name) = PVE::HA::Tools::parse_sid($sid); |
b47a7a1b | 382 | |
303a08aa | 383 | my $plugin = PVE::HA::Resources->lookup($service_type); |
85316fd0 TL |
384 | if (!$plugin) { |
385 | $self->log('err', "service type '$service_type' not implemented"); | |
386 | return EUNKNOWN_SERVICE_TYPE; | |
387 | } | |
303a08aa | 388 | |
85316fd0 TL |
389 | if ($service_config->{node} ne $nodename) { |
390 | $self->log('err', "service '$sid' not on this node"); | |
391 | return EWRONG_NODE; | |
392 | } | |
022e4e79 | 393 | |
b47a7a1b | 394 | my $vmid = $service_name; |
022e4e79 | 395 | |
303a08aa | 396 | my $running = $plugin->check_running($vmid); |
022e4e79 | 397 | |
303a08aa | 398 | if ($cmd eq 'started') { |
022e4e79 | 399 | |
a89ff919 | 400 | return SUCCESS if $running; |
022e4e79 DM |
401 | |
402 | $self->log("info", "starting service $sid"); | |
403 | ||
303a08aa TL |
404 | my $params = { |
405 | node => $nodename, | |
406 | vmid => $vmid | |
407 | }; | |
408 | ||
409 | $plugin->start($self, $params); | |
022e4e79 | 410 | |
303a08aa | 411 | $running = $plugin->check_running($vmid); |
022e4e79 DM |
412 | |
413 | if ($running) { | |
414 | $self->log("info", "service status $sid started"); | |
a89ff919 | 415 | return SUCCESS; |
022e4e79 | 416 | } else { |
4877c031 | 417 | $self->log("warning", "unable to start service $sid"); |
a89ff919 | 418 | return ERROR; |
022e4e79 DM |
419 | } |
420 | ||
421 | } elsif ($cmd eq 'request_stop' || $cmd eq 'stopped') { | |
422 | ||
a89ff919 | 423 | return SUCCESS if !$running; |
022e4e79 DM |
424 | |
425 | $self->log("info", "stopping service $sid"); | |
426 | ||
427 | my $timeout = 60; # fixme: make this configurable | |
303a08aa TL |
428 | |
429 | my $params = { | |
430 | node => $nodename, | |
431 | vmid => $vmid, | |
022e4e79 DM |
432 | timeout => $timeout, |
433 | forceStop => 1, | |
434 | }; | |
435 | ||
303a08aa | 436 | $plugin->shutdown($self, $params); |
022e4e79 | 437 | |
303a08aa | 438 | $running = $plugin->check_running($vmid); |
022e4e79 DM |
439 | |
440 | if (!$running) { | |
441 | $self->log("info", "service status $sid stopped"); | |
a89ff919 | 442 | return SUCCESS; |
022e4e79 | 443 | } else { |
a89ff919 | 444 | return ERROR; |
022e4e79 DM |
445 | } |
446 | ||
447 | } elsif ($cmd eq 'migrate' || $cmd eq 'relocate') { | |
448 | ||
6da27e23 | 449 | my $target = $params[0]; |
85316fd0 TL |
450 | if (!defined($target)) { |
451 | die "$cmd '$sid' failed - missing target\n" if !defined($target); | |
452 | return EINVALID_PARAMETER; | |
453 | } | |
e319b50c | 454 | |
6da27e23 DM |
455 | if ($service_config->{node} eq $target) { |
456 | # already there | |
a89ff919 | 457 | return SUCCESS; |
303a08aa | 458 | } |
e319b50c | 459 | |
303a08aa TL |
460 | # we always do (live) migration |
461 | my $params = { | |
462 | node => $nodename, | |
463 | vmid => $vmid, | |
464 | target => $target, | |
465 | online => 1, | |
466 | }; | |
e319b50c | 467 | |
303a08aa | 468 | my $oldconfig = $plugin->config_file($vmid, $nodename); |
e319b50c | 469 | |
303a08aa | 470 | $plugin->migrate($self, $params); |
e319b50c | 471 | |
303a08aa TL |
472 | # something went wrong if old config file is still there |
473 | if (-f $oldconfig) { | |
474 | $self->log("err", "service $sid not moved (migration error)"); | |
a89ff919 | 475 | return ERROR; |
6da27e23 | 476 | } |
303a08aa | 477 | |
a89ff919 | 478 | return SUCCESS; |
303a08aa | 479 | |
a2881965 TL |
480 | } elsif ($cmd eq 'error') { |
481 | ||
63f6a08c | 482 | if ($running) { |
a2881965 TL |
483 | $self->log("err", "service $sid is in an error state while running"); |
484 | } else { | |
4877c031 | 485 | $self->log("warning", "service $sid is not running and in an error state"); |
a2881965 | 486 | } |
a89ff919 | 487 | return SUCCESS; # error always succeeds |
a2881965 | 488 | |
022e4e79 DM |
489 | } |
490 | ||
85316fd0 TL |
491 | $self->log("err", "implement me (cmd '$cmd')"); |
492 | return EUNKNOWN_COMMAND; | |
c4a221bc DM |
493 | } |
494 | ||
714a4016 | 495 | 1; |