Skip to content
Merged
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
82 changes: 62 additions & 20 deletions library/juniper_junos_software.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@
required: false
default: C(/var/tmp/) + filename portion of I(local_package)
type: path
pkg_set:
description:
- install software on the members in a mixed Virtual Chassis. Currently
we are not doing target package check this option is provided.
required: false
default: false
type: list
validate:
description:
- Whether or not to have the target Junos device should validate the
Expand Down Expand Up @@ -434,6 +441,8 @@ def myprogress(_, report):
junos_module.logger.info(report)
return myprogress

# global variable to be used in progress function param to sw.install
progress_report = ''

def main():
CHECKSUM_ALGORITHM_CHOICES = ['md5', 'sha1', 'sha256']
Expand All @@ -449,6 +458,9 @@ def main():
# Default is '/var/tmp/' + filename from the
# local_package option, if set.
default=None),
pkg_set=dict(required=False,
type='list',
default=None),
version=dict(required=False,
aliases=['target_version', 'new_version',
'desired_version'],
Expand Down Expand Up @@ -517,13 +529,14 @@ def main():
# Mutually exclusive options.
mutually_exclusive=[['issu', 'nssu']],
# One of local_package and remote_package is required.
required_one_of=[['local_package', 'remote_package']],
required_one_of=[['local_package', 'remote_package', 'pkg_set']],
supports_check_mode=True
)

# Straight from params
local_package = junos_module.params.pop('local_package')
remote_package = junos_module.params.pop('remote_package')
pkg_set = junos_module.params.pop('pkg_set')
target_version = junos_module.params.pop('version')
no_copy = junos_module.params.pop('no_copy')
reboot = junos_module.params.pop('reboot')
Expand Down Expand Up @@ -552,31 +565,39 @@ def main():
if url is not None and local_package is not None:
junos_module.fail_json(msg='There remote_package (%s) is a URL. '
'The local_package option is not allowed.' %
(remote_package))
remote_package)

if url is not None and no_copy is True:
junos_module.fail_json(msg='There remote_package (%s) is a URL. '
'The no_copy option is not allowed.' %
(remote_package))
remote_package)

if url is None:
local_filename = None
if local_package is not None:
# Expand out the path.
local_package = os.path.abspath(local_package)
(local_dir, local_filename) = os.path.split(local_package)
if local_filename == '':
junos_module.fail_json(msg='There is no filename component to '
'the local_package (%s).' %
(local_package))
else:
# Local package was not specified, so we must assume no_copy.
local_package)
elif remote_package is not None:
# remote package was, so we must assume no_copy.
no_copy = True
local_filename = None

if no_copy is False and not os.path.isfile(local_package):
junos_module.fail_json(msg='The local_package (%s) is not a valid '
'file on the local Ansible control '
'machine.' % (local_package))
if no_copy is False:
if local_package is not None and not os.path.isfile(local_package):
junos_module.fail_json(msg='The local_package (%s) is not a '
'valid file on the local Ansible '
'control machine.' % local_package)
elif pkg_set is not None:
pkg_set = [os.path.abspath(item) for item in pkg_set]
for pkg_set_item in pkg_set:
if not os.path.isfile(pkg_set_item):
junos_module.fail_json(
msg='The pkg (%s) is not a valid file on the local'
' Ansible control machine.' % pkg_set_item)

if remote_filename == '':
# Use the same name as local_filename
Expand All @@ -593,7 +614,7 @@ def main():
if no_copy is True:
cleanfs = False

if target_version is None:
if target_version is None and pkg_set is None:
target_version = parse_version_from_filename(remote_filename)
junos_module.logger.debug("New target version is: %s.", target_version)

Expand Down Expand Up @@ -638,6 +659,8 @@ def main():
install_params['package'] = url
elif local_package is not None:
install_params['package'] = local_package
elif pkg_set is not None:
install_params['pkg_set'] = pkg_set
else:
install_params['package'] = remote_filename
if remote_dir is not None:
Expand All @@ -653,18 +676,24 @@ def main():
install_params[key] = value
if kwargs is not None:
install_params.update(kwargs)
def progress(dev, report):
global progress_report
progress_report = progress_report + report
install_params['progress'] = progress
try:
junos_module.logger.debug("Install parameters are: %s",
str(install_params))
str(install_params))
junos_module.add_sw()
ok = junos_module.sw.install(**install_params)
if ok is not True:
results['msg'] = 'Unable to install the software'
results['msg'] = 'Unable to install the software, ' \
'potential reason: %s' % progress_report
junos_module.fail_json(**results)
results['msg'] = 'Package %s successfully installed.' % \
(install_params['package'])
junos_module.logger.debug('Package %s successfully installed.',
install_params['package'])
msg = 'Package %s successfully installed.' % (
install_params.get('package') or
install_params.get('pkg_set'))
results['msg'] = msg
junos_module.logger.debug(msg)
except (junos_module.pyez_exception.ConnectError,
junos_module.pyez_exception.RpcError) as ex:
results['msg'] = 'Installation failed. Error: %s' % str(ex)
Expand Down Expand Up @@ -705,7 +734,18 @@ def main():
if junos_module.dev.facts['_is_linux']:
got = resp.text
else:
got = resp.findtext(xpath)
# there are cases where rpc-reply will have multiple
# child element, hence lets work on parent.
# for ex:
# <rpc-reply><output>Rebooting fpc1</output>
# <request-reboot-results>
# <request-reboot-status reboot-time="1561371395">
# Shutdown at Mon Jun 24 10:16:35 2019.
# [pid 1949]
# </request-reboot-status>
# </request-reboot-results></rpc-reply>
obj = resp.getparent()
got = obj.findtext(xpath)
if got is not None:
results['msg'] += ' Reboot successfully initiated.'
break
Expand All @@ -714,7 +754,9 @@ def main():
# It only gets executed if the loop finished without
# hitting the break.
results['msg'] += ' Did not find expected response ' \
'from reboot RPC.'
'from reboot RPC. RPC response is ' \
'%s' % \
junos_module.etree.tostring(resp)
junos_module.fail_json(**results)
else:
results['msg'] += ' Reboot successfully initiated.'
Expand Down