| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright 2013 Bruce Pennypacker <[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: airbrake_deployment |
| 11 |
author: |
| 12 |
- "Bruce Pennypacker (@bpennypacker)" |
| 13 |
- "Patrick Humpal (@phumpal)" |
| 14 |
short_description: Notify airbrake about app deployments |
| 15 |
description: |
| 16 |
- Notify airbrake about app deployments (see U(https://airbrake.io/docs/api/#deploys-v4)). |
| 17 |
extends_documentation_fragment: |
| 18 |
- community.general._attributes |
| 19 |
attributes: |
| 20 |
check_mode: |
| 21 |
support: full |
| 22 |
diff_mode: |
| 23 |
support: none |
| 24 |
options: |
| 25 |
project_id: |
| 26 |
description: |
| 27 |
- Airbrake PROJECT_ID. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
version_added: '0.2.0' |
| 31 |
project_key: |
| 32 |
description: |
| 33 |
- Airbrake PROJECT_KEY. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
version_added: '0.2.0' |
| 37 |
environment: |
| 38 |
description: |
| 39 |
- The airbrake environment name, typically v(production), V(staging), and so on. |
| 40 |
required: true |
| 41 |
type: str |
| 42 |
user: |
| 43 |
description: |
| 44 |
- The username of the person doing the deployment. |
| 45 |
type: str |
| 46 |
repo: |
| 47 |
description: |
| 48 |
- URL of the project repository. |
| 49 |
type: str |
| 50 |
revision: |
| 51 |
description: |
| 52 |
- A hash, number, tag, or other identifier showing what revision from version control was deployed. |
| 53 |
type: str |
| 54 |
version: |
| 55 |
description: |
| 56 |
- A string identifying what version was deployed. |
| 57 |
type: str |
| 58 |
version_added: '1.0.0' |
| 59 |
url: |
| 60 |
description: |
| 61 |
- Optional URL to submit the notification to. Use to send notifications to Airbrake-compliant tools like Errbit. |
| 62 |
default: "https://api.airbrake.io/api/v4/projects/" |
| 63 |
type: str |
| 64 |
validate_certs: |
| 65 |
description: |
| 66 |
- If V(false), SSL certificates for the target URL is not validated. This should only be used on personally controlled |
| 67 |
sites using self-signed certificates. |
| 68 |
default: true |
| 69 |
type: bool |
| 70 |
|
| 71 |
requirements: [] |
| 72 |
""" |
| 73 |
|
| 74 |
EXAMPLES = r""" |
| 75 |
- name: Notify airbrake about an app deployment |
| 76 |
community.general.airbrake_deployment: |
| 77 |
project_id: '12345' |
| 78 |
project_key: 'AAAAAA' |
| 79 |
environment: staging |
| 80 |
user: ansible |
| 81 |
revision: '4.2' |
| 82 |
|
| 83 |
- name: Notify airbrake about an app deployment, using git hash as revision |
| 84 |
community.general.airbrake_deployment: |
| 85 |
project_id: '12345' |
| 86 |
project_key: 'AAAAAA' |
| 87 |
environment: staging |
| 88 |
user: ansible |
| 89 |
revision: 'e54dd3a01f2c421b558ef33b5f79db936e2dcf15' |
| 90 |
version: '0.2.0' |
| 91 |
""" |
| 92 |
|
| 93 |
from ansible.module_utils.basic import AnsibleModule |
| 94 |
from ansible.module_utils.urls import fetch_url |
| 95 |
|
| 96 |
# =========================================== |
| 97 |
# Module execution. |
| 98 |
# |
| 99 |
|
| 100 |
|
| 101 |
def main(): |
| 102 |
module = AnsibleModule( |
| 103 |
argument_spec=dict( |
| 104 |
project_id=dict(required=True, no_log=True, type="str"), |
| 105 |
project_key=dict(required=True, no_log=True, type="str"), |
| 106 |
environment=dict(required=True, type="str"), |
| 107 |
user=dict(type="str"), |
| 108 |
repo=dict(type="str"), |
| 109 |
revision=dict(type="str"), |
| 110 |
version=dict(type="str"), |
| 111 |
url=dict(default="https://api.airbrake.io/api/v4/projects/", type="str"), |
| 112 |
validate_certs=dict(default=True, type="bool"), |
| 113 |
), |
| 114 |
supports_check_mode=True, |
| 115 |
) |
| 116 |
|
| 117 |
# Build list of params |
| 118 |
params = {} |
| 119 |
|
| 120 |
# If we're in check mode, just exit pretending like we succeeded |
| 121 |
if module.check_mode: |
| 122 |
module.exit_json(changed=True) |
| 123 |
|
| 124 |
# v4 API documented at https://airbrake.io/docs/api/#create-deploy-v4 |
| 125 |
if module.params["environment"]: |
| 126 |
params["environment"] = module.params["environment"] |
| 127 |
|
| 128 |
if module.params["user"]: |
| 129 |
params["username"] = module.params["user"] |
| 130 |
|
| 131 |
if module.params["repo"]: |
| 132 |
params["repository"] = module.params["repo"] |
| 133 |
|
| 134 |
if module.params["revision"]: |
| 135 |
params["revision"] = module.params["revision"] |
| 136 |
|
| 137 |
if module.params["version"]: |
| 138 |
params["version"] = module.params["version"] |
| 139 |
|
| 140 |
# Build deploy url |
| 141 |
url = f"{module.params.get('url')}{module.params['project_id']}/deploys?key={module.params['project_key']}" |
| 142 |
json_body = module.jsonify(params) |
| 143 |
|
| 144 |
# Build header |
| 145 |
headers = {"Content-Type": "application/json"} |
| 146 |
|
| 147 |
# Notify Airbrake of deploy |
| 148 |
response, info = fetch_url(module, url, data=json_body, headers=headers, method="POST") |
| 149 |
|
| 150 |
if info["status"] == 200 or info["status"] == 201: |
| 151 |
module.exit_json(changed=True) |
| 152 |
else: |
| 153 |
module.fail_json(msg=f"HTTP result code: {info['status']} connecting to {url}") |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |