Skip to content

Commit

Permalink
[sonic-py-common] get_platform(): Refactor method of retrieving platf…
Browse files Browse the repository at this point in the history
…orm identifier (sonic-net#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()`
  • Loading branch information
jleveque authored and santhosh-kt committed Feb 25, 2021
1 parent 7f8190b commit 5921812
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
4 changes: 2 additions & 2 deletions platform/vs/docker-sonic-vs/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# generate configuration

PLATFORM=x86_64-kvm_x86_64-r0
HWSKU=Force10-S6000
export PLATFORM=x86_64-kvm_x86_64-r0
export HWSKU=Force10-S6000

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

Expand Down
2 changes: 1 addition & 1 deletion src/sonic-py-common/sonic_py_common/daemon_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def load_platform_util(self, module_name, class_name):
platform_util = None

# Get path to platform and hwsku
(platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku()
(platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs()

try:
module_file = "/".join([platform_path, "plugins", module_name + ".py"])
Expand Down
41 changes: 35 additions & 6 deletions src/sonic-py-common/sonic_py_common/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,39 @@ def get_platform():
Returns:
A string containing the device's platform identifier
"""

# If we are running in a virtual switch Docker container, the environment
# variable 'PLATFORM' will be defined and will contain the platform
# identifier.
platform_env = os.getenv("PLATFORM")
if platform_env:
return platform_env

# If 'PLATFORM' env variable is not defined, we try to read the platform
# identifier from machine.conf. This is critical for sonic-config-engine,
# because it is responsible for populating this value in Config DB.
machine_info = get_machine_info()
if machine_info:
if 'onie_platform' in machine_info:
return machine_info['onie_platform']
elif 'aboot_platform' in machine_info:
return machine_info['aboot_platform']

# If we fail to read from machine.conf, we may be running inside a Docker
# container in SONiC, where the /host directory is not mounted. In this
# case the value should already be populated in Config DB so we finally
# try reading it from there.
try:
config_db = ConfigDBConnector()
config_db.connect()

metadata = config_db.get_table('DEVICE_METADATA')

if 'localhost' in metadata and 'platform' in metadata['localhost']:
return metadata['localhost']['platform']
except Exception:
pass

return None


Expand All @@ -75,15 +101,18 @@ def get_hwsku():
Returns:
A string containing the device's hardware SKU identifier
"""
config_db = ConfigDBConnector()
config_db.connect()
try:
config_db = ConfigDBConnector()
config_db.connect()

metadata = config_db.get_table('DEVICE_METADATA')
metadata = config_db.get_table('DEVICE_METADATA')

if 'localhost' in metadata and 'hwsku' in metadata['localhost']:
return metadata['localhost']['hwsku']
if 'localhost' in metadata and 'hwsku' in metadata['localhost']:
return metadata['localhost']['hwsku']
except Exception:
pass

return ""
return None


def get_platform_and_hwsku():
Expand Down

0 comments on commit 5921812

Please sign in to comment.