Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy missing values from INIT_CFG to config_db during db_migration #1522

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def reload(filename, yes, load_sysinfo, no_service_restart):
log.log_info("'reload' stopping services...")
_stop_services()

# In Single AISC platforms we have single DB service. In multi-ASIC platforms we have a global DB
# In Single ASIC platforms we have single DB service. In multi-ASIC platforms we have a global DB
# service running in the host + DB services running in each ASIC namespace created per ASIC.
# In the below logic, we get all namespaces in this platform and add an empty namespace ''
# denoting the current namespace which we are in ( the linux host )
Expand Down
23 changes: 22 additions & 1 deletion scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import argparse
import json
import sys
import traceback

Expand All @@ -19,7 +20,7 @@
except KeyError:
pass


INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
SYSLOG_IDENTIFIER = 'db_migrator'

# Global logger instance
Expand Down Expand Up @@ -254,6 +255,24 @@ def set_version(self, version=None):
self.configDB.set_entry(self.TABLE_NAME, self.TABLE_KEY, entry)


def common_migration_ops(self):
try:
with open(INIT_CFG_FILE) as f:
init_db = json.load(f)
except Exception as e:
raise Exception(str(e))

for init_cfg_table, table_val in init_db.items():
data = self.configDB.get_table(init_cfg_table)
if data:
# Ignore overriding the values that pre-exist in configDB
continue
log.log_info("Migrating table {} from INIT_CFG to config_db".format(init_cfg_table))
# Update all tables that do not exist in configDB but are present in INIT_CFG
for init_table_key, init_table_val in table_val.items():
self.configDB.set_entry(init_cfg_table, init_table_key, init_table_val)


def migrate(self):
version = self.get_version()
log.log_info('Upgrading from version ' + version)
Expand All @@ -262,6 +281,8 @@ def migrate(self):
if next_version == version:
raise Exception('Version migrate from %s stuck in same version' % version)
version = next_version
# Perform common migration ops
self.common_migration_ops()


def main():
Expand Down
25 changes: 14 additions & 11 deletions sonic-utilities-tests/db_migrator_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mock
import os
import pytest
import sys
Expand Down Expand Up @@ -61,12 +62,13 @@ def test_mellanox_buffer_migrator_negative_cold_reboot(self, scenario):
db_after_migrate = scenario + '-expected'
device_info.get_sonic_version_info = get_sonic_version_info_mlnx
_ = self.mock_dedicated_config_db(db_before_migrate)
import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
dbmgtr.migrate()
expected_db = self.mock_dedicated_config_db(db_after_migrate)
self.check_config_db(dbmgtr.configDB, expected_db.cfgdb)
assert not dbmgtr.mellanox_buffer_migrator.is_buffer_config_default
with mock.patch("db_migrator.DBMigrator.common_migration_ops"):
import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
dbmgtr.migrate()
expected_db = self.mock_dedicated_config_db(db_after_migrate)
self.check_config_db(dbmgtr.configDB, expected_db.cfgdb)
assert not dbmgtr.mellanox_buffer_migrator.is_buffer_config_default

@pytest.mark.parametrize('sku_version',
[('ACS-MSN2700', 'version_1_0_1'),
Expand Down Expand Up @@ -97,10 +99,11 @@ def test_mellanox_buffer_migrator_for_cold_reboot(self, sku_version, topo):
# migration from any version between start_version and the current version (inclusive) to the current version will be verified
for version in self.version_list[start_index:]:
_ = self.mock_dedicated_config_db(self.make_db_name_by_sku_topo_version(sku, topo, version))
import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
dbmgtr.migrate()
self.check_config_db(dbmgtr.configDB, expected_db.cfgdb)
assert dbmgtr.mellanox_buffer_migrator.is_buffer_config_default
with mock.patch("db_migrator.DBMigrator.common_migration_ops"):
import db_migrator
dbmgtr = db_migrator.DBMigrator(None)
dbmgtr.migrate()
self.check_config_db(dbmgtr.configDB, expected_db.cfgdb)
assert dbmgtr.mellanox_buffer_migrator.is_buffer_config_default

self.clear_dedicated_mock_dbs()