]>
git.proxmox.com Git - pve-ha-manager.git/blob - src/PVE/HA/Sim/TestHardware.pm
1 package PVE
::HA
::Sim
::TestHardware
;
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);
22 use PVE
::HA
::Sim
::TestEnv
;
23 use base
qw(PVE::HA::Sim::Hardware);
25 my $max_sim_time = 10000;
28 my ($this, $testdir) = @_;
30 my $class = ref($this) || $this;
32 my $self = $class->SUPER::new
($testdir);
34 my $raw = PVE
::Tools
::file_get_contents
("$testdir/cmdlist");
35 $self->{cmdlist
} = decode_json
($raw);
37 $self->{loop_count
} = 0;
38 $self->{cur_time
} = 0;
40 my $statusdir = $self->statusdir();
41 my $logfile = "$statusdir/log";
42 $self->{logfh
} = IO
::File-
>new(">>$logfile") ||
43 die "unable to open '$logfile' - $!";
45 foreach my $node (sort keys %{$self->{nodes
}}) {
47 my $d = $self->{nodes
}->{$node};
50 PVE
::HA
::Env-
>new('PVE::HA::Sim::TestEnv', $node, $self, 'crm');
53 PVE
::HA
::Env-
>new('PVE::HA::Sim::TestEnv', $node, $self, 'lrm');
55 $d->{crm
} = undef; # create on power on
56 $d->{lrm
} = undef; # create on power on
65 return $self->{cur_time
};
69 my ($self, $level, $msg, $id) = @_;
73 my $time = $self->get_time();
75 $id = 'hardware' if !$id;
77 my $line = sprintf("%-5s %5d %12s: $msg\n", $level, $time, $id);
80 $self->{logfh
}->print($line);
81 $self->{logfh
}->flush();
84 # simulate hardware commands
85 # power <node> <on|off>
86 # network <node> <on|off>
90 sub sim_hardware_cmd
{
91 my ($self, $cmdstr, $logid) = @_;
95 my $cstatus = $self->read_hardware_status_nolock();
97 my ($cmd, $node, $action) = split(/\s+/, $cmdstr);
99 die "sim_hardware_cmd: no node specified" if !$node;
101 my $d = $self->{nodes
}->{$node};
102 die "sim_hardware_cmd: no such node '$node'\n" if !$d;
104 $self->log('info', "execute $cmdstr", $logid);
106 if ($cmd eq 'power') {
107 die "sim_hardware_cmd: unknown action '$action'" if $action !~ m/^(on|off)$/;
108 if ($cstatus->{$node}->{power
} ne $action) {
109 if ($action eq 'on') {
110 $d->{crm
} = PVE
::HA
::CRM-
>new($d->{crm_env
}) if !$d->{crm
};
111 $d->{lrm
} = PVE
::HA
::LRM-
>new($d->{lrm_env
}) if !$d->{lrm
};
114 $d->{crm_env
}->log('info', "killed by poweroff");
118 $d->{lrm_env
}->log('info', "killed by poweroff");
124 $cstatus->{$node}->{power
} = $action;
125 $cstatus->{$node}->{network
} = $action;
126 $cstatus->{$node}->{shutdown} = undef;
128 $self->write_hardware_status_nolock($cstatus);
130 } elsif ($cmd eq 'network') {
131 die "sim_hardware_cmd: unknown action '$action'" if $action !~ m/^(on|off)$/;
132 $cstatus->{$node}->{network
} = $action;
134 $self->write_hardware_status_nolock($cstatus);
136 } elsif ($cmd eq 'reboot' || $cmd eq 'shutdown') {
137 $cstatus->{$node}->{shutdown} = $cmd;
139 $self->write_hardware_status_nolock($cstatus);
141 $d->{lrm
}->shutdown_request() if $d->{lrm
};
144 die "sim_hardware_cmd: unknown command '$cmdstr'\n";
149 return $self->global_lock($code);
155 my $last_command_time = 0;
160 my $starttime = $self->get_time();
162 my @nodes = sort keys %{$self->{nodes
}};
164 my $nodecount = scalar(@nodes);
166 my $looptime = $nodecount*2;
167 $looptime = 20 if $looptime < 20;
169 die "unable to simulate so many nodes. You need to increate watchdog/lock timeouts.\n"
172 foreach my $node (@nodes) {
174 my $d = $self->{nodes
}->{$node};
176 if (my $crm = $d->{crm
}) {
178 $d->{crm_env
}->loop_start_hook($self->get_time());
180 die "implement me (CRM exit)" if !$crm->do_one_iteration();
182 $d->{crm_env
}->loop_end_hook();
184 my $nodetime = $d->{crm_env
}->get_time();
185 $self->{cur_time
} = $nodetime if $nodetime > $self->{cur_time
};
188 if (my $lrm = $d->{lrm
}) {
190 $d->{lrm_env
}->loop_start_hook($self->get_time());
192 my $exit_lrm = !$lrm->do_one_iteration();
194 $d->{lrm_env
}->loop_end_hook();
196 my $nodetime = $d->{lrm_env
}->get_time();
197 $self->{cur_time
} = $nodetime if $nodetime > $self->{cur_time
};
200 $d->{lrm_env
}->log('info', "exit (loop end)");
202 my $cstatus = $self->read_hardware_status_nolock();
203 my $nstatus = $cstatus->{$node} || die "no node status for node '$node'";
204 my $shutdown = $nstatus->{shutdown};
205 if ($shutdown eq 'reboot') {
206 $self->sim_hardware_cmd("power $node off", 'reboot');
207 $self->sim_hardware_cmd("power $node on", 'reboot');
208 } elsif ($shutdown eq 'shutdown') {
209 $self->sim_hardware_cmd("power $node off", 'shutdown');
211 die "unexpected LRM exit - not implemented"
216 foreach my $n (@nodes) {
217 if (!$self->watchdog_check($n)) {
218 $self->sim_hardware_cmd("power $n off", 'watchdog');
219 $self->log('info', "server '$n' stopped by poweroff (watchdog)");
220 $self->{nodes
}->{$n}->{crm
} = undef;
221 $self->{nodes
}->{$n}->{lrm
} = undef;
227 $self->{cur_time
} = $starttime + $looptime
228 if ($self->{cur_time
} - $starttime) < $looptime;
230 die "simulation end\n" if $self->{cur_time
} > $max_sim_time;
232 foreach my $node (@nodes) {
233 my $d = $self->{nodes
}->{$node};
235 $d->{lrm_env
}->loop_start_hook($self->get_time());
236 $d->{crm_env
}->loop_start_hook($self->get_time());
239 next if $self->{cur_time
} < $next_cmd_at;
241 # apply new comand after 5 loop iterations
243 if (($self->{loop_count
} % 5) == 0) {
244 my $list = shift @{$self->{cmdlist
}};
246 # end sumulation (500 seconds after last command)
247 return if (($self->{cur_time
} - $last_command_time) > 500);
250 foreach my $cmd (@$list) {
251 $last_command_time = $self->{cur_time
};
253 if ($cmd =~ m/^delay\s+(\d+)\s*$/) {
254 $next_cmd_at = $self->{cur_time
} + $1;
256 $self->sim_hardware_cmd($cmd, 'cmdlist');
261 ++$self->{loop_count
};