#!/bin/bash CONFIG_DB_INDEX=4 reload_minigraph() { echo "Reloading minigraph..." if [ ! -f /etc/sonic/init_cfg.json ]; then echo "{}" > /etc/sonic/init_cfg.json fi redis-cli -n $CONFIG_DB_INDEX FLUSHDB sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --write-to-db redis-cli -n $CONFIG_DB_INDEX SET "CONFIG_DB_INITIALIZED" "1" if [ -f /etc/sonic/acl.json ]; then acl-loader update full /etc/sonic/acl.json fi config qos reload DEVICE_TYPE=`sonic-cfggen -m -v DEVICE_METADATA.localhost.type` if [ "${DEVICE_TYPE}" != "MgmtToRRouter" ]; then pfcwd start_default fi if [[ -x /usr/bin/db_migrator.py ]]; then # Set latest version number /usr/bin/db_migrator.py -o set_version fi } function copy_config_files_and_directories() { for file_dir in $@; do if [ -f /etc/sonic/old_config/${file_dir} ] || [ -d /etc/sonic/old_config/${file_dir} ]; then logger "Copying SONiC configuration ${file_dir} ..." cp -ar /etc/sonic/old_config/${file_dir} /etc/sonic/ else logger "Missing SONiC configuration ${file_dir} ..." fi done sync } function check_system_warm_boot() { SYSTEM_WARM_START=`/usr/bin/redis-cli -n 6 hget "WARM_RESTART_ENABLE_TABLE|system" enable` # SYSTEM_WARM_START could be empty, always make WARM_BOOT meaningful. if [[ x"$SYSTEM_WARM_START" == x"true" ]]; then WARM_BOOT="true" else WARM_BOOT="false" fi } if [ ! -f /etc/sonic/updategraph.conf ]; then echo "No updategraph.conf found, generating a default one." echo "enabled=false" >/etc/sonic/updategraph.conf fi . /etc/sonic/updategraph.conf check_system_warm_boot copy_list="minigraph.xml snmp.yml acl.json config_db.json frr" if [ -f /tmp/pending_config_migration ]; then copy_config_files_and_directories $copy_list if [ x"${WARM_BOOT}" == x"true" ]; then echo "Warm reboot detected..." elif [ -r /etc/sonic/config_db.json ]; then echo "Use config_db.json from old system..." sonic-cfggen -j /etc/sonic/config_db.json --write-to-db if [[ -x /usr/bin/db_migrator.py ]]; then # Migrate the DB to the latest schema version if needed /usr/bin/db_migrator.py -o migrate fi elif [ -r /etc/sonic/minigraph.xml ]; then echo "Use minigraph.xml from old system..." reload_minigraph sonic-cfggen -d --print-data > /etc/sonic/config_db.json else echo "Didn't found neither config_db.json nor minigraph.xml ..." fi rm -f /tmp/pending_config_migration sed -i "/enabled=/d" /etc/sonic/updategraph.conf echo "enabled=false" >> /etc/sonic/updategraph.conf exit 0 fi if [ -f /tmp/pending_config_initialization ]; then rm -f /tmp/pending_config_initialization if [ "$enabled" != "true" ]; then PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform` PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`) sonic-cfggen -H -k ${PRESET[0]} --preset ${PRESET[1]} > /etc/sonic/config_db.json redis-cli -n $CONFIG_DB_INDEX FLUSHDB sonic-cfggen -j /etc/sonic/config_db.json --write-to-db redis-cli -n $CONFIG_DB_INDEX SET "CONFIG_DB_INITIALIZED" "1" exit 0 fi fi if [ "$enabled" = "reload_only" ]; then reload_minigraph sed -i "/enabled=/d" /etc/sonic/updategraph.conf echo "enabled=false" >> /etc/sonic/updategraph.conf exit 0 fi if [ "$enabled" != "true" ]; then echo "Disabled in updategraph.conf. Skipping graph update." exit 0 fi ACL_URL=$acl_src if [ "$src" = "dhcp" ]; then # Enable dhcp client on management port eth0 /sbin/dhclient -4 -v -pf /run/dhclient.eth0.pid -lf /var/lib/dhcp/dhclient.eth0.leases -I -df /var/lib/dhcp/dhclient6.eth0.leases eth0 & disown while [ ! -f /tmp/dhcp_graph_url ]; do echo "Waiting for DHCP response..." sleep 1 done 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." PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform` if [ -f /etc/sonic/minigraph.xml ]; then sonic-cfggen -H -m /etc/sonic/minigraph.xml --preset empty > /tmp/device_meta.json else PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`) sonic-cfggen -H -k ${PRESET[0]} --preset empty > /tmp/device_meta.json fi if [ -f /etc/sonic/init_cfg.json ]; then sonic-cfggen -j /tmp/device_meta.json -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json else cp -f /tmp/device_meta.json /etc/sonic/config_db.json fi redis-cli -n $CONFIG_DB_INDEX FLUSHDB sonic-cfggen -j /etc/sonic/config_db.json --write-to-db redis-cli -n $CONFIG_DB_INDEX SET "CONFIG_DB_INITIALIZED" "1" if [ "$dhcp_as_static" = "true" ]; then sed -i "/enabled=/d" /etc/sonic/updategraph.conf echo "enabled=false" >> /etc/sonic/updategraph.conf fi exit 0 fi HOSTNAME=`hostname -s` GRAPH_URL=`sonic-cfggen -t /tmp/dhcp_graph_url -a "{\"hostname\": \"$HOSTNAME\"}"` URL_REGEX='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$' if [[ ! $GRAPH_URL =~ $URL_REGEX ]]; then echo "\"$GRAPH_URL\" is not a valid url. Skipping graph update." exit 0 fi if [ "$dhcp_as_static" = "true" ]; then sed -i "/src=/d" /etc/sonic/updategraph.conf echo "src=\"$GRAPH_URL\"" >> /etc/sonic/updategraph.conf fi if [ -f /tmp/dhcp_acl_url ]; then ACL_URL=`sonic-cfggen -t /tmp/dhcp_acl_url -a "{\"hostname\": \"$HOSTNAME\"}"` if [[ ! $ACL_URL =~ $URL_REGEX ]]; then echo "\"$ACL_URL\" is not a valid url. Skipping acl update." ACL_URL="" fi if [ "$dhcp_as_static" = "true" ]; then sed -i "/acl_src=/d" /etc/sonic/updategraph.conf echo "acl_src=\"$ACL_URL\"" >> /etc/sonic/updategraph.conf fi fi else GRAPH_URL=$src fi if [ -f /etc/sonic/minigraph.xml ]; then echo "Renaming minigraph.xml to minigraph.old" mv /etc/sonic/minigraph.xml /etc/sonic/minigraph.old fi echo "Getting minigraph from $GRAPH_URL" while true; do curl -f $GRAPH_URL -o /etc/sonic/minigraph.xml --connect-timeout 15 && break sleep 5 done if [ -n "$ACL_URL" ]; then if [ -f /etc/sonic/acl.json ]; then echo "Renaming acl.json to acl.json.old" mv /etc/sonic/acl.json /etc/sonic/acl.json.old fi echo "Getting ACL config from $ACL_URL" while true; do curl -f $ACL_URL -o /etc/sonic/acl.json --connect-timeout 15 && break sleep 5 done else echo "Skip ACL config download." fi reload_minigraph sonic-cfggen -d --print-data > /etc/sonic/config_db.json # Mark as disabled after graph is successfully downloaded sed -i "/enabled=/d" /etc/sonic/updategraph.conf echo "enabled=false" >> /etc/sonic/updategraph.conf