| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Benjamin Copeland (@bhcopeland) <[email protected]> |
| 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: statusio_maintenance |
| 11 |
short_description: Create maintenance windows for your status.io dashboard |
| 12 |
description: |
| 13 |
- Creates or deletes a maintenance window for status.io. |
| 14 |
notes: |
| 15 |
- You can use the apiary API URL (U(http://docs.statusio.apiary.io/)) to capture API traffic. |
| 16 |
- Use start_date and start_time with minutes to set future maintenance window. |
| 17 |
author: Benjamin Copeland (@bhcopeland) <[email protected]> |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
title: |
| 27 |
type: str |
| 28 |
description: |
| 29 |
- A descriptive title for the maintenance window. |
| 30 |
default: "A new maintenance window" |
| 31 |
desc: |
| 32 |
type: str |
| 33 |
description: |
| 34 |
- Message describing the maintenance window. |
| 35 |
default: "Created by Ansible" |
| 36 |
state: |
| 37 |
type: str |
| 38 |
description: |
| 39 |
- Desired state of the package. |
| 40 |
default: "present" |
| 41 |
choices: ["present", "absent"] |
| 42 |
api_id: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- Your unique API ID from status.io. |
| 46 |
required: true |
| 47 |
api_key: |
| 48 |
type: str |
| 49 |
description: |
| 50 |
- Your unique API Key from status.io. |
| 51 |
required: true |
| 52 |
statuspage: |
| 53 |
type: str |
| 54 |
description: |
| 55 |
- Your unique StatusPage ID from status.io. |
| 56 |
required: true |
| 57 |
url: |
| 58 |
type: str |
| 59 |
description: |
| 60 |
- Status.io API URL. A private apiary can be used instead. |
| 61 |
default: "https://api.status.io" |
| 62 |
components: |
| 63 |
type: list |
| 64 |
elements: str |
| 65 |
description: |
| 66 |
- The given name of your component (server name). |
| 67 |
aliases: ['component'] |
| 68 |
containers: |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
description: |
| 72 |
- The given name of your container (data center). |
| 73 |
aliases: ['container'] |
| 74 |
all_infrastructure_affected: |
| 75 |
description: |
| 76 |
- If it affects all components and containers. |
| 77 |
type: bool |
| 78 |
default: false |
| 79 |
automation: |
| 80 |
description: |
| 81 |
- Automatically start and end the maintenance window. |
| 82 |
type: bool |
| 83 |
default: false |
| 84 |
maintenance_notify_now: |
| 85 |
description: |
| 86 |
- Notify subscribers now. |
| 87 |
type: bool |
| 88 |
default: false |
| 89 |
maintenance_notify_72_hr: |
| 90 |
description: |
| 91 |
- Notify subscribers 72 hours before maintenance start time. |
| 92 |
type: bool |
| 93 |
default: false |
| 94 |
maintenance_notify_24_hr: |
| 95 |
description: |
| 96 |
- Notify subscribers 24 hours before maintenance start time. |
| 97 |
type: bool |
| 98 |
default: false |
| 99 |
maintenance_notify_1_hr: |
| 100 |
description: |
| 101 |
- Notify subscribers 1 hour before maintenance start time. |
| 102 |
type: bool |
| 103 |
default: false |
| 104 |
maintenance_id: |
| 105 |
type: str |
| 106 |
description: |
| 107 |
- The maintenance ID number when deleting a maintenance window. |
| 108 |
minutes: |
| 109 |
type: int |
| 110 |
description: |
| 111 |
- The duration of the maintenance window (starting from playbook runtime). |
| 112 |
default: 10 |
| 113 |
start_date: |
| 114 |
type: str |
| 115 |
description: |
| 116 |
- Date maintenance is expected to start (Month/Day/Year) (UTC). |
| 117 |
- End Date is worked out from O(start_date) + O(minutes). |
| 118 |
start_time: |
| 119 |
type: str |
| 120 |
description: |
| 121 |
- Time maintenance is expected to start (Hour:Minutes) (UTC). |
| 122 |
- End Time is worked out from O(start_time) + O(minutes). |
| 123 |
""" |
| 124 |
|
| 125 |
EXAMPLES = r""" |
| 126 |
- name: Create a maintenance window for 10 minutes on server1, with automation to stop the maintenance |
| 127 |
community.general.statusio_maintenance: |
| 128 |
title: Router Upgrade from ansible |
| 129 |
desc: Performing a Router Upgrade |
| 130 |
components: server1.example.com |
| 131 |
api_id: api_id |
| 132 |
api_key: api_key |
| 133 |
statuspage: statuspage_id |
| 134 |
maintenance_notify_1_hr: true |
| 135 |
automation: true |
| 136 |
|
| 137 |
- name: Create a maintenance window for 60 minutes on server1 and server2 |
| 138 |
community.general.statusio_maintenance: |
| 139 |
title: Routine maintenance |
| 140 |
desc: Some security updates |
| 141 |
components: |
| 142 |
- server1.example.com |
| 143 |
- server2.example.com |
| 144 |
minutes: 60 |
| 145 |
api_id: api_id |
| 146 |
api_key: api_key |
| 147 |
statuspage: statuspage_id |
| 148 |
maintenance_notify_1_hr: true |
| 149 |
automation: true |
| 150 |
delegate_to: localhost |
| 151 |
|
| 152 |
- name: Create a future maintenance window for 24 hours to all hosts inside the Primary Data Center |
| 153 |
community.general.statusio_maintenance: |
| 154 |
title: Data center downtime |
| 155 |
desc: Performing a Upgrade to our data center |
| 156 |
components: Primary Data Center |
| 157 |
api_id: api_id |
| 158 |
api_key: api_key |
| 159 |
statuspage: statuspage_id |
| 160 |
start_date: 01/01/2016 |
| 161 |
start_time: 12:00 |
| 162 |
minutes: 1440 |
| 163 |
|
| 164 |
- name: Delete a maintenance window |
| 165 |
community.general.statusio_maintenance: |
| 166 |
title: Remove a maintenance window |
| 167 |
maintenance_id: 561f90faf74bc94a4700087b |
| 168 |
statuspage: statuspage_id |
| 169 |
api_id: api_id |
| 170 |
api_key: api_key |
| 171 |
state: absent |
| 172 |
""" |
| 173 |
# TODO: Add RETURN documentation. |
| 174 |
RETURN = """ # """ |
| 175 |
|
| 176 |
import datetime |
| 177 |
import json |
| 178 |
|
| 179 |
from ansible.module_utils.basic import AnsibleModule |
| 180 |
from ansible.module_utils.urls import open_url |
| 181 |
|
| 182 |
from ansible_collections.community.general.plugins.module_utils._datetime import ( |
| 183 |
now, |
| 184 |
) |
| 185 |
|
| 186 |
|
| 187 |
def get_api_auth_headers(api_id, api_key, url, statuspage): |
| 188 |
headers = {"x-api-id": api_id, "x-api-key": api_key, "Content-Type": "application/json"} |
| 189 |
|
| 190 |
try: |
| 191 |
response = open_url(f"{url}/v2/component/list/{statuspage}", headers=headers) |
| 192 |
data = json.loads(response.read()) |
| 193 |
if data["status"]["message"] == "Authentication failed": |
| 194 |
return 1, None, None, "Authentication failed: Check api_id/api_key and statuspage id." |
| 195 |
else: |
| 196 |
auth_headers = headers |
| 197 |
auth_content = data |
| 198 |
except Exception as e: |
| 199 |
return 1, None, None, f"{e}" |
| 200 |
return 0, auth_headers, auth_content, None |
| 201 |
|
| 202 |
|
| 203 |
def get_component_ids(auth_content, components): |
| 204 |
host_ids = [] |
| 205 |
lower_components = [x.lower() for x in components] |
| 206 |
for result in auth_content["result"]: |
| 207 |
if result["name"].lower() in lower_components: |
| 208 |
data = {"component_id": result["_id"], "container_id": result["containers"][0]["_id"]} |
| 209 |
host_ids.append(data) |
| 210 |
lower_components.remove(result["name"].lower()) |
| 211 |
if len(lower_components): |
| 212 |
# items not found in the api |
| 213 |
return 1, None, lower_components |
| 214 |
return 0, host_ids, None |
| 215 |
|
| 216 |
|
| 217 |
def get_container_ids(auth_content, containers): |
| 218 |
host_ids = [] |
| 219 |
lower_containers = [x.lower() for x in containers] |
| 220 |
for result in auth_content["result"]: |
| 221 |
if result["containers"][0]["name"].lower() in lower_containers: |
| 222 |
data = {"component_id": result["_id"], "container_id": result["containers"][0]["_id"]} |
| 223 |
host_ids.append(data) |
| 224 |
lower_containers.remove(result["containers"][0]["name"].lower()) |
| 225 |
|
| 226 |
if len(lower_containers): |
| 227 |
# items not found in the api |
| 228 |
return 1, None, lower_containers |
| 229 |
return 0, host_ids, None |
| 230 |
|
| 231 |
|
| 232 |
def get_date_time(start_date, start_time, minutes): |
| 233 |
returned_date = [] |
| 234 |
if start_date and start_time: |
| 235 |
try: |
| 236 |
datetime.datetime.strptime(start_date, "%m/%d/%Y") |
| 237 |
returned_date.append(start_date) |
| 238 |
except (NameError, ValueError): |
| 239 |
return 1, None, "Not a valid start_date format." |
| 240 |
try: |
| 241 |
datetime.datetime.strptime(start_time, "%H:%M") |
| 242 |
returned_date.append(start_time) |
| 243 |
except (NameError, ValueError): |
| 244 |
return 1, None, "Not a valid start_time format." |
| 245 |
try: |
| 246 |
# Work out end date/time based on minutes |
| 247 |
date_time_start = datetime.datetime.strptime(start_time + start_date, "%H:%M%m/%d/%Y") |
| 248 |
delta = date_time_start + datetime.timedelta(minutes=minutes) |
| 249 |
returned_date.append(delta.strftime("%m/%d/%Y")) |
| 250 |
returned_date.append(delta.strftime("%H:%M")) |
| 251 |
except (NameError, ValueError): |
| 252 |
return 1, None, "Couldn't work out a valid date" |
| 253 |
else: |
| 254 |
now_t = now() |
| 255 |
delta = now_t + datetime.timedelta(minutes=minutes) |
| 256 |
# start_date |
| 257 |
returned_date.append(now_t.strftime("%m/%d/%Y")) |
| 258 |
returned_date.append(now_t.strftime("%H:%M")) |
| 259 |
# end_date |
| 260 |
returned_date.append(delta.strftime("%m/%d/%Y")) |
| 261 |
returned_date.append(delta.strftime("%H:%M")) |
| 262 |
return 0, returned_date, None |
| 263 |
|
| 264 |
|
| 265 |
def create_maintenance( |
| 266 |
auth_headers, |
| 267 |
url, |
| 268 |
statuspage, |
| 269 |
host_ids, |
| 270 |
all_infrastructure_affected, |
| 271 |
automation, |
| 272 |
title, |
| 273 |
desc, |
| 274 |
returned_date, |
| 275 |
maintenance_notify_now, |
| 276 |
maintenance_notify_72_hr, |
| 277 |
maintenance_notify_24_hr, |
| 278 |
maintenance_notify_1_hr, |
| 279 |
): |
| 280 |
component_id = [] |
| 281 |
container_id = [] |
| 282 |
for val in host_ids: |
| 283 |
component_id.append(val["component_id"]) |
| 284 |
container_id.append(val["container_id"]) |
| 285 |
infrastructure_id = [f"{i}-{j}" for i, j in zip(component_id, container_id)] |
| 286 |
try: |
| 287 |
values = json.dumps( |
| 288 |
{ |
| 289 |
"statuspage_id": statuspage, |
| 290 |
"all_infrastructure_affected": str(int(all_infrastructure_affected)), |
| 291 |
"infrastructure_affected": infrastructure_id, |
| 292 |
"automation": str(int(automation)), |
| 293 |
"maintenance_name": title, |
| 294 |
"maintenance_details": desc, |
| 295 |
"date_planned_start": returned_date[0], |
| 296 |
"time_planned_start": returned_date[1], |
| 297 |
"date_planned_end": returned_date[2], |
| 298 |
"time_planned_end": returned_date[3], |
| 299 |
"maintenance_notify_now": str(int(maintenance_notify_now)), |
| 300 |
"maintenance_notify_72_hr": str(int(maintenance_notify_72_hr)), |
| 301 |
"maintenance_notify_24_hr": str(int(maintenance_notify_24_hr)), |
| 302 |
"maintenance_notify_1_hr": str(int(maintenance_notify_1_hr)), |
| 303 |
} |
| 304 |
) |
| 305 |
response = open_url(f"{url}/v2/maintenance/schedule", data=values, headers=auth_headers) |
| 306 |
data = json.loads(response.read()) |
| 307 |
|
| 308 |
if data["status"]["error"] == "yes": |
| 309 |
return 1, None, data["status"]["message"] |
| 310 |
except Exception as e: |
| 311 |
return 1, None, f"{e}" |
| 312 |
return 0, None, None |
| 313 |
|
| 314 |
|
| 315 |
def delete_maintenance(auth_headers, url, statuspage, maintenance_id): |
| 316 |
try: |
| 317 |
values = json.dumps( |
| 318 |
{ |
| 319 |
"statuspage_id": statuspage, |
| 320 |
"maintenance_id": maintenance_id, |
| 321 |
} |
| 322 |
) |
| 323 |
response = open_url(url=f"{url}/v2/maintenance/delete", data=values, headers=auth_headers) |
| 324 |
data = json.loads(response.read()) |
| 325 |
if data["status"]["error"] == "yes": |
| 326 |
return 1, None, "Invalid maintenance_id" |
| 327 |
except Exception as e: |
| 328 |
return 1, None, f"{e}" |
| 329 |
return 0, None, None |
| 330 |
|
| 331 |
|
| 332 |
def main(): |
| 333 |
module = AnsibleModule( |
| 334 |
argument_spec=dict( |
| 335 |
api_id=dict(required=True), |
| 336 |
api_key=dict(required=True, no_log=True), |
| 337 |
statuspage=dict(required=True), |
| 338 |
state=dict(default="present", choices=["present", "absent"]), |
| 339 |
url=dict(default="https://api.status.io"), |
| 340 |
components=dict(type="list", elements="str", aliases=["component"]), |
| 341 |
containers=dict(type="list", elements="str", aliases=["container"]), |
| 342 |
all_infrastructure_affected=dict(type="bool", default=False), |
| 343 |
automation=dict(type="bool", default=False), |
| 344 |
title=dict(default="A new maintenance window"), |
| 345 |
desc=dict(default="Created by Ansible"), |
| 346 |
minutes=dict(type="int", default=10), |
| 347 |
maintenance_notify_now=dict(type="bool", default=False), |
| 348 |
maintenance_notify_72_hr=dict(type="bool", default=False), |
| 349 |
maintenance_notify_24_hr=dict(type="bool", default=False), |
| 350 |
maintenance_notify_1_hr=dict(type="bool", default=False), |
| 351 |
maintenance_id=dict(), |
| 352 |
start_date=dict(), |
| 353 |
start_time=dict(), |
| 354 |
), |
| 355 |
supports_check_mode=True, |
| 356 |
) |
| 357 |
|
| 358 |
api_id = module.params["api_id"] |
| 359 |
api_key = module.params["api_key"] |
| 360 |
statuspage = module.params["statuspage"] |
| 361 |
state = module.params["state"] |
| 362 |
url = module.params["url"] |
| 363 |
components = module.params["components"] |
| 364 |
containers = module.params["containers"] |
| 365 |
all_infrastructure_affected = module.params["all_infrastructure_affected"] |
| 366 |
automation = module.params["automation"] |
| 367 |
title = module.params["title"] |
| 368 |
desc = module.params["desc"] |
| 369 |
minutes = module.params["minutes"] |
| 370 |
maintenance_notify_now = module.params["maintenance_notify_now"] |
| 371 |
maintenance_notify_72_hr = module.params["maintenance_notify_72_hr"] |
| 372 |
maintenance_notify_24_hr = module.params["maintenance_notify_24_hr"] |
| 373 |
maintenance_notify_1_hr = module.params["maintenance_notify_1_hr"] |
| 374 |
maintenance_id = module.params["maintenance_id"] |
| 375 |
start_date = module.params["start_date"] |
| 376 |
start_time = module.params["start_time"] |
| 377 |
|
| 378 |
if state == "present": |
| 379 |
if api_id and api_key: |
| 380 |
(rc, auth_headers, auth_content, error) = get_api_auth_headers(api_id, api_key, url, statuspage) |
| 381 |
if rc != 0: |
| 382 |
module.fail_json(msg=f"Failed to get auth keys: {error}") |
| 383 |
else: |
| 384 |
auth_headers = {} |
| 385 |
auth_content = {} |
| 386 |
|
| 387 |
if minutes or start_time and start_date: |
| 388 |
(rc, returned_date, error) = get_date_time(start_date, start_time, minutes) |
| 389 |
if rc != 0: |
| 390 |
module.fail_json(msg=f"Failed to set date/time: {error}") |
| 391 |
|
| 392 |
if not components and not containers: |
| 393 |
return module.fail_json(msg="A Component or Container must be defined") |
| 394 |
elif components and containers: |
| 395 |
return module.fail_json(msg="Components and containers cannot be used together") |
| 396 |
else: |
| 397 |
if components: |
| 398 |
(rc, host_ids, error) = get_component_ids(auth_content, components) |
| 399 |
if rc != 0: |
| 400 |
module.fail_json(msg=f"Failed to find component {error}") |
| 401 |
|
| 402 |
if containers: |
| 403 |
(rc, host_ids, error) = get_container_ids(auth_content, containers) |
| 404 |
if rc != 0: |
| 405 |
module.fail_json(msg=f"Failed to find container {error}") |
| 406 |
|
| 407 |
if module.check_mode: |
| 408 |
module.exit_json(changed=True) |
| 409 |
else: |
| 410 |
(rc, dummy, error) = create_maintenance( |
| 411 |
auth_headers, |
| 412 |
url, |
| 413 |
statuspage, |
| 414 |
host_ids, |
| 415 |
all_infrastructure_affected, |
| 416 |
automation, |
| 417 |
title, |
| 418 |
desc, |
| 419 |
returned_date, |
| 420 |
maintenance_notify_now, |
| 421 |
maintenance_notify_72_hr, |
| 422 |
maintenance_notify_24_hr, |
| 423 |
maintenance_notify_1_hr, |
| 424 |
) |
| 425 |
if rc == 0: |
| 426 |
module.exit_json(changed=True, result="Successfully created maintenance") |
| 427 |
else: |
| 428 |
module.fail_json(msg=f"Failed to create maintenance: {error}") |
| 429 |
|
| 430 |
if state == "absent": |
| 431 |
if api_id and api_key: |
| 432 |
(rc, auth_headers, auth_content, error) = get_api_auth_headers(api_id, api_key, url, statuspage) |
| 433 |
if rc != 0: |
| 434 |
module.fail_json(msg=f"Failed to get auth keys: {error}") |
| 435 |
else: |
| 436 |
auth_headers = {} |
| 437 |
|
| 438 |
if module.check_mode: |
| 439 |
module.exit_json(changed=True) |
| 440 |
else: |
| 441 |
(rc, dummy, error) = delete_maintenance(auth_headers, url, statuspage, maintenance_id) |
| 442 |
if rc == 0: |
| 443 |
module.exit_json(changed=True, result="Successfully deleted maintenance") |
| 444 |
else: |
| 445 |
module.fail_json(msg=f"Failed to delete maintenance: {error}") |
| 446 |
|
| 447 |
|
| 448 |
if __name__ == "__main__": |
| 449 |
main() |