Skip to content

Commit

Permalink
Move all minigraph-related action from rc.local to updategraph
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyl-ms committed Mar 9, 2018
1 parent 02b62ac commit c03a6b9
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 82 deletions.
4 changes: 2 additions & 2 deletions files/build_templates/bgp.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=BGP container
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
User={{ sonicadmin_user }}
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/dhcp_relay.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=DHCP relay container
Requires=docker.service teamd.service
After=swss.service teamd.service
Requires=updategraph.service swss.service teamd.service
After=updategraph.service swss.service teamd.service

[Service]
User={{ sonicadmin_user }}
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/lldp.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=LLDP container
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
User={{ sonicadmin_user }}
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/pmon.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Platform monitor container
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
User={{ sonicadmin_user }}
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/router_advertiser.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Router advertiser container
Requires=docker.service
After=swss.service
Requires=updategraph.service
After=updategraph.service swss.service

[Service]
User={{ sonicadmin_user }}
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/snmp.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=SNMP container
Requires=database.service swss.service
After=database.service swss.service
Requires=updategraph.service swss.service
After=updategraph.service swss.service

[Service]
ExecStartPre=/usr/bin/{{docker_container_name}}.sh start
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/swss.service.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[Unit]
Description=switch state service
Requires=database.service
Requires=database.service updategraph.service
{% if sonic_asic_platform == 'broadcom' %}
Requires=opennsl-modules-3.16.0-5-amd64.service
{% elif sonic_asic_platform == 'nephos' %}
Requires=nps-modules-3.16.0-5-amd64.service
{% endif %}
After=database.service
After=database.service updategraph.service
After=interfaces-config.service
{% if sonic_asic_platform == 'broadcom' %}
After=opennsl-modules-3.16.0-5-amd64.service
Expand Down
4 changes: 2 additions & 2 deletions files/build_templates/teamd.service.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=TEAMD container
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
User={{ sonicadmin_user }}
Expand Down
4 changes: 2 additions & 2 deletions files/image_config/interfaces/interfaces-config.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Update interfaces configuration
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
Type=oneshot
Expand Down
4 changes: 2 additions & 2 deletions files/image_config/ntp/ntp-config.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Update NTP configuration
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
Type=oneshot
Expand Down
56 changes: 10 additions & 46 deletions files/image_config/platform/rc.local
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,6 @@ firsttime_exit()
exit 0
}

test_config()
{
if [ -d /host/old_config ] && ( [ -f /host/old_config/minigraph.xml ] || [ -f /host/old_config/config_db.json ] ); then
return 0
fi

return 1
}

# Given a string of tuples of the form field=value, extract the value for a field
# In : $string, $field
# Out: $value
Expand Down Expand Up @@ -193,46 +184,19 @@ if [ -f /host/image-$sonic_version/platform/firsttime ]; then
firsttime_exit
fi

if [ ! -f /etc/sonic/init_cfg.json ]; then
# Generate an empty init_cfg.json
echo '{}' > /etc/sonic/init_cfg.json
fi

