25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.3KB

  1. import yaml
  2. from flask import json, url_for, Response
  3. from flask_classy import FlaskView, route
  4. from git import Repo, Git
  5. from modules.app_config import cbpi
  6. import pprint
  7. import time
  8. class SystemView(FlaskView):
  9. def doShutdown(self):
  10. time.sleep(5)
  11. from subprocess import call
  12. call("halt")
  13. @route('/shutdown', methods=['POST'])
  14. def shutdown(self):
  15. """
  16. Shutdown hook
  17. :return: HTTP 204
  18. """
  19. self.doShutdown()
  20. return ('', 204)
  21. def doReboot(self):
  22. time.sleep(5)
  23. from subprocess import call
  24. call("reboot")
  25. @route('/reboot', methods=['POST'])
  26. def reboot(self):
  27. """
  28. Reboot hook
  29. :return: HTTP 204
  30. """
  31. self.doReboot()
  32. return ('', 204)
  33. @route('/tags/<name>', methods=['GET'])
  34. def checkout_tag(self,name):
  35. repo = Repo('./')
  36. repo.git.reset('--hard')
  37. o = repo.remotes.origin
  38. o.fetch()
  39. g = Git('./')
  40. g.checkout(name)
  41. cbpi.notify("Checkout successful", "Please restart the system")
  42. return ('', 204)
  43. @route('/git/status', methods=['GET'])
  44. def git_status(self):
  45. repo = Repo('./')
  46. o = repo.remotes.origin
  47. o.fetch()
  48. # Tags
  49. tags = []
  50. for t in repo.tags:
  51. tags.append({"name": t.name, "commit": str(t.commit), "date": t.commit.committed_date,
  52. "committer": t.commit.committer.name, "message": t.commit.message})
  53. for b in repo.branches:
  54. tags.append({"name": b.name, "commit": str(b.commit), "date": b.commit.committed_date,
  55. "committer": b.commit.committer.name, "message": b.commit.message})
  56. try:
  57. branch_name = repo.active_branch.name
  58. # test1
  59. except:
  60. branch_name = None
  61. changes = []
  62. commits_behind = repo.iter_commits('master..origin/master')
  63. for c in list(commits_behind):
  64. changes.append({"committer": c.committer.name, "message": c.message})
  65. return json.dumps({"tags": tags, "headcommit": str(repo.head.commit), "branchname": branch_name,
  66. "master": {"changes": changes}})
  67. @route('/check_update', methods=['GET'])
  68. def check_update(self):
  69. repo = Repo('./')
  70. o = repo.remotes.origin
  71. o.fetch()
  72. changes = []
  73. commits_behind = repo.iter_commits('master..origin/master')
  74. for c in list(commits_behind):
  75. changes.append({"committer": c.committer.name, "message": c.message})
  76. return json.dumps(changes)
  77. @route('/git/pull', methods=['POST'])
  78. def update(self):
  79. repo = Repo('./')
  80. o = repo.remotes.origin
  81. info = o.pull()
  82. cbpi.notify("Pull successful", "The lasted updated was downloaded. Please restart the system")
  83. return ('', 204)
  84. @route('/dump', methods=['GET'])
  85. def dump(self):
  86. return json.dumps(cbpi.cache)
  87. @route('/endpoints', methods=['GET'])
  88. def endpoints(self):
  89. import urllib
  90. output = []
  91. vf = self.api.app.view_functions
  92. for f in self.api.app.view_functions:
  93. print f
  94. endpoints = {}
  95. re = {
  96. "swagger": "2.0",
  97. "host": "",
  98. "info": {
  99. "description":"",
  100. "version": "",
  101. "title": "CraftBeerPi"
  102. },
  103. "schemes": ["http"],
  104. "paths": endpoints}
  105. for rule in self.api.app.url_map.iter_rules():
  106. r = rule
  107. endpoints[rule.rule] = {}
  108. if "HEAD" in r.methods: r.methods.remove("HEAD")
  109. if "OPTIONS" in r.methods: r.methods.remove("OPTIONS")
  110. for m in rule.methods:
  111. endpoints[rule.rule][m] = dict(summary="", description="", consumes=["application/json"],produces=["application/json"])
  112. with open("config/version.yaml", 'r') as stream:
  113. y = yaml.load(stream)
  114. pprint.pprint(y)
  115. pprint.pprint(re)
  116. return Response(yaml.dump(re), mimetype='text/yaml')
  117. @cbpi.initalizer()
  118. def init(cbpi):
  119. SystemView.api = cbpi
  120. SystemView.register(cbpi.app, route_base='/api/system')