[platform] Add Support For Environment Variable File (#5010)
* [platform] Add Support For Environment Variable This PR adds the ability to read environment file from /etc/sonic. the file contains immutable SONiC config attributes such as platform, hwsku, version, device_type. The aim is to minimize calls being made into sonic-cfggen during boot time. singed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
This commit is contained in:
parent
47ca5e2fa0
commit
7872b4e196
@ -39,7 +39,10 @@ rm -f /ready
|
|||||||
# Restore FDB and ARP table ASAP
|
# Restore FDB and ARP table ASAP
|
||||||
fast_reboot
|
fast_reboot
|
||||||
|
|
||||||
HWSKU=`sonic-cfggen -d -v "DEVICE_METADATA['localhost']['hwsku']"`
|
# read SONiC immutable variables
|
||||||
|
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
|
||||||
|
|
||||||
|
HWSKU=${HWSKU:-`sonic-cfggen -d -v "DEVICE_METADATA['localhost']['hwsku']"`}
|
||||||
|
|
||||||
# Don't load json config if system warm start or
|
# Don't load json config if system warm start or
|
||||||
# swss docker warm start is enabled, the data already exists in appDB.
|
# swss docker warm start is enabled, the data already exists in appDB.
|
||||||
|
@ -156,7 +156,7 @@ start() {
|
|||||||
BOOT_TYPE=`getBootType`
|
BOOT_TYPE=`getBootType`
|
||||||
|
|
||||||
# Obtain our platform as we will mount directories with these names in each docker
|
# Obtain our platform as we will mount directories with these names in each docker
|
||||||
PLATFORM=`$SONIC_CFGGEN -H -v DEVICE_METADATA.localhost.platform`
|
PLATFORM=${PLATFORM:-`$SONIC_CFGGEN -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
|
|
||||||
# Parse the device specific asic conf file, if it exists
|
# Parse the device specific asic conf file, if it exists
|
||||||
ASIC_CONF=/usr/share/sonic/device/$PLATFORM/asic.conf
|
ASIC_CONF=/usr/share/sonic/device/$PLATFORM/asic.conf
|
||||||
@ -169,7 +169,7 @@ start() {
|
|||||||
HWSKU=""
|
HWSKU=""
|
||||||
{%- else %}
|
{%- else %}
|
||||||
# Obtain our HWSKU as we will mount directories with these names in each docker
|
# Obtain our HWSKU as we will mount directories with these names in each docker
|
||||||
HWSKU=`$SONIC_CFGGEN -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`
|
HWSKU=${HWSKU:-`$SONIC_CFGGEN -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
|
|
||||||
DOCKERCHECK=`docker inspect --type container {{docker_container_name}}$DEV 2>/dev/null`
|
DOCKERCHECK=`docker inspect --type container {{docker_container_name}}$DEV 2>/dev/null`
|
||||||
@ -362,6 +362,9 @@ if [ "$DEV" ]; then
|
|||||||
SONIC_DB_CLI="sonic-db-cli"
|
SONIC_DB_CLI="sonic-db-cli"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# read SONiC immutable variables
|
||||||
|
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start|wait|stop)
|
start|wait|stop)
|
||||||
$1
|
$1
|
||||||
|
@ -208,7 +208,7 @@ load_config()
|
|||||||
generate_config()
|
generate_config()
|
||||||
{
|
{
|
||||||
# Collect all information needed to generate configuration
|
# Collect all information needed to generate configuration
|
||||||
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
|
PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`)
|
PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`)
|
||||||
HW_KEY=${PRESET[0]}
|
HW_KEY=${PRESET[0]}
|
||||||
DEFAULT_PRESET=${PRESET[1]}
|
DEFAULT_PRESET=${PRESET[1]}
|
||||||
@ -403,8 +403,11 @@ boot_config()
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# read SONiC immutable variables
|
||||||
|
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
|
||||||
|
|
||||||
### Execution starts here ###
|
### Execution starts here ###
|
||||||
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
|
PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
# Parse the device specific asic conf file, if it exists
|
# Parse the device specific asic conf file, if it exists
|
||||||
ASIC_CONF=/usr/share/sonic/device/$PLATFORM/asic.conf
|
ASIC_CONF=/usr/share/sonic/device/$PLATFORM/asic.conf
|
||||||
if [[ -f "$ASIC_CONF" ]]; then
|
if [[ -f "$ASIC_CONF" ]]; then
|
||||||
|
@ -11,9 +11,17 @@
|
|||||||
#
|
#
|
||||||
# By default this script does nothing.
|
# By default this script does nothing.
|
||||||
|
|
||||||
SONIC_VERSION=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v build_version)
|
SONIC_VERSION=$(cat /etc/sonic/sonic_version.yml | grep "build_version" | sed -e "s/build_version: //g;s/'//g")
|
||||||
FIRST_BOOT_FILE="/host/image-${SONIC_VERSION}/platform/firsttime"
|
FIRST_BOOT_FILE="/host/image-${SONIC_VERSION}/platform/firsttime"
|
||||||
|
|
||||||
|
# Move sonic-environment to /etc/sonic
|
||||||
|
SONIC_CONFIG_DIR="/host/image-${SONIC_VERSION}/sonic-config"
|
||||||
|
SONIC_ENV_FILE=${SONIC_CONFIG_DIR}/sonic-environment
|
||||||
|
if [ -d ${SONIC_CONFIG_DIR} -a -f ${SONIC_ENV_FILE} ]; then
|
||||||
|
echo "moving file ${SONIC_ENV_FILE} to /etc/sonic" 1>&2
|
||||||
|
mv ${SONIC_ENV_FILE} /etc/sonic
|
||||||
|
fi
|
||||||
|
|
||||||
# In case the unit is migrating from another NOS, save the logs
|
# In case the unit is migrating from another NOS, save the logs
|
||||||
log_migration() {
|
log_migration() {
|
||||||
echo $1 >> /host/migration/migration.log
|
echo $1 >> /host/migration/migration.log
|
||||||
|
@ -25,6 +25,7 @@ etc/sonic/config_db.json
|
|||||||
etc/sonic/minigraph.xml
|
etc/sonic/minigraph.xml
|
||||||
etc/sonic/old_config/.*
|
etc/sonic/old_config/.*
|
||||||
etc/sonic/snmp.yml
|
etc/sonic/snmp.yml
|
||||||
|
etc/sonic/sonic-environment
|
||||||
etc/sonic/updategraph.conf
|
etc/sonic/updategraph.conf
|
||||||
etc/ssh/ssh_host_rsa_key.pub
|
etc/ssh/ssh_host_rsa_key.pub
|
||||||
etc/ssh/ssh_host_rsa_key
|
etc/ssh/ssh_host_rsa_key
|
||||||
|
@ -7,17 +7,20 @@
|
|||||||
|
|
||||||
start() {
|
start() {
|
||||||
TOPOLOGY_SCRIPT="topology.sh"
|
TOPOLOGY_SCRIPT="topology.sh"
|
||||||
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
|
PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
HWSKU=`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`
|
HWSKU=${HWSKU:-`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`}
|
||||||
/usr/share/sonic/device/$PLATFORM/$HWSKU/$TOPOLOGY_SCRIPT start
|
/usr/share/sonic/device/$PLATFORM/$HWSKU/$TOPOLOGY_SCRIPT start
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
TOPOLOGY_SCRIPT="topology.sh"
|
TOPOLOGY_SCRIPT="topology.sh"
|
||||||
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
|
PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
HWSKU=`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`
|
HWSKU=${HWSKU:-`sonic-cfggen -d -v 'DEVICE_METADATA["localhost"]["hwsku"]'`}
|
||||||
usr/share/sonic/device/$PLATFORM/$HWSKU/$TOPOLOGY_SCRIPT stop
|
usr/share/sonic/device/$PLATFORM/$HWSKU/$TOPOLOGY_SCRIPT stop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# read SONiC immutable variables
|
||||||
|
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
start|stop)
|
start|stop)
|
||||||
$1
|
$1
|
||||||
|
@ -7,6 +7,9 @@ reload_minigraph()
|
|||||||
config save -y
|
config save -y
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# read SONiC immutable variables
|
||||||
|
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
|
||||||
|
|
||||||
if [ ! -f /etc/sonic/updategraph.conf ]; then
|
if [ ! -f /etc/sonic/updategraph.conf ]; then
|
||||||
echo "No updategraph.conf found, generating a default one."
|
echo "No updategraph.conf found, generating a default one."
|
||||||
echo "enabled=false" >/etc/sonic/updategraph.conf
|
echo "enabled=false" >/etc/sonic/updategraph.conf
|
||||||
@ -46,7 +49,7 @@ if [ "$src" = "dhcp" ]; then
|
|||||||
|
|
||||||
if [ "`cat /tmp/dhcp_graph_url`" = "N/A" ]; then
|
if [ "`cat /tmp/dhcp_graph_url`" = "N/A" ]; then
|
||||||
echo "No graph_url option in DHCP response. Skipping graph update and generating an empty configuration."
|
echo "No graph_url option in DHCP response. Skipping graph update and generating an empty configuration."
|
||||||
PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`
|
PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
if [ -f /etc/sonic/minigraph.xml ]; then
|
if [ -f /etc/sonic/minigraph.xml ]; then
|
||||||
sonic-cfggen -H -m /etc/sonic/minigraph.xml --preset empty > /tmp/device_meta.json
|
sonic-cfggen -H -m /etc/sonic/minigraph.xml --preset empty > /tmp/device_meta.json
|
||||||
else
|
else
|
||||||
|
@ -76,7 +76,7 @@ switch_board_qsfp_lpmode() {
|
|||||||
|
|
||||||
install_python_api_package() {
|
install_python_api_package() {
|
||||||
device="/usr/share/sonic/device"
|
device="/usr/share/sonic/device"
|
||||||
platform=$(/usr/local/bin/sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)
|
platform=${PLATFORM:-`/usr/local/bin/sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
|
|
||||||
if [ -e $device/$platform/sonic_platform-1.0-py2-none-any.whl ]; then
|
if [ -e $device/$platform/sonic_platform-1.0-py2-none-any.whl ]; then
|
||||||
rv=$(pip install $device/$platform/sonic_platform-1.0-py2-none-any.whl)
|
rv=$(pip install $device/$platform/sonic_platform-1.0-py2-none-any.whl)
|
||||||
@ -90,6 +90,9 @@ remove_python_api_package() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# read SONiC immutable variables
|
||||||
|
[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment
|
||||||
|
|
||||||
if [[ "$1" == "init" ]]; then
|
if [[ "$1" == "init" ]]; then
|
||||||
depmod -a
|
depmod -a
|
||||||
modprobe nvram
|
modprobe nvram
|
||||||
|
@ -18,6 +18,9 @@ REDIS_TIMEOUT_MSECS = 0
|
|||||||
# Platform root directory inside docker
|
# Platform root directory inside docker
|
||||||
PLATFORM_ROOT_DOCKER = '/usr/share/sonic/platform'
|
PLATFORM_ROOT_DOCKER = '/usr/share/sonic/platform'
|
||||||
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
|
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
|
||||||
|
SONIC_ENV_FILE = '/etc/sonic/sonic-environment'
|
||||||
|
PLATFORM_ENVVAR = 'PLATFORM'
|
||||||
|
HWSKU_ENVVAR = 'HWSKU'
|
||||||
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
|
HWSKU_KEY = 'DEVICE_METADATA.localhost.hwsku'
|
||||||
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
|
PLATFORM_KEY = 'DEVICE_METADATA.localhost.platform'
|
||||||
|
|
||||||
@ -126,20 +129,31 @@ class DaemonBase(object):
|
|||||||
# Returns platform and hwsku
|
# Returns platform and hwsku
|
||||||
def get_platform_and_hwsku(self):
|
def get_platform_and_hwsku(self):
|
||||||
try:
|
try:
|
||||||
|
platform = hwsku = None
|
||||||
|
if os.path.exists(SONIC_ENV_FILE):
|
||||||
|
with open(SONIC_ENV_FILE, "r") as env_file:
|
||||||
|
for line in env_file:
|
||||||
|
if PLATFORM_ENVVAR in line:
|
||||||
|
platform = line.split('=')[1]
|
||||||
|
if HWSKU_ENVVAR in line:
|
||||||
|
hwsku = line.split('=')[1]
|
||||||
|
|
||||||
|
if not platform:
|
||||||
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY],
|
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-H', '-v', PLATFORM_KEY],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
shell=False,
|
stderr=subprocess.PIPE,
|
||||||
stderr=subprocess.STDOUT)
|
shell=False)
|
||||||
stdout = proc.communicate()[0]
|
stdout, stderr = proc.communicate()
|
||||||
proc.wait()
|
assert not stderr and not proc.returncode, "Failed to detect platform: %s, rc: %s" % (stderr, proc.returncode)
|
||||||
platform = stdout.rstrip('\n')
|
platform = stdout.rstrip('\n')
|
||||||
|
|
||||||
|
if not hwsku:
|
||||||
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY],
|
proc = subprocess.Popen([SONIC_CFGGEN_PATH, '-d', '-v', HWSKU_KEY],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
shell=False,
|
stderr=subprocess.PIPE,
|
||||||
stderr=subprocess.STDOUT)
|
shell=False)
|
||||||
stdout = proc.communicate()[0]
|
stdout, stderr = proc.communicate()
|
||||||
proc.wait()
|
assert not stderr and not proc.returncode, "Failed to detect hwsku: %s, rc: %s" % (stderr, proc.returncode)
|
||||||
hwsku = stdout.rstrip('\n')
|
hwsku = stdout.rstrip('\n')
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
raise OSError("Failed to detect platform: %s" % (str(e)))
|
raise OSError("Failed to detect platform: %s" % (str(e)))
|
||||||
|
Loading…
Reference in New Issue
Block a user