From a9b2207bcf1cff6bb5e6f61035c0ba59c4b6b4f8 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 6 Sep 2019 16:34:19 -0700 Subject: [PATCH] ci: Allow threads to send ssh commands The goal here is to decouple the Guest instance from the ssh connection to send some commands to the guest. The reason being to allow ssh commands to be issued from a different thread, which can be useful to wait for the end of a command with a thread.join(). Signed-off-by: Sebastien Boeuf --- src/main.rs | 83 ++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5b7e16d06..7dba09b4d 100755 --- a/src/main.rs +++ b/src/main.rs @@ -577,6 +577,44 @@ mod tests { (child, virtiofsd_socket_path) } + fn ssh_command_ip(command: &str, ip: &str) -> Result { + let mut s = String::new(); + + let mut counter = 0; + loop { + match (|| -> Result<(), Error> { + let tcp = + TcpStream::connect(format!("{}:22", ip)).map_err(|_| Error::Connection)?; + let mut sess = Session::new().unwrap(); + sess.handshake(&tcp).map_err(|_| Error::Connection)?; + + sess.userauth_password("cloud", "cloud123") + .map_err(|_| Error::Authentication)?; + assert!(sess.authenticated()); + + let mut channel = sess.channel_session().map_err(|_| Error::Command)?; + channel.exec(command).map_err(|_| Error::Command)?; + + // Intentionally ignore these results here as their failure + // does not precipitate a repeat + let _ = channel.read_to_string(&mut s); + let _ = channel.close(); + let _ = channel.wait_close(); + Ok(()) + })() { + Ok(_) => break, + Err(e) => { + counter += 1; + if counter >= 6 { + return Err(e); + } + } + }; + thread::sleep(std::time::Duration::new(10 * counter, 0)); + } + Ok(s) + } + #[derive(Debug)] enum Error { Connection, @@ -636,54 +674,16 @@ mod tests { ) } - fn ssh_command_ip(&self, command: &str, ip: &str) -> Result { - let mut s = String::new(); - - let mut counter = 0; - loop { - match (|| -> Result<(), Error> { - let tcp = - TcpStream::connect(format!("{}:22", ip)).map_err(|_| Error::Connection)?; - let mut sess = Session::new().unwrap(); - sess.handshake(&tcp).map_err(|_| Error::Connection)?; - - sess.userauth_password("cloud", "cloud123") - .map_err(|_| Error::Authentication)?; - assert!(sess.authenticated()); - - let mut channel = sess.channel_session().map_err(|_| Error::Command)?; - channel.exec(command).map_err(|_| Error::Command)?; - - // Intentionally ignore these results here as their failure - // does not precipitate a repeat - let _ = channel.read_to_string(&mut s); - let _ = channel.close(); - let _ = channel.wait_close(); - Ok(()) - })() { - Ok(_) => break, - Err(e) => { - counter += 1; - if counter >= 6 { - return Err(e); - } - } - }; - thread::sleep(std::time::Duration::new(10 * counter, 0)); - } - Ok(s) - } - fn ssh_command(&self, command: &str) -> Result { - self.ssh_command_ip(command, &self.network.guest_ip) + ssh_command_ip(command, &self.network.guest_ip) } fn ssh_command_l1(&self, command: &str) -> Result { - self.ssh_command_ip(command, &self.network.guest_ip) + ssh_command_ip(command, &self.network.guest_ip) } fn ssh_command_l2(&self, command: &str) -> Result { - self.ssh_command_ip(command, &self.network.l2_guest_ip) + ssh_command_ip(command, &self.network.l2_guest_ip) } fn get_cpu_count(&self) -> Result { @@ -2225,5 +2225,4 @@ mod tests { Ok(()) }); } - }