COSC Learning Lab

01_device_connect.py

Related Scripts:

Table of Contents

Preamble

Establish a context in which the sample script will succeed.


In [2]:
%run ../learning_lab/01_inventory_dismount_atomic.py
from basics.odl_http import http_history_clear
http_history_clear()


device_dismount(xrvr-511-53U)
device_dismount(xrvr-531)
Disconnecting xrvr-531
Dismounted 2 device(s), slept for 0.5 seconds

Documentation


In [1]:
help('learning_lab.01_device_connect')


Help on module learning_lab.01_device_connect in learning_lab:

NAME
    learning_lab.01_device_connect - Sample usage of composite of functions 'device_mount' and 'connected'.

DESCRIPTION
    Mount any one device that is configured and not mounted.
    Pause while the Controller connects to the device.
    
    An unreachable device will never connect, so a 'time out' is enforced.
    Repeat the demonstration on a different device if 'time out' occurs.
    
    Exit code is zero when one device is both mounted and connected, otherwise non-zero.

FUNCTIONS
    demonstrate(device_name)
        Mount *and* connect the specified device.
        
        The device must not be mounted already.
        The Controller will attempt connection to the device.
        Return True if connection succeeds before the time-out period elapses.
    
    main()
        Demonstrate on the unmounted devices, stopping when a connection to any device is established.
    
    mount_from_settings(device_name)
        Mount the specified device with the configured settings.

DATA
    config = {'network_device': {'xrvr-1': {'address': '172.16.1.76', 'pas...
    time_interval = 0.2
    time_out = 10.0

FILE
    /home/virl/git/cosc-learning-labs/src/learning_lab/01_device_connect.py


Implementation


In [2]:
from importlib import import_module
script = import_module('learning_lab.01_device_connect')
from inspect import getsource
print(getsource(script.main))


def main():
    """Demonstrate on the unmounted devices, stopping when a connection to any device is established."""
    unmounted_list = inventory_unmounted()
    if not unmounted_list:
        print('All configured devices are mounted. Demonstration cancelled.')
    else:
        for device_name in unmounted_list:
            if demonstrate(device_name):
                return os.EX_OK
    return os.EX_TEMPFAIL


In [3]:
print(getsource(script.demonstrate))


def demonstrate(device_name):
    """ Mount *and* connect the specified device.

        The device must not be mounted already.
        The Controller will attempt connection to the device.
        Return True if connection succeeds before the time-out period elapses.
    """
    mount_from_settings(device_name)
    time_accum = 0.0
    num_checks = 0
    while time_accum < time_out:
        num_checks += 1
        expanding_interval = time_interval * num_checks
        time_accum += expanding_interval  
        # Don't hammer the Controller or it will crash.
        # This not a denial-of-service (DOS) attack ;-)
        time.sleep(expanding_interval)
        print('connected(' + device_name, end='): ')
        if connected(device_name):
            print(True, 'after %s checks and %s seconds.' % (num_checks, time_accum))
            return True
        else:
            print(False)
            continue
    print('Unconnected after %s checks and %s seconds.' % (num_checks, time_accum))
    return False


In [4]:
print(getsource(script.mount_from_settings))


def mount_from_settings(device_name):
    """Mount the specified device with the configured settings."""
    device_config = config['network_device'][device_name]
    print('device_mount(' + device_name, *device_config.values(), sep=', ', end=')\n')
    device_mount(
        device_name,
        device_config['address'],
        device_config['port'],
        device_config['username'],
        device_config['password'])

Execution


In [6]:
run ../learning_lab/01_device_connect.py


device_mount(xrvr-1, 172.16.1.76, cisco, cisco, 830)
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
connected(xrvr-1): False
Unconnected after 10 checks and 11.000000000000002 seconds.
device_mount(xrvr-2, 172.16.1.75, cisco, cisco, 830)
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
connected(xrvr-2): False
Unconnected after 10 checks and 11.000000000000002 seconds.
exit code 75

HTTP


In [ ]:
from basics.odl_http import http_history
from basics.http import http_history_to_html
from IPython.core.display import HTML
HTML(http_history_to_html(http_history()))