Appendix: ZTP Python library

This appendix describes the importable ZTP Python library.

For more information about the ZTP process, see Zero Touch Provisioning.

ZTPClient

The ZTPClient communicates with the SR Linux ZTP process. The APIClient is the core object of the ZTPClient. Each use of the ZTPClient passes through a call to one of its methods.

The path to the API client class is as follows:

class ztpclient.ztpclient.APIClient(base_url=None)

def __init__(self):
        self.client = ztpclient.APIClient()
    def get_option(self, item):
        ret = self.client.option_list()
        return ret['message'].get(item, None)
    def find_current_version(self):
        response = self.client.image_list()
        if response
            image = response['message']
            if image and isinstance(image, list) and len(list) > 0:
                return image[0].replace('*', '')
        return None
    def perform_ztp(self):
        self.nos_install()
        self.nos_configure()
        self.disable_autoboot()
    def nos_install(self):
        ret = self.client.image_upgrade(srlinux_image_url, srlinux_image_md5_url)
        if ret:
            return int(ret['status'])
        return -1
    def nos_configure(self):
        ret = self.client.configure(srlinux_config_url)
        if ret:
            return int(ret['status'])
        return -1
    def disable_autoboot(self):
        ret = self.client.option_autoboot(ztpclient.ZtpStatus.disable)
        if ret:
            return int(ret['status'])
        return -1
    if __name__ == '__main__':
        ztp = ZTP()
        ztp.perform_ztp()

Functions

This section describes the possible functions.

chassis_control()

Lists control card information.

Table 1. chassis_control()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise. The message attribute contains dictionary with control card information.

Example

>>> client.chassis_control()
{u'status': 0, u'message': {u'operation': u'active'}}
>>> client.chassis_control()
{u'status': 0, u'message': {u'operation': u'standby'}}

chassis_linecards()

Lists line card information for the chassis.

Table 2. chassis_linecards()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise. The message attribute contains list of dicts, where list item is dict with line card information.

Example

>>> client.chassis_linecards()
{u'status': 0, u'message': [{u'card_type': 127, u'card_name': u'imm32-
100g-qsfp28+4-400g-qsfpdd', u'slot_num': 1}, {u'card_type': 0, 
u'card_name': u'empty', u'slot_num': 2}, {u'card_type': 0, 
u'card_name': u'empty', u'slot_num': 3}, {u'card_type': 127, 
u'card_name': u'imm32-100g-qsfp28+4-400g-qsfpdd', u'slot_num': 4}]}

configure(configurl)

Downloads the configuration from a specific configurl and applies the configuration to SR Linux. If SR Linux services are not running, the services are started and the configuration is applied.

Table 3. configure(configurl)

Information

Description

Arguments

configurl (string): the URL from where the configuration will be downloaded

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Example

image_activate(version)

Reboots the chassis to the image version provided. If the current active version is the same as the specified version, no action is performed. If there is no image in the chassis of the specified version, no action is performed. If the specified version is available, the chassis will be rebooted to that version.

Table 4. image_activate(version)

Information

Description

Arguments

version (string): the image version

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Note: This API may result in a chassis reboot to activate the image version.

Examples

