Debian Codegraph @

lib/ansible/module_utils/facts/network/hurd.py

raw
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
16 from __future__ import annotations
17
18 import os
19
20 from ansible.module_utils.facts.network.base import Network, NetworkCollector
21
22
23 class HurdPfinetNetwork(Network):
24 """
25 This is a GNU Hurd specific subclass of Network. It use fsysopts to
26 get the ip address and support only pfinet.
27 """
28 platform = 'GNU'
29 _socket_dir = '/servers/socket/'
30
31 def assign_network_facts(self, network_facts, fsysopts_path, socket_path):
32 rc, out, err = self.module.run_command([fsysopts_path, '-L', socket_path])
33 # FIXME: build up a interfaces datastructure, then assign into network_facts
34 network_facts['interfaces'] = []
35 for i in out.split():
36 if '=' in i and i.startswith('--'):
37 k, v = i.split('=', 1)
38 # remove '--'
39 k = k[2:]
40 if k == 'interface':
41 # remove /dev/ from /dev/eth0
42 v = v[5:]
43 network_facts['interfaces'].append(v)
44 network_facts[v] = {
45 'active': True,
46 'device': v,
47 'ipv4': {},
48 'ipv6': [],
49 }
50 current_if = v
51 elif k == 'address':
52 network_facts[current_if]['ipv4']['address'] = v
53 elif k == 'netmask':
54 network_facts[current_if]['ipv4']['netmask'] = v
55 elif k == 'address6':
56 address, prefix = v.split('/')
57 network_facts[current_if]['ipv6'].append({
58 'address': address,
59 'prefix': prefix,
60 })
61 return network_facts
62
63 def populate(self, collected_facts=None):
64 network_facts = {}
65
66 fsysopts_path = self.module.get_bin_path('fsysopts')
67 if fsysopts_path is None:
68 return network_facts
69
70 socket_path = None
71
72 for l in ('inet', 'inet6'):
73 link = os.path.join(self._socket_dir, l)
74 if os.path.exists(link):
75 socket_path = link
76 break
77
78 if socket_path is None:
79 return network_facts
80
81 return self.assign_network_facts(network_facts, fsysopts_path, socket_path)
82
83
84 class HurdNetworkCollector(NetworkCollector):
85 _platform = 'GNU'
86 _fact_class = HurdPfinetNetwork