Debian Codegraph @

lib/jnpr/junos/facts/vmhost.py

raw
1 from jnpr.junos.exception import RpcError
2 import re
3 from lxml import etree
4
5
6 def _get_vmhost_version_information(device):
7 multi_re = False
8 try:
9 rsp = device.rpc.get_route_engine_information(normalize=True)
10 re_list = rsp.findall(".//route-engine")
11 if len(re_list) > 1:
12 multi_re = True
13 else:
14 multi_re = False
15 except RpcError as err:
16 raise RpcError()
17
18 if multi_re == True:
19 try:
20 return device.rpc.cli(
21 "show vmhost version invoke-on all-routing-engines",
22 format="xml",
23 normalize=True,
24 )
25 except RpcError as err:
26 raise RpcError()
27 else:
28 try:
29 return device.rpc.cli("show vmhost version", format="xml", normalize=True)
30 except RpcError as err:
31 raise RpcError()
32
33
34 def provides_facts():
35 """
36 Returns a dictionary keyed on the facts provided by this module. The value
37 of each key is the doc string describing the fact.
38 """
39 return {
40 "vmhost": "A boolean indicating if the device is vmhost.",
41 "vmhost_info": "A dictionary indicating vmhost RE partition JUNOS versions.",
42 }
43
44
45 def get_facts(device):
46 """
47 Gathers facts from the sysctl command.
48 """
49 SYSCTL_VMHOST_MODE = "sysctl -n hw.re.vmhost_mode"
50 vmhost = None
51 vmhost_info = {}
52 vm_ver_rsp = None
53 vmhost_current_root_set = None
54 vmhost_set_junos_version_set_p = None
55 vmhost_set_junos_version_set_b = None
56 re_name = None
57
58 if device.facts["_is_linux"]:
59 vmhost = False
60 else:
61 try:
62 rsp = device.rpc.request_shell_execute(command=SYSCTL_VMHOST_MODE)
63 if rsp.tag == "rpc-error":
64 raise RpcError()
65 result = re.sub("<[^<]+>", "", etree.tostring(rsp).decode())
66 if result.strip() == "1":
67 vmhost = True
68 else:
69 vmhost = False
70 except RpcError:
71 pass
72
73 if vmhost:
74 rsp = _get_vmhost_version_information(device)
75 # PR 1510446 fix for show vmhost version rpc supports form 22.2R3
76 if device.facts["version"] >= "22.2R3":
77 if rsp.tag == "vmhost-version-information":
78 vm_ver_rsp = [rsp]
79 else:
80 vm_ver_rsp = rsp.findall(".//vmhost-version-information")
81
82 for re_vm_ver_info in vm_ver_rsp:
83 re_name = re_vm_ver_info.findtext("../re-name", "re0")
84 vmhost_current_root_set = re_vm_ver_info.findtext("./current-root-set")
85 vmhost_set_junos_version_set_p = re_vm_ver_info.findtext(
86 "./set-disk-info[set-disk-name = 'set p']/set-junos-version"
87 )
88 vmhost_set_junos_version_set_b = re_vm_ver_info.findtext(
89 "./set-disk-info[set-disk-name = 'set b']/set-junos-version"
90 )
91
92 vmhost_info[re_name] = {
93 "vmhost_current_root_set": vmhost_current_root_set,
94 "vmhost_version_set_p": vmhost_set_junos_version_set_p,
95 "vmhost_version_set_b": vmhost_set_junos_version_set_b,
96 }
97 else:
98 pass
99
100 return {
101 "vmhost": vmhost,
102 "vmhost_info": vmhost_info,
103 }