Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

153 lines
3.9KB

  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.core.core 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. System Shutdown
  17. ---
  18. tags:
  19. - system
  20. responses:
  21. 200:
  22. description: Shutdown triggered
  23. """
  24. self.doShutdown()
  25. return ('', 204)
  26. def doReboot(self):
  27. time.sleep(5)
  28. from subprocess import call
  29. call("reboot")
  30. @route('/reboot', methods=['POST'])
  31. def reboot(self):
  32. """
  33. System Reboot
  34. ---
  35. tags:
  36. - system
  37. responses:
  38. 200:
  39. description: Reboot triggered
  40. """
  41. self.doReboot()
  42. return ('', 204)
  43. @route('/tags/<name>', methods=['GET'])
  44. def checkout_tag(self,name):
  45. repo = Repo('./')
  46. repo.git.reset('--hard')
  47. o = repo.remotes.origin
  48. o.fetch()
  49. g = Git('./')
  50. g.checkout(name)
  51. cbpi.notify("Checkout successful", "Please restart the system")
  52. return ('', 204)
  53. @route('/git/status', methods=['GET'])
  54. def git_status(self):
  55. """
  56. Check for GIT status
  57. ---
  58. tags:
  59. - system
  60. responses:
  61. 200:
  62. description: Git Status
  63. """
  64. repo = Repo('./')
  65. o = repo.remotes.origin
  66. o.fetch()
  67. # Tags
  68. tags = []
  69. for t in repo.tags:
  70. tags.append({"name": t.name, "commit": str(t.commit), "date": t.commit.committed_date,
  71. "committer": t.commit.committer.name, "message": t.commit.message})
  72. try:
  73. branch_name = repo.active_branch.name
  74. # test1
  75. except:
  76. branch_name = None
  77. changes = []
  78. commits_behind = repo.iter_commits('master..origin/master')
  79. for c in list(commits_behind):
  80. changes.append({"committer": c.committer.name, "message": c.message})
  81. return json.dumps({"tags": tags, "headcommit": str(repo.head.commit), "branchname": branch_name,
  82. "master": {"changes": changes}})
  83. @route('/check_update', methods=['GET'])
  84. def check_update(self):
  85. """
  86. Check for GIT update
  87. ---
  88. tags:
  89. - system
  90. responses:
  91. 200:
  92. description: Git Changes
  93. """
  94. repo = Repo('./')
  95. o = repo.remotes.origin
  96. o.fetch()
  97. changes = []
  98. commits_behind = repo.iter_commits('master..origin/master')
  99. for c in list(commits_behind):
  100. changes.append({"committer": c.committer.name, "message": c.message})
  101. return json.dumps(changes)
  102. @route('/git/pull', methods=['POST'])
  103. def update(self):
  104. """
  105. System Update
  106. ---
  107. tags:
  108. - system
  109. responses:
  110. 200:
  111. description: Git Pull Triggered
  112. """
  113. repo = Repo('./')
  114. o = repo.remotes.origin
  115. info = o.pull()
  116. cbpi.notify("Pull successful", "The lasted updated was downloaded. Please restart the system")
  117. return ('', 204)
  118. @route('/dump', methods=['GET'])
  119. def dump(self):
  120. """
  121. Dump Cache
  122. ---
  123. tags:
  124. - system
  125. responses:
  126. 200:
  127. description: CraftBeerPi System Cache
  128. """
  129. return json.dumps(cbpi.cache)
  130. @cbpi.addon.core.initializer()
  131. def init(cbpi):
  132. SystemView.api = cbpi
  133. SystemView.register(cbpi._app, route_base='/api/system')