bugfix: hosts info is wrongly processed 41/47241/7
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Wed, 15 Nov 2017 09:46:44 +0000 (17:46 +0800)
committerLeo wang <grakiss.wanglei@huawei.com>
Mon, 20 Nov 2017 01:50:37 +0000 (01:50 +0000)
changes:
1. add usage example in hosts.yaml.sample
2. fix /etc/hosts from wrong format {'hostname': 'ip'} to
   the right 'ip hostnames'
3. fix --add-hosts from wrong usage --add-host {'hostname': 'ip'} to
   the right --add-host='hostnames':ip
4. support per ip mapping mutiple hostnames, which is also
   in line with /etc/hosts schema

JIRA: DOVETAIL-556

Change-Id: I92e8cb0fd3476b61f5c73a69e34c4a66aef51d6e
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
dovetail/container.py
dovetail/userconfig/hosts.yaml.sample
dovetail/utils/dovetail_utils.py
requirements.txt

index e8840de..e119109 100644 (file)
@@ -167,13 +167,15 @@ class Container(object):
                                          'hosts.yaml')
         if os.path.isfile(hosts_config_file):
             with open(hosts_config_file) as f:
-                hosts_info = yaml.safe_load(f)
-            if hosts_info['hosts_info']:
-                for host in hosts_info['hosts_info']:
-                    dt_utils.add_hosts_info(host)
-                    hosts_config += " --add-host "
-                    hosts_config += str(host)
-                cls.logger.debug('Get hosts info {}.'.format(host))
+                hosts_yaml = yaml.safe_load(f)
+            if hosts_yaml['hosts_info']:
+                for ip, hostnames in hosts_yaml['hosts_info'].iteritems():
+                    dt_utils.add_hosts_info(ip, hostnames)
+                    names_str = ' '.join(hostname for hostname in hostnames)
+                    hosts_config += ' --add-host=\'{}\':{} '.format(names_str,
+                                                                    ip)
+                    cls.logger.debug('Get hosts info {}:{}.'.format(ip,
+                                                                    names_str))
 
         config = ""
         if type.lower() == "functest":
index e4687df..45f8db3 100644 (file)
@@ -1,2 +1,7 @@
 ---
 hosts_info:
+  192.168.141.101:
+    - ha-vip
+  <ip>:
+    - <hostname1>
+    - <hostname2>
index 99d4448..87efa50 100644 (file)
@@ -19,6 +19,7 @@ import urllib2
 from datetime import datetime
 from distutils.version import LooseVersion
 import yaml
+import python_hosts
 
 from dovetail_config import DovetailConfig as dt_cfg
 
@@ -214,10 +215,13 @@ def check_docker_version(logger=None):
                      "Docker client should be updated to at least 1.12.3.")
 
 
-def add_hosts_info(hosts_info):
-    hosts_file = '/etc/hosts'
-    with open(hosts_file, 'a') as f:
-        f.write("{}\n".format(hosts_info))
+def add_hosts_info(ip, hostnames):
+    hosts = python_hosts.Hosts(path='/etc/hosts')
+    new_entry = python_hosts.HostsEntry(entry_type='ipv4',
+                                        address=ip,
+                                        names=hostnames)
+    hosts.add([new_entry])
+    hosts.write()
 
 
 def get_hardware_info(logger=None):
index 9eaaf3c..58da19e 100644 (file)
@@ -9,3 +9,4 @@ python-openstackclient==3.9.0
 requests==2.10.0
 six==1.10.0
 stevedore==1.20.0
+python-hosts==0.4.1
\ No newline at end of file