@@ -102,58 +102,103 @@ def __init__(self, ansible_adhoc, hostname):
102102
103103class SonicHost (AnsibleHostBase ):
104104 """
105- @summary: Class for SONiC switch
105+ A remote host running SONiC.
106106
107- For running ansible module on the SONiC switch
107+ This type of host contains information about the SONiC device (device info, services, etc.),
108+ and also provides the ability to run Ansible modules on the SONiC device.
108109 """
110+
111+ # TODO: Because people are editing this variable in a bunch of places it should probably be
112+ # revised to "DEFAULT_CRITICAL_SERVICES", and we should make critical_services a property of
113+ # SonicHost
109114 CRITICAL_SERVICES = ["swss" , "syncd" , "database" , "teamd" , "bgp" , "pmon" , "lldp" , "snmp" ]
110115
111- def __init__ (self , ansible_adhoc , hostname , gather_facts = False ):
116+ def __init__ (self , ansible_adhoc , hostname ):
112117 AnsibleHostBase .__init__ (self , ansible_adhoc , hostname )
113- if gather_facts :
114- self .gather_facts ()
118+ self ._facts = self ._gather_facts ()
119+ self ._os_version = self ._get_os_version ()
120+
121+ if self .facts ["num_npu" ] > 1 :
122+ self ._update_critical_services_for_multi_npu ()
115123
116- def _get_critical_services_for_multi_npu (self ):
124+ @property
125+ def facts (self ):
117126 """
118- Update the critical_services with the service names for multi-npu platforms
127+ Platform information for this SONiC device.
128+
129+ Returns:
130+ dict: A dictionary containing the device platform information.
131+
132+ For example:
133+ {
134+ "platform": "x86_64-arista_7050_qx32s",
135+ "hwsku": "Arista-7050-QX-32S",
136+ "asic_type": "broadcom",
137+ "num_npu": 1
138+ }
119139 """
120- m_service = []
121- for service in self .CRITICAL_SERVICES :
122- for npu in self .facts ["num_npu" ]:
123- npu_service = service + npu
124- m_service .insert (npu , npu_service )
125- self .CRITICAL_SERVICES = m_service
126- print self .CRITICAL_SERVICES
127140
128- def _get_npu_info (self ):
141+ return self ._facts
142+
143+ @property
144+ def os_version (self ):
145+ """
146+ The OS version running on this SONiC device.
147+
148+ Returns:
149+ str: The SONiC OS version (e.g. "20181130.31")
150+ """
151+
152+ return self ._os_version
153+
154+ def _gather_facts (self ):
155+ """
156+ Gather facts about the platform for this SONiC device.
157+ """
158+
159+ facts = dict ()
160+ facts .update (self ._get_platform_info ())
161+ facts ["num_npu" ] = self ._get_npu_count (facts ["platform" ])
162+
163+ logging .debug ("Gathered SonicHost facts: %s" % json .dumps (facts ))
164+ return facts
165+
166+ def _get_npu_count (self , platform ):
129167 """
130- Check if the DUT is multi-npu platfrom and store the number of npus in the facts
168+ Gets the number of npus for this device.
131169 """
132- asic_conf_file_path = os .path .join ('/usr/share/sonic/device' , self .facts ["platform" ], 'asic.conf' )
170+
171+ asic_conf_file_path = os .path .join ("/usr/share/sonic/device" , platform , "asic.conf" )
133172 try :
134- output = self .shell ('cat %s' % asic_conf_file_path )["stdout_lines" ]
135- print output
173+ output = self .shell ("cat {}" .format (asic_conf_file_path ))["stdout_lines" ]
174+ logging .debug (output )
175+
136176 for line in output :
137- num_npu = line .split ("=" ,1 )[1 ].strip ()
138- print "num_npu = {}" .format (num_npu )
139- self .facts ["num_npu" ] = int (num_npu )
177+ num_npu = line .split ("=" , 1 )[1 ].strip ()
178+
179+ logging .debug ("num_npu = %s" % num_npu )
180+ return int (num_npu )
140181 except :
141- self . facts [ "num_npu" ] = 1
182+ return 1
142183
143- if self .facts ["num_npu" ] > 1 :
144- self ._get_critical_services_for_multi_npu
184+ def _update_critical_services_for_multi_npu (self ):
185+ """
186+ Updates the critical services for this device with the services for multi-npu platforms.
187+ """
145188
189+ m_service = []
190+ for service in self .CRITICAL_SERVICES :
191+ for npu in self .facts ["num_npu" ]:
192+ npu_service = service + npu
193+ m_service .insert (npu , npu_service )
194+ self .CRITICAL_SERVICES = m_service
195+ logging .debug (self .CRITICAL_SERVICES )
146196
147- def get_platform_info (self ):
197+ def _get_platform_info (self ):
148198 """
149- @summary: Get the platform information of the SONiC switch.
150- @return: Returns a dictionary containing preperties of the platform information, for example:
151- {
152- "platform": "",
153- "hwsku": "",
154- "asic_type": ""
155- }
199+ Gets platform information about this SONiC device.
156200 """
201+
157202 platform_info = self .command ("show platform summary" )["stdout_lines" ]
158203 result = {}
159204 for line in platform_info :
@@ -165,15 +210,13 @@ def get_platform_info(self):
165210 result ["asic_type" ] = line .split (":" )[1 ].strip ()
166211 return result
167212
168- def gather_facts (self ):
213+ def _get_os_version (self ):
169214 """
170- @summary: Gather facts of the SONiC switch and store the gathered facts in the dict type 'facts' attribute .
215+ Gets the SONiC OS version that is running on this device .
171216 """
172- self .facts = {}
173- platform_info = self .get_platform_info ()
174- self .facts .update (platform_info )
175- self ._get_npu_info ()
176- logging .debug ("SonicHost facts: %s" % json .dumps (self .facts ))
217+
218+ output = self .command ("sonic-cfggen -y /etc/sonic/sonic_version.yml -v build_version" )
219+ return output ["stdout_lines" ][0 ].strip ()
177220
178221 def get_service_props (self , service , props = ["ActiveState" , "SubState" ]):
179222 """
@@ -424,9 +467,6 @@ def get_image_info(self):
424467 ret ['installed_list' ] = images
425468 return ret
426469
427- def get_asic_type (self ):
428- return self .facts ["asic_type" ]
429-
430470 def shutdown (self , ifname ):
431471 """
432472 Shutdown interface specified by ifname
@@ -557,16 +597,6 @@ def check_bgp_session_nsf(self, neighbor_ip):
557597 return True
558598 return False
559599
560- def get_version (self ):
561- """
562- Gets the SONiC version this device is running.
563-
564- Returns:
565- str: the firmware version number (e.g. 20181130.31)
566- """
567- output = dut .command ("sonic-cfggen -y /etc/sonic/sonic_version.yml -v build_version" )
568- return output ["stdout_lines" ][0 ].strip ()
569-
570600class EosHost (AnsibleHostBase ):
571601 """
572602 @summary: Class for Eos switch
0 commit comments