# Try to take old configuration saved during installation
if test_config; then
rm -f /host/old_config/sonic_version.yml
mv -f /host/old_config/* /etc/sonic/
if [ ! -f /etc/sonic/config_db.json ]; then
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
fi
# If taken, create a flag in /tmp/ to let updategraph service know
if [ -d /host/old_config ]; then
mv -f /host/old_config /etc/sonic/
touch /tmp/migration
elif [ -f /host/minigraph.xml ]; then
mv /host/minigraph.xml /etc/sonic/
# Combine information in minigraph and init_cfg.json to form initiate config DB dump file.
# TODO: After moving all information from minigraph to DB, sample config DB dump should be provide
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
mkdir -p /etc/sonic/old_config
mv /host/minigraph.xml /etc/sonic/old_config/
touch /tmp/migration
elif [ -n "$migration" ] && [ -f /host/migration/minigraph.xml ]; then
# Use the minigraph that was imported from the NOS
mv /host/migration/minigraph.xml /etc/sonic/
sonic-cfggen -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
else
# Use default minigraph.xml
cp /usr/share/sonic/device/$platform/minigraph.xml /etc/sonic/
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
fi

HWSKU=`sonic-cfggen -m /etc/sonic/minigraph.xml -v DEVICE_METADATA.localhost.hwsku`
if [ -f /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 ]; then
# generate and merge buffers configuration into config file
sonic-cfggen -m -t /usr/share/sonic/device/$platform/$HWSKU/buffers.json.j2 > /tmp/buffers.json
sonic-cfggen -j /etc/sonic/config_db.json -j /tmp/buffers.json --print-data > /tmp/config_db.json
mv /tmp/config_db.json /etc/sonic/config_db.json

# Only apply qos.json when buffer configuration is available.
if [ -f /usr/share/sonic/device/$platform/$HWSKU/qos.json ]; then
# merge qos configuration into init config file
sonic-cfggen -j /etc/sonic/config_db.json -j /usr/share/sonic/device/$platform/$HWSKU/qos.json --print-data > /tmp/config_db.json
mv /tmp/config_db.json /etc/sonic/config_db.json
fi
mkdir -p /etc/sonic/old_config
mv /host/migration/minigraph.xml /etc/sonic/old_config/
touch /tmp/migration
fi

if [ -d /host/image-$sonic_version/platform/$platform ]; then
Expand Down
4 changes: 2 additions & 2 deletions files/image_config/rsyslog/rsyslog-config.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Update rsyslog configuration
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
Type=oneshot
Expand Down
91 changes: 79 additions & 12 deletions files/image_config/updategraph/updategraph
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
#!/bin/bash

reload_minigraph()
{
echo "Reloading minigraph..."
if [ ! -f /etc/sonic/init_cfg.json ]; then
echo "{}" > /etc/sonic/init_cfg.json
fi
redis-cli -n 4 FLUSHDB
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --write-to-db
redis-cli -n 4 SET "CONFIG_DB_INITIALIZED" "1"
if [ -f /etc/sonic/acl.json ]; then
acl-loader update full /etc/sonic/acl.json
fi
config qos reload
}

copy_default_minigraph()
{
. /host/machine.conf
if [ -n "$aboot_platform" ]; then
platform=$aboot_platform
elif [ -n "$onie_platform" ]; then
platform=$onie_platform
else
echo "Unknown sonic platform."
exit 1
fi
echo "Copying default minigraph..."
cp /usr/share/sonic/device/$platform/minigraph.xml /etc/sonic/
}


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

if [ -f /tmp/firstboot ]; then
if [ "$enabled" = "true" ]; then
echo "Use minigraph.xml from old system..."
cp /etc/sonic/old_config/minigraph.xml /etc/sonic/
if [ -f /etc/sonic/old_config/init_cfg.json ]; then
cp /etc/sonic/old_config/init_cfg.json /etc/sonic/
fi
if [ -f /etc/sonic/old_config/snmp.yml ]; then
cp /etc/sonic/old_config/snmp.yml /etc/sonic/
fi
if [ -f /etc/sonic/old_config/acl.json ]; then
cp /etc/sonic/old_config/acl.json /etc/sonic/
fi
reload_minigraph
sonic-cfggen -d --print_data > /etc/sonic/config_db.json
elif [ -f /etc/sonic/old_config/config_db.json ]; then
echo "Use config_db.json from old system..."
cp /etc/sonic/old_config/config_db.json /etc/sonic/
sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
else
copy_default_minigraph
reload_minigraph
sonic-cfggen -d --print_data > /etc/sonic/config_db.json
fi
rm -f /tmp/firstboot
sed -i "/enabled=/d" /etc/sonic/updategraph.conf
echo "enabled=false" >> /etc/sonic/updategraph.conf
exit 0
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
Expand Down Expand Up @@ -38,7 +106,9 @@ if [ "$src" = "dhcp" ]; then
else
cp -f /tmp/device_meta.json /etc/sonic/config_db.json
fi

redis-cli -n 4 FLUSHDB
sonic-cfggen -j /etc/sonic/config_db.json --write-to-db
redis-cli -n 4 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
Expand Down Expand Up @@ -86,17 +156,6 @@ while true; do
sleep 5
done

echo "Regenerating config DB from minigraph..."
if [ -f /etc/sonic/init_cfg.json ]; then
sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data > /etc/sonic/config_db.json
else
sonic-cfggen -H -m --print-data > /etc/sonic/config_db.json
fi

# Mark as disabled after graph is successfully downloaded
sed -i "/enabled=/d" /etc/sonic/updategraph.conf
echo "enabled=false" >> /etc/sonic/updategraph.conf

if [ -n "$ACL_URL" ]; then
if [ -f /etc/sonic/acl.json ]; then
echo "Renaming acl.json to acl.json.old"
Expand All @@ -111,3 +170,11 @@ if [ -n "$ACL_URL" ]; then
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

5 changes: 3 additions & 2 deletions files/image_config/updategraph/updategraph.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[Unit]
Description=download minigraph from graph service
Description=Update minigraph and set configuration based on minigraph
After=rc-local.service
Before=database.service
After=database.service
Requires=database.service

[Service]
Type=oneshot
Expand Down

0 comments on commit c03a6b9

Please sign in to comment.