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/^vm:(\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 set_service_state
{
118 my ($self, $sid, $state) = @_;
120 my $conf = $self->read_service_config();
121 die "no such service '$sid'" if !$conf->{$sid};
123 $conf->{$sid}->{state} = $state;
125 $self->write_service_config($conf);
130 sub change_service_location
{
131 my ($self, $sid, $current_node, $new_node) = @_;
133 my $conf = $self->read_service_config();
135 die "no such service '$sid'\n" if !$conf->{$sid};
137 die "current_node for '$sid' does not match ($current_node != $conf->{$sid}->{node})\n"
138 if $current_node ne $conf->{$sid}->{node
};
140 $conf->{$sid}->{node
} = $new_node;
142 $self->write_service_config($conf);
145 sub queue_crm_commands
{
146 my ($self, $cmd) = @_;
152 my $filename = "$self->{statusdir}/crm_commands";
154 $data = PVE
::Tools
::file_get_contents
($filename);
157 PVE
::Tools
::file_set_contents
($filename, $data);
160 $self->global_lock($code);
165 sub read_crm_commands
{
171 my $filename = "$self->{statusdir}/crm_commands";
173 $data = PVE
::Tools
::file_get_contents
($filename);
175 PVE
::Tools
::file_set_contents
($filename, '');
180 return $self->global_lock($code);
183 sub read_group_config
{
186 my $filename = "$self->{statusdir}/groups";
188 $raw = PVE
::Tools
::file_get_contents
($filename) if -f
$filename;
190 return PVE
::HA
::Config
::parse_groups_config
($filename, $raw);
193 sub read_service_status
{
194 my ($self, $node) = @_;
196 my $filename = "$self->{statusdir}/service_status_$node";
197 return PVE
::HA
::Tools
::read_json_from_file
($filename);
200 sub write_service_status
{
201 my ($self, $node, $data) = @_;
203 my $filename = "$self->{statusdir}/service_status_$node";
204 my $res = PVE
::HA
::Tools
::write_json_to_file
($filename, $data);
206 # fixme: add test if a service runs on two nodes!!!
211 my $default_group_config = <<__EOD;
226 my ($this, $testdir) = @_;
228 die "missing testdir" if !$testdir;
230 my $class = ref($this) || $this;
232 my $self = bless {}, $class;
234 my $statusdir = $self->{statusdir
} = "$testdir/status";
236 remove_tree
($statusdir);
239 # copy initial configuartion
240 copy
("$testdir/manager_status", "$statusdir/manager_status"); # optional
242 if (-f
"$testdir/groups") {
243 copy
("$testdir/groups", "$statusdir/groups");
245 PVE
::Tools
::file_set_contents
("$statusdir/groups", $default_group_config);
248 if (-f
"$testdir/service_config") {
249 copy
("$testdir/service_config", "$statusdir/service_config");
252 'vm:101' => { node
=> 'node1', group
=> 'prefer_node1' },
253 'vm:102' => { node
=> 'node2', group
=> 'prefer_node2' },
254 'vm:103' => { node
=> 'node3', group
=> 'prefer_node3' },
255 'vm:104' => { node
=> 'node1', group
=> 'prefer_node1' },
256 'vm:105' => { node
=> 'node2', group
=> 'prefer_node2' },
257 'vm:106' => { node
=> 'node3', group
=> 'prefer_node3' },
259 $self->write_service_config($conf);
262 if (-f
"$testdir/hardware_status") {
263 copy
("$testdir/hardware_status", "$statusdir/hardware_status") ||
264 die "Copy failed: $!\n";
267 node1
=> { power
=> 'off', network
=> 'off' },
268 node2
=> { power
=> 'off', network
=> 'off' },
269 node3
=> { power
=> 'off', network
=> 'off' },
271 $self->write_hardware_status_nolock($cstatus);
275 my $cstatus = $self->read_hardware_status_nolock();
277 foreach my $node (sort keys %$cstatus) {
278 $self->{nodes
}->{$node} = {};
280 if (-f
"$testdir/service_status_$node") {
281 copy
("$testdir/service_status_$node", "$statusdir/service_status_$node");
283 $self->write_service_status($node, {});
287 $self->{service_config
} = $self->read_service_config();
295 die "implement in subclass";
299 my ($self, $level, $msg, $id) = @_;
303 my $time = $self->get_time();
305 $id = 'hardware' if !$id;
307 printf("%-5s %5d %12s: $msg\n", $level, $time, $id);
311 my ($self, $node) = @_;
313 return $self->{statusdir
};
317 my ($self, $code, @param) = @_;
319 my $lockfile = "$self->{statusdir}/hardware.lck";
320 my $fh = IO
::File-
>new(">>$lockfile") ||
321 die "unable to open '$lockfile'\n";
325 $success = flock($fh, LOCK_EX
);
326 if ($success || ($! != EINTR
)) {
331 die "can't acquire lock '$lockfile' - $!\n";
337 eval { $res = &$code($fh, @param) };
347 my $compute_node_info = sub {
348 my ($self, $cstatus) = @_;
353 my $online_count = 0;
355 foreach my $node (keys %$cstatus) {
356 my $d = $cstatus->{$node};
358 my $online = ($d->{power
} eq 'on' && $d->{network
} eq 'on') ?
1 : 0;
359 $node_info->{$node}->{online
} = $online;
362 $online_count++ if $online;
365 my $quorate = ($online_count > int($node_count/2)) ?
1 : 0;
368 foreach my $node (keys %$cstatus) {
369 my $d = $cstatus->{$node};
370 $node_info->{$node}->{online
} = 0;
374 return ($node_info, $quorate);
380 my $cstatus = $self->read_hardware_status_nolock();
381 my ($node_info, $quorate) = &$compute_node_info($self, $cstatus);
383 return ($node_info, $quorate);
386 # simulate hardware commands
387 # power <node> <on|off>
388 # network <node> <on|off>
390 sub sim_hardware_cmd
{
391 my ($self, $cmdstr, $logid) = @_;
393 die "implement in subclass";
399 die "implement in subclass";
402 my $modify_watchog = sub {
403 my ($self, $code) = @_;
405 my $update_cmd = sub {
407 my $filename = "$self->{statusdir}/watchdog_status";
409 my ($res, $wdstatus);
412 my $raw = PVE
::Tools
::file_get_contents
($filename);
413 $wdstatus = decode_json
($raw);
418 ($wdstatus, $res) = &$code($wdstatus);
420 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
425 return $self->global_lock($update_cmd);
428 sub watchdog_reset_nolock
{
429 my ($self, $node) = @_;
431 my $filename = "$self->{statusdir}/watchdog_status";
434 my $raw = PVE
::Tools
::file_get_contents
($filename);
435 my $wdstatus = decode_json
($raw);
437 foreach my $id (keys %$wdstatus) {
438 delete $wdstatus->{$id} if $wdstatus->{$id}->{node
} eq $node;
441 PVE
::Tools
::file_set_contents
($filename, encode_json
($wdstatus));
446 my ($self, $node) = @_;
453 foreach my $wfh (keys %$wdstatus) {
454 my $wd = $wdstatus->{$wfh};
455 next if $wd->{node
} ne $node;
457 my $ctime = $self->get_time();
458 my $tdiff = $ctime - $wd->{update_time
};
460 if ($tdiff > $watchdog_timeout) { # expired
462 delete $wdstatus->{$wfh};
466 return ($wdstatus, $res);
469 return &$modify_watchog($self, $code);
475 my ($self, $node) = @_;
482 my $id = "WD:$node:$$:$wdcounter";
484 die "internal error" if defined($wdstatus->{$id});
488 update_time
=> $self->get_time(),
491 return ($wdstatus, $id);
494 return &$modify_watchog($self, $code);
498 my ($self, $wfh) = @_;
503 my $wd = $wdstatus->{$wfh};
504 die "no such watchdog handle '$wfh'\n" if !defined($wd);
506 my $tdiff = $self->get_time() - $wd->{update_time
};
507 die "watchdog expired" if $tdiff > $watchdog_timeout;
509 delete $wdstatus->{$wfh};
514 return &$modify_watchog($self, $code);
517 sub watchdog_update
{
518 my ($self, $wfh) = @_;
523 my $wd = $wdstatus->{$wfh};
525 die "no such watchdog handle '$wfh'\n" if !defined($wd);
527 my $ctime = $self->get_time();
528 my $tdiff = $ctime - $wd->{update_time
};
530 die "watchdog expired" if $tdiff > $watchdog_timeout;
532 $wd->{update_time
} = $ctime;
537 return &$modify_watchog($self, $code);