| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright 2014 Benjamin Curtis <[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: honeybadger_deployment |
| 11 |
author: "Benjamin Curtis (@stympy)" |
| 12 |
short_description: Notify Honeybadger.io about app deployments |
| 13 |
description: |
| 14 |
- Notify Honeybadger.io about app deployments (see U(http://docs.honeybadger.io/article/188-deployment-tracking)). |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
token: |
| 24 |
type: str |
| 25 |
description: |
| 26 |
- API token. |
| 27 |
required: true |
| 28 |
environment: |
| 29 |
type: str |
| 30 |
description: |
| 31 |
- The environment name, typically V(production), V(staging), and so on. |
| 32 |
required: true |
| 33 |
user: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- The username of the person doing the deployment. |
| 37 |
repo: |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- URL of the project repository. |
| 41 |
revision: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- A hash, number, tag, or other identifier showing what revision was deployed. |
| 45 |
url: |
| 46 |
type: str |
| 47 |
description: |
| 48 |
- Optional URL to submit the notification to. |
| 49 |
default: "https://api.honeybadger.io/v1/deploys" |
| 50 |
validate_certs: |
| 51 |
description: |
| 52 |
- If V(false), SSL certificates for the target URL are not validated. This should only be used on personally controlled |
| 53 |
sites using self-signed certificates. |
| 54 |
type: bool |
| 55 |
default: true |
| 56 |
""" |
| 57 |
|
| 58 |
EXAMPLES = r""" |
| 59 |
- name: Notify Honeybadger.io about an app deployment |
| 60 |
community.general.honeybadger_deployment: |
| 61 |
token: AAAAAA |
| 62 |
environment: staging |
| 63 |
user: ansible |
| 64 |
revision: b6826b8 |
| 65 |
repo: '[email protected]:user/repo.git' |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = """#""" |
| 69 |
|
| 70 |
import traceback |
| 71 |
from urllib.parse import urlencode |
| 72 |
|
| 73 |
from ansible.module_utils.basic import AnsibleModule |
| 74 |
from ansible.module_utils.urls import fetch_url |
| 75 |
|
| 76 |
# =========================================== |
| 77 |
# Module execution. |
| 78 |
# |
| 79 |
|
| 80 |
|
| 81 |
def main(): |
| 82 |
module = AnsibleModule( |
| 83 |
argument_spec=dict( |
| 84 |
token=dict(required=True, no_log=True), |
| 85 |
environment=dict(required=True), |
| 86 |
user=dict(required=False), |
| 87 |
repo=dict(), |
| 88 |
revision=dict(), |
| 89 |
url=dict(default="https://api.honeybadger.io/v1/deploys"), |
| 90 |
validate_certs=dict(default=True, type="bool"), |
| 91 |
), |
| 92 |
supports_check_mode=True, |
| 93 |
) |
| 94 |
|
| 95 |
params = {} |
| 96 |
|
| 97 |
if module.params["environment"]: |
| 98 |
params["deploy[environment]"] = module.params["environment"] |
| 99 |
|
| 100 |
if module.params["user"]: |
| 101 |
params["deploy[local_username]"] = module.params["user"] |
| 102 |
|
| 103 |
if module.params["repo"]: |
| 104 |
params["deploy[repository]"] = module.params["repo"] |
| 105 |
|
| 106 |
if module.params["revision"]: |
| 107 |
params["deploy[revision]"] = module.params["revision"] |
| 108 |
|
| 109 |
params["api_key"] = module.params["token"] |
| 110 |
|
| 111 |
url = module.params.get("url") |
| 112 |
|
| 113 |
# If we're in check mode, just exit pretending like we succeeded |
| 114 |
if module.check_mode: |
| 115 |
module.exit_json(changed=True) |
| 116 |
|
| 117 |
try: |
| 118 |
data = urlencode(params) |
| 119 |
response, info = fetch_url(module, url, data=data) |
| 120 |
except Exception as e: |
| 121 |
module.fail_json(msg=f"Unable to notify Honeybadger: {e}", exception=traceback.format_exc()) |
| 122 |
else: |
| 123 |
if info["status"] == 201: |
| 124 |
module.exit_json(changed=True) |
| 125 |
else: |
| 126 |
module.fail_json(msg=f"HTTP result code: {info['status']} connecting to {url}") |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == "__main__": |
| 130 |
main() |