| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022 Western Digital Corporation |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: ocapi_command |
| 11 |
version_added: 6.3.0 |
| 12 |
short_description: Manages Out-Of-Band controllers using Open Composable API (OCAPI) |
| 13 |
description: |
| 14 |
- Builds OCAPI URIs locally and sends them to remote OOB controllers to perform an action. |
| 15 |
- Manages OOB controller such as Indicator LED, Reboot, Power Mode, Firmware Update. |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: full |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
category: |
| 25 |
required: true |
| 26 |
description: |
| 27 |
- Category to execute on OOB controller. |
| 28 |
type: str |
| 29 |
command: |
| 30 |
required: true |
| 31 |
description: |
| 32 |
- Command to execute on OOB controller. |
| 33 |
type: str |
| 34 |
baseuri: |
| 35 |
required: true |
| 36 |
description: |
| 37 |
- Base URI of OOB controller. |
| 38 |
type: str |
| 39 |
proxy_slot_number: |
| 40 |
description: For proxied inband requests, the slot number of the IOM. Only applies if O(baseuri) is a proxy server. |
| 41 |
type: int |
| 42 |
update_image_path: |
| 43 |
description: |
| 44 |
- For O(command=FWUpload), the path on the local filesystem of the firmware update image. |
| 45 |
type: str |
| 46 |
job_name: |
| 47 |
description: |
| 48 |
- For O(command=DeleteJob) command, the name of the job to delete. |
| 49 |
type: str |
| 50 |
username: |
| 51 |
required: true |
| 52 |
description: |
| 53 |
- Username for authenticating to OOB controller. |
| 54 |
type: str |
| 55 |
password: |
| 56 |
required: true |
| 57 |
description: |
| 58 |
- Password for authenticating to OOB controller. |
| 59 |
type: str |
| 60 |
timeout: |
| 61 |
description: |
| 62 |
- Timeout in seconds for URL requests to OOB controller. |
| 63 |
default: 10 |
| 64 |
type: int |
| 65 |
|
| 66 |
author: "Mike Moerk (@mikemoerk)" |
| 67 |
""" |
| 68 |
|
| 69 |
EXAMPLES = r""" |
| 70 |
- name: Set the power state to low |
| 71 |
community.general.ocapi_command: |
| 72 |
category: Chassis |
| 73 |
command: PowerModeLow |
| 74 |
baseuri: "{{ baseuri }}" |
| 75 |
username: "{{ username }}" |
| 76 |
password: "{{ password }}" |
| 77 |
|
| 78 |
- name: Set the power state to normal |
| 79 |
community.general.ocapi_command: |
| 80 |
category: Chassis |
| 81 |
command: PowerModeNormal |
| 82 |
baseuri: "{{ baseuri }}" |
| 83 |
username: "{{ username }}" |
| 84 |
password: "{{ password }}" |
| 85 |
- name: Set chassis indicator LED to on |
| 86 |
community.general.ocapi_command: |
| 87 |
category: Chassis |
| 88 |
command: IndicatorLedOn |
| 89 |
baseuri: "{{ baseuri }}" |
| 90 |
proxy_slot_number: 2 |
| 91 |
username: "{{ username }}" |
| 92 |
password: "{{ password }}" |
| 93 |
- name: Set chassis indicator LED to off |
| 94 |
community.general.ocapi_command: |
| 95 |
category: Chassis |
| 96 |
command: IndicatorLedOff |
| 97 |
baseuri: "{{ baseuri }}" |
| 98 |
proxy_slot_number: 2 |
| 99 |
username: "{{ username }}" |
| 100 |
password: "{{ password }}" |
| 101 |
- name: Reset Enclosure |
| 102 |
community.general.ocapi_command: |
| 103 |
category: Systems |
| 104 |
command: PowerGracefulRestart |
| 105 |
baseuri: "{{ baseuri }}" |
| 106 |
proxy_slot_number: 2 |
| 107 |
username: "{{ username }}" |
| 108 |
password: "{{ password }}" |
| 109 |
- name: Firmware Upload |
| 110 |
community.general.ocapi_command: |
| 111 |
category: Update |
| 112 |
command: FWUpload |
| 113 |
baseuri: "iom1.wdc.com" |
| 114 |
proxy_slot_number: 2 |
| 115 |
username: "{{ username }}" |
| 116 |
password: "{{ password }}" |
| 117 |
update_image_path: "/path/to/firmware.tar.gz" |
| 118 |
- name: Firmware Update |
| 119 |
community.general.ocapi_command: |
| 120 |
category: Update |
| 121 |
command: FWUpdate |
| 122 |
baseuri: "iom1.wdc.com" |
| 123 |
proxy_slot_number: 2 |
| 124 |
username: "{{ username }}" |
| 125 |
password: "{{ password }}" |
| 126 |
- name: Firmware Activate |
| 127 |
community.general.ocapi_command: |
| 128 |
category: Update |
| 129 |
command: FWActivate |
| 130 |
baseuri: "iom1.wdc.com" |
| 131 |
proxy_slot_number: 2 |
| 132 |
username: "{{ username }}" |
| 133 |
password: "{{ password }}" |
| 134 |
- name: Delete Job |
| 135 |
community.general.ocapi_command: |
| 136 |
category: Jobs |
| 137 |
command: DeleteJob |
| 138 |
job_name: FirmwareUpdate |
| 139 |
baseuri: "{{ baseuri }}" |
| 140 |
proxy_slot_number: 2 |
| 141 |
username: "{{ username }}" |
| 142 |
password: "{{ password }}" |
| 143 |
""" |
| 144 |
|
| 145 |
RETURN = r""" |
| 146 |
msg: |
| 147 |
description: Message with action result or error description. |
| 148 |
returned: always |
| 149 |
type: str |
| 150 |
sample: "Action was successful" |
| 151 |
|
| 152 |
jobUri: |
| 153 |
description: URI to use to monitor status of the operation. Returned for async commands such as Firmware Update, Firmware |
| 154 |
Activate. |
| 155 |
returned: when supported |
| 156 |
type: str |
| 157 |
sample: "https://ioma.wdc.com/Storage/Devices/openflex-data24-usalp03020qb0003/Jobs/FirmwareUpdate/" |
| 158 |
|
| 159 |
operationStatusId: |
| 160 |
description: OCAPI State ID (see OCAPI documentation for possible values). |
| 161 |
returned: when supported |
| 162 |
type: int |
| 163 |
sample: 2 |
| 164 |
""" |
| 165 |
|
| 166 |
from urllib.parse import urljoin |
| 167 |
|
| 168 |
from ansible.module_utils.basic import AnsibleModule |
| 169 |
from ansible.module_utils.common.text.converters import to_native |
| 170 |
|
| 171 |
from ansible_collections.community.general.plugins.module_utils._ocapi_utils import OcapiUtils |
| 172 |
|
| 173 |
# More will be added as module features are expanded |
| 174 |
CATEGORY_COMMANDS_ALL = { |
| 175 |
"Chassis": ["IndicatorLedOn", "IndicatorLedOff", "PowerModeLow", "PowerModeNormal"], |
| 176 |
"Systems": ["PowerGracefulRestart"], |
| 177 |
"Update": ["FWUpload", "FWUpdate", "FWActivate"], |
| 178 |
"Jobs": ["DeleteJob"], |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
def main(): |
| 183 |
result = {} |
| 184 |
module = AnsibleModule( |
| 185 |
argument_spec=dict( |
| 186 |
category=dict(required=True), |
| 187 |
command=dict(required=True, type="str"), |
| 188 |
job_name=dict(type="str"), |
| 189 |
baseuri=dict(required=True, type="str"), |
| 190 |
proxy_slot_number=dict(type="int"), |
| 191 |
update_image_path=dict(type="str"), |
| 192 |
username=dict(required=True), |
| 193 |
password=dict(required=True, no_log=True), |
| 194 |
timeout=dict(type="int", default=10), |
| 195 |
), |
| 196 |
supports_check_mode=True, |
| 197 |
) |
| 198 |
|
| 199 |
category = module.params["category"] |
| 200 |
command = module.params["command"] |
| 201 |
|
| 202 |
# admin credentials used for authentication |
| 203 |
creds = {"user": module.params["username"], "pswd": module.params["password"]} |
| 204 |
|
| 205 |
# timeout |
| 206 |
timeout = module.params["timeout"] |
| 207 |
|
| 208 |
base_uri = f"https://{module.params['baseuri']}" |
| 209 |
proxy_slot_number = module.params.get("proxy_slot_number") |
| 210 |
ocapi_utils = OcapiUtils(creds, base_uri, proxy_slot_number, timeout, module) |
| 211 |
|
| 212 |
# Check that Category is valid |
| 213 |
if category not in CATEGORY_COMMANDS_ALL: |
| 214 |
module.fail_json(msg=f"Invalid Category '{category}'. Valid Categories = {list(CATEGORY_COMMANDS_ALL.keys())}") |
| 215 |
|
| 216 |
# Check that the command is valid |
| 217 |
if command not in CATEGORY_COMMANDS_ALL[category]: |
| 218 |
module.fail_json(msg=f"Invalid Command '{command}'. Valid Commands = {CATEGORY_COMMANDS_ALL[category]}") |
| 219 |
|
| 220 |
# Organize by Categories / Commands |
| 221 |
if category == "Chassis": |
| 222 |
if command.startswith("IndicatorLed"): |
| 223 |
result = ocapi_utils.manage_chassis_indicator_led(command) |
| 224 |
elif command.startswith("PowerMode"): |
| 225 |
result = ocapi_utils.manage_system_power(command) |
| 226 |
elif category == "Systems": |
| 227 |
if command.startswith("Power"): |
| 228 |
result = ocapi_utils.manage_system_power(command) |
| 229 |
elif category == "Update": |
| 230 |
if command == "FWUpload": |
| 231 |
update_image_path = module.params.get("update_image_path") |
| 232 |
if update_image_path is None: |
| 233 |
module.fail_json(msg="Missing update_image_path.") |
| 234 |
result = ocapi_utils.upload_firmware_image(update_image_path) |
| 235 |
elif command == "FWUpdate": |
| 236 |
result = ocapi_utils.update_firmware_image() |
| 237 |
elif command == "FWActivate": |
| 238 |
result = ocapi_utils.activate_firmware_image() |
| 239 |
elif category == "Jobs": |
| 240 |
if command == "DeleteJob": |
| 241 |
job_name = module.params.get("job_name") |
| 242 |
if job_name is None: |
| 243 |
module.fail_json("Missing job_name") |
| 244 |
job_uri = urljoin(base_uri, f"Jobs/{job_name}") |
| 245 |
result = ocapi_utils.delete_job(job_uri) |
| 246 |
|
| 247 |
if result["ret"] is False: |
| 248 |
module.fail_json(msg=to_native(result["msg"])) |
| 249 |
else: |
| 250 |
del result["ret"] |
| 251 |
changed = result.get("changed", True) |
| 252 |
session = result.get("session", dict()) |
| 253 |
kwargs = { |
| 254 |
"changed": changed, |
| 255 |
"session": session, |
| 256 |
"msg": "Action was successful." |
| 257 |
if not module.check_mode |
| 258 |
else result.get("msg", "No action performed in check mode."), |
| 259 |
} |
| 260 |
result_keys = [result_key for result_key in result if result_key not in kwargs] |
| 261 |
for result_key in result_keys: |
| 262 |
kwargs[result_key] = result[result_key] |
| 263 |
module.exit_json(**kwargs) |
| 264 |
|
| 265 |
|
| 266 |
if __name__ == "__main__": |
| 267 |
main() |