|
| 1 | +# Validate Command |
| 2 | + |
| 3 | +Validates environment configuration files without creating deployments. Use this command to check configuration correctness before running the actual deployment workflow. |
| 4 | + |
| 5 | +## Command Syntax |
| 6 | + |
| 7 | +```bash |
| 8 | +torrust-tracker-deployer validate --env-file <CONFIG_FILE> |
| 9 | +``` |
| 10 | + |
| 11 | +### Options |
| 12 | + |
| 13 | +- `--env-file, -f <FILE>` - Path to the environment configuration file to validate (required) |
| 14 | + |
| 15 | +## Usage Examples |
| 16 | + |
| 17 | +### Basic Usage |
| 18 | + |
| 19 | +```bash |
| 20 | +torrust-tracker-deployer validate --env-file envs/my-environment.json |
| 21 | +``` |
| 22 | + |
| 23 | +### Validate with Short Flag |
| 24 | + |
| 25 | +```bash |
| 26 | +torrust-tracker-deployer validate -f config/production.json |
| 27 | +``` |
| 28 | + |
| 29 | +## What This Command Does |
| 30 | + |
| 31 | +The validate command performs comprehensive validation of environment configuration files: |
| 32 | + |
| 33 | +1. **File Validation** - Verifies the configuration file exists and is readable |
| 34 | +2. **JSON Schema Validation** - Checks JSON syntax and structure |
| 35 | +3. **Domain Validation** - Validates field constraints and business rules: |
| 36 | + - SSH key files must exist at specified paths |
| 37 | + - Environment names must follow naming rules (lowercase with dashes) |
| 38 | + - Port numbers must be valid |
| 39 | + - IP addresses must be well-formed |
| 40 | + - Domain names must follow DNS conventions |
| 41 | + - All required fields must be present |
| 42 | + |
| 43 | +## When to Use |
| 44 | + |
| 45 | +- **Before Creating Environments** - Catch configuration errors early before starting infrastructure provisioning |
| 46 | +- **CI/CD Pipelines** - Validate configurations as part of automated testing |
| 47 | +- **Configuration Review** - Verify configuration correctness during code review |
| 48 | +- **Troubleshooting** - Diagnose why environment creation failed |
| 49 | +- **Learning** - Understand configuration structure and requirements |
| 50 | + |
| 51 | +## When NOT to Use |
| 52 | + |
| 53 | +- **After Environment Creation** - Use `show` command to view existing environment details instead |
| 54 | +- **Creating Templates** - Use `create template` to generate valid configuration files |
| 55 | +- **Deployment** - Validate does not deploy - use full workflow commands for actual deployment |
| 56 | + |
| 57 | +## Output Details |
| 58 | + |
| 59 | +### Success Output |
| 60 | + |
| 61 | +```text |
| 62 | +⏳ [1/3] Loading configuration file... |
| 63 | +⏳ ✓ Configuration file loaded (took 0ms) |
| 64 | +⏳ [2/3] Validating JSON schema... |
| 65 | +⏳ ✓ Schema validation passed (took 0ms) |
| 66 | +⏳ [3/3] Validating configuration fields... |
| 67 | +⏳ ✓ Field validation passed (took 0ms) |
| 68 | +
|
| 69 | +✅ Configuration file 'envs/my-environment.json' is valid |
| 70 | +
|
| 71 | +Environment Details: |
| 72 | + • Name: my-environment |
| 73 | + • Provider: lxd |
| 74 | + • Prometheus: Enabled |
| 75 | + • Grafana: Enabled |
| 76 | + • HTTPS: Disabled |
| 77 | + • Backups: Enabled |
| 78 | +``` |
| 79 | + |
| 80 | +### Error Output Examples |
| 81 | + |
| 82 | +**File Not Found**: |
| 83 | + |
| 84 | +```text |
| 85 | +❌ Validate command failed: Configuration file not found: /tmp/nonexistent.json |
| 86 | +
|
| 87 | +For detailed troubleshooting: |
| 88 | +Verify the file path is correct: /tmp/nonexistent.json |
| 89 | +Use 'create template' to generate a valid configuration file. |
| 90 | +``` |
| 91 | + |
| 92 | +**Invalid JSON**: |
| 93 | + |
| 94 | +```text |
| 95 | +❌ Validate command failed: Validation failed for configuration file: config.json |
| 96 | +
|
| 97 | +For detailed troubleshooting: |
| 98 | +JSON parsing failed for file 'config.json'. |
| 99 | +
|
| 100 | +Error details: |
| 101 | +key must be a string at line 1 column 3 |
| 102 | +
|
| 103 | +Common issues: |
| 104 | +- Missing or extra commas |
| 105 | +- Unmatched braces or brackets |
| 106 | +- Invalid escape sequences |
| 107 | +- Comments (not allowed in JSON) |
| 108 | +
|
| 109 | +Tips: |
| 110 | +- Use a JSON validator or editor with syntax highlighting |
| 111 | +- Compare with a template: 'create template --provider lxd' |
| 112 | +- Check the JSON schema in schemas/environment-config.json |
| 113 | +``` |
| 114 | + |
| 115 | +**Missing SSH Keys**: |
| 116 | + |
| 117 | +```text |
| 118 | +❌ Validate command failed: Validation failed for configuration file: config.json |
| 119 | +
|
| 120 | +For detailed troubleshooting: |
| 121 | +Configuration validation failed. |
| 122 | +
|
| 123 | +Error: SSH private key file not found: /tmp/nonexistent-key |
| 124 | +
|
| 125 | +This means the configuration file has valid JSON syntax but violates |
| 126 | +domain constraints or business rules. |
| 127 | +
|
| 128 | +Common issues: |
| 129 | +- SSH key files don't exist at specified paths |
| 130 | +- Invalid environment name (must be lowercase with dashes) |
| 131 | +- Invalid port numbers or IP addresses |
| 132 | +- Missing required fields |
| 133 | +- HTTPS configured but no services have TLS enabled |
| 134 | +``` |
| 135 | + |
| 136 | +## Common Scenarios |
| 137 | + |
| 138 | +### Scenario 1: Pre-Deployment Validation |
| 139 | + |
| 140 | +Before starting a deployment, validate your configuration: |
| 141 | + |
| 142 | +```bash |
| 143 | +# Create configuration from template |
| 144 | +torrust-tracker-deployer create template -p lxd > envs/my-env.json |
| 145 | + |
| 146 | +# Edit configuration... |
| 147 | +# vim envs/my-env.json |
| 148 | + |
| 149 | +# Validate before deploying |
| 150 | +torrust-tracker-deployer validate -f envs/my-env.json |
| 151 | + |
| 152 | +# If valid, proceed with deployment |
| 153 | +torrust-tracker-deployer create environment -f envs/my-env.json |
| 154 | +``` |
| 155 | + |
| 156 | +### Scenario 2: CI/CD Pipeline Integration |
| 157 | + |
| 158 | +Add validation as a CI test: |
| 159 | + |
| 160 | +```bash |
| 161 | +#!/bin/bash |
| 162 | +# .github/workflows/validate-configs.sh |
| 163 | + |
| 164 | +for config in envs/*.json; do |
| 165 | + echo "Validating $config..." |
| 166 | + torrust-tracker-deployer validate -f "$config" |
| 167 | + |
| 168 | + if [ $? -ne 0 ]; then |
| 169 | + echo "❌ Validation failed for $config" |
| 170 | + exit 1 |
| 171 | + fi |
| 172 | +done |
| 173 | + |
| 174 | +echo "✅ All configurations valid" |
| 175 | +``` |
| 176 | + |
| 177 | +### Scenario 3: Troubleshooting Configuration Issues |
| 178 | + |
| 179 | +When environment creation fails, use validate to diagnose: |
| 180 | + |
| 181 | +```bash |
| 182 | +# Creation fails |
| 183 | +torrust-tracker-deployer create environment -f envs/broken.json |
| 184 | +# Error: SSH key not found... |
| 185 | + |
| 186 | +# Use validate for detailed error messages |
| 187 | +torrust-tracker-deployer validate -f envs/broken.json |
| 188 | +# Shows: SSH private key file not found: /path/to/key |
| 189 | +# With troubleshooting tips... |
| 190 | + |
| 191 | +# Fix issue and re-validate |
| 192 | +vim envs/broken.json |
| 193 | +torrust-tracker-deployer validate -f envs/broken.json |
| 194 | +# ✅ Configuration valid |
| 195 | + |
| 196 | +# Retry creation |
| 197 | +torrust-tracker-deployer create environment -f envs/broken.json |
| 198 | +``` |
| 199 | + |
| 200 | +## Troubleshooting |
| 201 | + |
| 202 | +### Error: Configuration file not found |
| 203 | + |
| 204 | +**Cause**: The file path is incorrect or the file doesn't exist. |
| 205 | + |
| 206 | +**Solution**: Verify the file path and ensure the file exists: |
| 207 | + |
| 208 | +```bash |
| 209 | +# Check if file exists |
| 210 | +ls -l envs/my-config.json |
| 211 | + |
| 212 | +# Use absolute path if relative path fails |
| 213 | +torrust-tracker-deployer validate -f /full/path/to/envs/my-config.json |
| 214 | + |
| 215 | +# Generate a template if starting fresh |
| 216 | +torrust-tracker-deployer create template -p lxd > envs/new-config.json |
| 217 | +``` |
| 218 | + |
| 219 | +### Error: JSON parsing failed |
| 220 | + |
| 221 | +**Cause**: The JSON syntax is invalid (missing commas, unmatched braces, etc.). |
| 222 | + |
| 223 | +**Solution**: Use a JSON validator or IDE with JSON support: |
| 224 | + |
| 225 | +```bash |
| 226 | +# Use jq to validate and format |
| 227 | +jq . envs/my-config.json |
| 228 | + |
| 229 | +# Use Python's JSON tool |
| 230 | +python -m json.tool < envs/my-config.json |
| 231 | + |
| 232 | +# Compare with template |
| 233 | +torrust-tracker-deployer create template -p lxd | jq . > valid-template.json |
| 234 | +diff envs/my-config.json valid-template.json |
| 235 | +``` |
| 236 | + |
| 237 | +### Error: SSH key file not found |
| 238 | + |
| 239 | +**Cause**: The SSH key paths in the configuration don't exist. |
| 240 | + |
| 241 | +**Solution**: Generate SSH keys or update paths to existing keys: |
| 242 | + |
| 243 | +```bash |
| 244 | +# Option 1: Generate new SSH keys |
| 245 | +ssh-keygen -t rsa -b 4096 -f ~/.ssh/torrust_deployer -N "" |
| 246 | + |
| 247 | +# Option 2: Update configuration with correct paths |
| 248 | +vim envs/my-config.json |
| 249 | +# Update "private_key_path" and "public_key_path" |
| 250 | + |
| 251 | +# Validate |
| 252 | +torrust-tracker-deployer validate -f envs/my-config.json |
| 253 | +``` |
| 254 | + |
| 255 | +### Error: Invalid environment name |
| 256 | + |
| 257 | +**Cause**: Environment name doesn't follow naming rules. |
| 258 | + |
| 259 | +**Solution**: Use valid naming format (lowercase letters, numbers, dashes): |
| 260 | + |
| 261 | +```bash |
| 262 | +# Invalid names: |
| 263 | +# - "MyEnv" (uppercase) |
| 264 | +# - "my_env" (underscores) |
| 265 | +# - "123-env" (starts with number) |
| 266 | +# - "my-env-" (ends with dash) |
| 267 | + |
| 268 | +# Valid names: |
| 269 | +# - "my-env" |
| 270 | +# - "production" |
| 271 | +# - "dev-01" |
| 272 | +# - "staging-2025" |
| 273 | + |
| 274 | +# Fix in configuration |
| 275 | +vim envs/my-config.json |
| 276 | +# Change "name": "MyEnv" to "name": "my-env" |
| 277 | + |
| 278 | +torrust-tracker-deployer validate -f envs/my-config.json |
| 279 | +``` |
| 280 | + |
| 281 | +## Related Commands |
| 282 | + |
| 283 | +- [`create template`](./create-template.md) - Generate valid configuration templates |
| 284 | +- [`create environment`](./create-environment.md) - Create deployment environments (runs validation internally) |
| 285 | +- [`show`](./show.md) - Display existing environment details |
| 286 | + |
| 287 | +## See Also |
| 288 | + |
| 289 | +- [Environment Creation Configuration](../../user-guide/environment-creation-config.md) |
| 290 | +- [JSON Schema](../../../schemas/environment-config.json) |
| 291 | +- [SSH Key Setup](../../user-guide/ssh-key-setup.md) |
| 292 | +- [Troubleshooting Guide](../../user-guide/troubleshooting.md) |
0 commit comments