Some checks failed
Architecture Lint / Linter Tests (pull_request) Successful in 8s
PR Checklist / pr-checklist (pull_request) Failing after 1m14s
Smoke Test / smoke (pull_request) Failing after 5s
Validate Config / YAML Lint (pull_request) Failing after 4s
Validate Config / Shell Script Lint (pull_request) Failing after 11s
Validate Config / Cron Syntax Check (pull_request) Successful in 3s
Validate Config / Deploy Script Dry Run (pull_request) Successful in 3s
Validate Config / JSON Validate (pull_request) Successful in 4s
Validate Config / Python Syntax & Import Check (pull_request) Failing after 21s
Validate Config / Playbook Schema Validation (pull_request) Successful in 6s
Architecture Lint / Lint Repository (pull_request) Has been cancelled
Validate Config / Python Test Suite (pull_request) Has been cancelled
208 lines
6.3 KiB
Python
208 lines
6.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
validate-sidecar-config.py — Pre-deploy validation for timmy-config sidecar.
|
|
|
|
Validates YAML syntax, required keys, value types before deploy.
|
|
Rejects bad config with clear errors.
|
|
|
|
Usage:
|
|
python3 scripts/validate-sidecar-config.py ~/.timmy/config.yaml
|
|
python3 scripts/validate-sidecar-config.py --all # Validate all config files
|
|
python3 scripts/validate-sidecar-config.py --schema # Print expected schema
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
try:
|
|
import yaml
|
|
HAS_YAML = True
|
|
except ImportError:
|
|
HAS_YAML = False
|
|
|
|
# Expected schema: field -> type
|
|
REQUIRED_SCHEMA = {
|
|
"model": str,
|
|
"provider": str,
|
|
}
|
|
|
|
OPTIONAL_SCHEMA = {
|
|
"api_base": str,
|
|
"max_tokens": int,
|
|
"temperature": (int, float),
|
|
"system_prompt": str,
|
|
"tools": list,
|
|
"memory_enabled": bool,
|
|
"session_timeout": int,
|
|
"log_level": str,
|
|
}
|
|
|
|
CONFIG_DIRS = [
|
|
Path.home() / ".timmy",
|
|
Path.home() / ".hermes",
|
|
]
|
|
|
|
|
|
def validate_yaml_syntax(filepath: Path) -> list[str]:
|
|
"""Validate YAML can be parsed."""
|
|
errors = []
|
|
try:
|
|
content = filepath.read_text(errors="ignore")
|
|
if HAS_YAML:
|
|
yaml.safe_load(content)
|
|
else:
|
|
# Fallback: check for basic JSON-in-YAML
|
|
json.loads(content)
|
|
except (yaml.YAMLError if HAS_YAML else Exception, json.JSONDecodeError) as e:
|
|
errors.append(f"YAML syntax error: {e}")
|
|
except OSError as e:
|
|
errors.append(f"Cannot read file: {e}")
|
|
return errors
|
|
|
|
|
|
def validate_schema(filepath: Path) -> list[str]:
|
|
"""Validate config against expected schema."""
|
|
errors = []
|
|
try:
|
|
content = filepath.read_text(errors="ignore")
|
|
if HAS_YAML:
|
|
config = yaml.safe_load(content) or {}
|
|
else:
|
|
config = json.loads(content)
|
|
except Exception as e:
|
|
return [f"Cannot parse: {e}"]
|
|
|
|
if not isinstance(config, dict):
|
|
return ["Config must be a YAML/JSON object (dict)"]
|
|
|
|
# Check required keys
|
|
for key, expected_type in REQUIRED_SCHEMA.items():
|
|
if key not in config:
|
|
errors.append(f"Missing required key: '{key}' (expected {expected_type.__name__})")
|
|
elif not isinstance(config[key], expected_type):
|
|
errors.append(f"Wrong type for '{key}': expected {expected_type.__name__}, got {type(config[key]).__name__}")
|
|
|
|
# Check optional keys types
|
|
for key, expected_type in OPTIONAL_SCHEMA.items():
|
|
if key in config:
|
|
if isinstance(expected_type, tuple):
|
|
if not isinstance(config[key], expected_type):
|
|
type_names = " or ".join(t.__name__ for t in expected_type)
|
|
errors.append(f"Wrong type for '{key}': expected {type_names}, got {type(config[key]).__name__}")
|
|
else:
|
|
if not isinstance(config[key], expected_type):
|
|
errors.append(f"Wrong type for '{key}': expected {expected_type.__name__}, got {type(config[key]).__name__}")
|
|
|
|
# Check for common mistakes
|
|
if "model" in config and isinstance(config["model"], str):
|
|
if config["model"].startswith("http"):
|
|
errors.append("'model' looks like a URL — did you mean 'api_base'?")
|
|
|
|
if "api_base" in config and isinstance(config["api_base"], str):
|
|
if not config["api_base"].startswith("http"):
|
|
errors.append("'api_base' should start with http:// or https://")
|
|
|
|
return errors
|
|
|
|
|
|
def validate_file(filepath: Path) -> tuple[bool, list[str]]:
|
|
"""Full validation of a config file. Returns (valid, errors)."""
|
|
errors = []
|
|
errors.extend(validate_yaml_syntax(filepath))
|
|
if not errors:
|
|
errors.extend(validate_schema(filepath))
|
|
return len(errors) == 0, errors
|
|
|
|
|
|
def find_config_files() -> list[Path]:
|
|
"""Find config files in standard locations."""
|
|
configs = []
|
|
for d in CONFIG_DIRS:
|
|
if not d.exists():
|
|
continue
|
|
for f in d.rglob("*.yaml"):
|
|
if f.name in ("config.yaml", "settings.yaml", "env.yaml", "config.yml"):
|
|
configs.append(f)
|
|
for f in d.rglob("*.yml"):
|
|
if "config" in f.name.lower():
|
|
configs.append(f)
|
|
for f in d.rglob("*.json"):
|
|
if f.name in ("config.json", "settings.json"):
|
|
configs.append(f)
|
|
return sorted(set(configs))
|
|
|
|
|
|
def cmd_validate(filepath: str) -> bool:
|
|
path = Path(filepath)
|
|
if not path.exists():
|
|
print(f"ERROR: {path} not found")
|
|
return False
|
|
|
|
valid, errors = validate_file(path)
|
|
if valid:
|
|
print(f"OK: {path}")
|
|
else:
|
|
print(f"FAIL: {path}")
|
|
for e in errors:
|
|
print(f" - {e}")
|
|
return valid
|
|
|
|
|
|
def cmd_validate_all() -> bool:
|
|
configs = find_config_files()
|
|
if not configs:
|
|
print("No config files found in standard locations.")
|
|
return True
|
|
|
|
all_valid = True
|
|
for config in configs:
|
|
valid, errors = validate_file(config)
|
|
if valid:
|
|
print(f"OK: {config}")
|
|
else:
|
|
all_valid = False
|
|
print(f"FAIL: {config}")
|
|
for e in errors:
|
|
print(f" - {e}")
|
|
|
|
print(f"\n{'All configs valid.' if all_valid else 'Validation failures found.'}")
|
|
return all_valid
|
|
|
|
|
|
def cmd_schema():
|
|
print("Required keys:")
|
|
for key, typ in REQUIRED_SCHEMA.items():
|
|
print(f" {key}: {typ.__name__}")
|
|
print("\nOptional keys:")
|
|
for key, typ in OPTIONAL_SCHEMA.items():
|
|
if isinstance(typ, tuple):
|
|
print(f" {key}: {'|'.join(t.__name__ for t in typ)}")
|
|
else:
|
|
print(f" {key}: {typ.__name__}")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Validate sidecar config files")
|
|
parser.add_argument("file", nargs="?", help="Config file to validate")
|
|
parser.add_argument("--all", action="store_true", help="Validate all config files")
|
|
parser.add_argument("--schema", action="store_true", help="Print expected schema")
|
|
args = parser.parse_args()
|
|
|
|
if args.schema:
|
|
cmd_schema()
|
|
elif args.all:
|
|
ok = cmd_validate_all()
|
|
sys.exit(0 if ok else 1)
|
|
elif args.file:
|
|
ok = cmd_validate(args.file)
|
|
sys.exit(0 if ok else 1)
|
|
else:
|
|
parser.print_help()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|