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
33 # $testdir/datacenter.cfg Datacenter wide HA configuration
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 update_service_config
{
113 my ($self, $sid, $param) = @_;
115 my $conf = $self->read_service_config();
117 my $sconf = $conf->{$sid} || die "no such resource '$sid'\n";
119 foreach my $k (%$param) {
120 $sconf->{$k} = $param->{$k};
123 $self->write_service_config($conf);
126 sub write_service_config
{
127 my ($self, $conf) = @_;
129 $self->{service_config
} = $conf;
131 my $filename = "$self->{statusdir}/service_config";
132 return PVE
::HA
::Tools
::write_json_to_file
($filename, $conf);
135 sub read_fence_config
{
140 my $filename = "$self->{statusdir}/fence.cfg";
142 $raw = PVE
::Tools
::file_get_contents
($filename);
145 return PVE
::HA
::FenceConfig
::parse_config
($filename, $raw);
148 sub exec_fence_agent
{
149 my ($self, $agent, $node, @param) = @_;
151 # let all agent succeed and behave the same for now
152 $self->sim_hardware_cmd("power $node off", $agent);
154 return 0; # EXIT_SUCCESS
157 sub set_service_state
{
158 my ($self, $sid, $state) = @_;
160 my $conf = $self->read_service_config();
161 die "no such service '$sid'" if !$conf->{$sid};
163 $conf->{$sid}->{state} = $state;
165 $self->write_service_config($conf);
171 my ($self, $sid, $opts) = @_;
173 my $conf = $self->read_service_config();
174 die "resource ID '$sid' already defined\n" if $conf->{$sid};
176 $conf->{$sid} = $opts;
178 $self->write_service_config($conf);
184 my ($self, $sid) = @_;
186 my $conf = $self->read_service_config();
188 die "no such service '$sid'" if !$conf->{$sid};
190 delete $conf->{$sid};
192 $self->write_service_config($conf);
197 sub change_service_location
{
198 my ($self, $sid, $current_node, $new_node) = @_;
200 my $conf = $self->read_service_config();
202 die "no such service '$sid'\n" if !$conf->{$sid};
204 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
205 if $current_node ne $conf->{$sid}->{node
};
207 $conf->{$sid}->{node
} = $new_node;
209 $self->write_service_config($conf);
212 sub service_has_lock
{
213 my ($self, $sid) = @_;
215 my $conf = $self->read_service_config();
217 die "no such service '$sid'\n" if !$conf->{$sid};
219 return $conf->{$sid}->{lock};
223 my ($self, $sid, $lock) = @_;
225 my $conf = $self->read_service_config();
227 die "no such service '$sid'\n" if !$conf->{$sid};
229 $conf->{$sid}->{lock} = $lock || 'backup';
231 $self->write_service_config($conf);
237 my ($self, $sid, $lock) = @_;
239 my $conf = $self->read_service_config();
241 die "no such service '$sid'\n" if !$conf->{$sid};
243 if (!defined($conf->{$sid}->{lock})) {
247 if (defined($lock) && $conf->{$sid}->{lock} ne $lock) {
248 warn "found lock '$conf->{$sid}->{lock}' trying to remove '$lock' lock\n";
252 my $removed_lock = delete $conf->{$sid}->{lock};
254 $self->write_service_config($conf);
256 return $removed_lock;
259 sub queue_crm_commands_nolock
{
260 my ($self, $cmd) = @_;
265 my $filename = "$self->{statusdir}/crm_commands";
267 $data = PVE
::Tools
::file_get_contents
($filename);
270 PVE
::Tools
::file_set_contents
($filename, $data);
275 sub queue_crm_commands
{
276 my ($self, $cmd) = @_;
278 my $code = sub { $self->queue_crm_commands_nolock($cmd); };
280 $self->global_lock($code);
285 sub read_crm_commands
{
291 my $filename = "$self->{statusdir}/crm_commands";
293 $data = PVE
::Tools
::file_get_contents
($filename);
295 PVE
::Tools
::file_set_contents
($filename, '');
300 return $self->global_lock($code);
303 sub read_group_config
{
306 my $filename = "$self->{statusdir}/groups";
308 $raw = PVE
::Tools
::file_get_contents
($filename) if -f
$filename;
310 return PVE
::HA
::Groups-
>parse_config($filename, $raw);
313 sub read_service_status
{
314 my ($self, $node) = @_;
316 my $filename = "$self->{statusdir}/service_status_$node";
317 return PVE
::HA
::Tools
::read_json_from_file
($filename);
320 sub write_service_status
{
321 my ($self, $node, $data) = @_;
323 my $filename = "$self->{statusdir}/service_status_$node";
324 my $res = PVE
::HA
::Tools
::write_json_to_file
($filename, $data);
326 # fixme: add test if a service runs on two nodes!!!
331 my $default_group_config = <<__EOD;
346 my ($this, $testdir) = @_;
348 die "missing testdir" if !$testdir;
350 die "testdir '$testdir' does not exist or is not a directory!\n"
353 my $class = ref($this) || $this;
355 my $self = bless {}, $class;
357 my $statusdir = $self->{statusdir
} = "$testdir/status";
359 remove_tree
($statusdir);
362 # copy initial configuartion
363 copy
("$testdir/manager_status", "$statusdir/manager_status"); # optional
365 if (-f
"$testdir/groups") {
366 copy
("$testdir/groups", "$statusdir/groups");
368 PVE
::Tools
::file_set_contents
("$statusdir/groups", $default_group_config);
371 if (-f
"$testdir/service_config") {
372 copy
("$testdir/service_config", "$statusdir/service_config");
375 'vm:101' => { node
=> 'node1', group
=> 'prefer_node1' },
376 'vm:102' => { node
=> 'node2', group
=> 'prefer_node2' },
377 'vm:103' => { node
=> 'node3', group
=> 'prefer_node3' },
378 'vm:104' => { node
=> 'node1', group
=> 'prefer_node1' },
379 'vm:105' => { node
=> 'node2', group
=> 'prefer_node2' },
380 'vm:106' => { node
=> 'node3', group
=> 'prefer_node3' },
382 $self->write_service_config($conf);
385 if (-f
"$testdir/hardware_status") {
386 copy
("$testdir/hardware_status", "$statusdir/hardware_status") ||
387 die "Copy failed: $!\n";
390 node1
=> { power
=> 'off', network
=> 'off' },
391 node2
=> { power
=> 'off', network
=> 'off' },
392 node3
=> { power
=> 'off', network
=> 'off' },
394 $self->write_hardware_status_nolock($cstatus);
397 if (-f
"$testdir/fence.cfg") {
398 copy
("$testdir/fence.cfg", "$statusdir/fence.cfg");
401 if (-f
"$testdir/datacenter.cfg") {
402 copy
("$testdir/datacenter.cfg", "$statusdir/datacenter.cfg");
405 my $cstatus = $self->read_hardware_status_nolock();
407 foreach my $node (sort keys %$cstatus) {
408 $self->{nodes
}->{$node} = {};
410 if (-f
"$testdir/service_status_$node") {
411 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
413 $self->write_service_status($node, {});
417 $self->{service_config
} = $self->read_service_config();
425 die "implement in subclass";
429 my ($self, $level, $msg, $id) = @_;
433 my $time = $self->get_time();
435 $id = 'hardware' if !$id;
437 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
441 my ($self, $node) = @_;
443 return $self->{statusdir
};
446 sub read_datacenter_conf
{
447 my ($self, $node) = @_;
449 my $filename = "$self->{statusdir}/datacenter.cfg";
450 return PVE
::HA
::Tools
::read_json_from_file
($filename, {});
454 my ($self, $code, @param) = @_;
456 my $lockfile = "$self->{statusdir}/hardware.lck";
457 my $fh = IO
::File-
>new(">>$lockfile") ||
458 die "unable to open '$lockfile'\n";
462 $success = flock($fh, LOCK_EX
);
463 if ($success || ($! != EINTR
)) {
468 die "can't acquire lock '$lockfile' - $!\n";
474 eval { $res = &$code($fh, @param) };
484 my $compute_node_info = sub {
485 my ($self, $cstatus) = @_;
490 my $online_count = 0;
492 foreach my $node (keys %$cstatus) {
493 my $d = $cstatus->{$node};
495 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
496 $node_info->{$node}->{online
} = $online;
499 $online_count++ if $online;
502 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
505 foreach my $node (keys %$cstatus) {
506 my $d = $cstatus->{$node};
507 $node_info->{$node}->{online
} = 0;
511 return ($node_info, $quorate);
517 my $cstatus = $self->read_hardware_status_nolock();
518 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
520 return ($node_info, $quorate);
523 # helper for Sim/ only
525 my ($self, $node, $state) = @_;
527 # TODO: ensure nolock is OK when adding this to RTSim
528 my $cstatus = $self->read_hardware_status_nolock();
529 my $res = $cstatus->{$node}->{cfs
}->{$state};
531 # we assume default true if not defined
532 return !defined($res) || $res;
535 # simulate hardware commands, the following commands are available:
536 # power <node> <on|off>
537 # network <node> <on|off>
539 # cfs <node> <rw|update> <work|fail>
543 # service <sid> <started|disabled|stopped|ignored>
544 # service <sid> <migrate|relocate> <target>
545 # service <sid> stop <timeout>
546 # service <sid> lock/unlock [lockname]
547 # service <sid> <add|delete>
548 sub sim_hardware_cmd
{
549 my ($self, $cmdstr, $logid) = @_;
554 my $cstatus = $self->read_hardware_status_nolock();
556 my ($cmd, $objid, $action, $param) = split(/\s+/, $cmdstr);
558 die "sim_hardware_cmd: no node or service for command specified"
561 my ($node, $sid, $d);
563 if ($cmd eq 'service') {
564 $sid = PVE
::HA
::Tools
::pve_verify_ha_resource_id
($objid);
567 $d = $self->{nodes
}->{$node} ||
568 die "sim_hardware_cmd: no such node '$node'\n";
571 $self->log('info', "execute $cmdstr", $logid);
573 if ($cmd eq 'power') {
574 die "sim_hardware_cmd: unknown action '$action'\n"
575 if $action !~ m/^(on|off)$/;
577 if ($cstatus->{$node}->{power
} ne $action) {
578 if ($action eq 'on') {
580 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
581 $d->{lrm
} = $self->lrm_control('start', $d, $lock_fh) if !defined($d->{lrm
});
582 $d->{lrm_restart
} = undef;
583 $cstatus->{$node}->{cfs
} = {};
588 $d->{crm_env
}->log('info', "killed by poweroff");
589 $self->crm_control('stop', $d, $lock_fh);
593 $d->{lrm_env
}->log('info', "killed by poweroff");
594 $self->lrm_control('stop', $d, $lock_fh);
596 $d->{lrm_restart
} = undef;
599 $self->watchdog_reset_nolock($node);
600 $self->write_service_status($node, {});
604 $cstatus->{$node}->{power
} = $action;
605 $cstatus->{$node}->{network
} = $action;
606 $cstatus->{$node}->{shutdown} = undef;
608 $self->write_hardware_status_nolock($cstatus);
610 } elsif ($cmd eq 'network') {
611 die "sim_hardware_cmd: unknown network action '$action'"
612 if $action !~ m/^(on|off)$/;
613 $cstatus->{$node}->{network
} = $action;
615 $self->write_hardware_status_nolock($cstatus);
617 } elsif ($cmd eq 'cfs') {
618 die "sim_hardware_cmd: unknown cfs action '$action' for node '$node'"
619 if $action !~ m/^(rw|update)$/;
620 die "sim_hardware_cmd: unknown cfs command '$param' for '$action' on node '$node'"
621 if $param !~ m/^(work|fail)$/;
623 $cstatus->{$node}->{cfs
}->{$action} = $param eq 'work';
624 $self->write_hardware_status_nolock($cstatus);
626 } elsif ($cmd eq 'reboot' || $cmd eq 'shutdown') {
627 $cstatus->{$node}->{shutdown} = $cmd;
629 $self->write_hardware_status_nolock($cstatus);
631 $self->lrm_control('shutdown', $d, $lock_fh) if defined($d->{lrm
});
632 } elsif ($cmd eq 'restart-lrm') {
634 $d->{lrm_restart
} = 1;
635 $self->lrm_control('shutdown', $d, $lock_fh);
637 } elsif ($cmd eq 'crm') {
639 if ($action eq 'stop') {
642 $self->crm_control('shutdown', $d, $lock_fh);
644 } elsif ($action eq 'start') {
645 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
647 die "sim_hardware_cmd: unknown action '$action'";
650 } elsif ($cmd eq 'service') {
651 if ($action eq 'started' || $action eq 'disabled' ||
652 $action eq 'stopped' || $action eq 'ignored') {
654 $self->set_service_state($sid, $action);
656 } elsif ($action eq 'migrate' || $action eq 'relocate') {
658 die "sim_hardware_cmd: missing target node for '$action' command"
661 $self->queue_crm_commands_nolock("$action $sid $param");
663 } elsif ($action eq 'stop') {
665 die "sim_hardware_cmd: missing timeout for '$action' command"
668 $self->queue_crm_commands_nolock("$action $sid $param");
670 } elsif ($action eq 'add') {
672 $self->add_service($sid, {state => 'started', node
=> $param});
674 } elsif ($action eq 'delete') {
676 $self->delete_service($sid);
678 } elsif ($action eq 'lock') {
680 $self->lock_service($sid, $param);
682 } elsif ($action eq 'unlock') {
684 $self->unlock_service($sid, $param);
687 die "sim_hardware_cmd: unknown service action '$action' " .
688 "- not implemented\n"
691 die "sim_hardware_cmd: unknown command '$cmdstr'\n";
697 return $self->global_lock($code);
700 # for controlling the resource manager services
702 my ($self, $action, $data, $lock_fh) = @_;
704 die "implement in subclass";
708 my ($self, $action, $data, $lock_fh) = @_;
710 die "implement in subclass";
716 die "implement in subclass";
719 my $modify_watchog = sub {
720 my ($self, $code) = @_;
722 my $update_cmd = sub {
724 my $filename = "$self->{statusdir}/watchdog_status";
726 my ($res, $wdstatus);
729 my $raw = PVE
::Tools
::file_get_contents
($filename);
730 $wdstatus = decode_json
($raw);
735 ($wdstatus, $res) = &$code($wdstatus);
737 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
742 return $self->global_lock($update_cmd);
745 sub watchdog_reset_nolock
{
746 my ($self, $node) = @_;
748 my $filename = "$self->{statusdir}/watchdog_status";
751 my $raw = PVE
::Tools
::file_get_contents
($filename);
752 my $wdstatus = decode_json
($raw);
754 foreach my $id (keys %$wdstatus) {
755 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
758 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
763 my ($self, $node) = @_;
770 foreach my $wfh (keys %$wdstatus) {
771 my $wd = $wdstatus->{$wfh};
772 next if $wd->{node
} ne $node;
774 my $ctime = $self->get_time();
775 my $tdiff = $ctime - $wd->{update_time
};
777 if ($tdiff > $watchdog_timeout) { # expired
779 delete $wdstatus->{$wfh};
783 return ($wdstatus, $res);
786 return &$modify_watchog($self, $code);
792 my ($self, $node) = @_;
799 my $id = "WD:$node:$$:$wdcounter";
801 die "internal error" if defined($wdstatus->{$id});
805 update_time
=> $self->get_time(),
808 return ($wdstatus, $id);
811 return &$modify_watchog($self, $code);
815 my ($self, $wfh) = @_;
820 my $wd = $wdstatus->{$wfh};
821 die "no such watchdog handle '$wfh'\n" if !defined($wd);
823 my $tdiff = $self->get_time() - $wd->{update_time
};
824 die "watchdog expired" if $tdiff > $watchdog_timeout;
826 delete $wdstatus->{$wfh};
831 return &$modify_watchog($self, $code);
834 sub watchdog_update
{
835 my ($self, $wfh) = @_;
840 my $wd = $wdstatus->{$wfh};
842 die "no such watchdog handle '$wfh'\n" if !defined($wd);
844 my $ctime = $self->get_time();
845 my $tdiff = $ctime - $wd->{update_time
};
847 die "watchdog expired" if $tdiff > $watchdog_timeout;
849 $wd->{update_time
} = $ctime;
854 return &$modify_watchog($self, $code);