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