1919from config_mgmt import ConfigMgmtDPB
2020from utilities_common .intf_filter import parse_interface_in_filter
2121from utilities_common .util_base import UtilHelper
22+ from utilities_common .db import Db
2223from portconfig import get_child_ports , get_port_config_file_name
2324
2425import aaa
5051CFG_LOOPBACK_NO = "<0-999>"
5152
5253asic_type = None
53- config_db = None
5454
5555# ========================== Syslog wrappers ==========================
5656
@@ -691,7 +691,7 @@ def _abort_if_false(ctx, param, value):
691691 ctx .abort ()
692692
693693
694- def _get_disabled_services_list ():
694+ def _get_disabled_services_list (config_db ):
695695 disabled_services_list = []
696696
697697 feature_table = config_db .get_table ('FEATURE' )
@@ -713,7 +713,7 @@ def _get_disabled_services_list():
713713
714714 return disabled_services_list
715715
716- def _stop_services ():
716+ def _stop_services (config_db ):
717717 # This list is order-dependent. Please add services in the order they should be stopped
718718 # on Mellanox platform pmon is stopped by syncd
719719 services_to_stop = [
@@ -730,7 +730,7 @@ def _stop_services():
730730 if asic_type == 'mellanox' and 'pmon' in services_to_stop :
731731 services_to_stop .remove ('pmon' )
732732
733- disabled_services = _get_disabled_services_list ()
733+ disabled_services = _get_disabled_services_list (config_db )
734734
735735 for service in disabled_services :
736736 if service in services_to_stop :
@@ -739,7 +739,7 @@ def _stop_services():
739739 execute_systemctl (services_to_stop , SYSTEMCTL_ACTION_STOP )
740740
741741
742- def _reset_failed_services ():
742+ def _reset_failed_services (config_db ):
743743 # This list is order-independent. Please keep list in alphabetical order
744744 services_to_reset = [
745745 'bgp' ,
@@ -762,7 +762,7 @@ def _reset_failed_services():
762762 'telemetry'
763763 ]
764764
765- disabled_services = _get_disabled_services_list ()
765+ disabled_services = _get_disabled_services_list (config_db )
766766
767767 for service in disabled_services :
768768 if service in services_to_reset :
@@ -771,7 +771,7 @@ def _reset_failed_services():
771771 execute_systemctl (services_to_reset , SYSTEMCTL_ACTION_RESET_FAILED )
772772
773773
774- def _restart_services ():
774+ def _restart_services (config_db ):
775775 # This list is order-dependent. Please add services in the order they should be started
776776 # on Mellanox platform pmon is started by syncd
777777 services_to_restart = [
@@ -790,7 +790,7 @@ def _restart_services():
790790 'telemetry'
791791 ]
792792
793- disabled_services = _get_disabled_services_list ()
793+ disabled_services = _get_disabled_services_list (config_db )
794794
795795 for service in disabled_services :
796796 if service in services_to_restart :
@@ -908,7 +908,8 @@ def validate_mirror_session_config(config_db, session_name, dst_port, src_port,
908908
909909# This is our main entrypoint - the main 'config' command
910910@click .group (cls = AbbreviationGroup , context_settings = CONTEXT_SETTINGS )
911- def config ():
911+ @click .pass_context
912+ def config (ctx ):
912913 """SONiC command line - 'config' command"""
913914 #
914915 # Load asic_type for further use
@@ -932,10 +933,9 @@ def config():
932933
933934 SonicDBConfig .load_sonic_global_db_config ()
934935
935- global config_db
936+ ctx . obj = Db ()
936937
937- config_db = ConfigDBConnector ()
938- config_db .connect ()
938+ pass_db = click .make_pass_decorator (Db , ensure = True )
939939
940940config .add_command (aaa .aaa )
941941config .add_command (aaa .tacacs )
@@ -1060,7 +1060,8 @@ def load(filename, yes):
10601060@click .option ('-l' , '--load-sysinfo' , is_flag = True , help = 'load system default information (mac, portmap etc) first.' )
10611061@click .option ('-n' , '--no_service_restart' , default = False , is_flag = True , help = 'Do not restart docker services' )
10621062@click .argument ('filename' , required = False )
1063- def reload (filename , yes , load_sysinfo , no_service_restart ):
1063+ @pass_db
1064+ def reload (db , filename , yes , load_sysinfo , no_service_restart ):
10641065 """Clear current configuration and import a previous saved config DB dump file.
10651066 <filename> : Names of configuration file(s) to load, separated by comma with no spaces in between
10661067 """
@@ -1102,7 +1103,7 @@ def reload(filename, yes, load_sysinfo, no_service_restart):
11021103 #Stop services before config push
11031104 if not no_service_restart :
11041105 log_info ("'reload' stopping services..." )
1105- _stop_services ()
1106+ _stop_services (db . cfgdb )
11061107
11071108 """ In Single AISC platforms we have single DB service. In multi-ASIC platforms we have a global DB
11081109 service running in the host + DB services running in each ASIC namespace created per ASIC.
@@ -1175,9 +1176,9 @@ def reload(filename, yes, load_sysinfo, no_service_restart):
11751176 # We first run "systemctl reset-failed" to remove the "failed"
11761177 # status from all services before we attempt to restart them
11771178 if not no_service_restart :
1178- _reset_failed_services ()
1179+ _reset_failed_services (db . cfgdb )
11791180 log_info ("'reload' restarting services..." )
1180- _restart_services ()
1181+ _restart_services (db . cfgdb )
11811182
11821183@config .command ("load_mgmt_config" )
11831184@click .option ('-y' , '--yes' , is_flag = True , callback = _abort_if_false ,
@@ -1208,14 +1209,15 @@ def load_mgmt_config(filename):
12081209@click .option ('-y' , '--yes' , is_flag = True , callback = _abort_if_false ,
12091210 expose_value = False , prompt = 'Reload config from minigraph?' )
12101211@click .option ('-n' , '--no_service_restart' , default = False , is_flag = True , help = 'Do not restart docker services' )
1211- def load_minigraph (no_service_restart ):
1212+ @pass_db
1213+ def load_minigraph (db , no_service_restart ):
12121214 """Reconfigure based on minigraph."""
12131215 log_info ("'load_minigraph' executing..." )
12141216
12151217 #Stop services before config push
12161218 if not no_service_restart :
12171219 log_info ("'load_minigraph' stopping services..." )
1218- _stop_services ()
1220+ _stop_services (db . cfgdb )
12191221
12201222 # For Single Asic platform the namespace list has the empty string
12211223 # for mulit Asic platform the empty string to generate the config
@@ -1269,10 +1271,10 @@ def load_minigraph(no_service_restart):
12691271 # We first run "systemctl reset-failed" to remove the "failed"
12701272 # status from all services before we attempt to restart them
12711273 if not no_service_restart :
1272- _reset_failed_services ()
1274+ _reset_failed_services (db . cfgdb )
12731275 #FIXME: After config DB daemon is implemented, we'll no longer need to restart every service.
12741276 log_info ("'load_minigraph' restarting services..." )
1275- _restart_services ()
1277+ _restart_services (db . cfgdb )
12761278 click .echo ("Please note setting loaded from minigraph will be lost after system reboot. To preserve setting, run `config save`." )
12771279
12781280
@@ -3810,25 +3812,27 @@ def feature():
38103812@feature .command ('state' , short_help = "Enable/disable a feature" )
38113813@click .argument ('name' , metavar = '<feature-name>' , required = True )
38123814@click .argument ('state' , metavar = '<state>' , required = True , type = click .Choice (["enabled" , "disabled" ]))
3813- def feature_state (name , state ):
3815+ @pass_db
3816+ def feature_state (db , name , state ):
38143817 """Enable/disable a feature"""
3815- state_data = config_db .get_entry ('FEATURE' , name )
3818+ state_data = db . cfgdb .get_entry ('FEATURE' , name )
38163819
38173820 if not state_data :
38183821 click .echo ("Feature '{}' doesn't exist" .format (name ))
38193822 sys .exit (1 )
38203823
3821- config_db .mod_entry ('FEATURE' , name , {'state' : state })
3824+ db . cfgdb .mod_entry ('FEATURE' , name , {'state' : state })
38223825
38233826#
38243827# 'autorestart' command ('config feature autorestart ...')
38253828#
38263829@feature .command (name = 'autorestart' , short_help = "Enable/disable autosrestart of a feature" )
38273830@click .argument ('name' , metavar = '<feature-name>' , required = True )
38283831@click .argument ('autorestart' , metavar = '<autorestart>' , required = True , type = click .Choice (["enabled" , "disabled" ]))
3829- def feature_autorestart (name , autorestart ):
3832+ @pass_db
3833+ def feature_autorestart (db , name , autorestart ):
38303834 """Enable/disable autorestart of a feature"""
3831- feature_table = config_db .get_table ('FEATURE' )
3835+ feature_table = db . cfgdb .get_table ('FEATURE' )
38323836 if not feature_table :
38333837 click .echo ("Unable to retrieve feature table from Config DB." )
38343838 sys .exit (1 )
@@ -3837,7 +3841,7 @@ def feature_autorestart(name, autorestart):
38373841 click .echo ("Unable to retrieve feature '{}'" .format (name ))
38383842 sys .exit (1 )
38393843
3840- config_db .mod_entry ('FEATURE' , name , {'auto_restart' : autorestart })
3844+ db . cfgdb .mod_entry ('FEATURE' , name , {'auto_restart' : autorestart })
38413845
38423846if __name__ == '__main__' :
38433847 config ()
0 commit comments