Fixed epic conflicts
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Füleki Fábián 2020-05-08 23:10:13 +02:00
commit 2e7a0546dd
8 changed files with 123 additions and 22 deletions

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sentry_sdk import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from flask import Flask from flask import Flask
import os import os
from db import redis_client from db import redis_client
@ -14,11 +15,22 @@ __copyright__ = "Copyright 2020, GoldenPogácsa Team"
__module_name__ = "app" __module_name__ = "app"
__version__text__ = "1" __version__text__ = "1"
sentry_sdk.init("https://0a106e104e114bc9a3fa47f9cb0db2f4@sentry.kmlabz.com/10") # Setup sentry
SENTRY_DSN = os.environ.get("SENTRY_DSN")
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[FlaskIntegration()],
send_default_pii=True,
release=os.environ.get('RELEASE_ID', 'test'),
environment=os.environ.get('RELEASEMODE', 'dev')
)
app = Flask(__name__) app = Flask(__name__)
app.config['REDIS_URL'] = os.environ['REDIS_URL'] app.config['REDIS_URL'] = os.environ['REDIS_URL']
app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID'] app.config['LOCAL_UUID'] = os.environ['LOCAL_UUID']
app.config['CUSTOMER_TIMEOUT'] = int(os.environ.get('CUSTOMER_TIMEOUT', 30))
app.config['PRODUCER_TIMEOUT'] = int(os.environ.get('PRODUCER_TIMEOUT', 60))
redis_client.init_app(app) redis_client.init_app(app)

View File

@ -0,0 +1,6 @@
import os
# Setup environment variables for testing
os.environ["INITIAL_SERVERS"] = "192.168.111.22"
os.environ["LOCAL_UUID"] = "d8b2e5e2-f675-4194-9324-af58e4b70c54"
os.environ["REDIS_URL"] = "redis://192.168.111.121/0"

View File

@ -0,0 +1,17 @@
import pytest
from flask import current_app
@pytest.fixture
def client():
current_app.config['TESTING'] = True
with current_app.test_client() as client:
yield client
def test_response_length(client):
r = client.get('/consumers')
assert len(r) == 0

View File

@ -0,0 +1,30 @@
import json
import os
import pytest
from flask import current_app
@pytest.fixture
def client():
current_app.config['TESTING'] = True
with current_app.test_client() as client:
yield client
def test_log_code_get(client):
r = client.get('/log')
assert r.status_code == 405
def test_log_code_post(client):
data = {
"uuid": os.environ["LOCAL_UUID"],
"message": "Hello There!"
}
r = client.post('/log', data = json.dumps(data))
assert r.status_code == 204

View File

@ -0,0 +1,30 @@
import json
import os
import pytest
from flask import current_app
@pytest.fixture
def client():
current_app.config['TESTING'] = True
with current_app.test_client() as client:
yield client
def test_log_code_get(client):
r = client.get('/sync')
assert r.status_code == 405
def test_log_code_post(client):
data = {
"uuid": os.environ["LOCAL_UUID"],
"message": "Hello There!"
}
r = client.post('/sync', data = json.dumps(data))
assert r.status_code == 204

View File

@ -8,11 +8,12 @@ from flask_classful import FlaskView
class ConsumersView(FlaskView): class ConsumersView(FlaskView):
def get(self): def get(self):
# load the currently available consumer list from the redis database keys = redis_client.keys('consumer_*')
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
# jsonify and return the list of active consumers list_of_customer_ips = []
if 'full' in request.args:
return jsonify(consumer_list) for key in keys:
else: info = json.loads((redis_client.get(key) or b"{}").decode('utf-8'))
return jsonify([v['ip'] for k, v in consumer_list.items()]) list_of_customer_ips.append(info['ip'])
return jsonify(list_of_customer_ips)

View File

@ -19,11 +19,12 @@ class LogView(FlaskView):
if not last_known_remote_ip: if not last_known_remote_ip:
current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}") current_app.logger.info(f"New producer {remote_uuid} at {remote_ip}")
elif last_known_remote_ip != remote_ip: elif last_known_remote_ip != remote_ip:
current_app.logger.info(f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}") current_app.logger.info(
f"IP address of producer {remote_uuid} have changed: {last_known_remote_ip} -> {remote_ip}")
# update expirity # update expirity
redis_client.set(prod_key, remote_ip.encode('utf-8')) redis_client.set(prod_key, remote_ip.encode('utf-8'))
redis_client.expire(prod_key, 240) redis_client.expire(prod_key, current_app.config["PRODUCER_TIMEOUT"])
# print out message # print out message
current_app.logger.info(f"New message: {request.json['message']}") current_app.logger.info(f"New message: {request.json['message']}")

View File

@ -1,4 +1,5 @@
import json import json
import time
from flask import request, current_app, jsonify from flask import request, current_app, jsonify
from flask_classful import FlaskView from flask_classful import FlaskView
from db import redis_client from db import redis_client
@ -10,22 +11,25 @@ class SyncView(FlaskView):
remote_uuid = request.json['uuid'] remote_uuid = request.json['uuid']
remote_ip = request.remote_addr remote_ip = request.remote_addr
# load the currently available consumer list from the redis database cust_key = f"consumer_{remote_uuid}"
consumer_list = json.loads((redis_client.get("consumer_list") or b"{}").decode('utf-8'))
if remote_uuid not in consumer_list.keys(): last_known_info = json.loads((redis_client.get(cust_key) or b"{}").decode('utf-8'))
# display newly registered consumer
if not last_known_info:
current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}") current_app.logger.info(f"New consumer registered (unknown UUID): {remote_uuid} at {remote_ip}")
else: else:
if consumer_list[remote_uuid]['ip'] != remote_ip: if last_known_info['ip'] != remote_ip:
# log address changes
current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}") current_app.logger.info(f"Address of consumer {remote_uuid} changed to {remote_ip}")
# update consumer list redis databasse info = {
consumer_list.update( "uuid": remote_uuid,
{remote_uuid: {"ip": remote_ip}} "ip": remote_ip,
) "last_seen": time.time()
redis_client.set("consumer_list", json.dumps(consumer_list).encode('utf-8')) }
redis_client.set(cust_key, json.dumps(info).encode('utf-8'))
redis_client.expire(cust_key, current_app.config["CUSTOMER_TIMEOUT"])
# return with the current UUID # return with the current UUID
response = { response = {