bugfix: hosts should be clean if update_hosts twice 89/38889/2
authorrexlee8776 <limingjiang@huawei.com>
Tue, 8 Aug 2017 08:09:41 +0000 (08:09 +0000)
committerrexlee8776 <limingjiang@huawei.com>
Wed, 9 Aug 2017 02:22:52 +0000 (02:22 +0000)
JIRA: YARDSTICK-774

Change-Id: Id610d43d236ab3a7d1aa4b8cfd230a40958fac38
Signed-off-by: rexlee8776 <limingjiang@huawei.com>
api/resources/write_hosts.py

index e4b6984..a3025d3 100644 (file)
@@ -13,11 +13,19 @@ import json
 
 
 def write_hosts(hosts_ip):
-    hosts_list = ('\n{} {}'.format(ip, host_name)
+
+    yardstick_flag = "# SUT hosts info for Yardstick"
+    hosts_list = ('\n{} {} {}'.format(ip, host_name, yardstick_flag)
                   for host_name, ip in hosts_ip.items())
-    with open("/etc/hosts", 'a') as f:
+
+    with open("/etc/hosts", 'r') as f:
+        origin_lines = [line for line in f if yardstick_flag not in line]
+
+    with open("/etc/hosts", 'w') as f:
+        f.writelines(origin_lines)
+        f.write(yardstick_flag)
         f.writelines(hosts_list)
-        f.write("\n")
+
 
 if __name__ == "__main__":
     write_hosts(json.load(sys.stdin))