48
48
CFG_LOOPBACK_NAME_TOTAL_LEN_MAX = 11
49
49
CFG_LOOPBACK_ID_MAX_VAL = 999
50
50
CFG_LOOPBACK_NO = "<0-999>"
51
+
52
+ asic_type = None
53
+ config_db = None
54
+
51
55
# ========================== Syslog wrappers ==========================
52
56
53
57
def log_debug (msg ):
@@ -112,17 +116,6 @@ def get_command(self, ctx, cmd_name):
112
116
113
117
ctx .fail ('Too many matches: %s' % ', ' .join (sorted (matches )))
114
118
115
-
116
- #
117
- # Load asic_type for further use
118
- #
119
-
120
- try :
121
- version_info = sonic_device_util .get_sonic_version_info ()
122
- asic_type = version_info ['asic_type' ]
123
- except (KeyError , TypeError ):
124
- raise click .Abort ()
125
-
126
119
#
127
120
# Load breakout config file for Dynamic Port Breakout
128
121
#
@@ -552,7 +545,7 @@ def _is_neighbor_ipaddress(config_db, ipaddress):
552
545
553
546
def _get_all_neighbor_ipaddresses (config_db , ignore_local_hosts = False ):
554
547
"""Returns list of strings containing IP addresses of all BGP neighbors
555
- if the flag ignore_local_hosts is set to True, additional check to see if
548
+ if the flag ignore_local_hosts is set to True, additional check to see if
556
549
if the BGP neighbor AS number is same as local BGP AS number, if so ignore that neigbor.
557
550
"""
558
551
addrs = []
@@ -692,12 +685,12 @@ def _get_disabled_services_list():
692
685
log_warning ("Feature is None" )
693
686
continue
694
687
695
- status = feature_table [feature_name ]['status ' ]
696
- if not status :
697
- log_warning ("Status of feature '{}' is None" .format (feature_name ))
688
+ state = feature_table [feature_name ]['state ' ]
689
+ if not state :
690
+ log_warning ("Enable state of feature '{}' is None" .format (feature_name ))
698
691
continue
699
692
700
- if status == "disabled" :
693
+ if state == "disabled" :
701
694
disabled_services_list .append (feature_name )
702
695
else :
703
696
log_warning ("Unable to retreive FEATURE table" )
@@ -890,6 +883,19 @@ def validate_mirror_session_config(config_db, session_name, dst_port, src_port,
890
883
@click .group (cls = AbbreviationGroup , context_settings = CONTEXT_SETTINGS )
891
884
def config ():
892
885
"""SONiC command line - 'config' command"""
886
+ #
887
+ # Load asic_type for further use
888
+ #
889
+ global asic_type
890
+
891
+ try :
892
+ version_info = sonic_device_util .get_sonic_version_info ()
893
+ asic_type = version_info ['asic_type' ]
894
+ except KeyError , TypeError :
895
+ raise click .Abort ()
896
+
897
+ if asic_type == 'mellanox' :
898
+ platform .add_command (mlnx .mlnx )
893
899
894
900
# Load the global config file database_global.json once.
895
901
SonicDBConfig .load_sonic_global_db_config ()
@@ -899,6 +905,10 @@ def config():
899
905
900
906
SonicDBConfig .load_sonic_global_db_config ()
901
907
908
+ global config_db
909
+
910
+ config_db = ConfigDBConnector ()
911
+ config_db .connect ()
902
912
903
913
config .add_command (aaa .aaa )
904
914
config .add_command (aaa .tacacs )
@@ -1007,7 +1017,7 @@ def load(filename, yes):
1007
1017
# if any of the config files in linux host OR namespace is not present, return
1008
1018
if not os .path .isfile (file ):
1009
1019
click .echo ("The config_db file {} doesn't exist" .format (file ))
1010
- return
1020
+ return
1011
1021
1012
1022
if namespace is None :
1013
1023
command = "{} -j {} --write-to-db" .format (SONIC_CFGGEN_PATH , file )
@@ -1225,7 +1235,7 @@ def load_minigraph(no_service_restart):
1225
1235
1226
1236
if os .path .isfile ('/etc/sonic/acl.json' ):
1227
1237
run_command ("acl-loader update full /etc/sonic/acl.json" , display_cmd = True )
1228
-
1238
+
1229
1239
# Write latest db version string into db
1230
1240
db_migrator = '/usr/bin/db_migrator.py'
1231
1241
if os .path .isfile (db_migrator ) and os .access (db_migrator , os .X_OK ):
@@ -1235,7 +1245,7 @@ def load_minigraph(no_service_restart):
1235
1245
else :
1236
1246
cfggen_namespace_option = " -n {}" .format (namespace )
1237
1247
run_command (db_migrator + ' -o set_version' + cfggen_namespace_option )
1238
-
1248
+
1239
1249
# We first run "systemctl reset-failed" to remove the "failed"
1240
1250
# status from all services before we attempt to restart them
1241
1251
if not no_service_restart :
@@ -1891,7 +1901,7 @@ def add_snmp_agent_address(ctx, agentip, port, vrf):
1891
1901
#Construct SNMP_AGENT_ADDRESS_CONFIG table key in the format ip|<port>|<vrf>
1892
1902
key = agentip + '|'
1893
1903
if port :
1894
- key = key + port
1904
+ key = key + port
1895
1905
key = key + '|'
1896
1906
if vrf :
1897
1907
key = key + vrf
@@ -1912,7 +1922,7 @@ def del_snmp_agent_address(ctx, agentip, port, vrf):
1912
1922
1913
1923
key = agentip + '|'
1914
1924
if port :
1915
- key = key + port
1925
+ key = key + port
1916
1926
key = key + '|'
1917
1927
if vrf :
1918
1928
key = key + vrf
@@ -2174,7 +2184,7 @@ def all(verbose):
2174
2184
config_db = ConfigDBConnector (use_unix_socket_path = True , namespace = namespace )
2175
2185
config_db .connect ()
2176
2186
bgp_neighbor_ip_list = _get_all_neighbor_ipaddresses (config_db , ignore_local_hosts )
2177
- for ipaddress in bgp_neighbor_ip_list :
2187
+ for ipaddress in bgp_neighbor_ip_list :
2178
2188
_change_bgp_session_status_by_addr (config_db , ipaddress , 'up' , verbose )
2179
2189
2180
2190
# 'neighbor' subcommand
@@ -2463,7 +2473,7 @@ def breakout(ctx, interface_name, mode, verbose, force_remove_dependencies, load
2463
2473
sys .exit (0 )
2464
2474
2465
2475
def _get_all_mgmtinterface_keys ():
2466
- """Returns list of strings containing mgmt interface keys
2476
+ """Returns list of strings containing mgmt interface keys
2467
2477
"""
2468
2478
config_db = ConfigDBConnector ()
2469
2479
config_db .connect ()
@@ -3191,9 +3201,9 @@ def priority(ctx, interface_name, priority, status):
3191
3201
interface_name = interface_alias_to_name (interface_name )
3192
3202
if interface_name is None :
3193
3203
ctx .fail ("'interface_name' is None!" )
3194
-
3204
+
3195
3205
run_command ("pfc config priority {0} {1} {2}" .format (status , interface_name , priority ))
3196
-
3206
+
3197
3207
#
3198
3208
# 'platform' group ('config platform ...')
3199
3209
#
@@ -3202,9 +3212,6 @@ def priority(ctx, interface_name, priority, status):
3202
3212
def platform ():
3203
3213
"""Platform-related configuration tasks"""
3204
3214
3205
- if asic_type == 'mellanox' :
3206
- platform .add_command (mlnx .mlnx )
3207
-
3208
3215
# 'firmware' subgroup ("config platform firmware ...")
3209
3216
@platform .group (cls = AbbreviationGroup )
3210
3217
def firmware ():
@@ -3292,10 +3299,10 @@ def naming_mode_alias():
3292
3299
def is_loopback_name_valid (loopback_name ):
3293
3300
"""Loopback name validation
3294
3301
"""
3295
-
3302
+
3296
3303
if loopback_name [:CFG_LOOPBACK_PREFIX_LEN ] != CFG_LOOPBACK_PREFIX :
3297
3304
return False
3298
- if (loopback_name [CFG_LOOPBACK_PREFIX_LEN :].isdigit () is False or
3305
+ if (loopback_name [CFG_LOOPBACK_PREFIX_LEN :].isdigit () is False or
3299
3306
int (loopback_name [CFG_LOOPBACK_PREFIX_LEN :]) > CFG_LOOPBACK_ID_MAX_VAL ) :
3300
3307
return False
3301
3308
if len (loopback_name ) > CFG_LOOPBACK_NAME_TOTAL_LEN_MAX :
@@ -3462,7 +3469,7 @@ def add_ntp_server(ctx, ntp_ip_address):
3462
3469
if ntp_ip_address in ntp_servers :
3463
3470
click .echo ("NTP server {} is already configured" .format (ntp_ip_address ))
3464
3471
return
3465
- else :
3472
+ else :
3466
3473
db .set_entry ('NTP_SERVER' , ntp_ip_address , {'NULL' : 'NULL' })
3467
3474
click .echo ("NTP server {} added to configuration" .format (ntp_ip_address ))
3468
3475
try :
@@ -3483,7 +3490,7 @@ def del_ntp_server(ctx, ntp_ip_address):
3483
3490
if ntp_ip_address in ntp_servers :
3484
3491
db .set_entry ('NTP_SERVER' , '{}' .format (ntp_ip_address ), None )
3485
3492
click .echo ("NTP server {} removed from configuration" .format (ntp_ip_address ))
3486
- else :
3493
+ else :
3487
3494
ctx .fail ("NTP server {} is not configured." .format (ntp_ip_address ))
3488
3495
try :
3489
3496
click .echo ("Restarting ntp-config service..." )
@@ -3770,58 +3777,47 @@ def delete(ctx):
3770
3777
config_db .set_entry ('SFLOW' , 'global' , sflow_tbl ['global' ])
3771
3778
3772
3779
#
3773
- # 'feature' command ('config feature name state')
3774
- #
3775
- @config .command ('feature' )
3776
- @click .argument ('name' , metavar = '<feature-name>' , required = True )
3777
- @click .argument ('state' , metavar = '<feature-state>' , required = True , type = click .Choice (["enabled" , "disabled" ]))
3778
- def feature_status (name , state ):
3779
- """ Configure status of feature"""
3780
- config_db = ConfigDBConnector ()
3781
- config_db .connect ()
3782
- status_data = config_db .get_entry ('FEATURE' , name )
3783
-
3784
- if not status_data :
3785
- click .echo (" Feature '{}' doesn't exist" .format (name ))
3786
- return
3787
-
3788
- config_db .mod_entry ('FEATURE' , name , {'status' : state })
3789
-
3790
- #
3791
- # 'container' group ('config container ...')
3780
+ # 'feature' group ('config feature ...')
3792
3781
#
3793
- @config .group (cls = AbbreviationGroup , name = 'container ' , invoke_without_command = False )
3794
- def container ():
3795
- """Modify configuration of containers """
3782
+ @config .group (cls = AbbreviationGroup , name = 'feature ' , invoke_without_command = False )
3783
+ def feature ():
3784
+ """Modify configuration of features """
3796
3785
pass
3797
3786
3798
3787
#
3799
- # 'feature' group ('config container feature ...')
3788
+ # 'state' command ('config feature state ...')
3800
3789
#
3801
- @container .group (cls = AbbreviationGroup , name = 'feature' , invoke_without_command = False )
3802
- def feature ():
3803
- """Modify configuration of container features"""
3804
- pass
3790
+ @feature .command ('state' , short_help = "Enable/disable a feature" )
3791
+ @click .argument ('name' , metavar = '<feature-name>' , required = True )
3792
+ @click .argument ('state' , metavar = '<state>' , required = True , type = click .Choice (["enabled" , "disabled" ]))
3793
+ def feature_state (name , state ):
3794
+ """Enable/disable a feature"""
3795
+ state_data = config_db .get_entry ('FEATURE' , name )
3796
+
3797
+ if not state_data :
3798
+ click .echo ("Feature '{}' doesn't exist" .format (name ))
3799
+ sys .exit (1 )
3800
+
3801
+ config_db .mod_entry ('FEATURE' , name , {'state' : state })
3805
3802
3806
3803
#
3807
- # 'autorestart' subcommand ('config container feature autorestart ...')
3804
+ # 'autorestart' command ('config feature autorestart ...')
3808
3805
#
3809
- @feature .command (name = 'autorestart' , short_help = "Configure the status of autorestart feature for specific container" )
3810
- @click .argument ('container_name' , metavar = '<container_name>' , required = True )
3811
- @click .argument ('autorestart_status' , metavar = '<autorestart_status>' , required = True , type = click .Choice (["enabled" , "disabled" ]))
3812
- def autorestart (container_name , autorestart_status ):
3813
- config_db = ConfigDBConnector ()
3814
- config_db .connect ()
3815
- container_feature_table = config_db .get_table ('CONTAINER_FEATURE' )
3816
- if not container_feature_table :
3817
- click .echo ("Unable to retrieve container feature table from Config DB." )
3818
- return
3806
+ @feature .command (name = 'autorestart' , short_help = "Enable/disable autosrestart of a feature" )
3807
+ @click .argument ('name' , metavar = '<feature-name>' , required = True )
3808
+ @click .argument ('autorestart' , metavar = '<autorestart>' , required = True , type = click .Choice (["enabled" , "disabled" ]))
3809
+ def feature_autorestart (name , autorestart ):
3810
+ """Enable/disable autorestart of a feature"""
3811
+ feature_table = config_db .get_table ('FEATURE' )
3812
+ if not feature_table :
3813
+ click .echo ("Unable to retrieve feature table from Config DB." )
3814
+ sys .exit (1 )
3819
3815
3820
- if not container_feature_table .has_key (container_name ):
3821
- click .echo ("Unable to retrieve features for container '{}'" .format (container_name ))
3822
- return
3816
+ if not feature_table .has_key (name ):
3817
+ click .echo ("Unable to retrieve feature '{}'" .format (name ))
3818
+ sys . exit ( 1 )
3823
3819
3824
- config_db .mod_entry ('CONTAINER_FEATURE ' , container_name , {'auto_restart' : autorestart_status })
3820
+ config_db .mod_entry ('FEATURE ' , name , {'auto_restart' : autorestart })
3825
3821
3826
3822
if __name__ == '__main__' :
3827
3823
config ()
0 commit comments