Skip to content

[sonic-py-common] get_platform(): Refactor method of retrieving platform identifier #5094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions platform/vs/docker-sonic-vs/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,8 @@ RUN rm /etc/frr/frr.conf
# Create /var/warmboot/teamd folder for teammgrd
RUN mkdir -p /var/warmboot/teamd

# Set an environment variable which applications can use to
# determine that the environment is virtualized
ENV SONIC_DOCKER_VIRTUAL_SWITCH=1

ENTRYPOINT ["/usr/bin/supervisord"]
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
45 changes: 39 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 @@ -29,6 +29,23 @@
BACKEND_ASIC_SUB_ROLE = "BackEnd"


def in_docker_container():
"""
Returns:
True if running in a Docker container on a SONiC device, else False
Note that we also check if we are running in a SONiC virtual switch,
which itself is in a Docker container. In this case, we are technically
in the 'host OS', even though it is containerized.
"""
with open("/proc/1/cgroup", "r") as f:
contents = f.read()
if "docker" in contents or "kubepods" in contents:
if os.getenv("SONIC_DOCKER_VIRTUAL_SWITCH", "0") == "0":
return True

return False


def get_machine_info():
"""
Retreives data from the machine configuration file
Expand Down Expand Up @@ -58,12 +75,28 @@ def get_platform():
Returns:
A string containing the device's platform identifier
"""
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 are running in a container, we won't have access
# to machine.conf, so instead we try reading the platform
# identifier from Config DB
if in_docker_container():
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
else:
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']

return None

Expand Down