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 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
::Groups-
>parse_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 if (-f
"$testdir/datacenter.cfg") {
388 copy
("$testdir/datacenter.cfg", "$statusdir/datacenter.cfg");
391 my $cstatus = $self->read_hardware_status_nolock();
393 foreach my $node (sort keys %$cstatus) {
394 $self->{nodes
}->{$node} = {};
396 if (-f
"$testdir/service_status_$node") {
397 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
399 $self->write_service_status($node, {});
403 $self->{service_config
} = $self->read_service_config();
411 die "implement in subclass";
415 my ($self, $level, $msg, $id) = @_;
419 my $time = $self->get_time();
421 $id = 'hardware' if !$id;
423 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
427 my ($self, $node) = @_;
429 return $self->{statusdir
};
432 sub read_datacenter_conf
{
433 my ($self, $node) = @_;
435 my $filename = "$self->{statusdir}/datacenter.cfg";
436 return PVE
::HA
::Tools
::read_json_from_file
($filename, {});
440 my ($self, $code, @param) = @_;
442 my $lockfile = "$self->{statusdir}/hardware.lck";
443 my $fh = IO
::File-
>new(">>$lockfile") ||
444 die "unable to open '$lockfile'\n";
448 $success = flock($fh, LOCK_EX
);
449 if ($success || ($! != EINTR
)) {
454 die "can't acquire lock '$lockfile' - $!\n";
460 eval { $res = &$code($fh, @param) };
470 my $compute_node_info = sub {
471 my ($self, $cstatus) = @_;
476 my $online_count = 0;
478 foreach my $node (keys %$cstatus) {
479 my $d = $cstatus->{$node};
481 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
482 $node_info->{$node}->{online
} = $online;
485 $online_count++ if $online;
488 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
491 foreach my $node (keys %$cstatus) {
492 my $d = $cstatus->{$node};
493 $node_info->{$node}->{online
} = 0;
497 return ($node_info, $quorate);
503 my $cstatus = $self->read_hardware_status_nolock();
504 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
506 return ($node_info, $quorate);
509 # helper for Sim/ only
511 my ($self, $node, $state) = @_;
513 # TODO: ensure nolock is OK when adding this to RTSim
514 my $cstatus = $self->read_hardware_status_nolock();
515 my $res = $cstatus->{$node}->{cfs
}->{$state};
517 # we assume default true if not defined
518 return !defined($res) || $res;
521 # simulate hardware commands
522 # power <node> <on|off>
523 # network <node> <on|off>
524 # cfs <node> <rw|update> <work|fail>
528 # service <sid> <started|disabled|stopped|ignored>
529 # service <sid> <migrate|relocate> <target>
530 # service <sid> lock/unlock [lockname]
532 sub sim_hardware_cmd
{
533 my ($self, $cmdstr, $logid) = @_;
538 my $cstatus = $self->read_hardware_status_nolock();
540 my ($cmd, $objid, $action, $param) = split(/\s+/, $cmdstr);
542 die "sim_hardware_cmd: no node or service for command specified"
545 my ($node, $sid, $d);
547 if ($cmd eq 'service') {
548 $sid = PVE
::HA
::Tools
::pve_verify_ha_resource_id
($objid);
551 $d = $self->{nodes
}->{$node} ||
552 die "sim_hardware_cmd: no such node '$node'\n";
555 $self->log('info', "execute $cmdstr", $logid);
557 if ($cmd eq 'power') {
558 die "sim_hardware_cmd: unknown action '$action'\n"
559 if $action !~ m/^(on|off)$/;
561 if ($cstatus->{$node}->{power
} ne $action) {
562 if ($action eq 'on') {
564 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
565 $d->{lrm
} = $self->lrm_control('start', $d, $lock_fh) if !defined($d->{lrm
});
566 $d->{lrm_restart
} = undef;
567 $cstatus->{$node}->{cfs
} = {};
572 $d->{crm_env
}->log('info', "killed by poweroff");
573 $self->crm_control('stop', $d, $lock_fh);
577 $d->{lrm_env
}->log('info', "killed by poweroff");
578 $self->lrm_control('stop', $d, $lock_fh);
580 $d->{lrm_restart
} = undef;
583 $self->watchdog_reset_nolock($node);
584 $self->write_service_status($node, {});
588 $cstatus->{$node}->{power
} = $action;
589 $cstatus->{$node}->{network
} = $action;
590 $cstatus->{$node}->{shutdown} = undef;
592 $self->write_hardware_status_nolock($cstatus);
594 } elsif ($cmd eq 'network') {
595 die "sim_hardware_cmd: unknown network action '$action'"
596 if $action !~ m/^(on|off)$/;
597 $cstatus->{$node}->{network
} = $action;
599 $self->write_hardware_status_nolock($cstatus);
601 } elsif ($cmd eq 'cfs') {
602 die "sim_hardware_cmd: unknown cfs action '$action' for node '$node'"
603 if $action !~ m/^(rw|update)$/;
604 die "sim_hardware_cmd: unknown cfs command '$param' for '$action' on node '$node'"
605 if $param !~ m/^(work|fail)$/;
607 $cstatus->{$node}->{cfs
}->{$action} = $param eq 'work';
608 $self->write_hardware_status_nolock($cstatus);
610 } elsif ($cmd eq 'reboot' || $cmd eq 'shutdown') {
611 $cstatus->{$node}->{shutdown} = $cmd;
613 $self->write_hardware_status_nolock($cstatus);
615 $self->lrm_control('shutdown', $d, $lock_fh) if defined($d->{lrm
});
616 } elsif ($cmd eq 'restart-lrm') {
618 $d->{lrm_restart
} = 1;
619 $self->lrm_control('shutdown', $d, $lock_fh);
621 } elsif ($cmd eq 'crm') {
623 if ($action eq 'stop') {
626 $self->crm_control('shutdown', $d, $lock_fh);
628 } elsif ($action eq 'start') {
629 $d->{crm
} = $self->crm_control('start', $d, $lock_fh) if !defined($d->{crm
});
631 die "sim_hardware_cmd: unknown action '$action'";
634 } elsif ($cmd eq 'service') {
635 if ($action eq 'started' || $action eq 'disabled' ||
636 $action eq 'stopped' || $action eq 'ignored') {
638 $self->set_service_state($sid, $action);
640 } elsif ($action eq 'migrate' || $action eq 'relocate') {
642 die "sim_hardware_cmd: missing target node for '$action' command"
645 $self->queue_crm_commands_nolock("$action $sid $param");
647 } elsif ($action eq 'add') {
649 $self->add_service($sid, {state => 'started', node
=> $param});
651 } elsif ($action eq 'delete') {
653 $self->delete_service($sid);
655 } elsif ($action eq 'lock') {
657 $self->lock_service($sid, $param);
659 } elsif ($action eq 'unlock') {
661 $self->unlock_service($sid, $param);
664 die "sim_hardware_cmd: unknown service action '$action' " .
665 "- not implemented\n"
668 die "sim_hardware_cmd: unknown command '$cmdstr'\n";
674 return $self->global_lock($code);
677 # for controlling the resource manager services
679 my ($self, $action, $data, $lock_fh) = @_;
681 die "implement in subclass";
685 my ($self, $action, $data, $lock_fh) = @_;
687 die "implement in subclass";
693 die "implement in subclass";
696 my $modify_watchog = sub {
697 my ($self, $code) = @_;
699 my $update_cmd = sub {
701 my $filename = "$self->{statusdir}/watchdog_status";
703 my ($res, $wdstatus);
706 my $raw = PVE
::Tools
::file_get_contents
($filename);
707 $wdstatus = decode_json
($raw);
712 ($wdstatus, $res) = &$code($wdstatus);
714 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
719 return $self->global_lock($update_cmd);
722 sub watchdog_reset_nolock
{
723 my ($self, $node) = @_;
725 my $filename = "$self->{statusdir}/watchdog_status";
728 my $raw = PVE
::Tools
::file_get_contents
($filename);
729 my $wdstatus = decode_json
($raw);
731 foreach my $id (keys %$wdstatus) {
732 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
735 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
740 my ($self, $node) = @_;
747 foreach my $wfh (keys %$wdstatus) {
748 my $wd = $wdstatus->{$wfh};
749 next if $wd->{node
} ne $node;
751 my $ctime = $self->get_time();
752 my $tdiff = $ctime - $wd->{update_time
};
754 if ($tdiff > $watchdog_timeout) { # expired
756 delete $wdstatus->{$wfh};
760 return ($wdstatus, $res);
763 return &$modify_watchog($self, $code);
769 my ($self, $node) = @_;
776 my $id = "WD:$node:$$:$wdcounter";
778 die "internal error" if defined($wdstatus->{$id});
782 update_time
=> $self->get_time(),
785 return ($wdstatus, $id);
788 return &$modify_watchog($self, $code);
792 my ($self, $wfh) = @_;
797 my $wd = $wdstatus->{$wfh};
798 die "no such watchdog handle '$wfh'\n" if !defined($wd);
800 my $tdiff = $self->get_time() - $wd->{update_time
};
801 die "watchdog expired" if $tdiff > $watchdog_timeout;
803 delete $wdstatus->{$wfh};
808 return &$modify_watchog($self, $code);
811 sub watchdog_update
{
812 my ($self, $wfh) = @_;
817 my $wd = $wdstatus->{$wfh};
819 die "no such watchdog handle '$wfh'\n" if !defined($wd);
821 my $ctime = $self->get_time();
822 my $tdiff = $ctime - $wd->{update_time
};
824 die "watchdog expired" if $tdiff > $watchdog_timeout;
826 $wd->{update_time
} = $ctime;
831 return &$modify_watchog($self, $code);