Commit ab1303d2 authored by Kirk Webb's avatar Kirk Webb

tbadb: Add 'nodewait' RPC.

parent 34f635ff
......@@ -45,6 +45,7 @@ sub cmd_loadimage($@);
sub cmd_forward($@);
sub cmd_unforward($@);
sub cmd_reboot($;@);
sub cmd_nodewait($;@);
sub GetRPCPipeHandles($);
sub ConnectRPCHost($);
......@@ -61,6 +62,7 @@ my $TBADB_CHECKIMAGE_TMO = 30;
my $TBADB_LOADIMAGE_TMO = 120;
my $TBADB_FORWARD_TMO = 15;
my $TBADB_REBOOT_TMO = 60;
my $TBADB_NODEWAIT_TMO = 60;
my $CHILD_WAIT_TMO = 10;
my $SCP = "/usr/bin/scp";
......@@ -70,6 +72,7 @@ my %DISPATCH = (
'forward' => \&cmd_forward,
'unforward' => \&cmd_unforward,
'reboot' => \&cmd_reboot,
'nodewait' => \&cmd_nodewait,
);
sub showhelp() {
......@@ -153,6 +156,7 @@ sub cmd_setup($@) {
# Individual commands will die() if they fail, so subsequent calls
# will not happen.
cmd_loadimage($node_id, $imagepid, $imagename);
cmd_nodewait($node_id);
cmd_forward($node_id, $thost);
# Done!
......@@ -413,7 +417,7 @@ sub cmd_reboot($;@) {
die "tbadb::cmd_reboot: Failed to receive valid response for 'reboot'\n"
if (RecvRPCData($rpcin, \$pdu, $TBADB_REBOOT_TMO) != 1);
my $data = DecodeRPCData($pdu);
die "tbadb::cmd_reboot: Could not decode RPC response from 'reboot'"
die "tbadb::cmd_reboot: Could not decode RPC response from 'reboot'\n"
if (!$data);
# Check returned result.
......@@ -428,6 +432,56 @@ sub cmd_reboot($;@) {
return 0;
}
#
# Given a valid node_id, wait for it to become available via adb.
#
sub cmd_nodewait($;@) {
my ($node_id) = @_;
# Check and untaint arguments;
die "tbadb::cmd_nodewait: node_id argument missing!\n"
if (!$node_id);
# Make sure user has access to requested node
my $node = Node->Lookup($node_id);
die "tbadb::cmd_nodewait: Invalid node name $node_id!\n"
if (!defined($node));
die("tbadb::cmd_nodewait: You do not have permission to access $node\n")
if (!$node->AccessCheck($this_user, TB_NODEACCESS_READINFO));
# Grab the RPC pipe.
my ($rpcin, $rpcout) = GetRPCPipeHandles($node);
die "tbadb::cmd_nodewait: Failed to get valid SSH pipe filehandles!\n"
if (!$rpcin || !$rpcout);
# Call nodewait RPC on device's console host.
die "tbadb::cmd_nodewiat: 'nodewait' RPC failed for $node!\n"
if (!SendRPCData($rpcout,
EncodeCall("nodewait", { NODE_ID => $node_id })));
# Wait for node.
my $pdu;
my $wres = RecvRPCData($rpcin, \$pdu, $TBADB_NODEWAIT_TMO);
die "tbadb::cmd_nodewait: invalid response from 'nodewait'\n"
if ($wres == 0);
die "tbadb::cmd_nodewait: 'nodewait' timed out for $node_id\n"
if ($wres == -1);
my $data = DecodeRPCData($pdu);
die "tbadb::cmd_nodewait: Could not decode RPC response from 'nodewait'\n"
if (!$data);
# Check returned result.
if (exists($data->{ERROR}) || !exists($data->{RESULT}->{SUCCESS})) {
warn "tbadb::cmd_nodewait: Received error from 'nodewait':\n";
warn "". Dumper($data);
exit 1;
}
# Done!
print "tbadb::cmd_nodewait: $node_id is ready.\n";
return 0;
}
# Helper that returns the RPC in/out pipe pair. Establishes the remote
# connection if necessary. Argument is a node object.
sub GetRPCPipeHandles($) {
......
......@@ -45,6 +45,7 @@ sub rpc_captureimage($);
sub rpc_reboot($);
sub rpc_forward($);
sub rpc_unforward($);
sub rpc_nodewait($);
sub rpc_ping($);
sub rpc_exit($);
sub send_error($$$);
......@@ -65,6 +66,7 @@ my %DISPATCH = (
'reboot' => \&rpc_reboot,
'forward' => \&rpc_forward,
'unforward' => \&rpc_unforward,
'nodewait' => \&rpc_nodewait,
'ping' => \&rpc_ping,
'exit' => \&rpc_exit,
);
......@@ -89,7 +91,7 @@ my $WM_LOW = 40 * 1000 * 1000 * 1000; # 40 GB
my $MINPORT = 8001;
my $MAXPORT = 8100;
my $FASTBOOT_TMO = 15;
my $ANDROID_BOOT_TMO = 60;
my $ANDROID_BOOT_TMO = 90;
my $IMGLOCK_TMO = 300;
my $LRULOCK_TMO = 60;
my $UNPACKLOCK_TMO = 30;
......@@ -619,6 +621,38 @@ sub rpc_unforward($) {
}
sub rpc_nodewait($) {
my ($data) = @_;
my $node_id = $data->{ARGS}->{NODE_ID};
if (!$node_id) {
warn "tbadb_proxy::rpc_nodewait: Missing node_id argument!\n";
send_error($data->{FID}, RPCERR_BADARGS, "node_id argument missing.");
exit 1;
}
if ($node_id !~ /^([-\w]+)$/) {
warn "tbadb_proxy::rpc_nodewait: Malformed node_id argument!\n";
send_error($data->{FID}, RPCERR_BADARGS, "Malformed node_id argument.");
exit 1;
}
$node_id = $1;
# Wait for a while for the node to appear in adb.
warn "tbadb_proxy::rpc_nodewait: waiting for $node_id to become available.\n";
if (!wait_for_android($node_id)) {
warn "tbadb_proxy::rpc_nodewait: failed while waiting for $node_id!\n";
send_error($data->{FID}, RPCERR_NODE_ERR, "node wait failed.");
exit 1;
}
# Report success.
warn "tbadb_proxy::rpc_nodewait: $node_id is now ready.\n";
SendRPCData(*STDOUT, EncodeResult($data->{FID}, { SUCCESS => 1 }));
return;
}
sub rpc_ping($) {
my ($data) = @_;
......@@ -891,6 +925,7 @@ sub reboot_android($) {
}
} else {
$state = `$ADB -s $serial get-state 2>&1`;
chomp $state;
if ($state ne "unknown") {
if (system("$ADB -s $serial reboot") != 0) {
warn "tbadb_proxy::reboot_android: adb failed to reboot $node_id!\n";
......@@ -920,6 +955,7 @@ sub wait_for_android($) {
my $stime = time();
while (1) {
my $state = `$ADB -s $serial get-state 2>&1`;
chomp $state;
if ($state eq "device") {
last;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment