2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2013 Matt Coddington <coddington@gmail.com>
|
2022-08-05 13:17:19 +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)"
|
|
|
|
short_description: Notify newrelic about app deployments
|
|
|
|
description:
|
|
|
|
- Notify newrelic about app deployments (see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/deployment-notifications#api)
|
|
|
|
options:
|
|
|
|
token:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- API token, to place in the x-api-key header.
|
|
|
|
required: true
|
|
|
|
app_name:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- (one of app_name or application_id are required) The value of app_name in the newrelic.yml file used by the application
|
|
|
|
required: false
|
|
|
|
application_id:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- (one of app_name or application_id are required) The application id, found in the URL when viewing the application in RPM
|
|
|
|
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)
|
|
|
|
required: false
|
|
|
|
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:
|
|
|
|
- Name of the application
|
|
|
|
required: false
|
|
|
|
environment:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The environment for this deployment
|
|
|
|
required: false
|
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
type: bool
|
|
|
|
|
|
|
|
requirements: []
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Notify newrelic 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
|
|
|
|
from ansible.module_utils.six.moves.urllib.parse import urlencode
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# 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),
|
|
|
|
revision=dict(required=False),
|
|
|
|
user=dict(required=False),
|
|
|
|
appname=dict(required=False),
|
|
|
|
environment=dict(required=False),
|
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")
|
|
|
|
|
|
|
|
if module.params["app_name"]:
|
|
|
|
params["app_name"] = module.params["app_name"]
|
|
|
|
elif module.params["application_id"]:
|
|
|
|
params["application_id"] = module.params["application_id"]
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="you must set one of 'app_name' or 'application_id'")
|
|
|
|
|
|
|
|
for item in ["changelog", "description", "revision", "user", "appname", "environment"]:
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Send the data to NewRelic
|
|
|
|
url = "https://rpm.newrelic.com/deployments.xml"
|
|
|
|
data = urlencode(params)
|
|
|
|
headers = {
|
|
|
|
'x-api-key': module.params["token"],
|
|
|
|
}
|
|
|
|
response, info = fetch_url(module, url, data=data, headers=headers)
|
|
|
|
if info['status'] in (200, 201):
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="unable to update newrelic: %s" % info['msg'])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|