>>> client.image_list()
{u'status': 0, u'message': [u'20.6.1-18836*', u'20.6.1-17740', 
u'20.6.1-17738']}
>>> client.image_activate('20.6.1-3333')
{u'status': 127, u'message': u'20.6.1-3333 is not available'}
>>> client.image_activate('20.6.1-18836')
{u'status': 127, u'message': u'20.6.1-18836 is current active version. 
No additional change required'

image_bootorder(bootorder)

Sets the image bootorder in the Grub configuration. On the next reboot, the chassis reboots to the first image in the list.

Table 5. image_bootorder(bootorder)

Information

Description

Arguments

bootorder (list): the image version list

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.image_bootorder(['20.6.1-18836','20.6.1-17740','20.6.1-
17738'])
{u'status': 0, u'message': None}
>>> client.image_bootorder('20.6.1-18836,20.6.1-17740,20.6.1-17738')
{u'status': 0, u'message': None}

image_delete(version)

Removes the specified image version from the chassis. If the specified version is not available in the chassis, no action is performed. If the specified version is the current active version in the chassis, no action is performed.

Table 6. image_delete(version)

Information

Description

Arguments

version (string): the image version

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.image_list()
{u'status': 0, u'message': [u'20.6.1-18836*', u'20.6.1-17740', 
u'20.6.1-17738']}
>>> client.image_delete('20.6.1-3333')
{u'status': 0, u'message': u'20.6.1-3333 version not available'}
>>> client.image_delete('20.6.1-18836')
{u'status': 127, u'message': u'Cannot remove active version'}

image_list()

Lists all currently available image versions on the hardware.

Table 7. image_list()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise. The message attribute contains the list of images. The list item followed by an asterisk (*) indicates the current active image version.

Note: The image_list does not indicate the boot order.

Examples

>>> client.image_list()
{u'status': 0, u'message': [u'20.6.1-18836*', u'20.6.1-17740', 
u'20.6.1-17738']}

image_upgrade(image_url, md5_url, options)

Performs an image upgrade.

Table 8. image_upgrade(image_url, md5_url, options)

Information

Description

Arguments

image_url (string): the URL from where the image should be downloaded

md5_url (string): The URL from where the pre calculated md5sum of the image should be downloaded. After the image is downloaded, the calculated md5sum is checked against the downloaded md5sum. If the values do not match, the image upgrade is discarded.

no_reboot (boolean): If set to true, a chassis reboot is not triggered after an image upgrade. The new image will not be taken into use until the next reboot. The default is false.

skip_check (boolean): If set to true, the status check of the autoboot parameter is skipped, and a forced upgrade is performed. If set to false, the image upgrade will only be performed if autoboot is enabled. The default is false.

not_active (boolean): If set to true, after an image install, the image will not be marked as the active image (that is, will not reboot to the upgrade image). The current working image is still marked as active. The default is false.

Note: Based on the setting and outcome, the chassis can be rebooted when invoking this API.
Note: To perform ZTP, the autoboot flag must be enabled.

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

option_autoboot(status)

Sets the autoboot option status. This option determines if autoboot should be performed during ZTP. If disabled, ZTP skips all steps and starts the SR Linux application.

Table 9. option_autoboot(status)

Information

Description

Arguments

status (ZtpStatus): ztpclient.ZtpStatus.enable to enable the option, ztpclient.ZtpStatus.disable otherwise

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_autoboot(ztpclient.ZtpStatus.enable)
{u'status': 0, u'message': None}

option_bootintf(interface)

Sets the interface to be used by ZTP in various procedures. The default value is mgmt0.

Table 10. option_bootintf(interface)

Information

Description

Arguments

interface (string): the Linux network interface name

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_bootintf('mgmt0')
{u'status': 0, u'message': None}

option_clientid(type)

Sets the client ID used by ZTP when performing a DHCP request. The possible values are serialid and mac. When serialid is selected, the chassis serial number is used as the client ID in the DHCP request. When mac is selected, the Linux interface hardware address (chassis MAC address) is used as client identifier.

Table 11. option_clientid(type)

Information

Description

Arguments

type (ZtpClientId): the client identifier type

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_clientid(ztpclient.ZtpClientId.serialid)
{u'status': 0, u'message': None}
>>> client.option_clientid(ztpclient.ZtpClientId.mac)
{u'status': 0, u'message': None}

option_downgrade(status)

Sets the downgrade option status of ZTP. When enabled, the option allows ZTP to perform a downgrade of the image (that is, move from a later version image to an earlier version). When the option is disabled, only upgrades are allowed.

Table 12. option_downgrade(status)

Information

Description

Arguments

status (ZtpStatus): ztpclient.ZtpStatus.enable to enable the option, ztpclient.ZtpStatus.disable otherwise

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_downgrade(ztpclient.ZtpStatus.enable)
{u'status': 0, u'message': None}

option_duration(timeout, retry)

Sets the timeout and retry parameters of the ZTP process. If not successful, the ZTP process keeps retrying for the specified timeout seconds. When the timeout is reached, the process stops. If the number of attempts are equal to the retry value, the specified action is taken. The default action is to reboot.

Table 13. option_duration(timeout, retry)

Information

Description

Arguments

timeout (int): the number of seconds to perform ZTP before it is marked as failed

retry (int): the number of attempts before stopping the ZTP process

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_duration(3600,3)
{u'status': 0, u'message': None}

option_formatovl(status)

Sets the formatovl option status of ZTP. When enabled, the option sets the srl.formatovl flag in the Grub configuration. On the next reboot, if the srl.formatovl flag is set, the NOKIA-DATA overlay file system is formatted. Any change performed on the overlay file system is removed.

Table 14. option_formatovl(status)

Information

Description

Arguments

status (ZtpStatus): ztpclient.ZtpStatus.enable to enable the option, ztpclient.ZtpStatus.disable otherwise

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_formatovl(ztpclient.ZtpStatus.enable)
{u'status': 0, u'message': None}

option_formatsrletc(status)

Sets the formatsrletc option status of ZTP. When enabled, the option sets the srl.formatetc flag in the Grub configuration. On the next reboot, if the srl.formatetc flag is set, the NOKIA-ETC overlay file system is formatted. Any change performed on the overlay file system will be removed.

Table 15. option_formatsrletc(status)

Information

Description

Arguments

status (ZtpStatus): ztpclient.ZtpStatus.enable to enable the option, ztpclient.ZtpStatus.disable otherwise

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_formatsrletc(ztpclient.ZtpStatus.enable)
{u'status': 0, u'message': None}

option_formatsrlopt(status)

When enabled, the option sets the srl.formatopt flag in the Grub configuration. On the next reboot, if the srl.formatopt flag is set, the NOKIA-OPT overlay file system is formatted. Any change performed on the overlay file system will be removed.

Table 16. option_formatsrlopt(status)

Information

Description

Arguments

status (ZtpStatus): ztpclient.ZtpStatus.enable to enable the option, ztpclient.ZtpStatus.disable otherwise

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_formatsrlopt(ztpclient.ZtpStatus.enable)
{u'status': 0, u'message': None}

option_list()

Lists all the options of the ZTP process.

Table 17. option_list()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_list()
{u'status': 0, u'message': {u'formatsrletc': False, u'retry': 3, 
u'bootintf': u'mgmt0', u'clientid': u'serialid', u'autoboot': False, 
u'srlflags': u'no-reboot', u'formatovl': False, u'formatsrlopt': 
False, u'timeout': 3600, u'downgrade': True, u'nosinstall': False}}

option_nosinstall(status)

Sets the nosinstall option status. This option determines if an image upgrade should be performed during ZTP. Only the image upgrade step is skipped. All other steps of ZTP are still performed.

Table 18. option_nosinstall(status)

Information

Description

Arguments

status (ZtpStatus): ztpclient.ZtpStatus.enable to enable the option, ztpclient.ZtpStatus.disable otherwise

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.option_nosinstall(ztpclient.ZtpStatus.enable)
{u'status': 0, u'message': None}

provision(provisionurl)

Downloads the provision script from a specific provisionurl and executes the script. The script could be either Python or Bash.

Table 19. provision(provisionurl)

Information

Description

Arguments

provisionurl (string): the URL from where the provisioning script will be downloaded

Returns

(dict) The API response as a Python dictionary. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Note: If the script returns a non-zero exit code, the status attribute in the return dictionary is set to non-zero. It could be possible that the provisioning script has a chassis reboot command and a chassis will reboot while executing this API.
Note: To perform ZTP, the autoboot flag must be enabled.

Examples

>>> client.provision('http://135.227.248.118/duts/IDNS1833F0766/
srlinux_ztp.py')

service_restart()

Restarts the ZTP service.

Table 20. service_restart()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary, including the service status in the message attribute. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.service_restart()
{u'status': 0, u'message': {u'status': u'Service started'}}

service_start()

Starts the ZTP service (if not already running).

Table 21. service_start()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary, including the service status in the message attribute. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.service_start()
{u'status': 0, u'message': {u'status': u'Service started'}}

service_status()

Gets the current status of the ZTP service. The ZTP service will be running as a systemd service. It can be checked manually by executing the systemctl status ztp command.

Table 22. service_status()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary, including the service status in the message attribute. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.service_status()
{u'status': 0, u'message': {u'status': u'Inactive'}}
>>> client.service_status()
{u'status': 0, u'message': {u'status': u'Active'}}

service_stop()

Stops the ZTP service (if already running).

Table 23. service_stop()

Information

Description

Arguments

Returns

(dict) The API response as a Python dictionary, including the service status in the message attribute. The status attribute is set to 0 if successful, or a non-zero value otherwise.

Examples

>>> client.service_stop()
{u'status': 0, u'message': {u'status': u'Service stopped'}}