Skip to content

Commit 172354d

Browse files
authored
[sonic-py-common] get_platform(): Refactor method of retrieving platform identifier (#5094)
Applications running in the host OS can read the platform identifier from /host/machine.conf. When loading configuration, sonic-config-engine *needs* to read the platform identifier from machine.conf, as it it responsible for populating the value in Config DB. When an application is running inside a Docker container, the machine.conf file is not accessible, as the /host directory is not mounted. So we need to retrieve the platform identifier from Config DB if get_platform() is called from inside a Docker container. However, we can't simply check that we're running in a Docker container because the host OS of the SONiC virtual switch is running inside a Docker container. So I refactored `get_platform()` to: 1. Read from the `PLATFORM` environment variable if it exists (which is defined in a virtual switch Docker container) 2. Read from machine.conf if possible (works in the host OS of a standard SONiC image, critical for sonic-config-engine at boot) 3. Read the value from Config DB (needed for Docker containers running in SONiC, as machine.conf is not accessible to them) - Also fix typo in daemon_base.py - Also changes to align `get_hwsku()` with `get_platform()`
1 parent 0a409ff commit 172354d

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

platform/vs/docker-sonic-vs/start.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# generate configuration
44

5-
PLATFORM=x86_64-kvm_x86_64-r0
6-
HWSKU=Force10-S6000
5+
export PLATFORM=x86_64-kvm_x86_64-r0
6+
export HWSKU=Force10-S6000
77

88
ln -sf /usr/share/sonic/device/$PLATFORM/$HWSKU /usr/share/sonic/hwsku
99

src/sonic-py-common/sonic_py_common/daemon_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def load_platform_util(self, module_name, class_name):
5656
platform_util = None
5757

5858
# Get path to platform and hwsku
59-
(platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku()
59+
(platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs()
6060

6161
try:
6262
module_file = "/".join([platform_path, "plugins", module_name + ".py"])

src/sonic-py-common/sonic_py_common/device_info.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,39 @@ def get_platform():
5858
Returns:
5959
A string containing the device's platform identifier
6060
"""
61+
62+
# If we are running in a virtual switch Docker container, the environment
63+
# variable 'PLATFORM' will be defined and will contain the platform
64+
# identifier.
65+
platform_env = os.getenv("PLATFORM")
66+
if platform_env:
67+
return platform_env
68+
69+
# If 'PLATFORM' env variable is not defined, we try to read the platform
70+
# identifier from machine.conf. This is critical for sonic-config-engine,
71+
# because it is responsible for populating this value in Config DB.
6172
machine_info = get_machine_info()
6273
if machine_info:
6374
if 'onie_platform' in machine_info:
6475
return machine_info['onie_platform']
6576
elif 'aboot_platform' in machine_info:
6677
return machine_info['aboot_platform']
6778

79+
# If we fail to read from machine.conf, we may be running inside a Docker
80+
# container in SONiC, where the /host directory is not mounted. In this
81+
# case the value should already be populated in Config DB so we finally
82+
# try reading it from there.
83+
try:
84+
config_db = ConfigDBConnector()
85+
config_db.connect()
86+
87+
metadata = config_db.get_table('DEVICE_METADATA')
88+
89+
if 'localhost' in metadata and 'platform' in metadata['localhost']:
90+
return metadata['localhost']['platform']
91+
except Exception:
92+
pass
93+
6894
return None
6995

7096

@@ -75,15 +101,18 @@ def get_hwsku():
75101
Returns:
76102
A string containing the device's hardware SKU identifier
77103
"""
78-
config_db = ConfigDBConnector()
79-
config_db.connect()
104+
try:
105+
config_db = ConfigDBConnector()
106+
config_db.connect()
80107

81-
metadata = config_db.get_table('DEVICE_METADATA')
108+
metadata = config_db.get_table('DEVICE_METADATA')
82109

83-
if 'localhost' in metadata and 'hwsku' in metadata['localhost']:
84-
return metadata['localhost']['hwsku']
110+
if 'localhost' in metadata and 'hwsku' in metadata['localhost']:
111+
return metadata['localhost']['hwsku']
112+
except Exception:
113+
pass
85114

86-
return ""
115+
return None
87116

88117

89118
def get_platform_and_hwsku():

0 commit comments

Comments
 (0)