Debian Codegraph @

lib/jnpr/junos/ofacts/swver.py

raw
1 import re
2 from jnpr.junos.facts.swver import version_info
3
4
5 def _get_swver(dev, facts):
6 # See if we're VC Capable
7 if facts["vc_capable"] is True:
8 try:
9 return dev.rpc.cli("show version all-members", format="xml")
10 except:
11 pass
12 try:
13 return dev.rpc.cli("show version invoke-on all-routing-engines", format="xml")
14 except:
15 return dev.rpc.get_software_information()
16
17
18 def facts_software_version(junos, facts):
19 """
20 The following facts are required:
21 facts['master']
22
23 The following facts are assigned:
24 facts['hostname']
25 facts['version']
26 facts['version_<RE#>'] for each RE in dual-RE, cluster or VC system
27 facts['version_info'] for master RE
28 """
29
30 x_swver = _get_swver(junos, facts)
31
32 if not facts.get("model"):
33 # try to extract the model from the version information
34 facts["model"] = x_swver.findtext(".//product-model")
35
36 # ------------------------------------------------------------------------
37 # extract the version information out of the RPC response
38 # ------------------------------------------------------------------------
39
40 f_master = facts.get("master", "RE0")
41
42 if x_swver.tag == "multi-routing-engine-results":
43 # we need to find/identify each of the routing-engine (CPU) versions.
44 if len(x_swver.xpath("./multi-routing-engine-item")) > 1:
45 facts["2RE"] = True
46 versions = []
47
48 if isinstance(f_master, list):
49 xpath = (
50 './multi-routing-engine-item[re-name="{}"]/software-'
51 "information/host-name".format(f_master[0].lower())
52 )
53 else:
54 xpath = (
55 './multi-routing-engine-item[re-name="{}"'
56 "]/software-information/host-name".format(f_master.lower())
57 )
58
59 facts["hostname"] = x_swver.findtext(xpath)
60 if facts["hostname"] is None:
61 # then there the re-name is not what we are expecting; we should
62 # handle this better, eh? For now, just assume there is one
63 # software-information element and take that host-name. @@@ hack.
64 facts["hostname"] = x_swver.findtext(".//software-information/host-name")
65
66 for re_sw in x_swver.xpath(".//software-information"):
67 re_name = re_sw.xpath("preceding-sibling::re-name")[0].text
68
69 # handle the cases where the "RE name" could be things like
70 # "FPC<n>" or "ndoe<n>", and normalize to "RE<n>".
71 re_name = re.sub(r"(\w+)(\d+)", "RE\\2", re_name)
72
73 # First try the <junos-version> tag present in >= 15.1
74 swinfo = re_sw.findtext("junos-version", default=None)
75 if not swinfo:
76 # For < 15.1, get version from the "junos" package.
77 pkginfo = re_sw.xpath(
78 "package-information[normalize-space(name)=" '"junos"]/comment'
79 )[0].text
80 try:
81 swinfo = re.findall(r"\[(.*)\]", pkginfo)[0]
82 except:
83 swinfo = "0.0I0.0"
84 versions.append((re_name.upper(), swinfo))
85
86 # now add the versions to the facts <dict>
87 for re_ver in versions:
88 facts["version_" + re_ver[0]] = re_ver[1]
89
90 if f_master is not None:
91 master = f_master[0] if isinstance(f_master, list) else f_master
92 if "version_" + master in facts:
93 facts["version"] = facts["version_" + master]
94 else:
95 facts["version"] = versions[0][1]
96 else:
97 facts["version"] = versions[0][1]
98
99 else:
100 # single-RE
101 facts["hostname"] = x_swver.findtext("host-name")
102
103 # First try the <junos-version> tag present in >= 15.1
104 swinfo = x_swver.findtext(".//junos-version", default=None)
105 if not swinfo:
106 # For < 15.1, get version from the "junos" package.
107 pkginfo = x_swver.xpath(
108 './/package-information[normalize-space(name)="junos"]/comment'
109 )[0].text
110 try:
111 swinfo = re.findall(r"\[(.*)\]", pkginfo)[0]
112 except:
113 swinfo = "0.0I0.0"
114 facts["version"] = swinfo
115
116 # ------------------------------------------------------------------------
117 # create a 'version_info' object based on the master version
118 # ------------------------------------------------------------------------
119
120 facts["version_info"] = version_info(facts["version"])