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

[ZTP] Use config db instead of ZTP configuration profile while dhcp d… #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/etc/default/ztp
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ COVERAGE=""
# Change below to provide command coverage.py tool.
# (python3-coverage run --append is used if not specified)
COVERAGE_CMD=""

# Use ZTP configuration profile (default) or config_db
USE_DEFAULT_CONFIG="yes"
6 changes: 5 additions & 1 deletion src/usr/lib/ztp/sonic-ztp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ start()
# Custom ztp_cfg.json file
[ "${CONFIG_JSON}" != "" ] && CONFIG_JSON_ARGS="-C ${CONFIG_JSON}"

# Use ZTP configuration profile (default) or config_db
[ "${USE_DEFAULT_CONFIG}" = "no" ] && CONFIG_DB_ARGS="-o"


if [ "${COVERAGE}" = "yes" ]; then
if which python3-coverage > /dev/null; then
[ "${COVERAGE_CMD}" = "" ] && COVERAGE_EXP="python3-coverage run --append"
Expand All @@ -49,7 +53,7 @@ start()
fi

# Kickstart ZTP service daemon
${COVERAGE_EXP} ${ZTP_ENGINE} ${DEBUG_ARGS} ${TEST_ARGS} ${CONFIG_JSON_ARGS} &
${COVERAGE_EXP} ${ZTP_ENGINE} ${DEBUG_ARGS} ${TEST_ARGS} ${CONFIG_JSON_ARGS} ${CONFIG_DB_ARGS}&

ztp_engine_pid=$!
wait "$ztp_engine_pid"
Expand Down
66 changes: 44 additions & 22 deletions src/usr/lib/ztp/ztp-engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def __is_ztp_profile_active(self):
profile_active = True
return profile_active

def __link_scan(self):
def __link_scan(self, use_config_db):
'''!
Scan all in-band interface's operational status to detect a link up event
@return False - If a link scan did not detect at least one switch port link up event
Expand All @@ -217,15 +217,17 @@ def __link_scan(self):
self.__link_scan_enabled = None
return False

if self.__link_scan_enabled is None:
# Check if ZTP configuration is active
if self.__is_ztp_profile_active():
self.__link_scan_enabled = 'True'
else:
self.__link_scan_enabled = 'False'
if (not use_config_db) :

if self.__link_scan_enabled == 'False':
return False
if self.__link_scan_enabled is None:
# Check if ZTP configuration is active
if self.__is_ztp_profile_active():
self.__link_scan_enabled = 'True'
else:
self.__link_scan_enabled = 'False'

if self.__link_scan_enabled == 'False':
return False

# Populate data of all ztp eligible interfaces
link_scan_result = self.__detect_intf_state()
Expand Down Expand Up @@ -303,6 +305,15 @@ def __loadZTPProfile(self, event):
return True
return False

def __discoverOnly(self):
# Do not attempt to install ZTP configuration if working in unit test mode
if self.test_mode:
return False

cmd = getCfg('ztp-lib-dir')+'/ztp-profile.sh discoverOnly'
rc = runCommand(cmd, capture_stdout=False)
return True

def __createProvScriptJson(self):
'''!
Create ZTP JSON data to execute provisioning script specified by DHCP Option 239 URL.
Expand Down Expand Up @@ -545,7 +556,7 @@ def __processConfigSections(self):
# Check reboot on result flags
self.__rebootAction(section)

