summaryrefslogtreecommitdiff
path: root/monitoring/stackdriver.py
blob: b20b191158858407fa8e80c19f0a652255cedaaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
ANSIBLE_METADATA = {'status': ['preview'],
                    'supported_by': 'community',
                    'version': '1.0'}

DOCUMENTATION = '''

module: stackdriver
short_description: Send code deploy and annotation events to stackdriver
description:
  - Send code deploy and annotation events to Stackdriver
version_added: "1.6"
author: "Ben Whaley (@bwhaley)"
options:
  key:
    description:
      - API key.
    required: true
    default: null
  event:
    description:
      - The type of event to send, either annotation or deploy
    choices: ['annotation', 'deploy']
    required: false
    default: null
  revision_id:
    description:
      - The revision of the code that was deployed. Required for deploy events
    required: false
    default: null
  deployed_by:
    description:
      - The person or robot responsible for deploying the code
    required: false
    default: "Ansible"
  deployed_to:
    description:
      - "The environment code was deployed to. (ie: development, staging, production)"
    required: false
    default: null
  repository:
    description:
      - The repository (or project) deployed
    required: false
    default: null
  msg:
    description: 
      - The contents of the annotation message, in plain text.  Limited to 256 characters. Required for annotation.
    required: false
    default: null
  annotated_by: 
    description:
      - The person or robot who the annotation should be attributed to.
    required: false
    default: "Ansible"
  level: 
    description:
      - one of INFO/WARN/ERROR, defaults to INFO if not supplied.  May affect display.
    choices: ['INFO', 'WARN', 'ERROR']
    required: false
    default: 'INFO'
  instance_id:
    description:
      - id of an EC2 instance that this event should be attached to, which will limit the contexts where this event is shown
    required: false
    default: null
  event_epoch:
    description:
      - "Unix timestamp of where the event should appear in the timeline, defaults to now. Be careful with this."
    required: false
    default: null
'''

EXAMPLES = '''
- stackdriver:
    key: AAAAAA
    event: deploy
    deployed_to: production
    deployed_by: leeroyjenkins
    repository: MyWebApp
    revision_id: abcd123

- stackdriver:
    key: AAAAAA
    event: annotation
    msg: Greetings from Ansible
    annotated_by: leeroyjenkins
    level: WARN
    instance_id: i-abcd1234
'''

# ===========================================
# Stackdriver module specific support methods.
#

try:
    import json
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        # Let snippet from module_utils/basic.py return a proper error in this case
        pass

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.urls import fetch_url


def send_deploy_event(module, key, revision_id, deployed_by='Ansible', deployed_to=None, repository=None):
    """Send a deploy event to Stackdriver"""
    deploy_api = "https://event-gateway.stackdriver.com/v1/deployevent"

    params = {}
    params['revision_id'] = revision_id
    params['deployed_by'] = deployed_by
    if deployed_to:
        params['deployed_to'] = deployed_to
    if repository:
        params['repository'] = repository

    return do_send_request(module, deploy_api, params, key)

def send_annotation_event(module, key, msg, annotated_by='Ansible', level=None, instance_id=None, event_epoch=None):
    """Send an annotation event to Stackdriver"""
    annotation_api = "https://event-gateway.stackdriver.com/v1/annotationevent"

    params = {}
    params['message'] = msg
    if annotated_by:
        params['annotated_by'] = annotated_by
    if level:
        params['level'] = level
    if instance_id:
        params['instance_id'] = instance_id
    if event_epoch:
        params['event_epoch'] = event_epoch

    return do_send_request(module, annotation_api, params, key)

def do_send_request(module, url, params, key):
    data = json.dumps(params)
    headers = {
        'Content-Type': 'application/json',
        'x-stackdriver-apikey': key
    }
    response, info =  fetch_url(module, url, headers=headers, data=data, method='POST')
    if info['status'] != 200:
        module.fail_json(msg="Unable to send msg: %s" % info['msg'])


# ===========================================
# Module execution.
#

def main():

    module = AnsibleModule(
        argument_spec=dict(
            key=dict(required=True),
            event=dict(required=True, choices=['deploy', 'annotation']),
            msg=dict(),
            revision_id=dict(),
            annotated_by=dict(default='Ansible'),
            level=dict(default='INFO', choices=['INFO', 'WARN', 'ERROR']),
            instance_id=dict(),
            event_epoch=dict(),
            deployed_by=dict(default='Ansible'),
            deployed_to=dict(),
            repository=dict(),
        ),
        supports_check_mode=True
    )

    key = module.params["key"]
    event = module.params["event"]

    # Annotation params
    msg = module.params["msg"]
    annotated_by = module.params["annotated_by"]
    level = module.params["level"]
    instance_id = module.params["instance_id"]
    event_epoch = module.params["event_epoch"]

    # Deploy params
    revision_id = module.params["revision_id"]
    deployed_by = module.params["deployed_by"]
    deployed_to = module.params["deployed_to"]
    repository = module.params["repository"]

    ##################################################################
    # deploy requires revision_id
    # annotation requires msg
    # We verify these manually
    ##################################################################

    if event == 'deploy':
        if not revision_id:
            module.fail_json(msg="revision_id required for deploy events")
        try:
            send_deploy_event(module, key, revision_id, deployed_by, deployed_to, repository)
        except Exception:
            e = get_exception()
            module.fail_json(msg="unable to sent deploy event: %s" % e)

    if event == 'annotation':
        if not msg:
            module.fail_json(msg="msg required for annotation events")
        try:
            send_annotation_event(module, key, msg, annotated_by, level, instance_id, event_epoch)
        except Exception:
            e = get_exception()
            module.fail_json(msg="unable to sent annotation event: %s" % e)

    changed = True
    module.exit_json(changed=changed, deployed_by=deployed_by)


if __name__ == '__main__':
    main()