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);
14 use Fcntl
qw(:DEFAULT :flock);
16 use File
::Path
qw(make_path remove_tree);
17 use PVE
::HA
::FenceConfig
;
20 my $watchdog_timeout = 60;
23 # Status directory layout
27 # $testdir/cmdlist Command list for simulation
28 # $testdir/hardware_status Hardware description (number of nodes, ...)
29 # $testdir/manager_status CRM status (start with {})
30 # $testdir/service_config Service configuration
31 # $testdir/groups HA groups configuration
32 # $testdir/service_status_<node> Service status
35 # runtime status for simulation system
37 # $testdir/status/cluster_locks Cluster locks
38 # $testdir/status/hardware_status Hardware status (power/network on/off)
39 # $testdir/status/watchdog_status Watchdog status
43 # $testdir/status/lrm_status_<node> LRM status
44 # $testdir/status/manager_status CRM status
45 # $testdir/status/crm_commands CRM command queue
46 # $testdir/status/service_config Service configuration
47 # $testdir/status/service_status_<node> Service status
48 # $testdir/status/groups HA groups configuration
51 my ($self, $node) = @_;
53 my $filename = "$self->{statusdir}/lrm_status_$node";
55 return PVE
::HA
::Tools
::read_json_from_file
($filename, {});
58 sub write_lrm_status
{
59 my ($self, $node, $status_obj) = @_;
61 my $filename = "$self->{statusdir}/lrm_status_$node";
63 PVE
::HA
::Tools
::write_json_to_file
($filename, $status_obj);
66 sub read_hardware_status_nolock
{
69 my $filename = "$self->{statusdir}/hardware_status";
71 my $raw = PVE
::Tools
::file_get_contents
($filename);
72 my $cstatus = decode_json
($raw);
77 sub write_hardware_status_nolock
{
78 my ($self, $cstatus) = @_;
80 my $filename = "$self->{statusdir}/hardware_status";
82 PVE
::Tools
::file_set_contents
($filename, encode_json
($cstatus));
85 sub read_service_config
{
88 my $filename = "$self->{statusdir}/service_config";
89 my $conf = PVE
::HA
::Tools
::read_json_from_file
($filename);
91 foreach my $sid (keys %$conf) {
92 my $d = $conf->{$sid};
94 die "service '$sid' without assigned node!" if !$d->{node
};
96 if ($sid =~ m/^(vm|ct|fa):(\d+)$/) {
102 $d->{state} = 'disabled' if !$d->{state};
103 $d->{state} = 'started' if $d->{state} eq 'enabled'; # backward compatibility
104 $d->{max_restart
} = 1 if !defined($d->{max_restart
});
105 $d->{max_relocate
} = 1 if !defined($d->{max_relocate
});
111 sub write_service_config
{
112 my ($self, $conf) = @_;
114 $self->{service_config
} = $conf;
116 my $filename = "$self->{statusdir}/service_config";
117 return PVE
::HA
::Tools
::write_json_to_file
($filename, $conf);
120 sub read_fence_config
{
125 my $filename = "$self->{statusdir}/fence.cfg";
127 $raw = PVE
::Tools
::file_get_contents
($filename);
130 return PVE
::HA
::FenceConfig
::parse_config
($filename, $raw);
133 sub exec_fence_agent
{
134 my ($self, $agent, $node, @param) = @_;
136 # let all agent succeed and behave the same for now
137 $self->sim_hardware_cmd("power $node off", $agent);
139 return 0; # EXIT_SUCCESS
142 sub set_service_state
{
143 my ($self, $sid, $state) = @_;
145 my $conf = $self->read_service_config();
146 die "no such service '$sid'" if !$conf->{$sid};
148 $conf->{$sid}->{state} = $state;
150 $self->write_service_config($conf);
156 my ($self, $sid, $opts) = @_;
158 my $conf = $self->read_service_config();
159 die "resource ID '$sid' already defined\n" if $conf->{$sid};
161 $conf->{$sid} = $opts;
163 $self->write_service_config($conf);
169 my ($self, $sid) = @_;
171 my $conf = $self->read_service_config();
173 die "no such service '$sid'" if !$conf->{$sid};
175 delete $conf->{$sid};
177 $self->write_service_config($conf);
182 sub change_service_location
{
183 my ($self, $sid, $current_node, $new_node) = @_;
185 my $conf = $self->read_service_config();
187 die "no such service '$sid'\n" if !$conf->{$sid};
189 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
190 if $current_node ne $conf->{$sid}->{node
};
192 $conf->{$sid}->{node
} = $new_node;
194 $self->write_service_config($conf);
197 sub service_has_lock
{
198 my ($self, $sid) = @_;
200 my $conf = $self->read_service_config();
202 die "no such service '$sid'\n" if !$conf->{$sid};
204 return $conf->{$sid}->{lock};
208 my ($self, $sid, $lock) = @_;
210 my $conf = $self->read_service_config();
212 die "no such service '$sid'\n" if !$conf->{$sid};
214 $conf->{$sid}->{lock} = $lock || 'backup';
216 $self->write_service_config($conf);
222 my ($self, $sid, $lock) = @_;
224 my $conf = $self->read_service_config();
226 die "no such service '$sid'\n" if !$conf->{$sid};
228 if (!defined($conf->{$sid}->{lock})) {
232 if (defined($lock) && $conf->{$sid}->{lock} ne $lock) {
233 warn "found lock '$conf->{$sid}->{lock}' trying to remove '$lock' lock\n";
237 my $removed_lock = delete $conf->{$sid}->{lock};
239 $self->write_service_config($conf);
241 return $removed_lock;
244 sub queue_crm_commands_nolock
{
245 my ($self, $cmd) = @_;
250 my $filename = "$self->{statusdir}/crm_commands";
252 $data = PVE
::Tools
::file_get_contents
($filename);
255 PVE
::Tools
::file_set_contents
($filename, $data);
260 sub queue_crm_commands
{
261 my ($self, $cmd) = @_;
263 my $code = sub { $self->queue_crm_commands_nolock($cmd); };
265 $self->global_lock($code);
270 sub read_crm_commands
{
276 my $filename = "$self->{statusdir}/crm_commands";
278 $data = PVE
::Tools
::file_get_contents
($filename);
280 PVE
::Tools
::file_set_contents
($filename, '');
285 return $self->global_lock($code);
288 sub read_group_config
{
291 my $filename = "$self->{statusdir}/groups";
293 $raw = PVE
::Tools
::file_get_contents
($filename) if -f
$filename;
295 return PVE
::HA
::Groups-
>parse_config($filename, $raw);
298 sub read_service_status
{
299 my ($self, $node) = @_;
301 my $filename = "$self->{statusdir}/service_status_$node";
302 return PVE
::HA
::Tools
::read_json_from_file
($filename);
305 sub write_service_status
{
306 my ($self, $node, $data) = @_;
308 my $filename = "$self->{statusdir}/service_status_$node";
309 my $res = PVE
::HA
::Tools
::write_json_to_file
($filename, $data);
311 # fixme: add test if a service runs on two nodes!!!
316 my $default_group_config = <<__EOD;
331 my ($this, $testdir) = @_;
333 die "missing testdir" if !$testdir;
335 die "testdir '$testdir' does not exist or is not a directory!\n"
338 my $class = ref($this) || $this;
340 my $self = bless {}, $class;
342 my $statusdir = $self->{statusdir
} = "$testdir/status";
344 remove_tree
($statusdir);
347 # copy initial configuartion
348 copy
("$testdir/manager_status", "$statusdir/manager_status"); # optional
350 if (-f
"$testdir/groups") {
351 copy
("$testdir/groups", "$statusdir/groups");
353 PVE
::Tools
::file_set_contents
("$statusdir/groups", $default_group_config);
356 if (-f
"$testdir/service_config") {
357 copy
("$testdir/service_config", "$statusdir/service_config");
360 'vm:101' => { node
=> 'node1', group
=> 'prefer_node1' },
361 'vm:102' => { node
=> 'node2', group
=> 'prefer_node2' },
362 'vm:103' => { node
=> 'node3', group
=> 'prefer_node3' },
363 'vm:104' => { node
=> 'node1', group
=> 'prefer_node1' },
364 'vm:105' => { node
=> 'node2', group
=> 'prefer_node2' },
365 'vm:106' => { node
=> 'node3', group
=> 'prefer_node3' },
367 $self->write_service_config($conf);
370 if (-f
"$testdir/hardware_status") {
371 copy
("$testdir/hardware_status", "$statusdir/hardware_status") ||
372 die "Copy failed: $!\n";
375 node1
=> { power
=> 'off', network
=> 'off' },
376 node2
=> { power
=> 'off', network
=> 'off' },
377 node3
=> { power
=> 'off', network
=> 'off' },
379 $self->write_hardware_status_nolock($cstatus);
382 if (-f
"$testdir/fence.cfg") {
383 copy
("$testdir/fence.cfg", "$statusdir/fence.cfg");
386 my $cstatus = $self->read_hardware_status_nolock();
388 foreach my $node (sort keys %$cstatus) {
389 $self->{nodes
}->{$node} = {};
391 if (-f
"$testdir/service_status_$node") {
392 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
394 $self->write_service_status($node, {});
398 $self->{service_config
} = $self->read_service_config();
406 die "implement in subclass";
410 my ($self, $level, $msg, $id) = @_;
414 my $time = $self->get_time();
416 $id = 'hardware' if !$id;
418 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
422 my ($self, $node) = @_;
424 return $self->{statusdir
};
428 my ($self, $code, @param) = @_;
430 my $lockfile = "$self->{statusdir}/hardware.lck";
431 my $fh = IO
::File-
>new(">>$lockfile") ||
432 die "unable to open '$lockfile'\n";
436 $success = flock($fh, LOCK_EX
);
437 if ($success || ($! != EINTR
)) {
442 die "can't acquire lock '$lockfile' - $!\n";
448 eval { $res = &$code($fh, @param) };
458 my $compute_node_info = sub {
459 my ($self, $cstatus) = @_;
464 my $online_count = 0;
466 foreach my $node (keys %$cstatus) {
467 my $d = $cstatus->{$node};
469 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
470 $node_info->{$node}->{online
} = $online;
473 $online_count++ if $online;
476 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
479 foreach my $node (keys %$cstatus) {
480 my $d = $cstatus->{$node};
481 $node_info->{$node}->{online
} = 0;
485 return ($node_info, $quorate);
491 my $cstatus = $self->read_hardware_status_nolock();
492 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
494 return ($node_info, $quorate);
497 # helper for Sim/ only
499 my ($self, $node, $state) = @_;
501 # TODO: ensure nolock is OK when adding this to RTSim
502 my $cstatus = $self->read_hardware_status_nolock();
503 my $res = $cstatus->{$node}->{cfs
}->{$state};
505 # we assume default true if not defined
506 return !defined($res) || $res;
509 # simulate hardware commands
510 # power <node> <on|off>
511 # network <node> <on|off>
512 # cfs <node> <rw|update> <work|fail>
516 # service <sid> <started|disabled|stopped|ignored>
517 # service <sid> <migrate|relocate> <target>
518 # service <sid> lock/unlock [lockname]
520 sub sim_hardware_cmd
{
521 my ($self, $cmdstr, $logid) = @_;
526 my $cstatus = $self->read_hardware_status_nolock();
528 my ($cmd, $objid, $action, $target) = split(/\s+/, $cmdstr);
530 die "sim_hardware_cmd: no node or service for command specified"
533 my ($node, $sid, $d);
535 if ($cmd eq 'service') {
536 $sid = PVE
::HA
::Tools
::pve_verify_ha_resource_id
($objid);
539 $d = $self->{nodes
}->{$node} ||
540 die "sim_hardware_cmd: no such node '$node'\n";
543 $self->log('info', "execute $cmdstr", $logid);
545 if ($cmd eq 'power') {
546 die "sim_hardware_cmd: unknown action '$action'\n"
547 if $action !~ m/^(on|off)$/;
549 if ($cstatus->{$node}->{power
} ne $action) {
550 if ($action eq 'on') {
552 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
553 $d->{lrm
} = $self->lrm_control('start', $d, $lock_fh) if !defined($d->{lrm
});
554 $d->{lrm_restart
} = undef;
555 $cstatus->{$node}->{cfs
} = {};
560 $d->{crm_env
}->log('info', "killed by poweroff");
561 $self->crm_control('stop', $d, $lock_fh);
565 $d->{lrm_env
}->log('info', "killed by poweroff");
566 $self->lrm_control('stop', $d, $lock_fh);
568 $d->{lrm_restart
} = undef;
571 $self->watchdog_reset_nolock($node);
572 $self->write_service_status($node, {});
576 $cstatus->{$node}->{power
} = $action;
577 $cstatus->{$node}->{network
} = $action;
578 $cstatus->{$node}->{shutdown} = undef;
580 $self->write_hardware_status_nolock($cstatus);
582 } elsif ($cmd eq 'network') {
583 die "sim_hardware_cmd: unknown network action '$action'"
584 if $action !~ m/^(on|off)$/;
585 $cstatus->{$node}->{network
} = $action;
587 $self->write_hardware_status_nolock($cstatus);
589 } elsif ($cmd eq 'cfs') {
590 die "sim_hardware_cmd: unknown cfs action '$action' for node '$node'"
591 if $action !~ m/^(rw|update)$/;
592 die "sim_hardware_cmd: unknown cfs command '$target' for '$action' on node '$node'"
593 if $target !~ m/^(work|fail)$/;
595 $cstatus->{$node}->{cfs
}->{$action} = $target eq 'work';
596 $self->write_hardware_status_nolock($cstatus);
598 } elsif ($cmd eq 'reboot' || $cmd eq 'shutdown') {
599 $cstatus->{$node}->{shutdown} = $cmd;
601 $self->write_hardware_status_nolock($cstatus);
603 $self->lrm_control('shutdown', $d, $lock_fh) if defined($d->{lrm
});
604 } elsif ($cmd eq 'restart-lrm') {
606 $d->{lrm_restart
} = 1;
607 $self->lrm_control('shutdown', $d, $lock_fh);
609 } elsif ($cmd eq 'crm') {
611 if ($action eq 'stop') {
614 $self->crm_control('shutdown', $d, $lock_fh);
616 } elsif ($action eq 'start') {
617 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
619 die "sim_hardware_cmd: unknown action '$action'";
622 } elsif ($cmd eq 'service') {
623 if ($action eq 'started' || $action eq 'disabled' ||
624 $action eq 'stopped' || $action eq 'ignored') {
626 $self->set_service_state($sid, $action);
628 } elsif ($action eq 'migrate' || $action eq 'relocate') {
630 die "sim_hardware_cmd: missing target node for '$action' command"
633 $self->queue_crm_commands_nolock("$action $sid $target");
635 } elsif ($action eq 'add') {
637 $self->add_service($sid, {state => 'started', node
=> $target});
639 } elsif ($action eq 'delete') {
641 $self->delete_service($sid);
643 } elsif ($action eq 'lock') {
645 $self->lock_service($sid, $target);
647 } elsif ($action eq 'unlock') {
649 $self->unlock_service($sid, $target);
652 die "sim_hardware_cmd: unknown service action '$action' " .
653 "- not implemented\n"
656 die "sim_hardware_cmd: unknown command '$cmdstr'\n";
662 return $self->global_lock($code);
665 # for controlling the resource manager services
667 my ($self, $action, $data, $lock_fh) = @_;
669 die "implement in subclass";
673 my ($self, $action, $data, $lock_fh) = @_;
675 die "implement in subclass";
681 die "implement in subclass";
684 my $modify_watchog = sub {
685 my ($self, $code) = @_;
687 my $update_cmd = sub {
689 my $filename = "$self->{statusdir}/watchdog_status";
691 my ($res, $wdstatus);
694 my $raw = PVE
::Tools
::file_get_contents
($filename);
695 $wdstatus = decode_json
($raw);
700 ($wdstatus, $res) = &$code($wdstatus);
702 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
707 return $self->global_lock($update_cmd);
710 sub watchdog_reset_nolock
{
711 my ($self, $node) = @_;
713 my $filename = "$self->{statusdir}/watchdog_status";
716 my $raw = PVE
::Tools
::file_get_contents
($filename);
717 my $wdstatus = decode_json
($raw);
719 foreach my $id (keys %$wdstatus) {
720 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
723 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
728 my ($self, $node) = @_;
735 foreach my $wfh (keys %$wdstatus) {
736 my $wd = $wdstatus->{$wfh};
737 next if $wd->{node
} ne $node;
739 my $ctime = $self->get_time();
740 my $tdiff = $ctime - $wd->{update_time
};
742 if ($tdiff > $watchdog_timeout) { # expired
744 delete $wdstatus->{$wfh};
748 return ($wdstatus, $res);
751 return &$modify_watchog($self, $code);
757 my ($self, $node) = @_;
764 my $id = "WD:$node:$$:$wdcounter";
766 die "internal error" if defined($wdstatus->{$id});
770 update_time
=> $self->get_time(),
773 return ($wdstatus, $id);
776 return &$modify_watchog($self, $code);
780 my ($self, $wfh) = @_;
785 my $wd = $wdstatus->{$wfh};
786 die "no such watchdog handle '$wfh'\n" if !defined($wd);
788 my $tdiff = $self->get_time() - $wd->{update_time
};
789 die "watchdog expired" if $tdiff > $watchdog_timeout;
791 delete $wdstatus->{$wfh};
796 return &$modify_watchog($self, $code);
799 sub watchdog_update
{
800 my ($self, $wfh) = @_;
805 my $wd = $wdstatus->{$wfh};
807 die "no such watchdog handle '$wfh'\n" if !defined($wd);
809 my $ctime = $self->get_time();
810 my $tdiff = $ctime - $wd->{update_time
};
812 die "watchdog expired" if $tdiff > $watchdog_timeout;
814 $wd->{update_time
} = $ctime;
819 return &$modify_watchog($self, $code);