Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

178 wiersze
4.6KB

  1. import yaml
  2. from flask import json, url_for, Response
  3. from flask_classy import FlaskView, route
  4. from flask_login import login_required, current_user
  5. from git import Repo, Git
  6. from modules.core.core import cbpi
  7. import pprint
  8. import time
  9. class SystemView(FlaskView):
  10. def doShutdown(self):
  11. time.sleep(5)
  12. from subprocess import call
  13. call("halt")
  14. @login_required
  15. @route('/shutdown', methods=['POST'])
  16. def shutdown(self):
  17. """
  18. System Shutdown
  19. ---
  20. tags:
  21. - system
  22. responses:
  23. 200:
  24. description: Shutdown triggered
  25. """
  26. self.doShutdown()
  27. return ('', 204)
  28. def doReboot(self):
  29. time.sleep(5)
  30. from subprocess import call
  31. call("reboot")
  32. @login_required
  33. @route('/reboot', methods=['POST'])
  34. def reboot(self):
  35. """
  36. System Reboot
  37. ---
  38. tags:
  39. - system
  40. responses:
  41. 200:
  42. description: Reboot triggered
  43. """
  44. self.doReboot()
  45. return ('', 204)
  46. @login_required
  47. @route('/tags/<name>', methods=['GET'])
  48. def checkout_tag(self,name):
  49. repo = Repo('./')
  50. repo.git.reset('--hard')
  51. o = repo.remotes.origin
  52. o.fetch()
  53. g = Git('./')
  54. g.checkout(name)
  55. cbpi.notify("Checkout successful", "Please restart the system")
  56. return ('', 204)
  57. @login_required
  58. @route('/git/status', methods=['GET'])
  59. def git_status(self):
  60. """
  61. Check for GIT status
  62. ---
  63. tags:
  64. - system
  65. responses:
  66. 200:
  67. description: Git Status
  68. """
  69. repo = Repo('./')
  70. o = repo.remotes.origin
  71. o.fetch()
  72. # Tags
  73. tags = []
  74. for t in repo.tags:
  75. tags.append({"name": t.name, "commit": str(t.commit), "date": t.commit.committed_date,
  76. "committer": t.commit.committer.name, "message": t.commit.message})
  77. try:
  78. branch_name = repo.active_branch.name
  79. # test1
  80. except:
  81. branch_name = None
  82. changes = []
  83. commits_behind = repo.iter_commits('master..origin/master')
  84. for c in list(commits_behind):
  85. changes.append({"committer": c.committer.name, "message": c.message})
  86. return json.dumps({"tags": tags, "headcommit": str(repo.head.commit), "branchname": branch_name,
  87. "master": {"changes": changes}})
  88. @login_required
  89. @route('/check_update', methods=['GET'])
  90. def check_update(self):
  91. """
  92. Check for GIT update
  93. ---
  94. tags:
  95. - system
  96. responses:
  97. 200:
  98. description: Git Changes
  99. """
  100. repo = Repo('./')
  101. o = repo.remotes.origin
  102. o.fetch()
  103. changes = []
  104. commits_behind = repo.iter_commits('master..origin/master')
  105. for c in list(commits_behind):
  106. changes.append({"committer": c.committer.name, "message": c.message})
  107. return json.dumps(changes)
  108. @login_required
  109. @route('/git/pull', methods=['POST'])
  110. def update(self):
  111. """
  112. System Update
  113. ---
  114. tags:
  115. - system
  116. responses:
  117. 200:
  118. description: Git Pull Triggered
  119. """
  120. repo = Repo('./')
  121. o = repo.remotes.origin
  122. info = o.pull()
  123. cbpi.notify("Pull successful", "The lasted updated was downloaded. Please restart the system")
  124. return ('', 204)
  125. @route('/connect', methods=['GET'])
  126. def connect(self):
  127. """
  128. Connect
  129. ---
  130. tags:
  131. - system
  132. responses:
  133. 200:
  134. description: CraftBeerPi System Cache
  135. """
  136. if self.api.get_config_parameter("setup", "YES") == "YES":
  137. return json.dumps(dict(setup=True, loggedin= current_user.is_authenticated ))
  138. else:
  139. return json.dumps(dict(setup=False, loggedin= current_user.is_authenticated))
  140. @login_required
  141. @route('/dump', methods=['GET'])
  142. def dump(self):
  143. """
  144. Dump Cache
  145. ---
  146. tags:
  147. - system
  148. responses:
  149. 200:
  150. description: CraftBeerPi System Cache
  151. """
  152. return json.dumps(cbpi.cache)
  153. @cbpi.addon.core.initializer()
  154. def init(cbpi):
  155. SystemView.api = cbpi
  156. SystemView.register(cbpi._app, route_base='/api/system')