1# This file is part of Ansible
2#
3# Ansible is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# Ansible is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
15
16from __future__ import (absolute_import, division, print_function)
17__metaclass__ = type
18
19import os
20
21from ansible.module_utils.facts.network.base import Network, NetworkCollector
22
23
24class HurdPfinetNetwork(Network):
25    """
26    This is a GNU Hurd specific subclass of Network. It use fsysopts to
27    get the ip address and support only pfinet.
28    """
29    platform = 'GNU'
30    _socket_dir = '/servers/socket/'
31
32    def assign_network_facts(self, network_facts, fsysopts_path, socket_path):
33        rc, out, err = self.module.run_command([fsysopts_path, '-L', socket_path])
34        # FIXME: build up a interfaces datastructure, then assign into network_facts
35        network_facts['interfaces'] = []
36        for i in out.split():
37            if '=' in i and i.startswith('--'):
38                k, v = i.split('=', 1)
39                # remove '--'
40                k = k[2:]
41                if k == 'interface':
42                    # remove /dev/ from /dev/eth0
43                    v = v[5:]
44                    network_facts['interfaces'].append(v)
45                    network_facts[v] = {
46                        'active': True,
47                        'device': v,
48                        'ipv4': {},
49                        'ipv6': [],
50                    }
51                    current_if = v
52                elif k == 'address':
53                    network_facts[current_if]['ipv4']['address'] = v
54                elif k == 'netmask':
55                    network_facts[current_if]['ipv4']['netmask'] = v
56                elif k == 'address6':
57                    address, prefix = v.split('/')
58                    network_facts[current_if]['ipv6'].append({
59                        'address': address,
60                        'prefix': prefix,
61                    })
62        return network_facts
63
64    def populate(self, collected_facts=None):
65        network_facts = {}
66
67        fsysopts_path = self.module.get_bin_path('fsysopts')
68        if fsysopts_path is None:
69            return network_facts
70
71        socket_path = None
72
73        for l in ('inet', 'inet6'):
74            link = os.path.join(self._socket_dir, l)
75            if os.path.exists(link):
76                socket_path = link
77                break
78
79        if socket_path is None:
80            return network_facts
81
82        return self.assign_network_facts(network_facts, fsysopts_path, socket_path)
83
84
85class HurdNetworkCollector(NetworkCollector):
86    _platform = 'GNU'
87    _fact_class = HurdPfinetNetwork
88