Project

General

Profile

Task #2275 ยป foreman_callback.py

Ansible foreman callback - Andrea Dell'Amico, Mar 16, 2016 12:47 PM

 
1
import os
2
from datetime import datetime
3
from collections import defaultdict
4
import json
5
import uuid
6
import requests
7
import time
8

    
9
try:
10
    from ansible.plugins.callback import CallbackBase
11
    parent_class = CallbackBase
12
except ImportError:
13
    parent_class = object
14

    
15
FOREMAN_URL = os.getenv('FOREMAN_URL', "http://cm.research-infrastructures.eu:80")
16
# Substitute by a real SSL certificate and key if your Foreman uses HTTPS
17
FOREMAN_SSL_CERT = (os.getenv('FOREMAN_SSL_CERT', ""),
18
                    os.getenv('FOREMAN_SSL_KEY', ""))
19
FOREMAN_SSL_VERIFY = os.getenv('FOREMAN_SSL_VERIFY', "0")
20
FOREMAN_HEADERS = {
21
    "Content-Type": "application/json",
22
    "Accept": "application/json"
23
}
24
TIME_FORMAT="%Y-%m-%d %H:%M:%S %f"
25
FACTS_FORMAT="""
26
{
27
  "name":"%(host)s",
28
  "facts": %(data)s
29
}
30
"""
31
REPORT_FORMAT="""
32
{
33
"report":
34
  {
35
    "host":"%(host)s",
36
    "reported_at":"%(now)s",
37
    "metrics": %(metrics)s,
38
    "status": %(status)s,
39
    "logs" : %(log)s
40
  }
41
}
42
"""
43

    
44
class CallbackModule(parent_class):
45

    
46
    """
47
    Sends Ansible facts (if ansible -m setup ran) and reports
48
    """
49
    def __init__(self):
50
        super(CallbackModule, self).__init__()
51
        self.items = defaultdict(list)
52
        self.start_time = int(time.time())
53
        self.ssl_verify = self._ssl_verify()
54

    
55
    def log(self, host, category, data):
56
        if type(data) != dict:
57
            data = dict(msg=data)
58
        data['category'] = category
59
        if 'ansible_facts' in data:
60
            self.send_facts(host, data)
61
        self.send_report(host, data)
62

    
63
    def _ssl_verify(self):
64
        if FOREMAN_SSL_VERIFY.lower() in [ "1", "true", "on" ]:
65
            verify = True
66
        elif FOREMAN_SSL_VERIFY.lower() in [ "0", "false", "off" ]:
67
            requests.packages.urllib3.disable_warnings()
68
            #print ("plugin %s: SSL verification of %s disabled" % (os.path.basename(__file__), FOREMAN_URL))
69
            verify = False
70
        else:  # Set ta a CA bundle:
71
            verify = FOREMAN_SSL_VERIFY
72
        return verify
73

    
74
    def send_facts(self, host, data):
75
        """
76
        Sends facts to Foreman, to be parsed by foreman_ansible fact
77
        parser.  The default fact importer should import these facts
78
        properly.
79
        """
80
        data["_type"] = "ansible"
81
        data["_timestamp"] = datetime.now().strftime(TIME_FORMAT)
82
        data = json.dumps(data)
83
        facts_json = FACTS_FORMAT % dict(host=host, data=data)
84

    
85

    
86
        requests.post(url=FOREMAN_URL + '/api/v2/hosts/facts',
87
                      data=facts_json,
88
                      headers=FOREMAN_HEADERS,
89
                      cert=FOREMAN_SSL_CERT,
90
                      verify=self.ssl_verify)
91

    
92

    
93
    def _build_log(self, data):
94
        logs = []
95
        for entry in data:
96
            if isinstance(entry, tuple):
97
                # v2 plugins have the task name
98
                source, msg = entry
99
            else:
100
                if 'invocation' in entry:
101
                    source = json.dumps(entry['invocation'])
102
                else:
103
                    source = json.dumps({"encrypted": "true"})                    
104
                msg = entry
105
            if 'failed' in msg:
106
                level = 'err'
107
            else:
108
                level = 'notice' if 'changed' in msg and msg['changed'] else 'info'
