__init__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Docker Swarm module with compose-compatible validation."""
  2. from __future__ import annotations
  3. import logging
  4. from typing import Annotated
  5. from typer import Argument, Option
  6. from ...core.module import Module
  7. from ...core.module.base_commands import ValidationConfig, validate_templates
  8. from ...core.registry import registry
  9. from ..compose.validate import ComposeDockerValidator
  10. logger = logging.getLogger(__name__)
  11. class SwarmModule(Module):
  12. """Docker Swarm module."""
  13. name = "swarm"
  14. description = "Manage Docker Swarm stack templates"
  15. kind_validator_class = ComposeDockerValidator
  16. def validate( # noqa: PLR0913
  17. self,
  18. template_id: Annotated[
  19. str | None,
  20. Argument(help="Template ID to validate (omit to validate all templates)"),
  21. ] = None,
  22. *,
  23. path: Annotated[
  24. str | None,
  25. Option("--path", help="Path to template directory for validation"),
  26. ] = None,
  27. all_templates: Annotated[
  28. bool,
  29. Option("--all", help="Validate all Swarm templates (default when no template ID is provided)"),
  30. ] = False,
  31. verbose: Annotated[bool, Option("--verbose", "-v", help="Show detailed validation information")] = False,
  32. semantic: Annotated[
  33. bool,
  34. Option(
  35. "--semantic/--no-semantic",
  36. help="Enable semantic validation for rendered files",
  37. ),
  38. ] = True,
  39. matrix: Annotated[
  40. bool,
  41. Option(
  42. "--matrix",
  43. help="Validate all reachable dependency states for a single template",
  44. ),
  45. ] = False,
  46. kind: Annotated[
  47. bool,
  48. Option(
  49. "--kind",
  50. help="Enable dependency-matrix Docker Compose validation",
  51. ),
  52. ] = False,
  53. docker: Annotated[
  54. bool,
  55. Option(
  56. "--docker/--no-docker",
  57. help="Alias for --kind Docker Compose validation",
  58. ),
  59. ] = False,
  60. docker_test_all: Annotated[
  61. bool,
  62. Option(
  63. "--docker-test-all",
  64. help="Alias for --matrix --kind Docker Compose validation",
  65. ),
  66. ] = False,
  67. ) -> None:
  68. """Validate Swarm templates."""
  69. kind_enabled = kind or docker or docker_test_all
  70. matrix_enabled = matrix or docker_test_all
  71. kind_validator = self.kind_validator_class(verbose).validate_rendered_files if kind_enabled else None
  72. validate_templates(
  73. self,
  74. template_id,
  75. path,
  76. ValidationConfig(
  77. verbose=verbose,
  78. semantic=semantic,
  79. matrix=matrix_enabled,
  80. kind=kind_enabled,
  81. all_templates=all_templates,
  82. kind_validator=kind_validator,
  83. ),
  84. )
  85. registry.register(SwarmModule)