diff --git a/files/build_templates/bgp.service.j2 b/files/build_templates/bgp.service.j2 index c7ba8b8a5f52..6c3e343fcf44 100644 --- a/files/build_templates/bgp.service.j2 +++ b/files/build_templates/bgp.service.j2 @@ -1,7 +1,7 @@ [Unit] Description=BGP container -Requires=database.service -After=database.service +Requires=updategraph.service +After=updategraph.service [Service] User={{ sonicadmin_user }} diff --git a/files/build_templates/dhcp_relay.service.j2 b/files/build_templates/dhcp_relay.service.j2 index 966e2782abe0..ea53fa2deda4 100644 --- a/files/build_templates/dhcp_relay.service.j2 +++ b/files/build_templates/dhcp_relay.service.j2 @@ -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 }} diff --git a/files/build_templates/lldp.service.j2 b/files/build_templates/lldp.service.j2 index 1ddda15a49c4..d35cdacca0f3 100644 --- a/files/build_templates/lldp.service.j2 +++ b/files/build_templates/lldp.service.j2 @@ -1,7 +1,7 @@ [Unit] Description=LLDP container -Requires=database.service -After=database.service +Requires=updategraph.service +After=updategraph.service [Service] User={{ sonicadmin_user }} diff --git a/files/build_templates/pmon.service.j2 b/files/build_templates/pmon.service.j2 index 9f1a0298240c..83dd8e4b648d 100644 --- a/files/build_templates/pmon.service.j2 +++ b/files/build_templates/pmon.service.j2 @@ -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 }} diff --git a/files/build_templates/router_advertiser.service.j2 b/files/build_templates/router_advertiser.service.j2 index 12bdb371369d..cd8b817eb64f 100644 --- a/files/build_templates/router_advertiser.service.j2 +++ b/files/build_templates/router_advertiser.service.j2 @@ -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 }} diff --git a/files/build_templates/snmp.service.j2 b/files/build_templates/snmp.service.j2 index 0c1558ad64aa..f0612a9cfcb6 100644 --- a/files/build_templates/snmp.service.j2 +++ b/files/build_templates/snmp.service.j2 @@ -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 diff --git a/files/build_templates/swss.service.j2 b/files/build_templates/swss.service.j2 index 030761015923..e16ca6636121 100644 --- a/files/build_templates/swss.service.j2 +++ b/files/build_templates/swss.service.j2 @@ -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 diff --git a/files/build_templates/teamd.service.j2 b/files/build_templates/teamd.service.j2 index e291a39b387d..bbd04a81b05b 100644 --- a/files/build_templates/teamd.service.j2 +++ b/files/build_templates/teamd.service.j2 @@ -1,7 +1,7 @@ [Unit] Description=TEAMD container -Requires=database.service -After=database.service +Requires=updategraph.service +After=updategraph.service [Service] User={{ sonicadmin_user }} diff --git a/files/image_config/caclmgrd/caclmgrd.service b/files/image_config/caclmgrd/caclmgrd.service index 1acc23458c90..c63f1f7c5c7f 100644 --- a/files/image_config/caclmgrd/caclmgrd.service +++ b/files/image_config/caclmgrd/caclmgrd.service @@ -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 diff --git a/files/image_config/hostcfgd/hostcfgd.service b/files/image_config/hostcfgd/hostcfgd.service index 4f6151a471fe..762786ad830e 100644 --- a/files/image_config/hostcfgd/hostcfgd.service +++ b/files/image_config/hostcfgd/hostcfgd.service @@ -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 diff --git a/files/image_config/hostname/hostname-config.service b/files/image_config/hostname/hostname-config.service index e6e8f489883f..c23ae10227a4 100644 --- a/files/image_config/hostname/hostname-config.service +++ b/files/image_config/hostname/hostname-config.service @@ -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 diff --git a/files/image_config/interfaces/interfaces-config.service b/files/image_config/interfaces/interfaces-config.service index 86706da22518..71178affdee8 100644 --- a/files/image_config/interfaces/interfaces-config.service +++ b/files/image_config/interfaces/interfaces-config.service @@ -1,7 +1,7 @@ [Unit] Description=Update interfaces configuration -Requires=database.service -After=database.service +Requires=updategraph.service +After=updategraph.service [Service] Type=oneshot diff --git a/files/image_config/ntp/ntp-config.service b/files/image_config/ntp/ntp-config.service index d41f1ba2ddca..c86710e3d209 100644 --- a/files/image_config/ntp/ntp-config.service +++ b/files/image_config/ntp/ntp-config.service @@ -1,7 +1,7 @@ [Unit] Description=Update NTP configuration -Requires=database.service -After=database.service +Requires=updategraph.service +After=updategraph.service [Service] Type=oneshot diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index c5bfae19f9b8..2ec4a72e5340 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -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 @@ -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 diff --git a/files/image_config/rsyslog/rsyslog-config.service b/files/image_config/rsyslog/rsyslog-config.service index e013b992030d..ee62a36ccf2d 100644 --- a/files/image_config/rsyslog/rsyslog-config.service +++ b/files/image_config/rsyslog/rsyslog-config.service @@ -1,7 +1,7 @@ [Unit] Description=Update rsyslog configuration -Requires=database.service -After=database.service +Requires=updategraph.service +After=updategraph.service [Service] Type=oneshot diff --git a/files/image_config/ssh/sshd-config-updater.service b/files/image_config/ssh/sshd-config-updater.service index 306d35f4e6d1..a8e773d48593 100644 --- a/files/image_config/ssh/sshd-config-updater.service +++ b/files/image_config/ssh/sshd-config-updater.service @@ -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] diff --git a/files/image_config/updategraph/updategraph b/files/image_config/updategraph/updategraph index 421bbbe5396c..2ce251dfb36e 100755 --- a/files/image_config/updategraph/updategraph +++ b/files/image_config/updategraph/updategraph @@ -1,5 +1,38 @@ #!/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 @@ -7,6 +40,47 @@ 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 @@ -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 @@ -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" @@ -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 + diff --git a/files/image_config/updategraph/updategraph.service b/files/image_config/updategraph/updategraph.service index 0162754ab2db..4599f56bdc64 100644 --- a/files/image_config/updategraph/updategraph.service +++ b/files/image_config/updategraph/updategraph.service @@ -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