update_manager.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 using Docker."""
  47. error_log = []
  48. logger.info("Starting software update process")
  49. def run_command(command, error_message, capture_output=False):
  50. try:
  51. logger.debug(f"Running command: {' '.join(command)}")
  52. result = subprocess.run(command, check=True, capture_output=capture_output, text=True)
  53. return result.stdout if capture_output else None
  54. except subprocess.CalledProcessError as e:
  55. logger.error(f"{error_message}: {e}")
  56. error_log.append(error_message)
  57. return None
  58. # Pull new Docker images for both frontend and backend
  59. logger.info("Pulling latest Docker images...")
  60. run_command(
  61. ["docker", "pull", "ghcr.io/tuanchris/dune-weaver:main"],
  62. "Failed to pull backend Docker image"
  63. )
  64. run_command(
  65. ["docker", "pull", "ghcr.io/tuanchris/dune-weaver-frontend:main"],
  66. "Failed to pull frontend Docker image"
  67. )
  68. # Recreate containers with new images using docker-compose
  69. # Try docker-compose first, then docker compose (v2)
  70. logger.info("Recreating containers with new images...")
  71. compose_success = False
  72. # Try docker-compose (v1)
  73. try:
  74. subprocess.run(
  75. ["docker-compose", "up", "-d", "--force-recreate"],
  76. check=True,
  77. cwd="/app"
  78. )
  79. compose_success = True
  80. logger.info("Containers recreated successfully with docker-compose")
  81. except (subprocess.CalledProcessError, FileNotFoundError):
  82. logger.debug("docker-compose not available, trying docker compose")
  83. # Try docker compose (v2) if v1 failed
  84. if not compose_success:
  85. try:
  86. subprocess.run(
  87. ["docker", "compose", "up", "-d", "--force-recreate"],
  88. check=True,
  89. cwd="/app"
  90. )
  91. compose_success = True
  92. logger.info("Containers recreated successfully with docker compose")
  93. except (subprocess.CalledProcessError, FileNotFoundError):
  94. logger.debug("docker compose not available, falling back to individual restarts")
  95. # Fallback: restart individual containers
  96. if not compose_success:
  97. logger.info("Falling back to individual container restarts...")
  98. run_command(
  99. ["docker", "restart", "dune-weaver-frontend"],
  100. "Failed to restart frontend container"
  101. )
  102. run_command(
  103. ["docker", "restart", "dune-weaver-backend"],
  104. "Failed to restart backend container"
  105. )
  106. if error_log:
  107. logger.error(f"Software update completed with errors: {error_log}")
  108. return False, "Update completed with errors", error_log
  109. logger.info("Software update completed successfully")
  110. return True, None, None