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
12 use Fcntl
qw(:DEFAULT :flock);
14 use File
::Path
qw(make_path remove_tree);
17 use POSIX
qw(strftime EINTR);
19 use PVE
::HA
::FenceConfig
;
22 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
34 # $testdir/datacenter.cfg Datacenter wide HA configuration
37 # runtime status for simulation system
39 # $testdir/status/cluster_locks Cluster locks
40 # $testdir/status/hardware_status Hardware status (power/network on/off)
41 # $testdir/status/watchdog_status Watchdog status
45 # $testdir/status/lrm_status_<node> LRM status
46 # $testdir/status/manager_status CRM status
47 # $testdir/status/crm_commands CRM command queue
48 # $testdir/status/service_config Service configuration
49 # $testdir/status/service_status_<node> Service status
50 # $testdir/status/groups HA groups configuration
53 my ($self, $node) = @_;
55 my $filename = "$self->{statusdir}/lrm_status_$node";
57 return PVE
::HA
::Tools
::read_json_from_file
($filename, {});
60 sub write_lrm_status
{
61 my ($self, $node, $status_obj) = @_;
63 my $filename = "$self->{statusdir}/lrm_status_$node";
65 PVE
::HA
::Tools
::write_json_to_file
($filename, $status_obj);
68 sub read_hardware_status_nolock
{
71 my $filename = "$self->{statusdir}/hardware_status";
73 my $raw = PVE
::Tools
::file_get_contents
($filename);
74 my $cstatus = decode_json
($raw);
79 sub write_hardware_status_nolock
{
80 my ($self, $cstatus) = @_;
82 my $filename = "$self->{statusdir}/hardware_status";
84 PVE
::Tools
::file_set_contents
($filename, encode_json
($cstatus));
87 sub read_service_config
{
90 my $filename = "$self->{statusdir}/service_config";
91 my $conf = PVE
::HA
::Tools
::read_json_from_file
($filename);
93 foreach my $sid (keys %$conf) {
94 my $d = $conf->{$sid};
96 die "service '$sid' without assigned node!" if !$d->{node
};
98 if ($sid =~ m/^(vm|ct|fa):(\d+)$/) {
104 $d->{state} = 'disabled' if !$d->{state};
105 $d->{state} = 'started' if $d->{state} eq 'enabled'; # backward compatibility
106 $d->{max_restart
} = 1 if !defined($d->{max_restart
});
107 $d->{max_relocate
} = 1 if !defined($d->{max_relocate
});
113 sub update_service_config
{
114 my ($self, $sid, $param) = @_;
116 my $conf = $self->read_service_config();
118 my $sconf = $conf->{$sid} || die "no such resource '$sid'\n";
120 foreach my $k (%$param) {
121 $sconf->{$k} = $param->{$k};
124 $self->write_service_config($conf);
127 sub write_service_config
{
128 my ($self, $conf) = @_;
130 $self->{service_config
} = $conf;
132 my $filename = "$self->{statusdir}/service_config";
133 return PVE
::HA
::Tools
::write_json_to_file
($filename, $conf);
136 sub read_fence_config
{
141 my $filename = "$self->{statusdir}/fence.cfg";
143 $raw = PVE
::Tools
::file_get_contents
($filename);
146 return PVE
::HA
::FenceConfig
::parse_config
($filename, $raw);
149 sub exec_fence_agent
{
150 my ($self, $agent, $node, @param) = @_;
152 # let all agent succeed and behave the same for now
153 $self->sim_hardware_cmd("power $node off", $agent);
155 return 0; # EXIT_SUCCESS
158 sub set_service_state
{
159 my ($self, $sid, $state) = @_;
161 my $conf = $self->read_service_config();
162 die "no such service '$sid'" if !$conf->{$sid};
164 $conf->{$sid}->{state} = $state;
166 $self->write_service_config($conf);
172 my ($self, $sid, $opts) = @_;
174 my $conf = $self->read_service_config();
175 die "resource ID '$sid' already defined\n" if $conf->{$sid};
177 $conf->{$sid} = $opts;
178 $conf->{$sid}->@{qw(type name)} = split(/:/, $sid);
180 $self->write_service_config($conf);
186 my ($self, $sid) = @_;
188 my $conf = $self->read_service_config();
190 die "no such service '$sid'" if !$conf->{$sid};
192 delete $conf->{$sid};
194 $self->write_service_config($conf);
199 sub change_service_location
{
200 my ($self, $sid, $current_node, $new_node) = @_;
202 my $conf = $self->read_service_config();
204 die "no such service '$sid'\n" if !$conf->{$sid};
206 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
207 if $current_node ne $conf->{$sid}->{node
};
209 $conf->{$sid}->{node
} = $new_node;
211 $self->write_service_config($conf);
214 sub service_has_lock
{
215 my ($self, $sid) = @_;
217 my $conf = $self->read_service_config();
219 die "no such service '$sid'\n" if !$conf->{$sid};
221 return $conf->{$sid}->{lock};
225 my ($self, $sid, $lock) = @_;
227 my $conf = $self->read_service_config();
229 die "no such service '$sid'\n" if !$conf->{$sid};
231 $conf->{$sid}->{lock} = $lock || 'backup';
233 $self->write_service_config($conf);
239 my ($self, $sid, $lock) = @_;
241 my $conf = $self->read_service_config();
243 die "no such service '$sid'\n" if !$conf->{$sid};
245 if (!defined($conf->{$sid}->{lock})) {
249 if (defined($lock) && $conf->{$sid}->{lock} ne $lock) {
250 warn "found lock '$conf->{$sid}->{lock}' trying to remove '$lock' lock\n";
254 my $removed_lock = delete $conf->{$sid}->{lock};
256 $self->write_service_config($conf);
258 return $removed_lock;
261 sub queue_crm_commands_nolock
{
262 my ($self, $cmd) = @_;
267 my $filename = "$self->{statusdir}/crm_commands";
269 $data = PVE
::Tools
::file_get_contents
($filename);
272 PVE
::Tools
::file_set_contents
($filename, $data);
277 sub queue_crm_commands
{
278 my ($self, $cmd) = @_;
280 my $code = sub { $self->queue_crm_commands_nolock($cmd); };
282 $self->global_lock($code);
287 sub read_crm_commands
{
293 my $filename = "$self->{statusdir}/crm_commands";
295 $data = PVE
::Tools
::file_get_contents
($filename);
297 PVE
::Tools
::file_set_contents
($filename, '');
302 return $self->global_lock($code);
305 sub read_group_config
{
308 my $filename = "$self->{statusdir}/groups";
310 $raw = PVE
::Tools
::file_get_contents
($filename) if -f
$filename;
312 return PVE
::HA
::Groups-
>parse_config($filename, $raw);
315 sub read_service_status
{
316 my ($self, $node) = @_;
318 my $filename = "$self->{statusdir}/service_status_$node";
319 return PVE
::HA
::Tools
::read_json_from_file
($filename);
322 sub write_service_status
{
323 my ($self, $node, $data) = @_;
325 my $filename = "$self->{statusdir}/service_status_$node";
326 my $res = PVE
::HA
::Tools
::write_json_to_file
($filename, $data);
328 # fixme: add test if a service runs on two nodes!!!
333 my $default_group_config = <<__EOD;
348 my ($this, $testdir) = @_;
350 die "missing testdir" if !$testdir;
352 die "testdir '$testdir' does not exist or is not a directory!\n"
355 my $class = ref($this) || $this;
357 my $self = bless {}, $class;
359 my $statusdir = $self->{statusdir
} = "$testdir/status";
361 remove_tree
($statusdir);
364 # copy initial configuartion
365 copy
("$testdir/manager_status", "$statusdir/manager_status"); # optional
367 if (-f
"$testdir/groups") {
368 copy
("$testdir/groups", "$statusdir/groups");
370 PVE
::Tools
::file_set_contents
("$statusdir/groups", $default_group_config);
373 if (-f
"$testdir/service_config") {
374 copy
("$testdir/service_config", "$statusdir/service_config");
377 'vm:101' => { node
=> 'node1', group
=> 'prefer_node1' },
378 'vm:102' => { node
=> 'node2', group
=> 'prefer_node2' },
379 'vm:103' => { node
=> 'node3', group
=> 'prefer_node3' },
380 'vm:104' => { node
=> 'node1', group
=> 'prefer_node1' },
381 'vm:105' => { node
=> 'node2', group
=> 'prefer_node2' },
382 'vm:106' => { node
=> 'node3', group
=> 'prefer_node3' },
384 $self->write_service_config($conf);
387 if (-f
"$testdir/hardware_status") {
388 copy
("$testdir/hardware_status", "$statusdir/hardware_status") ||
389 die "Copy failed: $!\n";
392 node1
=> { power
=> 'off', network
=> 'off' },
393 node2
=> { power
=> 'off', network
=> 'off' },
394 node3
=> { power
=> 'off', network
=> 'off' },
396 $self->write_hardware_status_nolock($cstatus);
399 if (-f
"$testdir/fence.cfg") {
400 copy
("$testdir/fence.cfg", "$statusdir/fence.cfg");
403 if (-f
"$testdir/datacenter.cfg") {
404 copy
("$testdir/datacenter.cfg", "$statusdir/datacenter.cfg");
407 my $cstatus = $self->read_hardware_status_nolock();
409 foreach my $node (sort keys %$cstatus) {
410 $self->{nodes
}->{$node} = {};
412 if (-f
"$testdir/service_status_$node") {
413 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
415 $self->write_service_status($node, {});
419 $self->{service_config
} = $self->read_service_config();
427 die "implement in subclass";
431 my ($self, $level, $msg, $id) = @_;
435 my $time = $self->get_time();
437 $id = 'hardware' if !$id;
439 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
443 my ($self, $node) = @_;
445 return $self->{statusdir
};
448 sub read_datacenter_conf
{
449 my ($self, $node) = @_;
451 my $filename = "$self->{statusdir}/datacenter.cfg";
452 return PVE
::HA
::Tools
::read_json_from_file
($filename, {});
456 my ($self, $code, @param) = @_;
458 my $lockfile = "$self->{statusdir}/hardware.lck";
459 my $fh = IO
::File-
>new(">>$lockfile") ||
460 die "unable to open '$lockfile'\n";
464 $success = flock($fh, LOCK_EX
);
465 if ($success || ($! != EINTR
)) {
470 die "can't acquire lock '$lockfile' - $!\n";
476 eval { $res = &$code($fh, @param) };
486 my $compute_node_info = sub {
487 my ($self, $cstatus) = @_;
492 my $online_count = 0;
494 foreach my $node (keys %$cstatus) {
495 my $d = $cstatus->{$node};
497 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
498 $node_info->{$node}->{online
} = $online;
501 $online_count++ if $online;
504 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
507 foreach my $node (keys %$cstatus) {
508 my $d = $cstatus->{$node};
509 $node_info->{$node}->{online
} = 0;
513 return ($node_info, $quorate);
519 my $cstatus = $self->read_hardware_status_nolock();
520 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
522 return ($node_info, $quorate);
525 # helper for Sim/ only
527 my ($self, $node, $state) = @_;
529 # TODO: ensure nolock is OK when adding this to RTSim
530 my $cstatus = $self->read_hardware_status_nolock();
531 my $res = $cstatus->{$node}->{cfs
}->{$state};
533 # we assume default true if not defined
534 return !defined($res) || $res;
537 # simulate hardware commands, the following commands are available:
538 # power <node> <on|off>
539 # network <node> <on|off>
541 # cfs <node> <rw|update> <work|fail>
545 # service <sid> <started|disabled|stopped|ignored>
546 # service <sid> <migrate|relocate> <target>
547 # service <sid> stop <timeout>
548 # service <sid> lock/unlock [lockname]
549 # service <sid> <add|delete>
550 sub sim_hardware_cmd
{
551 my ($self, $cmdstr, $logid) = @_;
556 my $cstatus = $self->read_hardware_status_nolock();
558 my ($cmd, $objid, $action, @params) = split(/\s+/, $cmdstr);
559 my $param = $params[0]; # for convenience/legacy
561 die "sim_hardware_cmd: no node or service for command specified"
564 my ($node, $sid, $d);
566 if ($cmd eq 'service') {
567 $sid = PVE
::HA
::Tools
::pve_verify_ha_resource_id
($objid);
570 $d = $self->{nodes
}->{$node} ||
571 die "sim_hardware_cmd: no such node '$node'\n";
574 $self->log('info', "execute $cmdstr", $logid);
576 if ($cmd eq 'power') {
577 die "sim_hardware_cmd: unknown action '$action'\n"
578 if $action !~ m/^(on|off)$/;
580 if ($cstatus->{$node}->{power
} ne $action) {
581 if ($action eq 'on') {
583 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
584 $d->{lrm
} = $self->lrm_control('start', $d, $lock_fh) if !defined($d->{lrm
});
585 $d->{lrm_restart
} = undef;
586 $cstatus->{$node}->{cfs
} = {};
591 $d->{crm_env
}->log('info', "killed by poweroff");
592 $self->crm_control('stop', $d, $lock_fh);
596 $d->{lrm_env
}->log('info', "killed by poweroff");
597 $self->lrm_control('stop', $d, $lock_fh);
599 $d->{lrm_restart
} = undef;
602 $self->watchdog_reset_nolock($node);
603 $self->write_service_status($node, {});
607 $cstatus->{$node}->{power
} = $action;
608 $cstatus->{$node}->{network
} = $action;
609 $cstatus->{$node}->{shutdown} = undef;
611 $self->write_hardware_status_nolock($cstatus);
613 } elsif ($cmd eq 'network') {
614 die "sim_hardware_cmd: unknown network action '$action'"
615 if $action !~ m/^(on|off)$/;
616 $cstatus->{$node}->{network
} = $action;
618 $self->write_hardware_status_nolock($cstatus);
620 } elsif ($cmd eq 'cfs') {
621 die "sim_hardware_cmd: unknown cfs action '$action' for node '$node'"
622 if $action !~ m/^(rw|update)$/;
623 die "sim_hardware_cmd: unknown cfs command '$param' for '$action' on node '$node'"
624 if $param !~ m/^(work|fail)$/;
626 $cstatus->{$node}->{cfs
}->{$action} = $param eq 'work';
627 $self->write_hardware_status_nolock($cstatus);
629 } elsif ($cmd eq 'reboot' || $cmd eq 'shutdown') {
630 $cstatus->{$node}->{shutdown} = $cmd;
632 $self->write_hardware_status_nolock($cstatus);
634 $self->lrm_control('shutdown', $d, $lock_fh) if defined($d->{lrm
});
635 } elsif ($cmd eq 'restart-lrm') {
637 $d->{lrm_restart
} = 1;
638 $self->lrm_control('shutdown', $d, $lock_fh);
640 } elsif ($cmd eq 'crm') {
642 if ($action eq 'stop') {
645 $self->crm_control('shutdown', $d, $lock_fh);
647 } elsif ($action eq 'start') {
648 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
650 die "sim_hardware_cmd: unknown action '$action'";
653 } elsif ($cmd eq 'service') {
654 if ($action eq 'started' || $action eq 'disabled' ||
655 $action eq 'stopped' || $action eq 'ignored') {
657 $self->set_service_state($sid, $action);
659 } elsif ($action eq 'migrate' || $action eq 'relocate') {
661 die "sim_hardware_cmd: missing target node for '$action' command"
664 $self->queue_crm_commands_nolock("$action $sid $param");
666 } elsif ($action eq 'stop') {
668 die "sim_hardware_cmd: missing timeout for '$action' command"
671 $self->queue_crm_commands_nolock("$action $sid $param");
673 } elsif ($action eq 'add') {
675 $self->add_service($sid, {state => $params[1] || 'started', node
=> $param});
677 } elsif ($action eq 'delete') {
679 $self->delete_service($sid);
681 } elsif ($action eq 'lock') {
683 $self->lock_service($sid, $param);
685 } elsif ($action eq 'unlock') {
687 $self->unlock_service($sid, $param);
690 die "sim_hardware_cmd: unknown service action '$action' " .
691 "- not implemented\n"
694 die "sim_hardware_cmd: unknown command '$cmdstr'\n";
700 return $self->global_lock($code);
703 # for controlling the resource manager services
705 my ($self, $action, $data, $lock_fh) = @_;
707 die "implement in subclass";
711 my ($self, $action, $data, $lock_fh) = @_;
713 die "implement in subclass";
719 die "implement in subclass";
722 my $modify_watchog = sub {
723 my ($self, $code) = @_;
725 my $update_cmd = sub {
727 my $filename = "$self->{statusdir}/watchdog_status";
729 my ($res, $wdstatus);
732 my $raw = PVE
::Tools
::file_get_contents
($filename);
733 $wdstatus = decode_json
($raw);
738 ($wdstatus, $res) = &$code($wdstatus);
740 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
745 return $self->global_lock($update_cmd);
748 sub watchdog_reset_nolock
{
749 my ($self, $node) = @_;
751 my $filename = "$self->{statusdir}/watchdog_status";
754 my $raw = PVE
::Tools
::file_get_contents
($filename);
755 my $wdstatus = decode_json
($raw);
757 foreach my $id (keys %$wdstatus) {
758 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
761 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
766 my ($self, $node) = @_;
773 foreach my $wfh (keys %$wdstatus) {
774 my $wd = $wdstatus->{$wfh};
775 next if $wd->{node
} ne $node;
777 my $ctime = $self->get_time();
778 my $tdiff = $ctime - $wd->{update_time
};
780 if ($tdiff > $watchdog_timeout) { # expired
782 delete $wdstatus->{$wfh};
786 return ($wdstatus, $res);
789 return &$modify_watchog($self, $code);
795 my ($self, $node) = @_;
802 my $id = "WD:$node:$$:$wdcounter";
804 die "internal error" if defined($wdstatus->{$id});
808 update_time
=> $self->get_time(),
811 return ($wdstatus, $id);
814 return &$modify_watchog($self, $code);
818 my ($self, $wfh) = @_;
823 my $wd = $wdstatus->{$wfh};
824 die "no such watchdog handle '$wfh'\n" if !defined($wd);
826 my $tdiff = $self->get_time() - $wd->{update_time
};
827 die "watchdog expired" if $tdiff > $watchdog_timeout;
829 delete $wdstatus->{$wfh};
834 return &$modify_watchog($self, $code);
837 sub watchdog_update
{
838 my ($self, $wfh) = @_;
843 my $wd = $wdstatus->{$wfh};
845 die "no such watchdog handle '$wfh'\n" if !defined($wd);
847 my $ctime = $self->get_time();
848 my $tdiff = $ctime - $wd->{update_time
};
850 die "watchdog expired" if $tdiff > $watchdog_timeout;
852 $wd->{update_time
} = $ctime;
857 return &$modify_watchog($self, $code);