Skip to content

Commit

Permalink
Move all minigraph-related action from rc.local to updategraph (#1452)
Browse files Browse the repository at this point in the history
- Move all minigraph-related action from rc.local to updategraph
- updategraph service is now after database. All feature services are now after and depending on updategraph
  • Loading branch information
taoyl-ms authored Mar 10, 2018
1 parent 16f81af commit e84e093
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 88 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/caclmgrd/caclmgrd.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Control Plane ACL configuration daemon
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
Type=simple
Expand Down
4 changes: 2 additions & 2 deletions files/image_config/hostcfgd/hostcfgd.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Host config enforcer daemon
Requires=database.service
After=database.service
Requires=updategraph.service
After=updategraph.service

[Service]
Type=simple
Expand Down
4 changes: 2 additions & 2 deletions files/image_config/hostname/hostname-config.service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=Update hostname based on configdb
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/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: 11 additions & 45 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,21 @@ 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
# and 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/pending_config_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/pending_config_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
mkdir -p /etc/sonic/old_config
mv /host/migration/minigraph.xml /etc/sonic/old_config/
touch /tmp/pending_config_migration
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
touch /tmp/pending_config_initialization
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
3 changes: 2 additions & 1 deletion files/image_config/ssh/sshd-config-updater.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[Unit]
Description=Takes care of updates to SSH config file with respect to the SSH allow list
After=database.service
After=updategraph.service
Requires=updategraph.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
Expand Down
97 changes: 85 additions & 12 deletions files/image_config/updategraph/updategraph
Original file line number Diff line number Diff line change
@@ -1,12 +1,86 @@
#!/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
}

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/pending_config_migration ]; 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
else
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
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 ] && [ "$enabled" != "true" ]; then
copy_default_minigraph
reload_minigraph
sonic-cfggen -d --print-data > /etc/sonic/config_db.json
rm -f /tmp/pending_config_initialization
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 +112,9 @@ if [ "$src" = "dhcp" ]; then
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
Expand Down Expand Up @@ -86,17 +162,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 +176,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 e84e093

Please sign in to comment.