2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2013 Matt Coddington <coddington@gmail.com>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: newrelic_deployment
|
|
|
|
author: "Matt Coddington (@mcodd)"
|
2022-10-12 10:42:17 +02:00
|
|
|
short_description: Notify New Relic about app deployments
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-12 10:42:17 +02:00
|
|
|
- Notify New Relic about app deployments (see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/record-monitor-deployments/)
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
token:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-12 10:42:17 +02:00
|
|
|
- API token to place in the Api-Key header.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
app_name:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-12 10:42:17 +02:00
|
|
|
- The value of app_name in the newrelic.yml file used by the application.
|
|
|
|
- One of I(app_name) or I(application_id) is required.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
application_id:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-12 10:42:17 +02:00
|
|
|
- The application ID found in the metadata of the application in APM.
|
|
|
|
- One of I(app_name) or I(application_id) is required.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
changelog:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- A list of changes for this deployment
|
|
|
|
required: false
|
|
|
|
description:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Text annotation for the deployment - notes for you
|
|
|
|
required: false
|
|
|
|
revision:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- A revision number (e.g., git commit SHA)
|
2022-10-12 10:42:17 +02:00
|
|
|
required: true
|
2020-03-09 10:11:07 +01:00
|
|
|
user:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The name of the user/process that triggered this deployment
|
|
|
|
required: false
|
|
|
|
appname:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-12 10:42:17 +02:00
|
|
|
- Name of the application.
|
|
|
|
- This option has been deprecated and will be removed in community.general 7.0.0. Please do not use.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
environment:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2022-10-12 10:42:17 +02:00
|
|
|
- The environment for this deployment.
|
|
|
|
- This option has been deprecated and will be removed community.general 7.0.0. Please do not use.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
validate_certs:
|
|
|
|
description:
|
2022-08-24 19:59:30 +02:00
|
|
|
- If C(false), SSL certificates will not be validated. This should only be used
|
2020-03-09 10:11:07 +01:00
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
2022-08-24 19:59:30 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
|
|
|
requirements: []
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2022-10-12 10:42:17 +02:00
|
|
|
- name: Notify New Relic about an app deployment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.newrelic_deployment:
|
2020-03-09 10:11:07 +01:00
|
|
|
token: AAAAAA
|
|
|
|
app_name: myapp
|
|
|
|
user: ansible deployment
|
|
|
|
revision: '1.0'
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
2022-10-12 10:42:17 +02:00
|
|
|
from ansible.module_utils.six.moves.urllib.parse import quote
|
|
|
|
import json
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
token=dict(required=True, no_log=True),
|
|
|
|
app_name=dict(required=False),
|
|
|
|
application_id=dict(required=False),
|
|
|
|
changelog=dict(required=False),
|
|
|
|
description=dict(required=False),
|
2022-10-12 10:42:17 +02:00
|
|
|
revision=dict(required=True),
|
2020-03-09 10:11:07 +01:00
|
|
|
user=dict(required=False),
|
2022-10-12 10:42:17 +02:00
|
|
|
appname=dict(required=False, removed_in_version='7.0.0', removed_from_collection='community.general'),
|
|
|
|
environment=dict(required=False, removed_in_version='7.0.0', removed_from_collection='community.general'),
|
2020-06-22 15:56:35 +02:00
|
|
|
validate_certs=dict(default=True, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
required_one_of=[['app_name', 'application_id']],
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# build list of params
|
|
|
|
params = {}
|
|
|
|
if module.params["app_name"] and module.params["application_id"]:
|
|
|
|
module.fail_json(msg="only one of 'app_name' or 'application_id' can be set")
|
|
|
|
|
2022-10-12 10:42:17 +02:00
|
|
|
app_id = None
|
2020-03-09 10:11:07 +01:00
|
|
|
if module.params["app_name"]:
|
2022-10-12 10:42:17 +02:00
|
|
|
app_id = get_application_id(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
elif module.params["application_id"]:
|
2022-10-12 10:42:17 +02:00
|
|
|
app_id = module.params["application_id"]
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
module.fail_json(msg="you must set one of 'app_name' or 'application_id'")
|
|
|
|
|
2022-10-12 10:42:17 +02:00
|
|
|
if app_id is None:
|
|
|
|
module.fail_json(msg="No application with name %s is found in NewRelic" % module.params["app_name"])
|
|
|
|
|
|
|
|
for item in ["changelog", "description", "revision", "user"]:
|
2020-03-09 10:11:07 +01:00
|
|
|
if module.params[item]:
|
|
|
|
params[item] = module.params[item]
|
|
|
|
|
|
|
|
# If we're in check mode, just exit pretending like we succeeded
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
2022-10-12 10:42:17 +02:00
|
|
|
# Send the data to New Relic
|
|
|
|
url = "https://api.newrelic.com/v2/applications/%s/deployments.json" % quote(str(app_id), safe='')
|
|
|
|
data = {
|
|
|
|
'deployment': params
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
headers = {
|
2022-10-12 10:42:17 +02:00
|
|
|
'Api-Key': module.params["token"],
|
|
|
|
'Content-Type': 'application/json',
|
2020-03-09 10:11:07 +01:00
|
|
|
}
|
2022-10-12 10:42:17 +02:00
|
|
|
response, info = fetch_url(module, url, data=module.jsonify(data), headers=headers, method="POST")
|
2020-03-09 10:11:07 +01:00
|
|
|
if info['status'] in (200, 201):
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
2022-10-12 10:42:17 +02:00
|
|
|
module.fail_json(msg="Unable to insert deployment marker: %s" % info['msg'])
|
|
|
|
|
|
|
|
|
|
|
|
def get_application_id(module):
|
|
|
|
url = "https://api.newrelic.com/v2/applications.json"
|
|
|
|
data = "filter[name]=%s" % module.params["app_name"]
|
|
|
|
headers = {
|
|
|
|
'Api-Key': module.params["token"],
|
|
|
|
}
|
|
|
|
response, info = fetch_url(module, url, data=data, headers=headers)
|
|
|
|
if info['status'] not in (200, 201):
|
|
|
|
module.fail_json(msg="Unable to get application: %s" % info['msg'])
|
|
|
|
|
|
|
|
result = json.loads(response.read())
|
|
|
|
if result is None or len(result.get("applications", "")) == 0:
|
|
|
|
module.fail_json(msg='No application found with name "%s"' % module.params["app_name"])
|
|
|
|
|
|
|
|
return result["applications"][0]["id"]
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|