]>
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; |
9e5ea8f7 DM |
18 | use PVE::HA::Resources; |
19 | use PVE::HA::Resources::PVEVM; | |
20 | use PVE::HA::Resources::PVECT; | |
714a4016 | 21 | |
9e5ea8f7 DM |
22 | PVE::HA::Resources::PVEVM->register(); |
23 | PVE::HA::Resources::PVECT->register(); | |
24 | ||
25 | PVE::HA::Resources->init(); | |
022e4e79 | 26 | |
007fcc8b DM |
27 | my $lockdir = "/etc/pve/priv/lock"; |
28 | ||
714a4016 DM |
29 | sub new { |
30 | my ($this, $nodename) = @_; | |
31 | ||
32 | die "missing nodename" if !$nodename; | |
33 | ||
34 | my $class = ref($this) || $this; | |
35 | ||
36 | my $self = bless {}, $class; | |
37 | ||
38 | $self->{nodename} = $nodename; | |
39 | ||
40 | return $self; | |
41 | } | |
42 | ||
43 | sub nodename { | |
44 | my ($self) = @_; | |
45 | ||
46 | return $self->{nodename}; | |
47 | } | |
48 | ||
dd9c0c9d TL |
49 | sub hardware { |
50 | my ($self) = @_; | |
51 | ||
52 | die "hardware is for testing and simulation only"; | |
53 | } | |
54 | ||
714a4016 DM |
55 | sub read_manager_status { |
56 | my ($self) = @_; | |
714a4016 | 57 | |
139a9b90 | 58 | return PVE::HA::Config::read_manager_status(); |
714a4016 DM |
59 | } |
60 | ||
61 | sub write_manager_status { | |
62 | my ($self, $status_obj) = @_; | |
63f6a08c | 63 | |
139a9b90 | 64 | PVE::HA::Config::write_manager_status($status_obj); |
714a4016 DM |
65 | } |
66 | ||
c4a221bc DM |
67 | sub read_lrm_status { |
68 | my ($self, $node) = @_; | |
69 | ||
70 | $node = $self->{nodename} if !defined($node); | |
71 | ||
139a9b90 | 72 | return PVE::HA::Config::read_lrm_status($node); |
c4a221bc DM |
73 | } |
74 | ||
75 | sub write_lrm_status { | |
76 | my ($self, $status_obj) = @_; | |
77 | ||
6cbcb5f7 | 78 | my $node = $self->{nodename}; |
63f6a08c | 79 | |
139a9b90 DM |
80 | PVE::HA::Config::write_lrm_status($node, $status_obj); |
81 | } | |
c4a221bc | 82 | |
cde77779 | 83 | sub is_node_shutdown { |
d42219a3 TL |
84 | my ($self) = @_; |
85 | ||
cde77779 | 86 | my $shutdown = 0; |
d42219a3 TL |
87 | |
88 | my $code = sub { | |
89 | my $line = shift; | |
90 | ||
cde77779 | 91 | $shutdown = 1 if ($line =~ m/shutdown\.target/); |
d42219a3 TL |
92 | }; |
93 | ||
94 | my $cmd = ['/bin/systemctl', 'list-jobs']; | |
95 | eval { PVE::Tools::run_command($cmd, outfunc => $code, noerr => 1); }; | |
96 | ||
cde77779 | 97 | return $shutdown; |
d42219a3 TL |
98 | } |
99 | ||
139a9b90 DM |
100 | sub queue_crm_commands { |
101 | my ($self, $cmd) = @_; | |
c4a221bc | 102 | |
139a9b90 DM |
103 | return PVE::HA::Config::queue_crm_commands($cmd); |
104 | } | |
105 | ||
106 | sub read_crm_commands { | |
107 | my ($self) = @_; | |
108 | ||
109 | return PVE::HA::Config::read_crm_commands(); | |
c4a221bc DM |
110 | } |
111 | ||
b83b4ae8 DM |
112 | sub read_service_config { |
113 | my ($self) = @_; | |
ce216792 | 114 | |
b83b4ae8 | 115 | my $res = PVE::HA::Config::read_resources_config(); |
63f6a08c | 116 | |
ce216792 DM |
117 | my $vmlist = PVE::Cluster::get_vmlist(); |
118 | my $conf = {}; | |
119 | ||
120 | foreach my $sid (keys %{$res->{ids}}) { | |
121 | my $d = $res->{ids}->{$sid}; | |
6ca2edcd | 122 | my (undef, undef, $name) = PVE::HA::Tools::parse_sid($sid); |
7a19642e | 123 | $d->{state} = 'enabled' if !defined($d->{state}); |
ea4443cc TL |
124 | $d->{max_restart} = 1 if !defined($d->{max_restart}); |
125 | $d->{max_relocate} = 1 if !defined($d->{max_relocate}); | |
303a08aa | 126 | if (PVE::HA::Resources->lookup($d->{type})) { |
b47a7a1b | 127 | if (my $vmd = $vmlist->{ids}->{$name}) { |
ce216792 | 128 | if (!$vmd) { |
b47a7a1b | 129 | warn "no such VM '$name'\n"; |
ce216792 DM |
130 | } else { |
131 | $d->{node} = $vmd->{node}; | |
132 | $conf->{$sid} = $d; | |
133 | } | |
134 | } else { | |
135 | if (defined($d->{node})) { | |
136 | $conf->{$sid} = $d; | |
137 | } else { | |
138 | warn "service '$sid' without node\n"; | |
139 | } | |
140 | } | |
141 | } | |
142 | } | |
63f6a08c | 143 | |
ce216792 | 144 | return $conf; |
714a4016 DM |
145 | } |
146 | ||
8456bde2 | 147 | sub change_service_location { |
6da27e23 | 148 | my ($self, $sid, $current_node, $new_node) = @_; |
8456bde2 | 149 | |
6ca2edcd | 150 | my (undef, $type, $name) = PVE::HA::Tools::parse_sid($sid); |
6da27e23 | 151 | |
303a08aa TL |
152 | if(my $plugin = PVE::HA::Resources->lookup($type)) { |
153 | my $old = $plugin->config_file($name, $current_node); | |
154 | my $new = $plugin->config_file($name, $new_node); | |
6da27e23 DM |
155 | rename($old, $new) || |
156 | die "rename '$old' to '$new' failed - $!\n"; | |
157 | } else { | |
158 | die "implement me"; | |
159 | } | |
8456bde2 DM |
160 | } |
161 | ||
abc920b4 DM |
162 | sub read_group_config { |
163 | my ($self) = @_; | |
164 | ||
139a9b90 | 165 | return PVE::HA::Config::read_group_config(); |
3b996922 DM |
166 | } |
167 | ||
714a4016 DM |
168 | # this should return a hash containing info |
169 | # what nodes are members and online. | |
170 | sub get_node_info { | |
171 | my ($self) = @_; | |
172 | ||
d706ef8b | 173 | my ($node_info, $quorate) = ({}, 0); |
63f6a08c | 174 | |
d706ef8b DM |
175 | my $nodename = $self->{nodename}; |
176 | ||
177 | $quorate = PVE::Cluster::check_cfs_quorum(1) || 0; | |
178 | ||
179 | my $members = PVE::Cluster::get_members(); | |
180 | ||
181 | foreach my $node (keys %$members) { | |
182 | my $d = $members->{$node}; | |
63f6a08c | 183 | $node_info->{$node}->{online} = $d->{online}; |
d706ef8b | 184 | } |
63f6a08c | 185 | |
d706ef8b | 186 | $node_info->{$nodename}->{online} = 1; # local node is always up |
63f6a08c | 187 | |
d706ef8b | 188 | return ($node_info, $quorate); |
714a4016 DM |
189 | } |
190 | ||
191 | sub log { | |
192 | my ($self, $level, $msg) = @_; | |
193 | ||
194 | chomp $msg; | |
195 | ||
196 | syslog($level, $msg); | |
197 | } | |
198 | ||
007fcc8b DM |
199 | my $last_lock_status = {}; |
200 | ||
201 | sub get_pve_lock { | |
202 | my ($self, $lockid) = @_; | |
714a4016 | 203 | |
007fcc8b | 204 | my $got_lock = 0; |
4d24e7db | 205 | |
4d24e7db DM |
206 | my $filename = "$lockdir/$lockid"; |
207 | ||
007fcc8b DM |
208 | my $last = $last_lock_status->{$lockid} || 0; |
209 | ||
210 | my $ctime = time(); | |
4d24e7db | 211 | |
75aca181 DM |
212 | my $retry = 0; |
213 | my $retry_timeout = 100; # fixme: what timeout | |
63f6a08c | 214 | |
4d24e7db DM |
215 | eval { |
216 | ||
217 | mkdir $lockdir; | |
218 | ||
007fcc8b DM |
219 | # pve cluster filesystem not online |
220 | die "can't create '$lockdir' (pmxcfs not mounted?)\n" if ! -d $lockdir; | |
221 | ||
75aca181 DM |
222 | if ($last && (($ctime - $last) < $retry_timeout)) { |
223 | # send cfs lock update request (utime) | |
224 | if (!utime(0, $ctime, $filename)) { | |
225 | $retry = 1; | |
007fcc8b | 226 | die "cfs lock update failed - $!\n"; |
75aca181 | 227 | } |
007fcc8b DM |
228 | } else { |
229 | ||
230 | # fixme: wait some time? | |
231 | if (!(mkdir $filename)) { | |
232 | utime 0, 0, $filename; # cfs unlock request | |
233 | die "can't get cfs lock\n"; | |
234 | } | |
235 | } | |
4d24e7db | 236 | |
007fcc8b | 237 | $got_lock = 1; |
4d24e7db DM |
238 | }; |
239 | ||
007fcc8b DM |
240 | my $err = $@; |
241 | ||
75aca181 DM |
242 | if ($retry) { |
243 | # $self->log('err', $err) if $err; # for debugging | |
244 | return 0; | |
245 | } | |
63f6a08c | 246 | |
007fcc8b DM |
247 | $last_lock_status->{$lockid} = $got_lock ? $ctime : 0; |
248 | ||
17e90af6 | 249 | if (!!$got_lock != !!$last) { |
007fcc8b | 250 | if ($got_lock) { |
63f6a08c | 251 | $self->log('info', "successfully acquired lock '$lockid'"); |
007fcc8b DM |
252 | } else { |
253 | my $msg = "lost lock '$lockid"; | |
63f6a08c | 254 | $msg .= " - $err" if $err; |
007fcc8b DM |
255 | $self->log('err', $msg); |
256 | } | |
75aca181 DM |
257 | } else { |
258 | # $self->log('err', $err) if $err; # for debugging | |
007fcc8b DM |
259 | } |
260 | ||
261 | return $got_lock; | |
262 | } | |
263 | ||
264 | sub get_ha_manager_lock { | |
265 | my ($self) = @_; | |
266 | ||
007fcc8b | 267 | return $self->get_pve_lock("ha_manager_lock"); |
714a4016 DM |
268 | } |
269 | ||
de002253 TL |
270 | # release the cluster wide manager lock. |
271 | # when released another CRM may step up and get the lock, thus this should only | |
272 | # get called when shutting down/deactivating the current master | |
273 | sub release_ha_manager_lock { | |
274 | my ($self) = @_; | |
275 | ||
276 | return rmdir("$lockdir/ha_manager_lock"); | |
277 | } | |
278 | ||
714a4016 | 279 | sub get_ha_agent_lock { |
714a4016 | 280 | my ($self, $node) = @_; |
63f6a08c | 281 | |
f5c29173 | 282 | $node = $self->nodename() if !defined($node); |
714a4016 | 283 | |
f5c29173 | 284 | return $self->get_pve_lock("ha_agent_${node}_lock"); |
714a4016 DM |
285 | } |
286 | ||
ff165cd8 TL |
287 | # release the respective node agent lock. |
288 | # this should only get called if the nodes LRM gracefully shuts down with | |
289 | # all services already cleanly stopped! | |
290 | sub release_ha_agent_lock { | |
291 | my ($self) = @_; | |
292 | ||
293 | my $node = $self->nodename(); | |
294 | ||
295 | return rmdir("$lockdir/ha_agent_${node}_lock"); | |
296 | } | |
297 | ||
714a4016 DM |
298 | sub quorate { |
299 | my ($self) = @_; | |
300 | ||
4d24e7db | 301 | my $quorate = 0; |
63f6a08c TL |
302 | eval { |
303 | $quorate = PVE::Cluster::check_cfs_quorum(); | |
4d24e7db | 304 | }; |
63f6a08c | 305 | |
4d24e7db | 306 | return $quorate; |
714a4016 DM |
307 | } |
308 | ||
309 | sub get_time { | |
310 | my ($self) = @_; | |
311 | ||
312 | return time(); | |
313 | } | |
314 | ||
315 | sub sleep { | |
316 | my ($self, $delay) = @_; | |
317 | ||
318 | CORE::sleep($delay); | |
319 | } | |
320 | ||
321 | sub sleep_until { | |
322 | my ($self, $end_time) = @_; | |
323 | ||
324 | for (;;) { | |
325 | my $cur_time = time(); | |
326 | ||
327 | last if $cur_time >= $end_time; | |
328 | ||
329 | $self->sleep(1); | |
330 | } | |
331 | } | |
332 | ||
333 | sub loop_start_hook { | |
334 | my ($self) = @_; | |
335 | ||
4d24e7db | 336 | PVE::Cluster::cfs_update(); |
63f6a08c | 337 | |
714a4016 DM |
338 | $self->{loop_start} = $self->get_time(); |
339 | } | |
340 | ||
341 | sub loop_end_hook { | |
342 | my ($self) = @_; | |
343 | ||
344 | my $delay = $self->get_time() - $self->{loop_start}; | |
63f6a08c | 345 | |
714a4016 DM |
346 | warn "loop take too long ($delay seconds)\n" if $delay > 30; |
347 | } | |
348 | ||
76737af5 DM |
349 | my $watchdog_fh; |
350 | ||
714a4016 DM |
351 | sub watchdog_open { |
352 | my ($self) = @_; | |
353 | ||
76737af5 DM |
354 | die "watchdog already open\n" if defined($watchdog_fh); |
355 | ||
115805fd DM |
356 | $watchdog_fh = IO::Socket::UNIX->new( |
357 | Type => SOCK_STREAM(), | |
358 | Peer => "/run/watchdog-mux.sock") || | |
359 | die "unable to open watchdog socket - $!\n"; | |
63f6a08c | 360 | |
76737af5 | 361 | $self->log('info', "watchdog active"); |
714a4016 DM |
362 | } |
363 | ||
364 | sub watchdog_update { | |
365 | my ($self, $wfh) = @_; | |
366 | ||
76737af5 DM |
367 | my $res = $watchdog_fh->syswrite("\0", 1); |
368 | if (!defined($res)) { | |
369 | $self->log('err', "watchdog update failed - $!\n"); | |
370 | return 0; | |
371 | } | |
372 | if ($res != 1) { | |
373 | $self->log('err', "watchdog update failed - write $res bytes\n"); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | return 1; | |
714a4016 DM |
378 | } |
379 | ||
380 | sub watchdog_close { | |
381 | my ($self, $wfh) = @_; | |
382 | ||
76737af5 DM |
383 | $watchdog_fh->syswrite("V", 1); # magic watchdog close |
384 | if (!$watchdog_fh->close()) { | |
385 | $self->log('err', "watchdog close failed - $!"); | |
386 | } else { | |
387 | $watchdog_fh = undef; | |
388 | $self->log('info', "watchdog closed (disabled)"); | |
389 | } | |
714a4016 DM |
390 | } |
391 | ||
a2aae08a TL |
392 | sub after_fork { |
393 | my ($self) = @_; | |
394 | ||
395 | # close inherited inotify FD from parent and reopen our own | |
396 | PVE::INotify::inotify_close(); | |
397 | PVE::INotify::inotify_init(); | |
398 | ||
399 | PVE::Cluster::cfs_update(); | |
400 | } | |
401 | ||
a28fa330 TL |
402 | sub get_max_workers { |
403 | my ($self) = @_; | |
404 | ||
405 | my $datacenterconfig = cfs_read_file('datacenter.cfg'); | |
406 | ||
407 | return $datacenterconfig->{max_workers} || 4; | |
408 | } | |
409 | ||
714a4016 | 410 | 1; |