updateSquidStats.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import shutil
  3. import subprocess
  4. import tempfile
  5. import requests
  6. from dotenv import load_dotenv
  7. from loguru import logger
  8. load_dotenv()
  9. def updateSquidStats():
  10. logger.info("Starting SquidStats web update process")
  11. try:
  12. proxy_url = os.getenv("HTTP_PROXY", "")
  13. https_proxy_url = os.getenv("HTTPS_PROXY", proxy_url)
  14. env = os.environ.copy()
  15. if proxy_url:
  16. logger.debug("HTTP proxy configured")
  17. env["http_proxy"] = proxy_url
  18. if https_proxy_url:
  19. logger.debug("HTTPS proxy configured")
  20. env["https_proxy"] = https_proxy_url
  21. proxies = None
  22. if proxy_url or https_proxy_url:
  23. proxies = {
  24. "http": proxy_url or None,
  25. "https": https_proxy_url or None,
  26. }
  27. try:
  28. # Fetch the latest release tag from GitHub API to always use the current version
  29. api_response = requests.get(
  30. "https://api.github.com/repos/kaelthasmanu/SquidStats/releases/latest",
  31. proxies=proxies,
  32. timeout=30,
  33. )
  34. api_response.raise_for_status()
  35. latest_tag = api_response.json().get("tag_name", "")
  36. if not latest_tag:
  37. logger.error("No se pudo obtener la última versión desde GitHub API")
  38. return False
  39. logger.info(f"Latest release tag resolved: {latest_tag}")
  40. script_url = f"https://github.com/kaelthasmanu/SquidStats/releases/download/{latest_tag}/install.sh"
  41. logger.info(f"Downloading update script from {script_url}")
  42. response = requests.get(
  43. script_url,
  44. proxies=proxies,
  45. timeout=30,
  46. )
  47. response.raise_for_status()
  48. sh_bin = "/bin/sh" if os.path.exists("/bin/sh") else shutil.which("sh")
  49. if not sh_bin:
  50. logger.error("sh no encontrado en el sistema")
  51. return False
  52. with tempfile.NamedTemporaryFile(delete=False, suffix=".sh") as tmp_script:
  53. tmp_script.write(response.content)
  54. tmp_script_path = tmp_script.name
  55. logger.debug(f"Written update script to temporary file: {tmp_script_path}")
  56. os.chmod(tmp_script_path, 0o700)
  57. args = [sh_bin, tmp_script_path, "--update"]
  58. logger.info(f"Executing update script with command: {args}")
  59. try:
  60. # The external update script is downloaded from the official GitHub
  61. # release feed, then written to a temporary file and executed.
  62. # This is intentionally executing remote installer content.
  63. subprocess.run(args, env=env, check=True, timeout=600) # noqa: S603
  64. logger.info("Update script executed successfully")
  65. finally:
  66. try:
  67. os.remove(tmp_script_path)
  68. except OSError:
  69. logger.warning(
  70. f"Unable to remove temporary update script: {tmp_script_path}"
  71. )
  72. return True
  73. except Exception:
  74. logger.exception("Error descargando el script de actualización")
  75. return False
  76. except Exception:
  77. logger.exception("Error crítico en updateSquidStats")
  78. return False