__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Docker Compose module with multi-schema support."""
  2. from typing import Annotated
  3. from typer import Argument, Option
  4. from ...core.module import Module
  5. from ...core.module.base_commands import validate_templates
  6. from ...core.registry import registry
  7. # Import schema specifications
  8. from .spec_v1_0 import spec as spec_1_0
  9. from .spec_v1_1 import spec as spec_1_1
  10. from .spec_v1_2 import spec as spec_1_2
  11. from .validate import run_docker_validation
  12. # Schema version mapping
  13. SCHEMAS = {
  14. "1.0": spec_1_0,
  15. "1.1": spec_1_1,
  16. "1.2": spec_1_2,
  17. }
  18. # Default spec points to latest version
  19. spec = spec_1_2
  20. class ComposeModule(Module):
  21. """Docker Compose module with extended validation."""
  22. name = "compose"
  23. description = "Manage Docker Compose configurations"
  24. schema_version = "1.2" # Current schema version supported by this module
  25. schemas = SCHEMAS # Available schema versions
  26. def validate( # noqa: PLR0913
  27. self,
  28. template_id: Annotated[
  29. str | None,
  30. Argument(help="Template ID to validate (omit to validate all templates)"),
  31. ] = None,
  32. *,
  33. path: Annotated[
  34. str | None,
  35. Option("--path", help="Path to template directory for validation"),
  36. ] = None,
  37. verbose: Annotated[bool, Option("--verbose", "-v", help="Show detailed validation information")] = False,
  38. semantic: Annotated[
  39. bool,
  40. Option(
  41. "--semantic/--no-semantic",
  42. help="Enable semantic validation (Docker Compose schema, etc.)",
  43. ),
  44. ] = True,
  45. docker: Annotated[
  46. bool,
  47. Option(
  48. "--docker/--no-docker",
  49. help="Enable Docker Compose validation using 'docker compose config'",
  50. ),
  51. ] = False,
  52. docker_test_all: Annotated[
  53. bool,
  54. Option(
  55. "--docker-test-all",
  56. help="Test all variable combinations (minimal, maximal, each toggle). Requires --docker",
  57. ),
  58. ] = False,
  59. ) -> None:
  60. """Validate templates for Jinja2 syntax, undefined variables, and semantic correctness.
  61. Extended for Docker Compose with optional docker compose config validation.
  62. Use --docker for single config test, --docker-test-all for comprehensive testing.
  63. Examples:
  64. # Validate specific template
  65. compose validate netbox
  66. # Validate all templates
  67. compose validate
  68. # Validate with Docker Compose config check
  69. compose validate netbox --docker
  70. """
  71. # Run standard validation first
  72. validate_templates(self, template_id, path, verbose, semantic)
  73. # If docker validation is enabled and we have a specific template
  74. if docker and (template_id or path):
  75. run_docker_validation(self, template_id, path, docker_test_all, verbose)
  76. registry.register(ComposeModule)