aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKubi <jean.gaoliang@huawei.com>2017-08-10 01:18:33 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-08-10 01:18:33 +0000
commit80c1d81ead5a5416e7e40f49ffc80a525e5737e0 (patch)
tree3a9b547f47b16764ba82d37ab62509f69cc6f667
parenta8626195f5e8530f989693a7d8dc73f97ebe115b (diff)
parent5331f681970c6c4a1cbca074a4b786963797b339 (diff)
Merge "bugfix: hosts should be clean if update_hosts twice"
-rw-r--r--api/resources/write_hosts.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/api/resources/write_hosts.py b/api/resources/write_hosts.py
index e4b69846b..a3025d3b0 100644
--- a/api/resources/write_hosts.py
+++ b/api/resources/write_hosts.py
@@ -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))