Skip to content

Commit

Permalink
[sonic-py-common] Add function to get hostname (sonic-net#5265)
Browse files Browse the repository at this point in the history
Added a function to get hostname. It get it from DEVICE_METADATA|localhost: hostname.
  • Loading branch information
renukamanavalan authored Aug 28, 2020
1 parent 4338d82 commit 5a166cc
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 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,25 @@
BACKEND_ASIC_SUB_ROLE = "BackEnd"


def get_localhost_info(field):
try:
config_db = ConfigDBConnector()
config_db.connect()

metadata = config_db.get_table('DEVICE_METADATA')

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

return None


def get_hostname():
return get_localhost_info('hostname')


def get_machine_info():
"""
Retreives data from the machine configuration file
Expand Down Expand Up @@ -80,18 +99,8 @@ def get_platform():
# 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

return get_localhost_info('platform')


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

metadata = config_db.get_table('DEVICE_METADATA')

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

return None
return get_localhost_info('hwsku')


def get_platform_and_hwsku():
Expand Down

0 comments on commit 5a166cc

Please sign in to comment.