1 package PVE
::HA
::Sim
::Hardware
;
3 # Simulate Hardware resources
5 # power supply for nodes: on/off
6 # network connection to nodes: on/off
7 # watchdog devices for nodes
11 use POSIX
qw(strftime EINTR);
15 use Fcntl
qw(:DEFAULT :flock);
17 use File
::Path
qw(make_path remove_tree);
19 use PVE
::HA
::FenceConfig
;
21 my $watchdog_timeout = 60;
24 # Status directory layout
28 # $testdir/cmdlist Command list for simulation
29 # $testdir/hardware_status Hardware description (number of nodes, ...)
30 # $testdir/manager_status CRM status (start with {})
31 # $testdir/service_config Service configuration
32 # $testdir/groups HA groups configuration
33 # $testdir/service_status_<node> Service status
36 # runtime status for simulation system
38 # $testdir/status/cluster_locks Cluster locks
39 # $testdir/status/hardware_status Hardware status (power/network on/off)
40 # $testdir/status/watchdog_status Watchdog status
44 # $testdir/status/lrm_status_<node> LRM status
45 # $testdir/status/manager_status CRM status
46 # $testdir/status/crm_commands CRM command queue
47 # $testdir/status/service_config Service configuration
48 # $testdir/status/service_status_<node> Service status
49 # $testdir/status/groups HA groups configuration
52 my ($self, $node) = @_;
54 my $filename = "$self->{statusdir}/lrm_status_$node";
56 return PVE
::HA
::Tools
::read_json_from_file
($filename, {});
59 sub write_lrm_status
{
60 my ($self, $node, $status_obj) = @_;
62 my $filename = "$self->{statusdir}/lrm_status_$node";
64 PVE
::HA
::Tools
::write_json_to_file
($filename, $status_obj);
67 sub read_hardware_status_nolock
{
70 my $filename = "$self->{statusdir}/hardware_status";
72 my $raw = PVE
::Tools
::file_get_contents
($filename);
73 my $cstatus = decode_json
($raw);
78 sub write_hardware_status_nolock
{
79 my ($self, $cstatus) = @_;
81 my $filename = "$self->{statusdir}/hardware_status";
83 PVE
::Tools
::file_set_contents
($filename, encode_json
($cstatus));
86 sub read_service_config
{
89 my $filename = "$self->{statusdir}/service_config";
90 my $conf = PVE
::HA
::Tools
::read_json_from_file
($filename);
92 foreach my $sid (keys %$conf) {
93 my $d = $conf->{$sid};
95 die "service '$sid' without assigned node!" if !$d->{node
};
97 if ($sid =~ m/^(vm|ct|fa):(\d+)$/) {
103 $d->{state} = 'disabled' if !$d->{state};
104 $d->{state} = 'started' if $d->{state} eq 'enabled'; # backward compatibility
105 $d->{max_restart
} = 1 if !defined($d->{max_restart
});
106 $d->{max_relocate
} = 1 if !defined($d->{max_relocate
});
112 sub write_service_config
{
113 my ($self, $conf) = @_;
115 $self->{service_config
} = $conf;
117 my $filename = "$self->{statusdir}/service_config";
118 return PVE
::HA
::Tools
::write_json_to_file
($filename, $conf);
121 sub read_fence_config
{
126 my $filename = "$self->{statusdir}/fence.cfg";
128 $raw = PVE
::Tools
::file_get_contents
($filename);
131 return PVE
::HA
::FenceConfig
::parse_config
($filename, $raw);
134 sub exec_fence_agent
{
135 my ($self, $agent, $node, @param) = @_;
137 # let all agent succeed and behave the same for now
138 $self->sim_hardware_cmd("power $node off", $agent);
140 return 0; # EXIT_SUCCESS
143 sub set_service_state
{
144 my ($self, $sid, $state) = @_;
146 my $conf = $self->read_service_config();
147 die "no such service '$sid'" if !$conf->{$sid};
149 $conf->{$sid}->{state} = $state;
151 $self->write_service_config($conf);
157 my ($self, $sid, $opts) = @_;
159 my $conf = $self->read_service_config();
160 die "resource ID '$sid' already defined\n" if $conf->{$sid};
162 $conf->{$sid} = $opts;
164 $self->write_service_config($conf);
170 my ($self, $sid) = @_;
172 my $conf = $self->read_service_config();
174 die "no such service '$sid'" if !$conf->{$sid};
176 delete $conf->{$sid};
178 $self->write_service_config($conf);
183 sub change_service_location
{
184 my ($self, $sid, $current_node, $new_node) = @_;
186 my $conf = $self->read_service_config();
188 die "no such service '$sid'\n" if !$conf->{$sid};
190 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
191 if $current_node ne $conf->{$sid}->{node
};
193 $conf->{$sid}->{node
} = $new_node;
195 $self->write_service_config($conf);
198 sub service_has_lock
{
199 my ($self, $sid) = @_;
201 my $conf = $self->read_service_config();
203 die "no such service '$sid'\n" if !$conf->{$sid};
205 return $conf->{$sid}->{lock};
209 my ($self, $sid, $lock) = @_;
211 my $conf = $self->read_service_config();
213 die "no such service '$sid'\n" if !$conf->{$sid};
215 $conf->{$sid}->{lock} = $lock || 'backup';
217 $self->write_service_config($conf);
223 my ($self, $sid, $lock) = @_;
225 my $conf = $self->read_service_config();
227 die "no such service '$sid'\n" if !$conf->{$sid};
229 if (!defined($conf->{$sid}->{lock})) {
233 if (defined($lock) && $conf->{$sid}->{lock} ne $lock) {
234 warn "found lock '$conf->{$sid}->{lock}' trying to remove '$lock' lock\n";
238 my $removed_lock = delete $conf->{$sid}->{lock};
240 $self->write_service_config($conf);
242 return $removed_lock;
245 sub queue_crm_commands_nolock
{
246 my ($self, $cmd) = @_;
251 my $filename = "$self->{statusdir}/crm_commands";
253 $data = PVE
::Tools
::file_get_contents
($filename);
256 PVE
::Tools
::file_set_contents
($filename, $data);
261 sub queue_crm_commands
{
262 my ($self, $cmd) = @_;
264 my $code = sub { $self->queue_crm_commands_nolock($cmd); };
266 $self->global_lock($code);
271 sub read_crm_commands
{
277 my $filename = "$self->{statusdir}/crm_commands";
279 $data = PVE
::Tools
::file_get_contents
($filename);
281 PVE
::Tools
::file_set_contents
($filename, '');
286 return $self->global_lock($code);
289 sub read_group_config
{
292 my $filename = "$self->{statusdir}/groups";
294 $raw = PVE
::Tools
::file_get_contents
($filename) if -f
$filename;
296 return PVE
::HA
::Config
::parse_groups_config
($filename, $raw);
299 sub read_service_status
{
300 my ($self, $node) = @_;
302 my $filename = "$self->{statusdir}/service_status_$node";
303 return PVE
::HA
::Tools
::read_json_from_file
($filename);
306 sub write_service_status
{
307 my ($self, $node, $data) = @_;
309 my $filename = "$self->{statusdir}/service_status_$node";
310 my $res = PVE
::HA
::Tools
::write_json_to_file
($filename, $data);
312 # fixme: add test if a service runs on two nodes!!!
317 my $default_group_config = <<__EOD;
332 my ($this, $testdir) = @_;
334 die "missing testdir" if !$testdir;
336 die "testdir '$testdir' does not exist or is not a directory!\n"
339 my $class = ref($this) || $this;
341 my $self = bless {}, $class;
343 my $statusdir = $self->{statusdir
} = "$testdir/status";
345 remove_tree
($statusdir);
348 # copy initial configuartion
349 copy
("$testdir/manager_status", "$statusdir/manager_status"); # optional
351 if (-f
"$testdir/groups") {
352 copy
("$testdir/groups", "$statusdir/groups");
354 PVE
::Tools
::file_set_contents
("$statusdir/groups", $default_group_config);
357 if (-f
"$testdir/service_config") {
358 copy
("$testdir/service_config", "$statusdir/service_config");
361 'vm:101' => { node
=> 'node1', group
=> 'prefer_node1' },
362 'vm:102' => { node
=> 'node2', group
=> 'prefer_node2' },
363 'vm:103' => { node
=> 'node3', group
=> 'prefer_node3' },
364 'vm:104' => { node
=> 'node1', group
=> 'prefer_node1' },
365 'vm:105' => { node
=> 'node2', group
=> 'prefer_node2' },
366 'vm:106' => { node
=> 'node3', group
=> 'prefer_node3' },
368 $self->write_service_config($conf);
371 if (-f
"$testdir/hardware_status") {
372 copy
("$testdir/hardware_status", "$statusdir/hardware_status") ||
373 die "Copy failed: $!\n";
376 node1
=> { power
=> 'off', network
=> 'off' },
377 node2
=> { power
=> 'off', network
=> 'off' },
378 node3
=> { power
=> 'off', network
=> 'off' },
380 $self->write_hardware_status_nolock($cstatus);
383 if (-f
"$testdir/fence.cfg") {
384 copy
("$testdir/fence.cfg", "$statusdir/fence.cfg");
387 my $cstatus = $self->read_hardware_status_nolock();
389 foreach my $node (sort keys %$cstatus) {
390 $self->{nodes
}->{$node} = {};
392 if (-f
"$testdir/service_status_$node") {
393 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
395 $self->write_service_status($node, {});
399 $self->{service_config
} = $self->read_service_config();
407 die "implement in subclass";
411 my ($self, $level, $msg, $id) = @_;
415 my $time = $self->get_time();
417 $id = 'hardware' if !$id;
419 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
423 my ($self, $node) = @_;
425 return $self->{statusdir
};
429 my ($self, $code, @param) = @_;
431 my $lockfile = "$self->{statusdir}/hardware.lck";
432 my $fh = IO
::File-
>new(">>$lockfile") ||
433 die "unable to open '$lockfile'\n";
437 $success = flock($fh, LOCK_EX
);
438 if ($success || ($! != EINTR
)) {
443 die "can't acquire lock '$lockfile' - $!\n";
449 eval { $res = &$code($fh, @param) };
459 my $compute_node_info = sub {
460 my ($self, $cstatus) = @_;
465 my $online_count = 0;
467 foreach my $node (keys %$cstatus) {
468 my $d = $cstatus->{$node};
470 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
471 $node_info->{$node}->{online
} = $online;
474 $online_count++ if $online;
477 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
480 foreach my $node (keys %$cstatus) {
481 my $d = $cstatus->{$node};
482 $node_info->{$node}->{online
} = 0;
486 return ($node_info, $quorate);
492 my $cstatus = $self->read_hardware_status_nolock();
493 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
495 return ($node_info, $quorate);
498 # simulate hardware commands
499 # power <node> <on|off>
500 # network <node> <on|off>
502 sub sim_hardware_cmd
{
503 my ($self, $cmdstr, $logid) = @_;
505 die "implement in subclass";
511 die "implement in subclass";
514 my $modify_watchog = sub {
515 my ($self, $code) = @_;
517 my $update_cmd = sub {
519 my $filename = "$self->{statusdir}/watchdog_status";
521 my ($res, $wdstatus);
524 my $raw = PVE
::Tools
::file_get_contents
($filename);
525 $wdstatus = decode_json
($raw);
530 ($wdstatus, $res) = &$code($wdstatus);
532 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
537 return $self->global_lock($update_cmd);
540 sub watchdog_reset_nolock
{
541 my ($self, $node) = @_;
543 my $filename = "$self->{statusdir}/watchdog_status";
546 my $raw = PVE
::Tools
::file_get_contents
($filename);
547 my $wdstatus = decode_json
($raw);
549 foreach my $id (keys %$wdstatus) {
550 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
553 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
558 my ($self, $node) = @_;
565 foreach my $wfh (keys %$wdstatus) {
566 my $wd = $wdstatus->{$wfh};
567 next if $wd->{node
} ne $node;
569 my $ctime = $self->get_time();
570 my $tdiff = $ctime - $wd->{update_time
};
572 if ($tdiff > $watchdog_timeout) { # expired
574 delete $wdstatus->{$wfh};
578 return ($wdstatus, $res);
581 return &$modify_watchog($self, $code);
587 my ($self, $node) = @_;
594 my $id = "WD:$node:$$:$wdcounter";
596 die "internal error" if defined($wdstatus->{$id});
600 update_time
=> $self->get_time(),
603 return ($wdstatus, $id);
606 return &$modify_watchog($self, $code);
610 my ($self, $wfh) = @_;
615 my $wd = $wdstatus->{$wfh};
616 die "no such watchdog handle '$wfh'\n" if !defined($wd);
618 my $tdiff = $self->get_time() - $wd->{update_time
};
619 die "watchdog expired" if $tdiff > $watchdog_timeout;
621 delete $wdstatus->{$wfh};
626 return &$modify_watchog($self, $code);
629 sub watchdog_update
{
630 my ($self, $wfh) = @_;
635 my $wd = $wdstatus->{$wfh};
637 die "no such watchdog handle '$wfh'\n" if !defined($wd);
639 my $ctime = $self->get_time();
640 my $tdiff = $ctime - $wd->{update_time
};
642 die "watchdog expired" if $tdiff > $watchdog_timeout;
644 $wd->{update_time
} = $ctime;
649 return &$modify_watchog($self, $code);