[pve-devel] [RFC pve-ha-manager 3/8] TestHardware: add helper commands functions

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Dec 11 20:01:40 CET 2015


This can be used to execute helper commands in the future, which
is really helpful when executing commands which han not be mapped
to a single transition, for example need restart the transitions:
restart cmd: restart -> power off -> power on
where transitions cannot happen instantly as the crm and lrm need
to work between them.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/PVE/HA/Sim/TestHardware.pm | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/src/PVE/HA/Sim/TestHardware.pm b/src/PVE/HA/Sim/TestHardware.pm
index d49d9c0..1e1f20c 100644
--- a/src/PVE/HA/Sim/TestHardware.pm
+++ b/src/PVE/HA/Sim/TestHardware.pm
@@ -34,6 +34,9 @@ sub new {
     my $raw = PVE::Tools::file_get_contents("$testdir/cmdlist");
     $self->{cmdlist} = decode_json($raw);
 
+    # can be used to run helper commands at a specific time
+    $self->{helpercmdlist} = {};
+
     $self->{loop_count} = 0;
     $self->{cur_time} = 0;
 
@@ -59,6 +62,39 @@ sub new {
     return $self;
 }
 
+my $add_helper_cmd = sub {
+    my ($self, $exec_time, $cmd) = @_;
+
+    if ($exec_time < $self->{cur_time}) {
+	$self->log('err', "helper_cmd '$cmd' cannot be executed in the past" .
+		   " ($exec_time<$self->{cur_time})");
+	return;
+    }
+
+    if ($self->{helpercmdlist}->{$exec_time}) {
+	warn "helper cmd at time '$exec_time' already declared";
+    } else {
+	$self->{helpercmdlist}->{$exec_time} = $cmd;
+    }
+
+};
+
+my $exec_helper_cmds = sub {
+    my ($self, $time) = @_;
+
+    return if !$self->{helpercmdlist};
+
+    foreach my $exec_time (sort keys %{$self->{helpercmdlist}}) {
+	return if $exec_time > $time;
+
+	my $cmd = $self->{helpercmdlist}->{$exec_time};
+
+	$self->sim_hardware_cmd($cmd, 'cmdhelper');
+
+	delete $self->{helpercmdlist}->{$exec_time};
+    }
+};
+
 sub get_time {
     my ($self) = @_;
 
@@ -182,6 +218,8 @@ sub run {
 		$self->{cur_time} = $nodetime if $nodetime > $self->{cur_time};
 	    }
 
+	    &$exec_helper_cmds($self, $self->{cur_time});
+
 	    foreach my $n (@nodes) {
 		if (!$self->watchdog_check($n)) {
 		    $self->sim_hardware_cmd("power $n off", 'watchdog');
-- 
2.1.4





More information about the pve-devel mailing list