mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Allow to to upgrade more than 1 node at a time. (#50780)
* Updates to software update * remove ip address * Fix version * Update author line * Review comment
This commit is contained in:
parent
c62a6988b1
commit
b56428c223
2 changed files with 248 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
# (c) 2018, NetApp, Inc
|
||||
# (c) 2018-2019, NetApp, Inc
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
@ -14,6 +14,7 @@ DOCUMENTATION = '''
|
|||
author: NetApp Ansible Team (@carchi8py) <ng-ansibleteam@netapp.com>
|
||||
description:
|
||||
- Update ONTAP software
|
||||
- Requires an https connection and is not supported over http
|
||||
extends_documentation_fragment:
|
||||
- netapp.na_ontap
|
||||
module: na_ontap_software_update
|
||||
|
@ -23,17 +24,19 @@ options:
|
|||
description:
|
||||
- Whether the specified ONTAP package should update or not.
|
||||
default: present
|
||||
node:
|
||||
nodes:
|
||||
description:
|
||||
- List of nodes to be updated, the nodes have to be a part of a HA Pair.
|
||||
aliases:
|
||||
- node
|
||||
package_version:
|
||||
required: true
|
||||
description:
|
||||
- Specifies the package version.
|
||||
- Specifies the package version to update software.
|
||||
package_url:
|
||||
required: true
|
||||
description:
|
||||
- Specifies the package URL.
|
||||
- Specifies the package URL to download the package.
|
||||
ignore_validation_warning:
|
||||
description:
|
||||
- Allows the update to continue if warnings are encountered during the validation phase.
|
||||
|
@ -48,7 +51,7 @@ EXAMPLES = """
|
|||
- name: ONTAP software update
|
||||
na_ontap_software_update:
|
||||
state: present
|
||||
node: laurentn-vsim1
|
||||
nodes: vsim1
|
||||
package_url: "{{ url }}"
|
||||
package_version: "{{ version_name }}"
|
||||
ignore_validation_warning: True
|
||||
|
@ -76,11 +79,10 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
||||
self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
|
||||
self.argument_spec.update(dict(
|
||||
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
|
||||
node=dict(required=False, type='list'),
|
||||
nodes=dict(required=False, type='list', aliases=["node"]),
|
||||
package_version=dict(required=True, type='str'),
|
||||
package_url=dict(required=True, type='str'),
|
||||
ignore_validation_warning=dict(required=False, type='bool', default=False)
|
||||
|
@ -120,8 +122,8 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
try:
|
||||
result = self.server.invoke_successfully(cluster_image_get_iter, enable_tunneling=True)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg='Error fetching cluster image details for %s: %s'
|
||||
% (self.parameters['node'], to_native(error)),
|
||||
self.module.fail_json(msg='Error fetching cluster image details: %s: %s'
|
||||
% (self.parameters['package_version'], to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
# return cluster image details
|
||||
if result.get_child_by_name('num-records') and \
|
||||
|
@ -129,10 +131,47 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
return True
|
||||
return None
|
||||
|
||||
def cluster_image_get_for_node(self, node_name):
|
||||
"""
|
||||
Get current cluster image info for given node
|
||||
"""
|
||||
cluster_image_get = netapp_utils.zapi.NaElement('cluster-image-get')
|
||||
cluster_image_get.add_new_child('node-id', node_name)
|
||||
try:
|
||||
self.server.invoke_successfully(cluster_image_get, enable_tunneling=True)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg='Error fetching cluster image details for %s: %s'
|
||||
% (node_name, to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
|
||||
def cluster_image_update_progress_get(self):
|
||||
"""
|
||||
Get current cluster image update progress info
|
||||
:return: Dictionary of cluster image update progress if query successful, else return None
|
||||
"""
|
||||
cluster_update_progress_get = netapp_utils.zapi.NaElement('cluster-image-update-progress-info')
|
||||
cluster_update_progress_info = dict()
|
||||
try:
|
||||
result = self.server.invoke_successfully(cluster_update_progress_get, enable_tunneling=True)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
# return empty dict on error to satisfy package delete upon image update
|
||||
if to_native(error.code) == 'Unexpected error' and self.parameters.get('https') is True:
|
||||
return cluster_update_progress_info
|
||||
else:
|
||||
self.module.fail_json(msg='Error fetching cluster image update progress details: %s'
|
||||
% (to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
# return cluster image update progress details
|
||||
if result.get_child_by_name('attributes').get_child_by_name('ndu-progress-info'):
|
||||
update_progress_info = result.get_child_by_name('attributes').get_child_by_name('ndu-progress-info')
|
||||
cluster_update_progress_info['overall_status'] = update_progress_info.get_child_content('overall-status')
|
||||
cluster_update_progress_info['completed_node_count'] = update_progress_info.\
|
||||
get_child_content('completed-node-count')
|
||||
return cluster_update_progress_info
|
||||
|
||||
def cluster_image_update(self):
|
||||
"""
|
||||
Update current cluster image
|
||||
:return: None
|
||||
"""
|
||||
cluster_update_info = netapp_utils.zapi.NaElement('cluster-image-update')
|
||||
cluster_update_info.add_new_child('package-version', self.parameters['package_version'])
|
||||
|
@ -140,7 +179,7 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
str(self.parameters['ignore_validation_warning']))
|
||||
if self.parameters.get('nodes'):
|
||||
cluster_nodes = netapp_utils.zapi.NaElement('nodes')
|
||||
for node in self.parameters['node']:
|
||||
for node in self.parameters['nodes']:
|
||||
cluster_nodes.add_new_child('node-name', node)
|
||||
cluster_update_info.add_child_elem(cluster_nodes)
|
||||
try:
|
||||
|
@ -169,6 +208,19 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
exception=traceback.format_exc())
|
||||
return False
|
||||
|
||||
def cluster_image_package_delete(self):
|
||||
"""
|
||||
Delete current cluster image package
|
||||
"""
|
||||
cluster_image_package_delete_info = netapp_utils.zapi.NaElement('cluster-image-package-delete')
|
||||
cluster_image_package_delete_info.add_new_child('package-version', self.parameters['package_version'])
|
||||
try:
|
||||
self.server.invoke_successfully(cluster_image_package_delete_info, enable_tunneling=True)
|
||||
except netapp_utils.zapi.NaApiError as error:
|
||||
self.module.fail_json(msg='Error deleting cluster image package for %s: %s'
|
||||
% (self.parameters['package_version'], to_native(error)),
|
||||
exception=traceback.format_exc())
|
||||
|
||||
def cluster_image_package_download_progress(self):
|
||||
"""
|
||||
Get current cluster image package download progress
|
||||
|
@ -192,15 +244,27 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
return cluster_download_progress_info
|
||||
return None
|
||||
|
||||
def autosupport_log(self):
|
||||
"""
|
||||
Autosupport log for software_update
|
||||
:return:
|
||||
"""
|
||||
results = netapp_utils.get_cserver(self.server)
|
||||
cserver = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=results)
|
||||
netapp_utils.ems_log_event("na_ontap_software_update", cserver)
|
||||
|
||||
def apply(self):
|
||||
"""
|
||||
Apply action to update ONTAP software
|
||||
"""
|
||||
if self.parameters.get('https') is not True:
|
||||
self.module.fail_json(msg='https parameter must be True')
|
||||
changed = False
|
||||
self.autosupport_log()
|
||||
current = self.cluster_image_get()
|
||||
results = netapp_utils.get_cserver(self.server)
|
||||
cserver = netapp_utils.setup_ontap_zapi(module=self.module, vserver=results)
|
||||
netapp_utils.ems_log_event("na_ontap_software_update", cserver)
|
||||
if self.parameters.get('nodes'):
|
||||
for node in self.parameters['nodes']:
|
||||
self.cluster_image_get_for_node(node)
|
||||
if self.parameters.get('state') == 'present' and current:
|
||||
package_exists = self.cluster_image_package_download()
|
||||
if package_exists is False:
|
||||
|
@ -217,6 +281,13 @@ class NetAppONTAPSoftwareUpdate(object):
|
|||
else:
|
||||
self.cluster_image_update()
|
||||
changed = True
|
||||
# delete package once update is completed
|
||||
cluster_update_progress = self.cluster_image_update_progress_get()
|
||||
while not cluster_update_progress or cluster_update_progress.get('overall_status') == 'in_progress':
|
||||
time.sleep(25)
|
||||
cluster_update_progress = self.cluster_image_update_progress_get()
|
||||
if cluster_update_progress.get('overall_status') == 'completed':
|
||||
self.cluster_image_package_delete()
|
||||
self.module.exit_json(changed=changed)
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,163 @@
|
|||
# (c) 2018, NetApp, Inc
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
''' unit tests ONTAP Ansible module: na_ontap_software_update '''
|
||||
|
||||
from __future__ import print_function
|
||||
import json
|
||||
import pytest
|
||||
|
||||
from units.compat import unittest
|
||||
from units.compat.mock import patch, Mock
|
||||
from ansible.module_utils import basic
|
||||
from ansible.module_utils._text import to_bytes
|
||||
import ansible.module_utils.netapp as netapp_utils
|
||||
|
||||
from ansible.modules.storage.netapp.na_ontap_software_update \
|
||||
import NetAppONTAPSoftwareUpdate as my_module # module under test
|
||||
|
||||
if not netapp_utils.has_netapp_lib():
|
||||
pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
|
||||
|
||||
|
||||
def set_module_args(args):
|
||||
"""prepare arguments so that they will be picked up during module creation"""
|
||||
args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
|
||||
basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access
|
||||
|
||||
|
||||
class AnsibleExitJson(Exception):
|
||||
"""Exception class to be raised by module.exit_json and caught by the test case"""
|
||||
pass
|
||||
|
||||
|
||||
class AnsibleFailJson(Exception):
|
||||
"""Exception class to be raised by module.fail_json and caught by the test case"""
|
||||
pass
|
||||
|
||||
|
||||
def exit_json(*args, **kwargs): # pylint: disable=unused-argument
|
||||
"""function to patch over exit_json; package return data into an exception"""
|
||||
if 'changed' not in kwargs:
|
||||
kwargs['changed'] = False
|
||||
raise AnsibleExitJson(kwargs)
|
||||
|
||||
|
||||
def fail_json(*args, **kwargs): # pylint: disable=unused-argument
|
||||
"""function to patch over fail_json; package return data into an exception"""
|
||||
kwargs['failed'] = True
|
||||
raise AnsibleFailJson(kwargs)
|
||||
|
||||
|
||||
class MockONTAPConnection(object):
|
||||
''' mock server connection to ONTAP host '''
|
||||
|
||||
def __init__(self, kind=None, parm1=None, parm2=None):
|
||||
''' save arguments '''
|
||||
self.type = kind
|
||||
self.parm1 = parm1
|
||||
self.parm2 = parm2
|
||||
self.xml_in = None
|
||||
self.xml_out = None
|
||||
|
||||
def invoke_successfully(self, xml, enable_tunneling): # pylint: disable=unused-argument
|
||||
''' mock invoke_successfully returning xml data '''
|
||||
self.xml_in = xml
|
||||
if self.type == 'software_update':
|
||||
xml = self.build_software_update_info(self.parm1, self.parm2)
|
||||
self.xml_out = xml
|
||||
return xml
|
||||
|
||||
def autosupport_log(self):
|
||||
''' mock autosupport log'''
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def build_software_update_info(status, node):
|
||||
''' build xml data for software-update-info '''
|
||||
xml = netapp_utils.zapi.NaElement('xml')
|
||||
data = {
|
||||
'num-records': 1,
|
||||
'attributes-list': {'cluster-image-info': {'node-id': node}},
|
||||
'progress-status': status,
|
||||
'attributes': {'ndu-progress-info': {'overall-status': 'completed',
|
||||
'completed-node-count': '0'}},
|
||||
}
|
||||
xml.translate_struct(data)
|
||||
print(xml.to_string())
|
||||
return xml
|
||||
|
||||
|
||||
class TestMyModule(unittest.TestCase):
|
||||
''' a group of related Unit Tests '''
|
||||
|
||||
def setUp(self):
|
||||
self.mock_module_helper = patch.multiple(basic.AnsibleModule,
|
||||
exit_json=exit_json,
|
||||
fail_json=fail_json)
|
||||
self.mock_module_helper.start()
|
||||
self.addCleanup(self.mock_module_helper.stop)
|
||||
self.server = MockONTAPConnection()
|
||||
self.use_vsim = False
|
||||
|
||||
def set_default_args(self):
|
||||
if self.use_vsim:
|
||||
hostname = '10.10.10.10'
|
||||
username = 'admin'
|
||||
password = 'admin'
|
||||
node = 'vsim1'
|
||||
package_version = 'Fattire__9.3.0'
|
||||
package_url = 'abc.com'
|
||||
else:
|
||||
hostname = 'hostname'
|
||||
username = 'username'
|
||||
password = 'password'
|
||||
node = 'abc'
|
||||
package_version = 'test'
|
||||
package_url = 'abc.com'
|
||||
return dict({
|
||||
'hostname': hostname,
|
||||
'username': username,
|
||||
'password': password,
|
||||
'nodes': node,
|
||||
'package_version': package_version,
|
||||
'package_url': package_url,
|
||||
'https': 'true'
|
||||
})
|
||||
|
||||
def test_module_fail_when_required_args_missing(self):
|
||||
''' required arguments are reported as errors '''
|
||||
with pytest.raises(AnsibleFailJson) as exc:
|
||||
set_module_args({})
|
||||
my_module()
|
||||
print('Info: %s' % exc.value.args[0]['msg'])
|
||||
|
||||
def test_ensure_image_get_called(self):
|
||||
''' a more interesting test '''
|
||||
set_module_args(self.set_default_args())
|
||||
my_obj = my_module()
|
||||
my_obj.server = self.server
|
||||
cluster_image_get = my_obj.cluster_image_get()
|
||||
print('Info: test_software_update_get: %s' % repr(cluster_image_get))
|
||||
assert cluster_image_get is None
|
||||
|
||||
def test_ensure_apply_for_update_called(self):
|
||||
''' updating software and checking idempotency '''
|
||||
module_args = {}
|
||||
module_args.update(self.set_default_args())
|
||||
module_args.update({'package_url': 'abc.com'})
|
||||
set_module_args(module_args)
|
||||
my_obj = my_module()
|
||||
my_obj.autosupport_log = Mock(return_value=None)
|
||||
if not self.use_vsim:
|
||||
my_obj.server = self.server
|
||||
with pytest.raises(AnsibleExitJson) as exc:
|
||||
my_obj.apply()
|
||||
print('Info: test_software_update_apply: %s' % repr(exc.value))
|
||||
assert not exc.value.args[0]['changed']
|
||||
if not self.use_vsim:
|
||||
my_obj.server = MockONTAPConnection('software_update', 'async_pkg_get_phase_complete', 'abc')
|
||||
with pytest.raises(AnsibleExitJson) as exc:
|
||||
my_obj.apply()
|
||||
print('Info: test_software_update_apply: %s' % repr(exc.value))
|
||||
assert exc.value.args[0]['changed']
|
Loading…
Reference in a new issue