Debian Codegraph @

lib/ansible/module_utils/facts/system/lsb.py

raw
1 # Collect facts related to LSB (Linux Standard Base)
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 import os
21 import typing as t
22
23 from ansible.module_utils.facts.utils import get_file_lines
24 from ansible.module_utils.facts.collector import BaseFactCollector
25
26
27 class LSBFactCollector(BaseFactCollector):
28 name = 'lsb'
29 _fact_ids = set() # type: t.Set[str]
30 STRIP_QUOTES = r'\'\"\\'
31
32 def _lsb_release_bin(self, lsb_path, module):
33 lsb_facts = {}
34
35 if not lsb_path:
36 return lsb_facts
37
38 rc, out, err = module.run_command([lsb_path, "-a"], errors='surrogate_then_replace')
39 if rc != 0:
40 return lsb_facts
41
42 for line in out.splitlines():
43 if len(line) < 1 or ':' not in line:
44 continue
45 value = line.split(':', 1)[1].strip()
46
47 if 'LSB Version:' in line:
48 lsb_facts['release'] = value
49 elif 'Distributor ID:' in line:
50 lsb_facts['id'] = value
51 elif 'Description:' in line:
52 lsb_facts['description'] = value
53 elif 'Release:' in line:
54 lsb_facts['release'] = value
55 elif 'Codename:' in line:
56 lsb_facts['codename'] = value
57
58 return lsb_facts
59
60 def _lsb_release_file(self, etc_lsb_release_location):
61 lsb_facts = {}
62
63 if not os.path.exists(etc_lsb_release_location):
64 return lsb_facts
65
66 for line in get_file_lines(etc_lsb_release_location):
67 value = line.split('=', 1)[1].strip()
68
69 if 'DISTRIB_ID' in line:
70 lsb_facts['id'] = value
71 elif 'DISTRIB_RELEASE' in line:
72 lsb_facts['release'] = value
73 elif 'DISTRIB_DESCRIPTION' in line:
74 lsb_facts['description'] = value
75 elif 'DISTRIB_CODENAME' in line:
76 lsb_facts['codename'] = value
77
78 return lsb_facts
79
80 def collect(self, module=None, collected_facts=None):
81 facts_dict = {}
82 lsb_facts = {}
83
84 if not module:
85 return facts_dict
86
87 lsb_path = module.get_bin_path('lsb_release')
88
89 # try the 'lsb_release' script first
90 if lsb_path:
91 lsb_facts = self._lsb_release_bin(lsb_path,
92 module=module)
93
94 # no lsb_release, try looking in /etc/lsb-release
95 if not lsb_facts:
96 lsb_facts = self._lsb_release_file('/etc/lsb-release')
97
98 if lsb_facts and 'release' in lsb_facts:
99 lsb_facts['major_release'] = lsb_facts['release'].split('.')[0]
100
101 for k, v in lsb_facts.items():
102 if v:
103 lsb_facts[k] = v.strip(LSBFactCollector.STRIP_QUOTES)
104
105 facts_dict['lsb'] = lsb_facts
106 return facts_dict