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);
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/^pvevm:(\d+)$/) {
102 $d->{state} = 'disabled' if !$d->{state};
108 sub write_service_config
{
109 my ($self, $conf) = @_;
111 $self->{service_config
} = $conf;
113 my $filename = "$self->{statusdir}/service_config";
114 return PVE
::HA
::Tools
::write_json_to_file
($filename, $conf);
117 sub change_service_location
{
118 my ($self, $sid, $current_node, $new_node) = @_;
120 my $conf = $self->read_service_config();
122 die "no such service '$sid'\n" if !$conf->{$sid};
124 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
125 if $current_node ne $conf->{$sid}->{node
};
127 $conf->{$sid}->{node
} = $new_node;
129 $self->write_service_config($conf);
132 sub queue_crm_commands
{
133 my ($self, $cmd) = @_;
139 my $filename = "$self->{statusdir}/crm_commands";
141 $data = PVE
::Tools
::file_get_contents
($filename);
144 PVE
::Tools
::file_set_contents
($filename, $data);
147 $self->global_lock($code);
152 sub read_crm_commands
{
158 my $filename = "$self->{statusdir}/crm_commands";
160 $data = PVE
::Tools
::file_get_contents
($filename);
162 PVE
::Tools
::file_set_contents
($filename, '');
167 return $self->global_lock($code);
170 sub read_group_config
{
173 my $filename = "$self->{statusdir}/groups";
175 $raw = PVE
::Tools
::file_get_contents
($filename) if -f
$filename;
177 return PVE
::HA
::Config
::parse_groups_config
($filename, $raw);
180 sub read_service_status
{
181 my ($self, $node) = @_;
183 my $filename = "$self->{statusdir}/service_status_$node";
184 return PVE
::HA
::Tools
::read_json_from_file
($filename);
187 sub write_service_status
{
188 my ($self, $node, $data) = @_;
190 my $filename = "$self->{statusdir}/service_status_$node";
191 my $res = PVE
::HA
::Tools
::write_json_to_file
($filename, $data);
193 # fixme: add test if a service runs on two nodes!!!
198 my $default_group_config = <<__EOD;
213 my ($this, $testdir) = @_;
215 die "missing testdir" if !$testdir;
217 my $class = ref($this) || $this;
219 my $self = bless {}, $class;
221 my $statusdir = $self->{statusdir
} = "$testdir/status";
223 remove_tree
($statusdir);
226 # copy initial configuartion
227 copy
("$testdir/manager_status", "$statusdir/manager_status"); # optional
229 if (-f
"$testdir/groups") {
230 copy
("$testdir/groups", "$statusdir/groups");
232 PVE
::Tools
::file_set_contents
("$statusdir/groups", $default_group_config);
235 if (-f
"$testdir/service_config") {
236 copy
("$testdir/service_config", "$statusdir/service_config");
239 'pvevm:101' => { node
=> 'node1', group
=> 'prefer_node1' },
240 'pvevm:102' => { node
=> 'node2', group
=> 'prefer_node2' },
241 'pvevm:103' => { node
=> 'node3', group
=> 'prefer_node3' },
242 'pvevm:104' => { node
=> 'node1', group
=> 'prefer_node1' },
243 'pvevm:105' => { node
=> 'node2', group
=> 'prefer_node2' },
244 'pvevm:106' => { node
=> 'node3', group
=> 'prefer_node3' },
246 $self->write_service_config($conf);
249 if (-f
"$testdir/hardware_status") {
250 copy
("$testdir/hardware_status", "$statusdir/hardware_status") ||
251 die "Copy failed: $!\n";
254 node1
=> { power
=> 'off', network
=> 'off' },
255 node2
=> { power
=> 'off', network
=> 'off' },
256 node3
=> { power
=> 'off', network
=> 'off' },
258 $self->write_hardware_status_nolock($cstatus);
262 my $cstatus = $self->read_hardware_status_nolock();
264 foreach my $node (sort keys %$cstatus) {
265 $self->{nodes
}->{$node} = {};
267 if (-f
"$testdir/service_status_$node") {
268 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
270 $self->write_service_status($node, {});
274 $self->{service_config
} = $self->read_service_config();
282 die "implement in subclass";
286 my ($self, $level, $msg, $id) = @_;
290 my $time = $self->get_time();
292 $id = 'hardware' if !$id;
294 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
298 my ($self, $node) = @_;
300 return $self->{statusdir
};
304 my ($self, $code, @param) = @_;
306 my $lockfile = "$self->{statusdir}/hardware.lck";
307 my $fh = IO
::File-
>new(">>$lockfile") ||
308 die "unable to open '$lockfile'\n";
312 $success = flock($fh, LOCK_EX
);
313 if ($success || ($! != EINTR
)) {
318 die "can't aquire lock '$lockfile' - $!\n";
324 eval { $res = &$code($fh, @param) };
334 my $compute_node_info = sub {
335 my ($self, $cstatus) = @_;
340 my $online_count = 0;
342 foreach my $node (keys %$cstatus) {
343 my $d = $cstatus->{$node};
345 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
346 $node_info->{$node}->{online
} = $online;
349 $online_count++ if $online;
352 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
355 foreach my $node (keys %$cstatus) {
356 my $d = $cstatus->{$node};
357 $node_info->{$node}->{online
} = 0;
361 return ($node_info, $quorate);
367 my ($node_info, $quorate);
370 my $cstatus = $self->read_hardware_status_nolock();
371 ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
374 $self->global_lock($code);
376 return ($node_info, $quorate);
379 # simulate hardware commands
380 # power <node> <on|off>
381 # network <node> <on|off>
383 sub sim_hardware_cmd
{
384 my ($self, $cmdstr, $logid) = @_;
386 die "implement in subclass";
392 die "implement in subclass";
395 my $modify_watchog = sub {
396 my ($self, $code) = @_;
398 my $update_cmd = sub {
400 my $filename = "$self->{statusdir}/watchdog_status";
402 my ($res, $wdstatus);
405 my $raw = PVE
::Tools
::file_get_contents
($filename);
406 $wdstatus = decode_json
($raw);
411 ($wdstatus, $res) = &$code($wdstatus);
413 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
418 return $self->global_lock($update_cmd);
421 sub watchdog_reset_nolock
{
422 my ($self, $node) = @_;
424 my $filename = "$self->{statusdir}/watchdog_status";
427 my $raw = PVE
::Tools
::file_get_contents
($filename);
428 my $wdstatus = decode_json
($raw);
430 foreach my $id (keys %$wdstatus) {
431 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
434 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
439 my ($self, $node) = @_;
446 foreach my $wfh (keys %$wdstatus) {
447 my $wd = $wdstatus->{$wfh};
448 next if $wd->{node
} ne $node;
450 my $ctime = $self->get_time();
451 my $tdiff = $ctime - $wd->{update_time
};
453 if ($tdiff > $watchdog_timeout) { # expired
455 delete $wdstatus->{$wfh};
459 return ($wdstatus, $res);
462 return &$modify_watchog($self, $code);
468 my ($self, $node) = @_;
475 my $id = "WD:$node:$$:$wdcounter";
477 die "internal error" if defined($wdstatus->{$id});
481 update_time
=> $self->get_time(),
484 return ($wdstatus, $id);
487 return &$modify_watchog($self, $code);
491 my ($self, $wfh) = @_;
496 my $wd = $wdstatus->{$wfh};
497 die "no such watchdog handle '$wfh'\n" if !defined($wd);
499 my $tdiff = $self->get_time() - $wd->{update_time
};
500 die "watchdog expired" if $tdiff > $watchdog_timeout;
502 delete $wdstatus->{$wfh};
507 return &$modify_watchog($self, $code);
510 sub watchdog_update
{
511 my ($self, $wfh) = @_;
516 my $wd = $wdstatus->{$wfh};
518 die "no such watchdog handle '$wfh'\n" if !defined($wd);
520 my $ctime = $self->get_time();
521 my $tdiff = $ctime - $wd->{update_time
};
523 die "watchdog expired" if $tdiff > $watchdog_timeout;
525 $wd->{update_time
} = $ctime;
530 return &$modify_watchog($self, $code);