109
            logs.append({ "log": {
110
                'sources'  : { 'source' : source },
111
                'messages' : { 'message': json.dumps(msg) },
112
                'level':     level
113
                }})
114
        return logs
115

    
116

    
117
    def send_reports(self, stats):
118
        """
119
        Send reports to Foreman, to be parsed by Foreman config report
120
        importer.  I massage the data get a report json that Foreman
121
        can handle without writing another report importer.
122

    
123
        Currently it just sets the status. It's missing:
124
          - metrics, which we can get from data, except for runtime
125
        """
126
        status = defaultdict(lambda:0)
127
        metrics = {}
128

    
129
        for host in stats.processed.keys():
130
            sum = stats.summarize(host)
131
            status["applied"] = sum['changed']
132
            status["failed"] = sum['failures'] + sum['unreachable']
133
            status["skipped"] = sum['skipped']
134
            log = self._build_log(self.items[host])
135
            metrics["time"] = { "total": int(time.time()) - self.start_time }
136
            self.items[host] = []
137

    
138
            report_json = REPORT_FORMAT % dict(host=host,
139
                now=datetime.now().strftime(TIME_FORMAT),
140
                metrics=json.dumps(metrics),
141
                status=json.dumps(status),
142
                log=json.dumps(log))
143
#           To be changed to /api/v2/config_reports in 1.11.
144
#           Maybe we could make a GET request to get the Foreman version & do this
145
#           automatically.
146
            requests.post(url=FOREMAN_URL + '/api/v2/reports',
147
                          data=report_json,
148
                          headers=FOREMAN_HEADERS,
149
                          cert=FOREMAN_SSL_CERT,
150
                          verify=self.ssl_verify)
151

    
152
    def on_any(self, *args, **kwargs):
153
        pass
154

    
155
    def runner_on_failed(self, host, res, ignore_errors=False):
156
        self.items[host].append(res)
157

    
158
    def runner_on_ok(self, host, res):
159
        if 'invocation' in res and res['invocation']['module_name'] == 'setup':
160
            self.send_facts(host, res)
161
        else:
162
            self.items[host].append(res)
163

    
164
    def runner_on_skipped(self, host, item=None):
165
        pass
166

    
167
    def runner_on_unreachable(self, host, res):
168
        self.items[host].append(res)
169

    
170
    def runner_on_no_hosts(self):
171
        pass
172

    
173
    def runner_on_async_poll(self, host, res, jid, clock):
174
        pass
175

    
176
    def runner_on_async_ok(self, host, res, jid):
177
        self.items[host].append(res)
178

    
179
    def runner_on_async_failed(self, host, res, jid):
180
        self.items[host].append(res)
181

    
182
    def playbook_on_start(self):
183
        pass
184

    
185
    def playbook_on_notify(self, host, handler):
186
        pass
187

    
188
    def playbook_on_no_hosts_matched(self):
189
        pass
190

    
191
    def playbook_on_no_hosts_remaining(self):
192
        pass
193

    
194
    def playbook_on_task_start(self, name, is_conditional):
195
        pass
196

    
197
    def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
198
        pass
199

    
200
    def playbook_on_setup(self):
201
        pass
202

    
203
    def playbook_on_import_for_host(self, host, imported_file):
204
        pass
205

    
206
    def playbook_on_not_import_for_host(self, host, missing_file):
207
        pass
208

    
209
    def playbook_on_play_start(self, name):
210
        pass
211

    
212
    def playbook_on_stats(self, stats):
213
        self.send_reports(stats)
214

    
215
    # v2 callback API
216
    def v2_runner_on_ok(self, result):
217
        res = result._result
218
        host = result._host.get_name()
219
        try:
220
            module = res['invocation']['module_name']
221
        except KeyError:
222
            module = None
223
        if module == 'setup':
224
            self.send_facts(host, res)
225
        else:
226
            name = result._task.get_name()
227
            self.items[host].append((name, res))
228

    
229
    def v2_runner_on_failed(self, result, ignore_errors=False):
230
        name = result._task.get_name()
231
        host = result._host.get_name()
232
        self.items[host].append((name, result._result))
    (1-1/1)
    Add picture from clipboard (Maximum size: 8.91 MB)