1
0

update_manager.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import subprocess
  2. import logging
  3. # Configure logging
  4. logger = logging.getLogger(__name__)
  5. def check_git_updates():
  6. """Check for available Git updates."""
  7. try:
  8. logger.debug("Checking for Git updates")
  9. subprocess.run(["git", "fetch", "--tags", "--force"], check=True)
  10. latest_remote_tag = subprocess.check_output(
  11. ["git", "describe", "--tags", "--abbrev=0", "origin/main"]
  12. ).strip().decode()
  13. latest_local_tag = subprocess.check_output(
  14. ["git", "describe", "--tags", "--abbrev=0"]
  15. ).strip().decode()
  16. tag_behind_count = 0
  17. if latest_local_tag != latest_remote_tag:
  18. tags = subprocess.check_output(
  19. ["git", "tag", "--merged", "origin/main"], text=True
  20. ).splitlines()
  21. found_local = False
  22. for tag in tags:
  23. if tag == latest_local_tag:
  24. found_local = True
  25. elif found_local:
  26. tag_behind_count += 1
  27. if tag == latest_remote_tag:
  28. break
  29. updates_available = latest_remote_tag != latest_local_tag
  30. logger.info(f"Updates available: {updates_available}, {tag_behind_count} versions behind")
  31. return {
  32. "updates_available": updates_available,
  33. "tag_behind_count": tag_behind_count,
  34. "latest_remote_tag": latest_remote_tag,
  35. "latest_local_tag": latest_local_tag,
  36. }
  37. except subprocess.CalledProcessError as e:
  38. logger.error(f"Error checking Git updates: {e}")
  39. return {
  40. "updates_available": False,
  41. "tag_behind_count": 0,
  42. "latest_remote_tag": None,
  43. "latest_local_tag": None,
  44. }
  45. def update_software():
  46. """Update the software to the latest version.
  47. Pulls latest code, installs updated Python dependencies,
  48. and restarts the systemd service.
  49. For a full update (including frontend rebuild), run 'dw update' instead.
  50. """
  51. error_log = []
  52. logger.info("Starting software update process")
  53. def run_command(command, error_message, capture_output=False, cwd=None):
  54. try:
  55. logger.debug(f"Running command: {' '.join(command)}")
  56. result = subprocess.run(command, check=True, capture_output=capture_output, text=True, cwd=cwd)
  57. return result.stdout if capture_output else True
  58. except subprocess.CalledProcessError as e:
  59. logger.error(f"{error_message}: {e}")
  60. error_log.append(error_message)
  61. return None
  62. # Step 1: Pull latest code via git
  63. logger.info("Pulling latest code from git...")
  64. git_result = run_command(
  65. ["git", "pull", "--ff-only"],
  66. "Failed to pull latest code from git"
  67. )
  68. if git_result:
  69. logger.info("Git pull completed successfully")
  70. # Step 2: Install updated Python dependencies
  71. logger.info("Installing updated dependencies...")
  72. run_command(
  73. [".venv/bin/pip", "install", "-r", "requirements.txt"],
  74. "Failed to install updated dependencies"
  75. )
  76. # Step 3: Restart the service
  77. logger.info("Restarting dune-weaver service...")
  78. restart_result = run_command(
  79. ["sudo", "systemctl", "restart", "dune-weaver"],
  80. "Failed to restart dune-weaver service"
  81. )
  82. if not restart_result:
  83. error_log.append("Service restart failed - please run 'dw restart' manually")
  84. if error_log:
  85. logger.error(f"Software update completed with errors: {error_log}")
  86. return False, "Update completed with errors. Run 'dw update' for a full update.", error_log
  87. logger.info("Software update completed successfully")
  88. return True, None, None