您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

146 行
4.0KB

  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. try:
  54. branch_name = repo.active_branch.name
  55. # test1
  56. except:
  57. branch_name = None
  58. changes = []
  59. commits_behind = repo.iter_commits('master..origin/master')
  60. for c in list(commits_behind):
  61. changes.append({"committer": c.committer.name, "message": c.message})
  62. return json.dumps({"tags": tags, "headcommit": str(repo.head.commit), "branchname": branch_name,
  63. "master": {"changes": changes}})
  64. @route('/check_update', methods=['GET'])
  65. def check_update(self):
  66. repo = Repo('./')
  67. o = repo.remotes.origin
  68. o.fetch()
  69. changes = []
  70. commits_behind = repo.iter_commits('master..origin/master')
  71. for c in list(commits_behind):
  72. changes.append({"committer": c.committer.name, "message": c.message})
  73. return json.dumps(changes)
  74. @route('/git/pull', methods=['POST'])
  75. def update(self):
  76. repo = Repo('./')
  77. o = repo.remotes.origin
  78. info = o.pull()
  79. cbpi.notify("Pull successful", "The lasted updated was downloaded. Please restart the system")
  80. return ('', 204)
  81. @route('/dump', methods=['GET'])
  82. def dump(self):
  83. return json.dumps(cbpi.cache)
  84. @route('/endpoints', methods=['GET'])
  85. def endpoints(self):
  86. import urllib.request, urllib.parse, urllib.error
  87. output = []
  88. vf = self.api.app.view_functions
  89. for f in self.api.app.view_functions:
  90. print(f)
  91. endpoints = {}
  92. re = {
  93. "swagger": "2.0",
  94. "host": "",
  95. "info": {
  96. "description":"",
  97. "version": "",
  98. "title": "CraftBeerPi"
  99. },
  100. "schemes": ["http"],
  101. "paths": endpoints}
  102. for rule in self.api.app.url_map.iter_rules():
  103. r = rule
  104. endpoints[rule.rule] = {}
  105. if "HEAD" in r.methods: r.methods.remove("HEAD")
  106. if "OPTIONS" in r.methods: r.methods.remove("OPTIONS")
  107. for m in rule.methods:
  108. endpoints[rule.rule][m] = dict(summary="", description="", consumes=["application/json"],produces=["application/json"])
  109. with open("config/version.yaml", 'r') as stream:
  110. y = yaml.load(stream)
  111. pprint.pprint(y)
  112. pprint.pprint(re)
  113. return Response(yaml.dump(re), mimetype='text/yaml')
  114. @cbpi.initalizer()
  115. def init(cbpi):
  116. SystemView.api = cbpi
  117. SystemView.register(cbpi.app, route_base='/api/system')