jnpr.junos.utils.config.
Config
(dev, mode=None)[source]¶Bases: jnpr.junos.utils.util.Util
Overivew of Configuration Utilities:
commit()
: commit changescommit_check()
: perform the commit check operationdiff()
: return the diff string between running and candidate configload()
: load changes into the candidate configlock()
: take an exclusive lock on the candidate configpdiff()
: prints the diff string (debug/helper)rescue()
: controls “rescue configuration”rollback()
: perform the load rollback commandunlock()
: release the exclusive lock__init__
(dev, mode=None)[source]¶Parameters: | mode (str) – Can be used only when creating Config object using context manager
Example: # mode can be private/dynamic/exclusive/batch/ephemeral
with Config(dev, mode='exclusive') as cu:
cu.load('set system services netconf traceoptions file xyz', format='set')
print cu.diff()
cu.commit()
|
---|
commit
(**kvargs)[source]¶Commit a configuration.
Parameters: |
|
---|---|
Returns: |
|
Raises CommitError: | |
When errors detected in candidate configuration. You can use the Exception errs variable to identify the specific problems |
Warning
If the function does not receive a reply prior to the timeout a RpcTimeoutError will be raised. It is possible the commit was successful. Manual verification may be required.
commit_check
()[source]¶Perform a commit check. If the commit check passes, this function
will return True
. If the commit-check results in warnings, they
are reported and available in the Exception errs.
Returns: |
|
---|---|
Raises: |
|
diff
(rb_id=0)[source]¶Retrieve a diff (patch-format) report of the candidate config against either the current active config, or a different rollback.
Parameters: | rb_id (int) – rollback id [0..49] |
---|---|
Returns: |
|
load
(*vargs, **kvargs)[source]¶Loads changes into the candidate configuration. Changes can be in the form of strings (text,set,xml), XML objects, and files. Files can be either static snippets of configuration or Jinja2 templates. When using Jinja2 Templates, this method will render variables into the templates and then load the resulting change; i.e. “template building”.
Parameters: |
|
---|---|
Returns: | RPC-reply as XML object. |
Raises: | ConfigLoadError: When errors detected while loading candidate configuration. You can use the Exception errs variable to identify the specific problems |
lock
()[source]¶Attempts an exclusive lock on the candidate configuration. This is a non-blocking call.
Returns: | True always when successful |
---|---|
Raises LockError: | |
When the lock cannot be obtained |
pdiff
(rb_id=0)[source]¶Helper method that calls print
on the diff (patch-format) between the
current candidate and the provided rollback.
Parameters: | rb_id (int) – the rollback id value [0-49] |
---|---|
Returns: | None |
rescue
(action, format='text')[source]¶Perform action on the “rescue configuration”.
Parameters: |
|
---|---|
Returns: |
Note
Note The rescue configuration is only loaded as the candidate, and not committed. You must commit to make the rescue configuration active. |
Raises ValueError: | |
If action is not one of the above |
jnpr.junos.utils.fs.
FS
(dev)[source]¶Bases: jnpr.junos.utils.util.Util
Filesystem (FS) utilities:
cat()
: show the contents of a filechecksum()
: calculate file checksum (md5,sha256,sha1)cp()
: local file copy (not scp)cwd()
: change working directoryls()
: return file/dir listingmkdir()
: create a directorypwd()
: get working directorymv()
: local file renamerm()
: local file deletermdir()
: remove a directorystat()
: return file/dir informationstorage_usage()
: return storage usagestorage_cleanup()
: perform storage storage_cleanupstorage_cleanup_check()
: returns a list of files to remove at cleanupsymlink()
: create a symlinktgz()
: tar+gzip a directorycat
(path)[source]¶Returns the contents of the file path.
Parameters: | path (str) – File-path |
---|---|
Returns: | contents of the file (str) or None if file does not exist |
checksum
(path, calc='md5')[source]¶Performs the checksum command on the given file path using the
required calculation method and returns the string value.
If the path is not found on the device, then None
is returned.
Parameters: | |
---|---|
Returns: | checksum value (str) or |
cp
(from_path, to_path)[source]¶Perform a local file copy where from_path and to_path can be any valid Junos path argument. Refer to the Junos “file copy” command documentation for details.
Parameters: |
---|
Returns: | True if OK, False if file does not exist. |
---|
cwd
(path)[source]¶Change working directory to path.
Parameters: | path (str) – path to working directory |
---|
ls
(path='.', brief=False, followlink=True)[source]¶File listing, returns a dict of file information. If the path is a symlink, then by default followlink will recursively call this method to obtain the symlink specific information.
Parameters: |
|
---|---|
Returns: | dict collection of file information or |
mkdir
(path)[source]¶Executes the ‘mkdir -p’ command on path.
Warning
REQUIRES SHELL PRIVILEGES
Returns: | True if OK, error-message (str) otherwise |
---|
mv
(from_path, to_path)[source]¶Perform a local file rename function, same as “file rename” Junos CLI.
Returns: | True if OK, False if file does not exist. |
---|
rm
(path)[source]¶Performs a local file delete action, per Junos CLI command “file delete”.
Returns: | True when successful, False otherwise. |
---|
rmdir
(path)[source]¶Executes the ‘rmdir’ command on path.
Warning
REQUIRES SHELL PRIVILEGES
Parameters: | path (str) – file-path to directory |
---|---|
Returns: | True if OK, error-message (str) otherwise |
stat
(path)[source]¶Returns a dictionary of status information on the path, or None
if the path does not exist.
Parameters: | path (str) – file-path on local device |
---|---|
Returns: | status information on the file |
Return type: | dict |
storage_cleanup
()[source]¶Perform the ‘request system storage cleanup’ command to remove
files from the filesystem. Return a dict
of file name/info
on the files that were removed.
Returns: | dict on files that were removed |
---|
storage_cleanup_check
()[source]¶Perform the ‘request system storage cleanup dry-run’ command
to return a dict
of files/info that would be removed if
the cleanup command was executed.
Returns: | dict of files that would be removed (dry-run) |
---|
storage_usage
()[source]¶Returns the storage usage, similar to the unix “df” command.
Returns: | dict of storage usage |
---|
jnpr.junos.utils.scp.
SCP
(junos, **scpargs)[source]¶Bases: object
The SCP utility is used to conjunction with jnpr.junos.utils.sw.SW
when transferring the Junos image to the device. The SCP
can be
used for other secure-copy use-cases as well; it is implemented to support
the python context-manager pattern. For example:
from jnpr.junos.utils.scp import SCP
with SCP(dev, progress=True) as scp:
scp.put(package, remote_path)
__init__
(junos, **scpargs)[source]¶Constructor that wraps paramiko
and scp
related objects.
Parameters: |
|
---|
open
(**scpargs)[source]¶Creates an instance of the scp object and return to caller for use.
Note
This method uses the same username/password authentication
credentials as used by jnpr.junos.device.Device
.
Warning
The jnpr.junos.device.Device
ssh_private_key_file
option is currently not supported.
Todo
add support for ssh_private_key_file
.
Returns: | SCPClient object |
---|
jnpr.junos.utils.start_shell.
StartShell
(nc)[source]¶Bases: object
Junos shell execution utility. This utility is written to support the “context manager” design pattern. For example:
def _ssh_exec(self, command):
with StartShell(self._dev) as sh:
got = sh.run(command)
ok = sh.last_ok
return (ok, got)
open
()[source]¶Open an ssh-client connection and issue the ‘start shell’ command to
drop into the Junos shell (csh). This process opens a
paramiko.SSHClient
instance.
run
(command, this='% ')[source]¶Run a shell command and wait for the response. The return is a tuple. The first item is True/False if exit-code is 0. The second item is the output of the command.
Parameters: | |
---|---|
Returns: | result of the executed shell command (str) |
Note
as a side-effect this method will set the self.last_ok
property. This property is set to True
if $?
is
“0”; indicating the last shell command was successful.
jnpr.junos.utils.sw.
SW
(dev)[source]¶Bases: jnpr.junos.utils.util.Util
Software Utility class, used to perform a software upgrade and associated functions. These methods have been tested on simple deployments. Refer to install for restricted use-cases for software upgrades.
install()
: perform the entire software installation processreboot()
: reboots the system for the new image to take effectpoweroff()
: shutdown the systemput()
: SCP put package file onto Junos devicepkgadd()
: performs the ‘request’ operation to install the packagevalidate()
: performs the ‘request’ to validate the packageinstall
(package=None, pkg_set=None, remote_path='/var/tmp', progress=None, validate=False, checksum=None, cleanfs=True, no_copy=False, timeout=1800, **kwargs)[source]¶Performs the complete installation of the package that includes the following steps:
Warning
This process has been validated on the following deployments.
Tested:
Known Restrictions:
You can get a progress report on this process by providing a progress callback.
Note
You will need to invoke the reboot()
method explicitly to reboot
the device.
Parameters: |
|
---|
inventory
¶Returns dictionary of file listing information for current and rollback Junos install packages. This information comes from the /packages directory.
Warning
Experimental method; may not work on all platforms. If you find this not working, please report issue.
local_md5
(package)[source]¶Computes the MD5 checksum value on the local package file.
Parameters: | package (str) – File-path to the package (*.tgz) file on the local server |
---|---|
Returns: | MD5 checksum (str) |
Raises IOError: | when package file does not exist |
local_sha1
(package)[source]¶Computes the SHA1 checksum value on the local package file.
Parameters: | package (str) – File-path to the package (*.tgz) file on the local server |
---|---|
Returns: | SHA1 checksum (str) |
Raises IOError: | when package file does not exist |
local_sha256
(package)[source]¶Computes the SHA-256 value on the package file.
Parameters: | package (str) – File-path to the package (*.tgz) file on the local server |
---|---|
Returns: | SHA-256 checksum (str) |
Raises IOError: | when package file does not exist |
pkgadd
(remote_package, **kvargs)[source]¶Issue the ‘request system software add’ command on the package.
The “no-validate” options is set by default. If you want to validate
the image, do that using the specific validate()
method. Also,
if you want to reboot the device, suggest using the reboot()
method
rather reboot=True
.
Parameters: |
|
---|
Todo
Add way to notify user why installation failed.
Warning
Refer to the restrictions listed in install()
.
poweroff
(in_min=0)[source]¶Perform a system shutdown, with optional delay (in minutes) .
If the device is equipped with dual-RE, then both RE will be rebooted. This code also handles EX/QFX VC.
Parameters: | in_min (int) – time (minutes) before rebooting the device. |
---|---|
Returns: |
|
Raises RpcError: | |
when command is not successful. |
Todo
need to better handle the exception event.
put
(package, remote_path='/var/tmp', progress=None)[source]¶SCP ‘put’ the package file from the local server to the remote device.
Parameters: |
|
---|
reboot
(in_min=0, at=None)[source]¶Perform a system reboot, with optional delay (in minutes) or at a specified date and time.
If the device is equipped with dual-RE, then both RE will be rebooted. This code also handles EX/QFX VC.
Parameters: | |
---|---|
Returns: |
|
Raises RpcError: | |
when command is not successful. |
Todo
need to better handle the exception event.
remote_checksum
(remote_package, timeout=300)[source]¶Computes the MD5 checksum on the remote device.
Parameters: |
|
---|---|
Returns: |
|
Raises RpcError: | |
RPC errors other than remote_package not found. |
rollback
()[source]¶Issues the ‘request’ command to do the rollback and returns the string output of the results.
Returns: | Rollback results (str) |
---|
safe_copy
(package, **kvargs)[source]¶Copy the install package safely to the remote device. By default this means to clean the filesystem to make space, perform the secure-copy, and then verify the MD5 checksum.
Parameters: |
|
---|---|
Returns: |
|