app.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import sys
  2. import json
  3. import flask
  4. sys.path.append('./lib')
  5. import brew.v2 as brew
  6. import db
  7. import periodic
  8. import utils
  9. app = flask.Flask('stackbrew')
  10. config = None
  11. with open('./config.json') as config_file:
  12. config = json.load(config_file)
  13. data = db.DbManager(config['db_url'], debug=config['debug'])
  14. history = {}
  15. brew.logger = app.logger
  16. brew.set_loglevel('DEBUG' if config['debug'] else 'INFO')
  17. @app.route('/')
  18. def home():
  19. return utils.resp(app, 'stackbrew')
  20. @app.route('/summary')
  21. @app.route('/status')
  22. def latest_summary():
  23. result = data.latest_status()
  24. return utils.resp(app, result)
  25. @app.route('/summary/<int:id>')
  26. def get_summary(id):
  27. result = data.get_summary(id)
  28. return utils.resp(app, result)
  29. @app.route('/success/<repo_name>')
  30. def latest_success(repo_name):
  31. tag = flask.request.args.get('tag', None)
  32. result = data.get_latest_successful(repo_name, tag)
  33. return utils.resp(app, result)
  34. if config['debug']:
  35. @app.route('/build/force', methods=['POST'])
  36. def force_build():
  37. build_task()
  38. def build_task():
  39. summary = data.new_summary()
  40. library = brew.StackbrewLibrary(config['library_repo'])
  41. builder = brew.LocalBuilder(
  42. library=library, namespaces=config['namespaces'],
  43. repo_cache=config['repos_folder']
  44. )
  45. builder.build_repo_list()
  46. builder.history = history
  47. builder.build_all(callback=summary.handle_build_result)
  48. if config['push']:
  49. builder.push_all()
  50. try:
  51. periodic.init_task(build_task, config['build_interval'],
  52. logger=app.logger)
  53. app.logger.info('Periodic build task initiated.')
  54. except RuntimeError:
  55. app.logger.warning('Periodic build task already locked.')
  56. app.run(
  57. host=config.get('host', '127.0.0.1'),
  58. port=config.get('port', 5000),
  59. debug=config['debug']
  60. )