pagure_ci_server.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. (c) 2016 - Copyright Red Hat Inc
  5. Authors:
  6. Pierre-Yves Chibon <pingou@pingoured.fr>
  7. This server listens to message sent via redis and send the corresponding
  8. web-hook request.
  9. Using this mechanism, we no longer block the main application if the
  10. receiving end is offline or so.
  11. """
  12. import json
  13. import logging
  14. logging.basicConfig(level=logging.DEBUG)
  15. import os
  16. import requests
  17. import trollius
  18. import trollius_redis
  19. LOG = logging.getLogger(__name__)
  20. if 'PAGURE_CONFIG' not in os.environ \
  21. and os.path.exists('/etc/pagure/pagure.cfg'):
  22. print 'Using configuration file `/etc/pagure/pagure.cfg`'
  23. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  24. import pagure
  25. import pagure.lib
  26. @trollius.coroutine
  27. def handle_messages():
  28. ''' Handles connecting to redis and acting upon messages received.
  29. In this case, it means triggering a build on jenkins based on the
  30. information provided.
  31. '''
  32. host = pagure.APP.config.get('REDIS_HOST', '0.0.0.0')
  33. port = pagure.APP.config.get('REDIS_PORT', 6379)
  34. dbname = pagure.APP.config.get('REDIS_DB', 0)
  35. connection = yield trollius.From(trollius_redis.Connection.create(
  36. host=host, port=port, db=dbname))
  37. # Create subscriber.
  38. subscriber = yield trollius.From(connection.start_subscribe())
  39. # Subscribe to channel.
  40. yield trollius.From(subscriber.subscribe(['pagure.ci']))
  41. # Inside a while loop, wait for incoming events.
  42. while True:
  43. reply = yield trollius.From(subscriber.next_published())
  44. LOG.info(
  45. 'Received: %s on channel: %s',
  46. repr(reply.value), reply.channel)
  47. data = json.loads(reply.value)
  48. pr_id = data['pr']['id']
  49. pr_uid = data['pr']['uid']
  50. branch = data['pr']['branch_from']
  51. LOG.info('Looking for PR: %s', pr_uid)
  52. request = pagure.lib.get_request_by_uid(pagure.SESSION, pr_uid)
  53. if not request:
  54. LOG.warning(
  55. 'No request could be found from the message %s', data)
  56. continue
  57. LOG.info(
  58. "Trigger on %s PR #%s from %s: %s",
  59. request.project.fullname, pr_id,
  60. request.project_from.fullname, branch)
  61. url = request.project.ci_hook.ci_url.rstrip('/')
  62. if data['ci_type'] == 'jenkins':
  63. url = url + '/buildWithParameters'
  64. repo = '%s/%s' % (
  65. pagure.APP.config['GIT_URL_GIT'].rstrip('/'),
  66. request.project_from.path)
  67. LOG.info(
  68. 'Triggering the build at: %s, for repo: %s', url, repo)
  69. requests.post(
  70. url,
  71. data={
  72. 'token': request.project.ci_hook.pagure_ci_token,
  73. 'cause': pr_id,
  74. 'REPO': repo,
  75. 'BRANCH': branch
  76. }
  77. )
  78. else:
  79. LOG.warning('Un-supported CI type')
  80. LOG.info('Ready for another')
  81. def main():
  82. ''' Start the main async loop. '''
  83. try:
  84. loop = trollius.get_event_loop()
  85. tasks = [
  86. trollius.async(handle_messages()),
  87. ]
  88. loop.run_until_complete(trollius.wait(tasks))
  89. loop.run_forever()
  90. except KeyboardInterrupt:
  91. pass
  92. except trollius.ConnectionResetError:
  93. pass
  94. LOG.info("End Connection")
  95. loop.close()
  96. LOG.info("End")
  97. if __name__ == '__main__':
  98. formatter = logging.Formatter(
  99. "%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s")
  100. # setup console logging
  101. LOG.setLevel(logging.DEBUG)
  102. shellhandler = logging.StreamHandler()
  103. shellhandler.setLevel(logging.DEBUG)
  104. aslog = logging.getLogger("asyncio")
  105. aslog.setLevel(logging.DEBUG)
  106. aslog = logging.getLogger("trollius")
  107. aslog.setLevel(logging.DEBUG)
  108. shellhandler.setFormatter(formatter)
  109. LOG.addHandler(shellhandler)
  110. main()