2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright Ansible Project
|
|
|
|
# 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: bigpanda
|
|
|
|
author: "Hagai Kariti (@hkariti)"
|
|
|
|
short_description: Notify BigPanda about deployments
|
|
|
|
description:
|
|
|
|
- Notify BigPanda when deployments start and end (successfully or not). Returns a deployment object containing all the parameters for future module calls.
|
|
|
|
options:
|
|
|
|
component:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- "The name of the component being deployed. Ex: billing"
|
|
|
|
required: true
|
|
|
|
aliases: ['name']
|
|
|
|
version:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The deployment version.
|
|
|
|
required: true
|
|
|
|
token:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- API token.
|
|
|
|
required: true
|
|
|
|
state:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- State of the deployment.
|
|
|
|
required: true
|
|
|
|
choices: ['started', 'finished', 'failed']
|
|
|
|
hosts:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Name of affected host name. Can be a list.
|
2020-12-01 21:28:40 +01:00
|
|
|
- If not specified, it defaults to the remote system's hostname.
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
aliases: ['host']
|
|
|
|
env:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The environment name, typically 'production', 'staging', etc.
|
|
|
|
required: false
|
|
|
|
owner:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- The person responsible for the deployment.
|
|
|
|
required: false
|
|
|
|
description:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Free text description of the deployment.
|
|
|
|
required: false
|
|
|
|
url:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Base URL of the API server.
|
|
|
|
required: False
|
|
|
|
default: https://api.bigpanda.io
|
|
|
|
validate_certs:
|
|
|
|
description:
|
2022-08-24 20:47:17 +02:00
|
|
|
- If C(false), SSL certificates for the target url 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 20:47:17 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
|
|
|
deployment_message:
|
2020-11-23 22:07:03 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Message about the deployment.
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-11-23 22:07:03 +01:00
|
|
|
source_system:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Source system used in the requests to the API
|
|
|
|
default: ansible
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# informational: requirements for nodes
|
|
|
|
requirements: [ ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Notify BigPanda about a deployment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.bigpanda:
|
2020-03-09 10:11:07 +01:00
|
|
|
component: myapp
|
|
|
|
version: '1.3'
|
|
|
|
token: '{{ bigpanda_token }}'
|
|
|
|
state: started
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Notify BigPanda about a deployment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.bigpanda:
|
2020-03-09 10:11:07 +01:00
|
|
|
component: myapp
|
|
|
|
version: '1.3'
|
|
|
|
token: '{{ bigpanda_token }}'
|
|
|
|
state: finished
|
|
|
|
|
|
|
|
# If outside servers aren't reachable from your machine, use delegate_to and override hosts:
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Notify BigPanda about a deployment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.bigpanda:
|
2020-03-09 10:11:07 +01:00
|
|
|
component: myapp
|
|
|
|
version: '1.3'
|
|
|
|
token: '{{ bigpanda_token }}'
|
|
|
|
hosts: '{{ ansible_hostname }}'
|
|
|
|
state: started
|
|
|
|
delegate_to: localhost
|
|
|
|
register: deployment
|
|
|
|
|
2020-05-15 12:12:41 +02:00
|
|
|
- name: Notify BigPanda about a deployment
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.bigpanda:
|
2020-03-09 10:11:07 +01:00
|
|
|
component: '{{ deployment.component }}'
|
|
|
|
version: '{{ deployment.version }}'
|
|
|
|
token: '{{ deployment.token }}'
|
|
|
|
state: finished
|
|
|
|
delegate_to: localhost
|
|
|
|
'''
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Module execution.
|
|
|
|
#
|
|
|
|
import json
|
|
|
|
import socket
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
component=dict(required=True, aliases=['name']),
|
|
|
|
version=dict(required=True),
|
|
|
|
token=dict(required=True, no_log=True),
|
|
|
|
state=dict(required=True, choices=['started', 'finished', 'failed']),
|
2020-12-01 21:28:40 +01:00
|
|
|
hosts=dict(required=False, aliases=['host']),
|
2020-03-09 10:11:07 +01:00
|
|
|
env=dict(required=False),
|
|
|
|
owner=dict(required=False),
|
|
|
|
description=dict(required=False),
|
2021-03-09 21:23:20 +01:00
|
|
|
deployment_message=dict(required=False),
|
2020-03-09 10:11:07 +01:00
|
|
|
source_system=dict(required=False, default='ansible'),
|
2020-06-22 15:56:35 +02:00
|
|
|
validate_certs=dict(default=True, type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
url=dict(required=False, default='https://api.bigpanda.io'),
|
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
token = module.params['token']
|
|
|
|
state = module.params['state']
|
|
|
|
url = module.params['url']
|
|
|
|
|
|
|
|
# Build the common request body
|
|
|
|
body = dict()
|
|
|
|
for k in ('component', 'version', 'hosts'):
|
|
|
|
v = module.params[k]
|
|
|
|
if v is not None:
|
|
|
|
body[k] = v
|
2020-12-01 21:28:40 +01:00
|
|
|
if body.get('hosts') is None:
|
|
|
|
body['hosts'] = [socket.gethostname()]
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not isinstance(body['hosts'], list):
|
|
|
|
body['hosts'] = [body['hosts']]
|
|
|
|
|
|
|
|
# Insert state-specific attributes to body
|
|
|
|
if state == 'started':
|
|
|
|
for k in ('source_system', 'env', 'owner', 'description'):
|
|
|
|
v = module.params[k]
|
|
|
|
if v is not None:
|
|
|
|
body[k] = v
|
|
|
|
|
|
|
|
request_url = url + '/data/events/deployments/start'
|
|
|
|
else:
|
2021-02-27 21:23:07 +01:00
|
|
|
message = module.params['deployment_message']
|
2020-03-09 10:11:07 +01:00
|
|
|
if message is not None:
|
|
|
|
body['errorMessage'] = message
|
|
|
|
|
|
|
|
if state == 'finished':
|
|
|
|
body['status'] = 'success'
|
|
|
|
else:
|
|
|
|
body['status'] = 'failure'
|
|
|
|
|
|
|
|
request_url = url + '/data/events/deployments/end'
|
|
|
|
|
|
|
|
# Build the deployment object we return
|
|
|
|
deployment = dict(token=token, url=url)
|
|
|
|
deployment.update(body)
|
|
|
|
if 'errorMessage' in deployment:
|
|
|
|
message = deployment.pop('errorMessage')
|
|
|
|
deployment['message'] = message
|
|
|
|
|
|
|
|
# If we're in check mode, just exit pretending like we succeeded
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True, **deployment)
|
|
|
|
|
|
|
|
# Send the data to bigpanda
|
|
|
|
data = json.dumps(body)
|
|
|
|
headers = {'Authorization': 'Bearer %s' % token, 'Content-Type': 'application/json'}
|
|
|
|
try:
|
|
|
|
response, info = fetch_url(module, request_url, data=data, headers=headers)
|
|
|
|
if info['status'] == 200:
|
|
|
|
module.exit_json(changed=True, **deployment)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=json.dumps(info))
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|