2014-02-06 18:36:32 +00:00
|
|
|
import logging
|
2014-02-07 01:58:26 +00:00
|
|
|
import json
|
2014-02-06 18:36:32 +00:00
|
|
|
|
2014-03-26 19:52:24 +00:00
|
|
|
from flask import request, Blueprint, abort, Response
|
|
|
|
from flask.ext.login import current_user
|
|
|
|
from auth.auth import require_session_login
|
2015-01-28 17:35:01 +00:00
|
|
|
from endpoints.common import route_show_if
|
2015-01-28 22:12:33 +00:00
|
|
|
from app import app, userevents
|
2015-01-28 17:35:01 +00:00
|
|
|
from auth.permissions import SuperUserPermission
|
2016-06-28 19:35:21 +00:00
|
|
|
from data.userevent import CannotReadUserEventsException
|
2015-01-28 17:35:01 +00:00
|
|
|
|
|
|
|
import features
|
|
|
|
import psutil
|
|
|
|
import time
|
2014-02-06 18:36:32 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
realtime = Blueprint('realtime', __name__)
|
|
|
|
|
|
|
|
|
2015-01-28 17:35:01 +00:00
|
|
|
@realtime.route("/ps")
|
|
|
|
@route_show_if(features.SUPER_USERS)
|
|
|
|
@require_session_login
|
|
|
|
def ps():
|
|
|
|
if not SuperUserPermission().can():
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
def generator():
|
|
|
|
while True:
|
2015-01-28 22:12:33 +00:00
|
|
|
build_status = {}
|
2015-02-17 16:31:50 +00:00
|
|
|
try:
|
|
|
|
builder_data = app.config['HTTPCLIENT'].get('http://localhost:8686/status', timeout=1)
|
|
|
|
if builder_data.status_code == 200:
|
|
|
|
build_status = json.loads(builder_data.text)
|
|
|
|
except:
|
|
|
|
pass
|
2015-01-28 22:12:33 +00:00
|
|
|
|
2015-02-18 00:18:56 +00:00
|
|
|
try:
|
|
|
|
data = {
|
|
|
|
'count': {
|
|
|
|
'cpu': psutil.cpu_percent(interval=1, percpu=True),
|
|
|
|
'virtual_mem': psutil.virtual_memory(),
|
|
|
|
'swap_mem': psutil.swap_memory(),
|
|
|
|
'connections': len(psutil.net_connections()),
|
|
|
|
'processes': len(psutil.pids()),
|
|
|
|
'network': psutil.net_io_counters()
|
|
|
|
},
|
|
|
|
'build': build_status
|
|
|
|
}
|
|
|
|
except psutil.AccessDenied:
|
|
|
|
data = {}
|
|
|
|
|
2015-01-28 17:35:01 +00:00
|
|
|
json_string = json.dumps(data)
|
|
|
|
yield 'data: %s\n\n' % json_string
|
2015-02-18 00:15:54 +00:00
|
|
|
time.sleep(1)
|
2015-01-28 17:35:01 +00:00
|
|
|
|
2015-02-18 00:15:54 +00:00
|
|
|
try:
|
|
|
|
return Response(generator(), mimetype="text/event-stream")
|
|
|
|
except:
|
|
|
|
pass
|
2015-01-28 17:35:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
@realtime.route("/user/")
|
2014-03-26 19:52:24 +00:00
|
|
|
@require_session_login
|
2014-02-06 18:36:32 +00:00
|
|
|
def index():
|
2014-03-26 19:52:24 +00:00
|
|
|
debug_template = """
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Server sent events</h1>
|
|
|
|
<div id="event"></div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
|
|
|
|
var eventOutputContainer = document.getElementById("event");
|
|
|
|
var evtSrc = new EventSource("/realtime/user/subscribe?events=docker-cli");
|
|
|
|
|
|
|
|
evtSrc.onmessage = function(e) {
|
|
|
|
console.log(e.data);
|
|
|
|
eventOutputContainer.innerHTML = e.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
2014-02-06 18:36:32 +00:00
|
|
|
"""
|
2014-03-26 19:52:24 +00:00
|
|
|
return(debug_template)
|
2014-02-06 18:36:32 +00:00
|
|
|
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
@realtime.route("/user/test")
|
2014-03-26 19:52:24 +00:00
|
|
|
@require_session_login
|
2014-02-07 01:58:26 +00:00
|
|
|
def user_test():
|
2014-05-30 18:25:29 +00:00
|
|
|
evt = userevents.get_event(current_user.db_user().username)
|
2014-02-07 01:58:26 +00:00
|
|
|
evt.publish_event_data('test', {'foo': 2})
|
|
|
|
return 'OK'
|
|
|
|
|
|
|
|
@realtime.route("/user/subscribe")
|
2014-03-26 19:52:24 +00:00
|
|
|
@require_session_login
|
2014-02-07 01:58:26 +00:00
|
|
|
def user_subscribe():
|
|
|
|
def wrapper(listener):
|
2016-07-27 20:41:26 +00:00
|
|
|
yield 'data: %s\n\n' % json.dumps({})
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
for event_id, data in listener.event_stream():
|
|
|
|
message = {'event': event_id, 'data': data}
|
|
|
|
json_string = json.dumps(message)
|
|
|
|
yield 'data: %s\n\n' % json_string
|
|
|
|
|
|
|
|
events = request.args.get('events', '').split(',')
|
|
|
|
if not events:
|
|
|
|
abort(404)
|
|
|
|
|
2016-06-28 19:35:21 +00:00
|
|
|
try:
|
|
|
|
listener = userevents.get_listener(current_user.db_user().username, events)
|
|
|
|
except CannotReadUserEventsException:
|
|
|
|
abort(504)
|
|
|
|
|
2014-02-07 01:58:26 +00:00
|
|
|
return Response(wrapper(listener), mimetype="text/event-stream")
|