app.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. return utils.resp(app, 'OK')
  39. def build_task():
  40. summary = data.new_summary(config['repos_folder'])
  41. library = brew.StackbrewLibrary(config['library_repo'])
  42. builder = brew.LocalBuilder(
  43. library=library, namespaces=config['namespaces'],
  44. repo_cache=config['repos_folder']
  45. )
  46. builder.build_repo_list()
  47. builder.history = history
  48. builder.build_all(callback=summary.handle_build_result)
  49. if config['push']:
  50. builder.push_all()
  51. try:
  52. periodic.init_task(build_task, config['build_interval'],
  53. logger=app.logger)
  54. app.logger.info('Periodic build task initiated.')
  55. except RuntimeError:
  56. app.logger.warning('Periodic build task already locked.')
  57. app.run(
  58. host=config.get('host', '127.0.0.1'),
  59. port=config.get('port', 5000),
  60. debug=config['debug']
  61. )