[autotest] Pull a upstream fix about known hosts file failure.

Need to be landed/pushed before cl 64331. Pull a upstream fix.

The host_file got by os.tmpfile() is not available for "ssh -o
UserKnownHostsFile " on ubuntu 11.10 and will have the error "Failed to
add the host to the list of known hosts (/dev/fd/11)", so the patch just
replace it with tempfile.mkstemp()

The original idea of not using '/dev/null' is that using it will
lead to too many messages like 'Warning: Permanently added
'locathost' (RSA) to the list of known hosts'.

This fix is needed when SiteHost is no longer be the parent class of
AbstractHost after 64331 is landed.
Code that creates SSHHost directly without mixing it with
SiteHost will hit this problem.

BUG=None
Test=Manually create a SSHHost (without SiteHost being a parent),
run a command via |run| on the host. Make sure it can ssh to the host
and won't hit the error. Also run dummy suite locally.

Change-Id: Id8d5c44f63a572ea482afeae074cd43cf2de9ec9
Reviewed-on: https://gerrit.chromium.org/gerrit/66136
Reviewed-by: Richard Barnette <[email protected]>
Tested-by: Fang Deng <[email protected]>
Commit-Queue: Fang Deng <[email protected]>
diff --git a/server/hosts/abstract_ssh.py b/server/hosts/abstract_ssh.py
index 0837078..0df6b41 100644
--- a/server/hosts/abstract_ssh.py
+++ b/server/hosts/abstract_ssh.py
@@ -1,4 +1,4 @@
-import os, time, types, socket, shutil, glob, logging, traceback
+import os, time, types, socket, shutil, glob, logging, traceback, tempfile
 from autotest_lib.client.common_lib import autotemp, error, logging_manager
 from autotest_lib.server import utils, autotest
 from autotest_lib.server.hosts import remote
@@ -50,9 +50,7 @@
         self.port = port
         self.password = password
         self._use_rsync = None
-        self.known_hosts_file = os.tmpfile()
-        known_hosts_fd = self.known_hosts_file.fileno()
-        self.known_hosts_fd = '/dev/fd/%s' % known_hosts_fd
+        self.known_hosts_file = tempfile.mkstemp()[1]
 
         """
         Master SSH connection background job, socket temp directory and socket
@@ -106,7 +104,7 @@
         """
         ssh_cmd = make_ssh_command(user=self.user, port=self.port,
                                    opts=self.master_ssh_option,
-                                   hosts_file=self.known_hosts_fd)
+                                   hosts_file=self.known_hosts_file)
         if delete_dest:
             delete_flag = "--delete"
         else:
@@ -127,7 +125,7 @@
         """
         base_cmd = make_ssh_command(user=self.user, port=self.port,
                                     opts=self.master_ssh_option,
-                                    hosts_file=self.known_hosts_fd)
+                                    hosts_file=self.known_hosts_file)
 
         return '%s %s "%s"' % (base_cmd, self.hostname, utils.sh_escape(cmd))
 
@@ -139,7 +137,7 @@
         """
         command = ("scp -rq %s -o StrictHostKeyChecking=no "
                    "-o UserKnownHostsFile=%s -P %d %s '%s'")
-        return command % (self.master_ssh_option, self.known_hosts_fd,
+        return command % (self.master_ssh_option, self.known_hosts_file,
                           self.port, " ".join(sources), dest)
 
 
@@ -589,7 +587,7 @@
     def close(self):
         super(AbstractSSHHost, self).close()
         self._cleanup_master_ssh()
-        self.known_hosts_file.close()
+        os.remove(self.known_hosts_file)
 
 
     def _cleanup_master_ssh(self):
@@ -649,7 +647,7 @@
         reduce the spam in the logs.
         """
         logging.info("Clearing known hosts for host '%s', file '%s'.",
-                     self.hostname, self.known_hosts_fd)
+                     self.hostname, self.known_hosts_file)
         # Clear out the file by opening it for writing and then closing.
-        fh = open(self.known_hosts_fd, "w")
+        fh = open(self.known_hosts_file, "w")
         fh.close()