Debian Codegraph @

lib/jnpr/junos/facts/get_software_information.py

raw
1 import re
2 import logging
3
4 from jnpr.junos.facts.swver import version_info
5 from jnpr.junos.exception import RpcError
6 from jnpr.junos import jxml as JXML
7
8 logger = logging.getLogger("jnpr.junos.facts.get_software_information")
9
10
11 def _get_software_information(device):
12 # See if device understands "invoke-on all-routing-engines"
13 try:
14 return device.rpc.cli(
15 "show version invoke-on all-routing-engines", format="xml", normalize=True
16 )
17 except RpcError as err:
18 # See if device runs on a linux kernel
19 if device.facts["_is_linux"]:
20 sw_info_all = device.rpc.get_software_information(
21 normalize=True, node="all"
22 )
23 sw_info_re0 = device.rpc.get_software_information(
24 normalize=True, node="re0"
25 )
26 sw_info_re1 = device.rpc.get_software_information(
27 normalize=True, node="re1"
28 )
29 re0_hostname = sw_info_re0.findtext("./host-name")
30 re1_hostname = sw_info_re1.findtext("./host-name")
31 for current_hostname in sw_info_all.findall(
32 "./multi-routing-engine-result/software-information/host-name"
33 ):
34 if current_hostname.text == re0_hostname:
35 current_hostname.getparent().getparent().append(
36 JXML("<re-name>re0</re-name>")
37 )
38 elif current_hostname.text == re1_hostname:
39 current_hostname.getparent().getparent().append(
40 JXML("<re-name>re1</re-name>")
41 )
42 return sw_info_all
43 # See if device is VC Capable
44 if device.facts["vc_capable"] is True:
45 try:
46 return device.rpc.cli(
47 "show version all-members", format="xml", normalize=True
48 )
49 except Exception:
50 pass
51 # check if rpc-reply got 2 child element, one rpc-error and another
52 # software information
53 elif (
54 hasattr(err, "rpc_error")
55 and err.rpc_error is not None
56 and "Could not connect to " in err.rpc_error.get("message")
57 ):
58 logger.debug(err.rpc_error.get("message"))
59 # getparent as rpc-reply got software-information in 2nd element
60 # and dev.cli return just 1st element.
61 rsp = err.xml.getparent()
62 rsp = JXML.remove_namespaces(rsp)
63 if rsp.xpath(".//software-information"):
64 return rsp
65 try:
66 # JDM for Junos Node Slicing
67 return device.rpc.get_software_information(
68 all_servers=True, format="xml", normalize=True
69 )
70 except Exception:
71 pass
72 try:
73 sw_info = device.rpc.get_software_information(normalize=True)
74 except Exception:
75 sw_info = True
76 try:
77 if sw_info is True:
78 # Possibly an NFX which requires 'local' and 'detail' args.
79 sw_info = device.rpc.get_software_information(
80 local=True, detail=True, normalize=True
81 )
82 return sw_info
83 except Exception:
84 pass
85
86
87 def provides_facts():
88 """
89 Returns a dictionary keyed on the facts provided by this module. The value
90 of each key is the doc string describing the fact.
91 """
92 return {
93 "junos_info": "A two-level dictionary providing Junos software "
94 "version information for each RE in the system. "
95 "The first-level key is the name of the RE. The "
96 "second level key is 'text' for the version as a "
97 "string and 'object' for the version as a "
98 "version_info object.",
99 "hostname": "A string containing the hostname of the current "
100 "Routing Engine.",
101 "hostname_info": "A dictionary keyed on Routing Engine name. The "
102 "value of each key is the hostname of the "
103 "Routing Engine.",
104 "model": "An uppercase string containing the model of the chassis "
105 "in which the current Routing Engine resides.",
106 "model_info": "A dictionary keyed on Routing Engine name. The "
107 "value of each key is an uppercase string "
108 "containing the model of the chassis in which the "
109 "Routing Engine resides.",
110 "version": "A string containing the Junos version of the current "
111 "Routing Engine.",
112 "version_info": "The Junos version of the current Routing Engine "
113 "as a version_info object.",
114 "version_RE0": "A string containing the Junos version of the "
115 "RE in slot 0. (Assuming the system contains an "
116 "RE0.)",
117 "version_RE1": "A string containing the Junos version of the "
118 "RE in slot 1. (Assuming the system contains an "
119 "RE1)",
120 }
121
122
123 def get_facts(device):
124 """
125 Gathers facts from the <get-software-information/> RPC.
126 """
127 junos_info = None
128 hostname = None
129 hostname_info = None
130 model = None
131 model_info = None
132 version = None
133 ver_info = None
134 version_RE0 = None
135 version_RE1 = None
136
137 rsp = _get_software_information(device)
138
139 if rsp.tag == "software-information":
140 si_rsp = [rsp]
141 else:
142 si_rsp = rsp.findall(".//software-information")
143
144 for re_sw_info in si_rsp:
145 re_name = re_sw_info.findtext("../re-name", "re0")
146 re_model = re_sw_info.findtext("./product-model")
147 re_hostname = re_sw_info.findtext("./host-name")
148 # First try the <junos-version> tag present in >= 15.1
149 re_version = re_sw_info.findtext("./junos-version")
150 if re_version is None:
151 # For < 15.1, get version from the "junos" package.
152 try:
153 re_pkg_info = re_sw_info.findtext(
154 './package-information[name="junos"]/comment'
155 )
156 if re_pkg_info is not None:
157 re_version = re.findall(r"\[(.*)\]", re_pkg_info)[0]
158 else:
159 # Junos Node Slicing JDM case
160 re_pkg_info = re_sw_info.findtext(
161 './package-information[name="JUNOS version"]/comment'
162 )
163 if re_pkg_info is not None:
164 # In this case, re_pkg_info might look like this:
165 # JUNOS version : 17.4-20170703_dev_common.0-secure
166 # Match everything from last space until the end.
167 re_version = re.findall(r".*\s+(.*)", re_pkg_info)[0]
168 else:
169 # NFX JDM case
170 re_pkg_info = re_sw_info.findtext(
171 './version-information[component="MGD"]/release'
172 )
173 re_version = re.findall(r"(.*\d+)", re_pkg_info)[0]
174 except Exception:
175 re_version = None
176 if model_info is None and re_model is not None:
177 model_info = {}
178 if re_model is not None:
179 model_info[re_name] = re_model.upper()
180 if hostname_info is None and re_hostname is not None:
181 hostname_info = {}
182 if re_hostname is not None:
183 hostname_info[re_name] = re_hostname
184 if junos_info is None and re_version is not None:
185 junos_info = {}
186 if re_version is not None:
187 junos_info[re_name] = {
188 "text": re_version,
189 "object": version_info(re_version),
190 }
191
192 # Check to see if re_name is the RE we are currently connected to.
193 # There are at least five cases to handle.
194 this_re = False
195 # 1) this device doesn't support the current_re fact and there's only
196 # one RE.
197 if device.facts["current_re"] is None and len(si_rsp) == 1:
198 this_re = True
199 # 2) re_name is in the current_re fact. The easy case.
200 elif re_name in device.facts["current_re"]:
201 this_re = True
202 # 3) Some single-RE devices (discovered on EX2200 running 11.4R1)
203 # don't include 'reX' in the current_re list. Check for this
204 # condition and still set default hostname, model, and version
205 elif (
206 re_name == "re0"
207 and "re1" not in device.facts["current_re"]
208 and "master" in device.facts["current_re"]
209 ):
210 this_re = True
211 # 4) For an lcc in a TX(P) re_name is 're0' or 're1', but the
212 # current_re fact is ['lcc1-re0', 'member1-re0', ...]. Check to see
213 # if any iri_name endswith the re_name.
214 if this_re is False:
215 for iri_name in device.facts["current_re"]:
216 if iri_name.endswith("-" + re_name):
217 this_re = True
218 break
219 # 5) For an MX configured with Node Virtualization then, re_name is
220 # bsys-reX, but the iri_name is still just reX.
221 if this_re is False:
222 for iri_name in device.facts["current_re"]:
223 if re_name == "bsys-" + iri_name:
224 this_re = True
225 break
226 # Set hostname, model, and version facts if we've found the RE to
227 # which we are currently connected.
228 if this_re is True:
229 if hostname is None:
230 hostname = re_hostname
231 if model is None:
232 model = re_model.upper()
233 if version is None:
234 version = re_version
235
236 if version is None:
237 version = "0.0I0.0"
238 ver_info = version_info(version)
239 if junos_info is not None:
240 if "re0" in junos_info:
241 version_RE0 = junos_info["re0"]["text"]
242 elif "node0" in junos_info:
243 version_RE0 = junos_info["node0"]["text"]
244 elif "bsys-re0" in junos_info:
245 if len(junos_info) > 4:
246 version_RE0 = junos_info["bsys-re0"]["text"]
247 else:
248 for key in junos_info.keys():
249 if key.startswith("gnf") and key.endswith("re0"):
250 version_RE0 = junos_info[key]["text"]
251 elif "server0" in junos_info:
252 version_RE0 = junos_info["server0"]["text"]
253 if "re1" in junos_info:
254 version_RE1 = junos_info["re1"]["text"]
255 elif "node1" in junos_info:
256 version_RE1 = junos_info["node1"]["text"]
257 elif "bsys-re1" in junos_info:
258 if len(junos_info) > 4:
259 version_RE1 = junos_info["bsys-re1"]["text"]
260 else:
261 for key in junos_info.keys():
262 if key.startswith("gnf") and key.endswith("re1"):
263 version_RE1 = junos_info[key]["text"]
264 elif "server1" in junos_info:
265 version_RE1 = junos_info["server1"]["text"]
266
267 return {
268 "junos_info": junos_info,
269 "hostname": hostname,
270 "hostname_info": hostname_info,
271 "model": model,
272 "model_info": model_info,
273 "version": version,
274 "version_info": ver_info,
275 "version_RE0": version_RE0,
276 "version_RE1": version_RE1,
277 }