Skip to content

Commit e775b26

Browse files
committed
Add WSGI changes to reporter
1 parent 636a787 commit e775b26

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

reporter/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask
2+
elasticsearch

reporter/usage-report-collector.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# specific language governing permissions and limitations
1717
# under the License.
1818

19-
from flask import abort, Flask, request, Response
19+
from flask import abort, Flask, request
2020
from elasticsearch import Elasticsearch
2121
import json
2222
import time
@@ -30,24 +30,33 @@ def generate_app(config=None):
3030
@app.route('/report/<unique_id>', methods=['POST'])
3131
def report(unique_id):
3232
# We expect JSON data, so if the Content-Type doesn't match JSON data we throw an error
33-
if 'Content-Type' in request.headers:
34-
if request.headers['Content-Type'] != 'application/json':
35-
abort(417, "No or incorrect Content-Type header was supplied")
33+
if request.headers.get('Content-Type') != 'application/json':
34+
abort(417, "No or incorrect Content-Type header was supplied")
35+
36+
try:
37+
payload = json.loads(request.data)
38+
except json.JSONDecodeError:
39+
abort(400, "Request body is not valid JSON")
3640

3741
index = "cloudstack-%s" % time.strftime("%Y.%m.%d", time.gmtime())
3842
timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
3943

4044
es = Elasticsearch()
41-
es.indices.create(index=index, ignore=400)
4245

43-
report = json.loads(request.data)
44-
report["unique_id"] = unique_id
45-
report["timestamp"] = timestamp
46+
try:
47+
es.indices.create(index=index)
48+
except Exception:
49+
pass # Index already exists
50+
51+
payload["unique_id"] = unique_id
52+
payload["timestamp"] = timestamp
4653

47-
es.index(index=index, doc_type="usage-report", body=json.dumps(report), timestamp=timestamp, refresh=True)
54+
try:
55+
es.index(index=index, body=json.dumps(payload), refresh=True)
56+
except Exception as e:
57+
abort(500, "Failed to store report: %s" % str(e))
4858

49-
response = {}
50-
return json_response(response)
59+
return json_response({})
5160

5261
return app
5362

reporter/wsgi.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# WSGI entry point for mod_wsgi, gunicorn, uWSGI, etc.
19+
# The main application file uses a hyphenated name which cannot be imported
20+
# directly, so this shim loads it via importlib.
21+
#
22+
# mod_wsgi (Apache):
23+
# WSGIScriptAlias /report /path/to/reporter/wsgi.py
24+
#
25+
# gunicorn:
26+
# gunicorn wsgi:application
27+
#
28+
# uWSGI:
29+
# uwsgi --wsgi-file wsgi.py --callable application
30+
31+
import importlib.util
32+
import os
33+
34+
_spec = importlib.util.spec_from_file_location(
35+
"usage_report_collector",
36+
os.path.join(os.path.dirname(os.path.abspath(__file__)), "usage-report-collector.py")
37+
)
38+
_mod = importlib.util.module_from_spec(_spec)
39+
_spec.loader.exec_module(_mod)
40+
41+
application = _mod.app

0 commit comments

Comments
 (0)