Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 рядки
2.9KB

  1. from flask import json
  2. from flask_classy import FlaskView, route
  3. from git import Repo, Git
  4. from modules.app_config import cbpi
  5. import pprint
  6. import time
  7. class SystemView(FlaskView):
  8. def doShutdown(self):
  9. time.sleep(5)
  10. from subprocess import call
  11. call("halt")
  12. @route('/shutdown', methods=['POST'])
  13. def shutdown(self):
  14. """
  15. Shutdown hook
  16. :return: HTTP 204
  17. """
  18. self.doShutdown()
  19. return ('', 204)
  20. def doReboot(self):
  21. time.sleep(5)
  22. from subprocess import call
  23. call("reboot")
  24. @route('/reboot', methods=['POST'])
  25. def reboot(self):
  26. """
  27. Reboot hook
  28. :return: HTTP 204
  29. """
  30. self.doReboot()
  31. return ('', 204)
  32. @route('/tags/<name>', methods=['GET'])
  33. def checkout_tag(self,name):
  34. repo = Repo('./')
  35. repo.git.reset('--hard')
  36. o = repo.remotes.origin
  37. o.fetch()
  38. g = Git('./')
  39. g.checkout(name)
  40. cbpi.notify("Checkout successful", "Please restart the system")
  41. return ('', 204)
  42. @route('/git/status', methods=['GET'])
  43. def git_status(self):
  44. repo = Repo('./')
  45. o = repo.remotes.origin
  46. o.fetch()
  47. # Tags
  48. tags = []
  49. for t in repo.tags:
  50. tags.append({"name": t.name, "commit": str(t.commit), "date": t.commit.committed_date,
  51. "committer": t.commit.committer.name, "message": t.commit.message})
  52. try:
  53. branch_name = repo.active_branch.name
  54. # test1
  55. except:
  56. branch_name = None
  57. changes = []
  58. commits_behind = repo.iter_commits('master..origin/master')
  59. for c in list(commits_behind):
  60. changes.append({"committer": c.committer.name, "message": c.message})
  61. return json.dumps({"tags": tags, "headcommit": str(repo.head.commit), "branchname": branch_name,
  62. "master": {"changes": changes}})
  63. @route('/check_update', methods=['GET'])
  64. def check_update(self):
  65. repo = Repo('./')
  66. o = repo.remotes.origin
  67. o.fetch()
  68. changes = []
  69. commits_behind = repo.iter_commits('master..origin/master')
  70. for c in list(commits_behind):
  71. changes.append({"committer": c.committer.name, "message": c.message})
  72. return json.dumps(changes)
  73. @route('/git/pull', methods=['POST'])
  74. def update(self):
  75. repo = Repo('./')
  76. o = repo.remotes.origin
  77. info = o.pull()
  78. cbpi.notify("Pull successful", "The lasted updated was downloaded. Please restart the system")
  79. return ('', 204)
  80. @route('/dump', methods=['GET'])
  81. def dump(self):
  82. return json.dumps(cbpi.cache)
  83. @cbpi.initalizer()
  84. def init(cbpi):
  85. print "INITIALIZE SYSTEM MODULE"
  86. SystemView.register(cbpi.app, route_base='/api/system')