class AbstractTask(ABC, Util):
"""Abstraction layer for creating a task on Juniper devices."""
def __init__(self, device: Device, config: dict) -> None:
"""Class constructor."""
...
@abstractmethod
def run(self) -> None:
"""Start task."""
pass
def pre_start(self) -> None:
"""
Initialize task before actually starting it.
Subclasses may override this method.
"""
pass
The main methods is run() and pre_start(), the latter may be used by subclasses to validate a custom configuration, for example. The state of the application when calling such methods is the following:
pre_start : called first, for each task. The device attribute, which is a local variable, is not connected yet, this allows changing the device configuration dynamically. In the following code extracted from SSHPassword task, it asks for user ssh password and set in the device:
def pre_start(self):
"""Ask password when needed."""
ssh_config = self.config['ssh']
needs_passwd = ssh_config.get('passwd') is None and \
ssh_config.get('ask_passwd') is True
if needs_passwd:
# ask user password
ssh_passwd = getpass.getpass(prompt='SSH password:')
# set auth password
self.dev._auth_password = ssh_passwd
run : abstract method that should have an implementation on subclasses. When called, the device is connected and ready.
Branch changes-modules-#15
Description
Once connected to Juno device, registered tasks can manage the device directly supplied by user configuration.
Specification
Full code of AbstractTask.
Note: truncated content for brevity
The main methods is
run()
andpre_start()
, the latter may be used by subclasses to validate a custom configuration, for example. The state of the application when calling such methods is the following:pre_start
: called first, for each task. Thedevice
attribute, which is a local variable, is not connected yet, this allows changing the device configuration dynamically. In the following code extracted from SSHPassword task, it asks for user ssh password and set in the device:run
: abstract method that should have an implementation on subclasses. When called, thedevice
is connected and ready.