def __processZTPJson(self):
def __processZTPJson(self, use_config_db):
'''!
Process ZTP JSON file downloaded using URL provided by DHCP Option 67, DHCPv6 Option 59 or
local ZTP JSON file.
Expand Down Expand Up @@ -601,8 +612,9 @@ def __processZTPJson(self):

logger.info('Starting ZTP using JSON file %s at %s.' % (self.json_src, self.objztpJson['timestamp']))

# Initialize connectivity if not done already
self.__loadZTPProfile("resume")
if (not use_config_db):
# Initialize connectivity if not done already
self.__loadZTPProfile("resume")

# Process available configuration sections in ZTP JSON
self.__processConfigSections()
Expand Down Expand Up @@ -795,11 +807,12 @@ def __forceRestartDiscovery(self, msg):
# Restart link-scan
self.__intf_state = dict()

def executeLoop(self, test_mode=False):
def executeLoop(self, test_mode=False, use_config_db = False):
'''!
ZTP service loop which peforms provisioning data discovery and initiates processing.
'''

if (use_config_db == True):
logger.info('use config db...')
updateActivity('Initializing')

# Set testing mode
Expand Down Expand Up @@ -838,26 +851,29 @@ def executeLoop(self, test_mode=False):
logger.debug(' ' + str(l[3]))
self.__forceRestartDiscovery("Invalid provisioning data received")
continue

if result:
if self.ztp_mode == 'MANUAL_CONFIG':
logger.info("Configuration file '%s' detected. Shutting down ZTP service." % (getCfg('config-db-json')))
break
elif self.ztp_mode != 'DISCOVERY':
(rv, msg) = self.__processZTPJson()
(rv, msg) = self.__processZTPJson(use_config_db)
if rv == "retry":
self.ztp_mode = 'DISCOVERY'
elif rv == "restart":
self.__forceRestartDiscovery(msg)
else:
break

# Initialize in-band interfaces to establish connectivity if not done already
self.__loadZTPProfile("discovery")
logger.debug('Provisioning data not found.')
if (not use_config_db):
# Initialize in-band interfaces to establish connectivity if not done already
self.__loadZTPProfile("discovery")
logger.debug('Provisioning data not found.')
else:
logger.debug('calling __discoverOnly...')
self.__discoverOnly()

# Scan for inband interfaces to link up and restart interface connectivity
if self.__link_scan():
if self.__link_scan(use_config_db):
updateActivity('Restarting network discovery after link scan')
logger.info('Restarting network discovery after link scan.')
runCommand('systemctl restart interfaces-config', capture_stdout=False)
Expand Down Expand Up @@ -905,6 +921,7 @@ def main():
parser.add_argument("-d", "--debug", action="store_true", help="Turn on debug level logging")
parser.add_argument("-t", "--test", action="store_true", default=False, help="Start service in test mode with restricted functionality")
parser.add_argument("-C", "--config-json", metavar='FILE', default=None, help="ZTP service configuration file")
parser.add_argument("-o", "--use-config-db", action="store_true", default=False, help="Use current config_db, don't install ztp_config")

# Parse provided arguments
options = parser.parse_args()
Expand All @@ -920,6 +937,11 @@ def main():
else:
_test_mode = False

if options.use_config_db:
_use_config_db = True
else:
_use_config_db = False

# Parse user provided configuration file
cfg_json = options.config_json

Expand Down Expand Up @@ -967,7 +989,7 @@ def main():
objEngine = ZTPEngine()

# Run ZTP service to completion
objEngine.executeLoop(_test_mode)
objEngine.executeLoop(_test_mode, _use_config_db)

sys.exit(0)

Expand Down
29 changes: 29 additions & 0 deletions src/usr/lib/ztp/ztp-profile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ usage()
and start DHCP discovery
remove - If the switch is running ZTP configuration, reload startup configuration
or factory default configuration if startup configuration is missing.
discoverOnly - use the config_db.json and start DHCP discovery
EOF
}

Expand Down Expand Up @@ -229,6 +230,34 @@ if [ "$CMD" = "install" ] ; then
fi
fi

if [ "$CMD" = "discoverOnly" ] ; then
echo "System is discoverOnly"
# setup rsyslog forwarding
touch ${SYSLOG_CONF_FILE}
if [ "$(get_feature console-logging)" = "true" ]; then
echo ":programname, contains, \"sonic-ztp\" /dev/console" > ${SYSLOG_CONSOLE_CONF_FILE}
fi
systemctl restart rsyslog

#setup db
sonic-db-cli CONFIG_DB HSET "ZTP|mode" "inband" "true"
sonic-db-cli CONFIG_DB HSET "ZTP|mode" "out-of-band" "true"
sonic-db-cli CONFIG_DB HSET "ZTP|mode" "ipv4" "true"
sonic-db-cli CONFIG_DB HSET "ZTP|mode" "ipv6" "true"

ln -sf /usr/lib/ztp/dhcp/ztp-rsyslog /etc/dhcp/dhclient-exit-hooks.d/ztp-rsyslog
echo "Initiating ZTP discovery."


# Install DHCP policy for interfaces participating in ZTP
dhcp_policy_create
echo "Restarting network configuration."
updateActivity "Restarting network configuration"
systemctl restart interfaces-config
echo "Restarted network configuration."

fi

# Process ZTP profile remove request
if [ "$CMD" = "remove" ] ; then

Expand Down
Loading