| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2024, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: na_santricity_proxy_firmware_upload |
| 12 |
short_description: NetApp E-Series manage proxy firmware uploads. |
| 13 |
description: |
| 14 |
- Ensure specific firmware versions are available on SANtricity Web Services Proxy. |
| 15 |
author: |
| 16 |
- Nathan Swartz (@swartzn) |
| 17 |
- Vu Tran (@VuTran007) |
| 18 |
extends_documentation_fragment: |
| 19 |
- netapp_eseries.santricity.santricity.santricity_proxy_doc |
| 20 |
options: |
| 21 |
firmware: |
| 22 |
description: |
| 23 |
- List of paths and/or directories containing firmware/NVSRAM files. |
| 24 |
- All firmware/NVSRAM files that are not specified will be removed from the proxy if they exist. |
| 25 |
type: list |
| 26 |
elements: str |
| 27 |
required: false |
| 28 |
""" |
| 29 |
EXAMPLES = """ |
| 30 |
- name: Ensure proxy has the expected firmware versions. |
| 31 |
na_santricity_proxy_firmware_upload: |
| 32 |
api_url: "https://192.168.1.100:8443/devmgr/v2" |
| 33 |
api_username: "admin" |
| 34 |
api_password: "adminpass" |
| 35 |
validate_certs: true |
| 36 |
firmware: |
| 37 |
- "path/to/firmware/dlp_files" |
| 38 |
- "path/to/nvsram.dlp" |
| 39 |
- "path/to/firmware.dlp" |
| 40 |
""" |
| 41 |
RETURN = """ |
| 42 |
msg: |
| 43 |
description: Status and version of firmware and NVSRAM. |
| 44 |
type: str |
| 45 |
returned: always |
| 46 |
sample: |
| 47 |
""" |
| 48 |
import os |
| 49 |
|
| 50 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity import NetAppESeriesModule, create_multipart_formdata |
| 51 |
|
| 52 |
|
| 53 |
class NetAppESeriesProxyFirmwareUpload(NetAppESeriesModule): |
| 54 |
def __init__(self): |
| 55 |
ansible_options = dict(firmware=dict(type="list", elements="str", required=False)) |
| 56 |
super(NetAppESeriesProxyFirmwareUpload, self).__init__(ansible_options=ansible_options, |
| 57 |
web_services_version="02.00.0000.0000", |
| 58 |
supports_check_mode=True, |
| 59 |
proxy_specific_task=True) |
| 60 |
|
| 61 |
args = self.module.params |
| 62 |
self.firmware = args["firmware"] |
| 63 |
self.files = None |
| 64 |
self.add_files = [] |
| 65 |
self.remove_files = [] |
| 66 |
self.upload_failures = [] |
| 67 |
|
| 68 |
def determine_file_paths(self): |
| 69 |
"""Determine all the drive firmware file paths.""" |
| 70 |
self.files = {} |
| 71 |
if self.firmware: |
| 72 |
for firmware_path in self.firmware: |
| 73 |
|
| 74 |
if not os.path.exists(firmware_path): |
| 75 |
self.module.fail_json(msg="Drive firmware file does not exist! File [%s]" % firmware_path) |
| 76 |
elif os.path.isdir(firmware_path): |
| 77 |
if not firmware_path.endswith("/"): |
| 78 |
firmware_path = firmware_path + "/" |
| 79 |
|
| 80 |
for dir_filename in os.listdir(firmware_path): |
| 81 |
if ".dlp" in dir_filename: |
| 82 |
self.files.update({dir_filename: firmware_path + dir_filename}) |
| 83 |
elif ".dlp" in firmware_path: |
| 84 |
name = os.path.basename(firmware_path) |
| 85 |
self.files.update({name: firmware_path}) |
| 86 |
|
| 87 |
def determine_changes(self): |
| 88 |
"""Determine whether files need to be added or removed.""" |
| 89 |
try: |
| 90 |
rc, results = self.request("firmware/cfw-files") |
| 91 |
current_files = [result["filename"] for result in results] |
| 92 |
|
| 93 |
for current_file in current_files: |
| 94 |
if current_file not in self.files.keys(): |
| 95 |
self.remove_files.append(current_file) |
| 96 |
|
| 97 |
for expected_file in self.files.keys(): |
| 98 |
if expected_file not in current_files: |
| 99 |
self.add_files.append(expected_file) |
| 100 |
except Exception as error: |
| 101 |
self.module.fail_json(msg="Failed to retrieve current firmware file listing.") |
| 102 |
|
| 103 |
def upload_files(self): |
| 104 |
"""Upload firmware and nvsram file.""" |
| 105 |
for filename in self.add_files: |
| 106 |
fields = [("validate", "true")] |
| 107 |
files = [("firmwareFile", filename, self.files[filename])] |
| 108 |
headers, data = create_multipart_formdata(files=files, fields=fields) |
| 109 |
try: |
| 110 |
rc, response = self.request("firmware/upload/", method="POST", data=data, headers=headers) |
| 111 |
except Exception as error: |
| 112 |
self.upload_failures.append(filename) |
| 113 |
self.module.warn("Failed to upload firmware file. File [%s]" % filename) |
| 114 |
|
| 115 |
def delete_files(self): |
| 116 |
"""Remove firmware and nvsram file.""" |
| 117 |
for filename in self.remove_files: |
| 118 |
try: |
| 119 |
rc, response = self.request("firmware/upload/%s" % filename, method="DELETE") |
| 120 |
except Exception as error: |
| 121 |
self.upload_failures.append(filename) |
| 122 |
self.module.warn("Failed to delete firmware file. File [%s]" % filename) |
| 123 |
|
| 124 |
def apply(self): |
| 125 |
"""Upgrade controller firmware.""" |
| 126 |
change_required = False |
| 127 |
if not self.is_proxy(): |
| 128 |
self.module.fail_json(msg="Module can only be executed against SANtricity Web Services Proxy.") |
| 129 |
|
| 130 |
self.determine_file_paths() |
| 131 |
self.determine_changes() |
| 132 |
if self.add_files or self.remove_files: |
| 133 |
change_required = True |
| 134 |
|
| 135 |
if change_required and not self.module.check_mode: |
| 136 |
self.upload_files() |
| 137 |
self.delete_files() |
| 138 |
|
| 139 |
if self.upload_failures: |
| 140 |
self.module.fail_json(msg="Some file failed to be uploaded! changed=%s, Files_added [%s]. Files_removed [%s]. Upload_failures [%s]" |
| 141 |
% (change_required, self.add_files, self.remove_files, self.upload_failures)) |
| 142 |
self.module.exit_json(changed=change_required, files_added=self.add_files, files_removed=self.remove_files) |
| 143 |
|
| 144 |
|
| 145 |
def main(): |
| 146 |
proxy_firmware_upload = NetAppESeriesProxyFirmwareUpload() |
| 147 |
proxy_firmware_upload.apply() |
| 148 |
|
| 149 |
|
| 150 |
if __name__ == "__main__": |
| 151 |
main() |