Merge "Enable vnf/tg instantiate as blocking call."
[yardstick.git] / api / resources / write_hosts.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10
11 import sys
12 import json
13
14
15 def write_hosts(hosts_ip):
16
17     yardstick_flag = "# SUT hosts info for Yardstick"
18     hosts_list = ('\n{} {} {}'.format(ip, host_name, yardstick_flag)
19                   for host_name, ip in hosts_ip.items())
20
21     with open("/etc/hosts", 'r') as f:
22         origin_lines = [line for line in f if yardstick_flag not in line]
23
24     with open("/etc/hosts", 'w') as f:
25         f.writelines(origin_lines)
26         f.write(yardstick_flag)
27         f.writelines(hosts_list)
28
29
30 if __name__ == "__main__":
31     write_hosts(json.load(sys.stdin))