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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#!/usr/bin/python3
"""
(c) 2015-2017 - Copyright Red Hat Inc
Authors:
Pierre-Yves Chibon <pingou@pingoured.fr>
Streaming server for pagure's eventsource feature
This server takes messages sent to redis and publish them at the specified
endpoint
To test, run this script and in another terminal
nc localhost 8080
HELLO
GET /test/issue/26?foo=bar HTTP/1.1
"""
from __future__ import unicode_literals, absolute_import
import logging
import os
import redis
import trololio
from six.moves.urllib.parse import urlparse
log = logging.getLogger(__name__)
if "PAGURE_CONFIG" not in os.environ and os.path.exists(
"/etc/pagure/pagure.cfg"
):
print("Using configuration file `/etc/pagure/pagure.cfg`")
os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"
import pagure # noqa: E402
import pagure.lib.model_base # noqa: E402
import pagure.lib.query # noqa: E402
from pagure.exceptions import PagureException, PagureEvException # noqa: E402
SERVER = None
SESSION = None
POOL = redis.ConnectionPool(
host=pagure.config.config["REDIS_HOST"],
port=pagure.config.config["REDIS_PORT"],
db=pagure.config.config["REDIS_DB"],
)
def _get_session():
global SESSION
if SESSION is None:
print(pagure.config.config["DB_URL"])
SESSION = pagure.lib.model_base.create_session(
pagure.config.config["DB_URL"]
)
return SESSION
def _get_issue(repo, objid):
"""Get a Ticket (issue) instance for a given repo (Project) and
objid (issue number).
"""
issue = None
if not repo.settings.get("issue_tracker", True):
raise PagureEvException("No issue tracker found for this project")
session = _get_session()
issue = pagure.lib.query.search_issues(session, repo, issueid=objid)
if issue is None or issue.project != repo:
raise PagureEvException("Issue '%s' not found" % objid)
if issue.private:
# TODO: find a way to do auth
raise PagureEvException(
"This issue is private and you are not allowed to view it"
)
return issue
def _get_pull_request(repo, objid):
"""Get a PullRequest instance for a given repo (Project) and objid
(request number).
"""
if not repo.settings.get("pull_requests", True):
raise PagureEvException(
"No pull-request tracker found for this project"
)
session = _get_session()
request = pagure.lib.query.search_pull_requests(
session, project_id=repo.id, requestid=objid
)
if request is None or request.project != repo:
raise PagureEvException("Pull-Request '%s' not found" % objid)
return request
# Dict representing known object types that we handle requests for,
# and the bound functions for getting an object instance from the
# parsed path data. Has to come after the functions it binds
OBJECTS = {"issue": _get_issue, "pull-request": _get_pull_request}
def get_obj_from_path(path):
""" Return the Ticket or Request object based on the path provided.
"""
(username, namespace, reponame, objtype, objid) = pagure.utils.parse_path(
path
)
session = _get_session()
repo = pagure.lib.query.get_authorized_project(
session, reponame, user=username, namespace=namespace
)
if repo is None:
raise PagureEvException("Project '%s' not found" % reponame)
# find the appropriate object getter function from OBJECTS
try:
getfunc = OBJECTS[objtype]
except KeyError:
raise PagureEvException("Invalid object provided: '%s'" % objtype)
return getfunc(repo, objid)
@trololio.coroutine
def handle_client(client_reader, client_writer):
data = None
while True:
# give client a chance to respond, timeout after 10 seconds
line = yield trololio.From(
trololio.asyncio.wait_for(client_reader.readline(), timeout=10.0)
)
if not line.decode().strip():
break
line = line.decode().rstrip()
if data is None:
data = line
if data is None:
log.warning("Expected ticket uid, received None")
return
data = data.decode().rstrip().split()
log.info("Received %s", data)
if not data:
log.warning("No URL provided: %s" % data)
return
if "/" not in data[1]:
log.warning("Invalid URL provided: %s" % data[1])
return
url = urlparse(data[1])
try:
obj = get_obj_from_path(url.path)
except PagureException as err:
log.warning(err.message)
return
origin = pagure.config.config.get("APP_URL")
if origin.endswith("/"):
origin = origin[:-1]
client_writer.write(
(
"HTTP/1.0 200 OK\n"
"Content-Type: text/event-stream\n"
"Cache: nocache\n"
"Connection: keep-alive\n"
"Access-Control-Allow-Origin: %s\n\n" % origin
).encode()
)
conn = redis.Redis(connection_pool=POOL)
subscriber = conn.pubsub(ignore_subscribe_messages=True)
try:
subscriber.subscribe("pagure.%s" % obj.uid)
# Inside a while loop, wait for incoming events.
oncall = 0
while True:
msg = subscriber.get_message()
if msg is None:
# Send a ping to see if the client is still alive
if oncall >= 5:
# Only send a ping once every 5 seconds
client_writer.write(("event: ping\n\n").encode())
oncall = 0
oncall += 1
yield trololio.From(client_writer.drain())
yield trololio.From(trololio.asyncio.sleep(1))
else:
log.info("Sending %s", msg["data"])
client_writer.write(("data: %s\n\n" % msg["data"]).encode())
yield trololio.From(client_writer.drain())
except OSError:
log.info("Client closed connection")
except trololio.ConnectionResetError as err:
log.exception("ERROR: ConnectionResetError in handle_client")
except Exception as err:
log.exception("ERROR: Exception in handle_client")
log.info(type(err))
finally:
# Wathever happens, close the connection.
log.info("Client left. Goodbye!")
subscriber.close()
client_writer.close()
@trololio.coroutine
def stats(client_reader, client_writer):
try:
log.info("Clients: %s", SERVER.active_count)
client_writer.write(
("HTTP/1.0 200 OK\n" "Cache: nocache\n\n").encode()
)
client_writer.write(("data: %s\n\n" % SERVER.active_count).encode())
yield trololio.From(client_writer.drain())
except trololio.ConnectionResetError as err:
log.info(err)
finally:
client_writer.close()
return
def main():
global SERVER
_get_session()
try:
loop = trololio.asyncio.get_event_loop()
coro = trololio.asyncio.start_server(
handle_client,
host=None,
port=pagure.config.config["EVENTSOURCE_PORT"],
loop=loop,
)
SERVER = loop.run_until_complete(coro)
log.info(
"Serving server at {}".format(SERVER.sockets[0].getsockname())
)
if pagure.config.config.get("EV_STATS_PORT"):
stats_coro = trololio.asyncio.start_server(
stats,
host=None,
port=pagure.config.config.get("EV_STATS_PORT"),
loop=loop,
)
stats_server = loop.run_until_complete(stats_coro)
log.info(
"Serving stats at {}".format(
stats_server.sockets[0].getsockname()
)
)
loop.run_forever()
except KeyboardInterrupt:
pass
except trololio.ConnectionResetError as err:
log.exception("ERROR: ConnectionResetError in main")
except Exception:
log.exception("ERROR: Exception in main")
finally:
# Close the server
SERVER.close()
if pagure.config.config.get("EV_STATS_PORT"):
stats_server.close()
log.info("End Connection")
loop.run_until_complete(SERVER.wait_closed())
loop.close()
log.info("End")
if __name__ == "__main__":
log = logging.getLogger("")
formatter = logging.Formatter(
"%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s"
)
# setup console logging
log.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
aslog = logging.getLogger("asyncio")
aslog.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
log.addHandler(ch)
main()
|