config.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """
  2. Application configuration and settings.
  3. """
  4. import os
  5. import logging
  6. from pathlib import Path
  7. from functools import lru_cache
  8. from typing import Optional
  9. logger = logging.getLogger(__name__)
  10. class Settings:
  11. """Application settings."""
  12. def __init__(self):
  13. # Application
  14. self.app_name: str = "CustomBuild API"
  15. self.app_version: str = "1.0.0"
  16. self.debug: bool = False
  17. # Paths
  18. self.base_dir: str = os.getenv(
  19. "CBS_BASEDIR",
  20. default=str(Path(__file__).parent.parent.parent.parent / "base")
  21. )
  22. # Redis
  23. self.redis_host: str = os.getenv(
  24. 'CBS_REDIS_HOST',
  25. default='localhost'
  26. )
  27. self.redis_port: str = os.getenv(
  28. 'CBS_REDIS_PORT',
  29. default='6379'
  30. )
  31. # Logging
  32. self.log_level: str = os.getenv('CBS_LOG_LEVEL', default='INFO')
  33. # ArduPilot Git Repository
  34. self.ap_git_url: str = "https://github.com/ardupilot/ardupilot.git"
  35. @property
  36. def source_dir(self) -> str:
  37. """ArduPilot source directory."""
  38. return os.path.join(self.base_dir, 'ardupilot')
  39. @property
  40. def artifacts_dir(self) -> str:
  41. """Build artifacts directory."""
  42. return os.path.join(self.base_dir, 'artifacts')
  43. @property
  44. def outdir_parent(self) -> str:
  45. """Build output directory (same as artifacts_dir)."""
  46. return self.artifacts_dir
  47. @property
  48. def workdir_parent(self) -> str:
  49. """Work directory parent."""
  50. return os.path.join(self.base_dir, 'workdir')
  51. @property
  52. def remotes_json_path(self) -> str:
  53. """Path to remotes.json configuration."""
  54. return os.path.join(self.base_dir, 'configs', 'remotes.json')
  55. @property
  56. def enable_inbuilt_builder(self) -> bool:
  57. """Whether to enable the inbuilt builder."""
  58. return os.getenv('CBS_ENABLE_INBUILT_BUILDER', '1') == '1'
  59. @property
  60. def remote_reload_token(self) -> Optional[str]:
  61. """
  62. Get remote reload token from file or environment variable.
  63. Tries to read token from file first, falls back to environment variable.
  64. Returns:
  65. The authorization token if found, None otherwise
  66. """
  67. token_file_path = os.path.join(self.base_dir, 'secrets', 'reload_token')
  68. try:
  69. # Try to read the secret token from the file
  70. with open(token_file_path, 'r') as file:
  71. token = file.read().strip()
  72. return token
  73. except (FileNotFoundError, PermissionError):
  74. # If the file does not exist or no permission, check environment
  75. env_token = os.getenv('CBS_REMOTES_RELOAD_TOKEN', '')
  76. return env_token if env_token != '' else None
  77. except Exception as e:
  78. logger.error(
  79. f"Unexpected error reading token file at {token_file_path}: {e}. "
  80. "Checking environment for token."
  81. )
  82. # For any other error, fall back to environment variable
  83. env_token = os.getenv('CBS_REMOTES_RELOAD_TOKEN', None)
  84. return env_token if env_token != '' else None
  85. @lru_cache()
  86. def get_settings() -> Settings:
  87. """Get cached settings instance."""
  88. return Settings()