Reduce logic in updategraph (#11010)
Why I did it The dhcp_graph_url used by internal service is always set as "N/A". So we can make the updategraph logic short. How I did it Shorten 'if statement' logic for /tmp/dhcp_graph_url
This commit is contained in:
parent
6a4105ad17
commit
cca3b5be5b
@ -35,100 +35,26 @@ if [ -e /usr/bin/ztp ] && [ "$(ztp status -c)" != "0:DISABLED" ]; then
|
|||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
ACL_URL=$acl_src
|
# "`cat /tmp/dhcp_graph_url`" is always "N/A"
|
||||||
|
echo "No graph_url option in DHCP response. Skipping graph update and generating an empty configuration."
|
||||||
if [ "$src" = "dhcp" ]; then
|
PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`}
|
||||||
# Enable dhcp client on management port eth0
|
if [ -f /etc/sonic/minigraph.xml ]; then
|
||||||
/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=${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
|
sonic-cfggen -H -m /etc/sonic/minigraph.xml --preset empty > /tmp/device_meta.json
|
||||||
else
|
else
|
||||||
PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`)
|
PRESET=(`head -n 1 /usr/share/sonic/device/$PLATFORM/default_sku`)
|
||||||
sonic-cfggen -H -k ${PRESET[0]} --preset empty > /tmp/device_meta.json
|
sonic-cfggen -H -k ${PRESET[0]} --preset empty > /tmp/device_meta.json
|
||||||
fi
|
fi
|
||||||
if [ -f /etc/sonic/init_cfg.json ]; then
|
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
|
sonic-cfggen -j /tmp/device_meta.json -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
|
||||||
else
|
else
|
||||||
cp -f /tmp/device_meta.json /etc/sonic/config_db.json
|
cp -f /tmp/device_meta.json /etc/sonic/config_db.json
|
||||||
fi
|
fi
|
||||||
sonic-db-cli CONFIG_DB FLUSHDB
|
sonic-db-cli CONFIG_DB FLUSHDB
|
||||||
sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
|
sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
|
||||||
sonic-db-cli CONFIG_DB SET "CONFIG_DB_INITIALIZED" "1"
|
sonic-db-cli CONFIG_DB SET "CONFIG_DB_INITIALIZED" "1"
|
||||||
if [ "$dhcp_as_static" = "true" ]; then
|
if [ "$dhcp_as_static" = "true" ]; then
|
||||||
sed -i "/enabled=/d" /etc/sonic/updategraph.conf
|
sed -i "/enabled=/d" /etc/sonic/updategraph.conf
|
||||||
echo "enabled=false" >> /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
|
fi
|
||||||
|
exit 0
|
||||||
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
|
|
||||||
|
|
||||||
# Mark as disabled after graph is successfully downloaded
|
|
||||||
sed -i "/enabled=/d" /etc/sonic/updategraph.conf
|
|
||||||
echo "enabled=false" >> /etc/sonic/updategraph.conf
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user