Allow mining for the dns records for local hosts records

Change-Id: I8f2a66c6edafc425794d7cedc8b9217df7ee5951
diff --git a/README.rst b/README.rst
index 9a3a5a5..6f81c5d 100644
--- a/README.rst
+++ b/README.rst
@@ -986,10 +986,9 @@
 
 
 Configure global environment variables
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-Linux /etc/environment:
-``/etc/environment`` is for static system wide variable assignment after boot. Variable expansion is frequently not supported.
+Use ``/etc/environment`` for static system wide variable assignment after
+boot. Variable expansion is frequently not supported.
 
 .. code-block:: yaml
 
@@ -1021,11 +1020,10 @@
             - .local
 
 Configure profile.d scripts
-~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-Linux /etc/profile.d:
-The profile.d scripts are being sourced during .sh execution and support variable expansion in opposite to /etc/environment
-global settings in ``/etc/environment``.
+The profile.d scripts are being sourced during .sh execution and support
+variable expansion in opposite to /etc/environment global settings in
+``/etc/environment``.
 
 .. code-block:: yaml
 
@@ -1048,11 +1046,11 @@
             export NO_PROXY='.local'
 
 Linux with hosts
-~~~~~~~~~~~~~~~~
 
 Parameter purge_hosts will enforce whole /etc/hosts file, removing entries
 that are not defined in model except defaults for both IPv4 and IPv6 localhost
 and hostname + fqdn.
+
 It's good to use this option if you want to ensure /etc/hosts is always in a
 clean state however it's not enabled by default for safety.
 
@@ -1060,7 +1058,6 @@
 
     linux:
       network:
-        ...
         purge_hosts: true
         host:
           # No need to define this one if purge_hosts is true
@@ -1080,9 +1077,27 @@
             - node2.domain.com
             - service2.domain.com
 
+Linux with hosts collected from mine
+
+In this case all dns records defined within infrastrucuture will be passed to
+local hosts records or any DNS server. Only hosts with `grain` parameter to
+true will be propagated to the mine.
+
+.. code-block:: yaml
+
+    linux:
+      network:
+        purge_hosts: true
+        mine_dns_records: true
+        host:
+          node1:
+            address: 192.168.10.200
+            grain: true
+            names:
+            - node2.domain.com
+            - service2.domain.com
 
 Setup resolv.conf, nameservers, domain and search domains
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
 .. code-block:: yaml
 
@@ -1101,7 +1116,7 @@
           - timeout: 2
           - attempts: 2
 
-**setting custom TX queue length for tap interfaces**
+setting custom TX queue length for tap interfaces
 
 .. code-block:: yaml
 
@@ -1110,7 +1125,6 @@
         tap_custom_txqueuelen: 10000
 
 DPDK OVS interfaces
---------------------
 
 **DPDK OVS NIC**
 
diff --git a/linux/files/hosts b/linux/files/hosts
index 66951f0..64f4113 100644
--- a/linux/files/hosts
+++ b/linux/files/hosts
@@ -31,7 +31,7 @@
         'ip6-allhosts'
     ],
 } -%}
-{%- for name, host in network.host.iteritems() -%}
+{%- for name, host in host_dict.iteritems() -%}
 {%- do hosts.update({host.address: host.names}) -%}
 {%- endfor %}
 {% for address, entries in hosts|dictsort %}
diff --git a/linux/map.jinja b/linux/map.jinja
index 05af281..771da0f 100644
--- a/linux/map.jinja
+++ b/linux/map.jinja
@@ -108,6 +108,7 @@
            'host': 'none',
         },
         'host': {},
+        'mine_dns_records': False,
         'dhclient_config': '/etc/dhcp/dhclient.conf',
     },
     'Debian': {
@@ -123,6 +124,7 @@
            'host': 'none'
         },
         'host': {},
+        'mine_dns_records': False,
         'dhclient_config': '/etc/dhcp/dhclient.conf',
     },
     'RedHat': {
@@ -137,6 +139,7 @@
            'host': 'none'
         },
         'host': {},
+        'mine_dns_records': False,
         'dhclient_config': '/etc/dhcp/dhclient.conf',
     },
 }, grain='os_family', merge=salt['pillar.get']('linux:network')) %}
diff --git a/linux/meta/salt.yml b/linux/meta/salt.yml
index 0a07522..4cb2e8e 100644
--- a/linux/meta/salt.yml
+++ b/linux/meta/salt.yml
@@ -1,4 +1,4 @@
-{%- from "linux/map.jinja" import system with context -%}
+{%- from "linux/map.jinja" import system,network with context -%}
 orchestrate:
   system:
     priority: 30
@@ -18,3 +18,11 @@
       {%- endif %}
     {%- endfor %}
     {{ service_grains|yaml(False)|indent(4) }}
+  {%- set dns_records = [] %}
+  {%- for host_name, host in network.host.items() %}
+  {%- if host.get('grain', False) %}
+  {%- do dns_records.append(host.pop('grain')) %}
+  {%- endif %}
+  {%- endfor %}
+  dns_records: 
+    dns_records: {{ dns_records|yaml }}
diff --git a/linux/network/host.sls b/linux/network/host.sls
index caa7f3b..14bd837 100644
--- a/linux/network/host.sls
+++ b/linux/network/host.sls
@@ -1,6 +1,21 @@
 {%- from "linux/map.jinja" import network with context %}
 {%- if network.enabled %}
 
+{%- set host_dict = network.host %}
+
+{%- if network.mine_dns_records %}
+
+{%- for node_name, node_grains in salt['mine.get']('*', 'grains.items').iteritems() %}
+{%- if node_grains.get('dns_records', []) is iterable %}
+{%- for record in node_grains.get('dns_records', []) %}
+{%- set record_key = node_name ~ '-' ~ loop.index %}
+{%- do host_dict.update({ record_key: {'address': record.address, 'names': record.names} }) %}
+{%- endfor %}
+{%- endif %}
+{%- endfor %}
+
+{%- endif %}
+
 {%- if network.get('purge_hosts', false) %}
 
 linux_hosts:
@@ -8,10 +23,12 @@
     - name: /etc/hosts
     - source: salt://linux/files/hosts
     - template: jinja
+    - defaults:
+        host_dict: {{ host_dict|yaml }}
 
 {%- else %}
 
-{%- for name, host in network.host.iteritems() %}
+{%- for name, host in host_dict.iteritems() %}
 
 {%- if host.names is defined %}