Status: ✅ Fully Implemented
Create a new deployment environment from a configuration file. This command initializes the environment with validated configuration, SSH credentials, and prepares it for provisioning.
Use when:
- Setting up a new deployment environment
- Initializing environment configuration from templates
- Preparing environments for provisioning
Command: torrust-tracker-deployer create environment
torrust-tracker-deployer create environment --env-file <FILE> [OPTIONS]Required Arguments:
--env-file <FILE>(or-f <FILE>) - Path to environment configuration file (JSON format)
Optional Flags:
--working-dir <DIR>- Working directory for environment data (default:.)--log-output <OUTPUT>- Logging output mode (default:file-only)file-only: Write logs to file only (production mode)file-and-stderr: Write logs to both file and stderr (development/testing mode)
--log-file-format <FORMAT>- Format for file logging (default:compact)pretty: Pretty-printed output for development (no ANSI codes in files)json: JSON output for production environmentscompact: Compact output for minimal verbosity
--log-stderr-format <FORMAT>- Format for stderr logging (default:pretty)pretty: Pretty-printed output with colors for developmentjson: JSON output for machine processingcompact: Compact output with colors for minimal verbosity
--log-dir <DIR>- Log directory (default:./data/logs)
{
"environment": {
"name": "my-environment"
},
"ssh_credentials": {
"private_key_path": "~/.ssh/id_rsa",
"public_key_path": "~/.ssh/id_rsa.pub",
"username": "torrust",
"port": 22
}
}environment.name (required):
- Unique environment identifier
- Must be lowercase alphanumeric with hyphens only
- Used for directory names and resource identification
- Example values:
dev-local,staging,production-01
ssh_credentials.private_key_path (required):
- Path to SSH private key file
- Supports
~for home directory expansion - File must exist and be readable
- Used for connecting to provisioned infrastructure
ssh_credentials.public_key_path (required):
- Path to SSH public key file
- Supports
~for home directory expansion - File must exist and be readable
- Used for configuring VM access
ssh_credentials.username (required):
- SSH username for VM access
- Default value:
torrust - Used for connecting to provisioned infrastructure
ssh_credentials.port (optional):
- SSH port number
- Default value:
22(standard SSH port) - Port number for SSH connections
The recommended workflow is to generate a template first:
# Step 1: Generate configuration template
torrust-tracker-deployer create template config.json
# Step 2: Edit the configuration file
nano config.json
# Replace placeholder values:
# - REPLACE_WITH_ENVIRONMENT_NAME
# - REPLACE_WITH_SSH_PRIVATE_KEY_PATH
# - REPLACE_WITH_SSH_PUBLIC_KEY_PATH
# Step 3: Create the environment
torrust-tracker-deployer create environment --env-file config.jsonResult: Environment created at ./data/my-environment/
Create an environment with a prepared configuration file:
torrust-tracker-deployer create environment --env-file my-config.jsonCreate an environment in the default location (./data/):
# Prepare configuration file
cat > dev-config.json << 'EOF'
{
"environment": {
"name": "dev-local"
},
"ssh_credentials": {
"private_key_path": "~/.ssh/id_rsa",
"public_key_path": "~/.ssh/id_rsa.pub",
"username": "torrust",
"port": 22
}
}
EOF
# Create the environment
torrust-tracker-deployer create environment --env-file dev-config.jsonResult: Environment created at ./data/dev-local/
Create an environment in a custom location:
torrust-tracker-deployer create environment \
--env-file config.json \
--working-dir /opt/deploymentsResult: Environment created at /opt/deployments/data/dev-local/
Create an environment with logging to both file and stderr for debugging:
torrust-tracker-deployer create environment \
--env-file config.json \
--log-output file-and-stderrResult: Logs written to both ./data/logs/log.txt and stderr
For development and testing, use the provided test SSH keys:
cat > test-config.json << 'EOF'
{
"environment": {
"name": "test-env"
},
"ssh_credentials": {
"private_key_path": "fixtures/testing_rsa",
"public_key_path": "fixtures/testing_rsa.pub",
"username": "developer",
"port": 22
}
}
EOF
torrust-tracker-deployer create environment --env-file test-config.jsonQuick setup for local development and testing:
# Use test SSH keys from fixtures
cat > dev-config.json << 'EOF'
{
"environment": {
"name": "dev-local"
},
"ssh_credentials": {
"private_key_path": "fixtures/testing_rsa",
"public_key_path": "fixtures/testing_rsa.pub",
"username": "developer",
"port": 22
}
}
EOF
torrust-tracker-deployer create environment --env-file dev-config.jsonSetup for CI/CD testing with unique environment names:
# Generate unique environment name for test run
TEST_ENV="test-$(date +%s)"
cat > test-config.json << EOF
{
"environment": {
"name": "${TEST_ENV}"
},
"ssh_credentials": {
"private_key_path": "${HOME}/.ssh/ci_key",
"public_key_path": "${HOME}/.ssh/ci_key.pub",
"username": "ci-runner",
"port": 22
}
}
EOF
torrust-tracker-deployer create environment --env-file test-config.jsonProduction setup with security best practices:
# Use dedicated production SSH key with passphrase
cat > prod-config.json << 'EOF'
{
"environment": {
"name": "production"
},
"ssh_credentials": {
"private_key_path": "/secure/keys/production_id_rsa",
"public_key_path": "/secure/keys/production_id_rsa.pub",
"username": "deploy-prod",
"port": 22
}
}
EOF
# Use dedicated working directory
sudo mkdir -p /opt/torrust-deployments
sudo chown $(whoami):$(whoami) /opt/torrust-deployments
torrust-tracker-deployer create environment \
--env-file prod-config.json \
--working-dir /opt/torrust-deployments \
--log-output file-onlyThe create environment command initializes:
-
Environment Directory Structure
- Creates
data/<environment-name>/directory - Stores environment configuration
- Prepares space for state files
- Creates
-
Environment State
- Initializes environment state to
Created - Records environment metadata
- Prepares for provisioning workflow
- Initializes environment state to
-
Validated Configuration
- Validates all configuration fields
- Verifies SSH key files exist and are readable
- Ensures environment name follows naming conventions
Problem: Environment name 'My_Env' is invalid
Error: Configuration validation failed
Environment name 'My_Env' is invalid: must contain only lowercase letters,
numbers, and hyphens
Solution: Use lowercase alphanumeric characters and hyphens only:
# ❌ Invalid names
"name": "My_Env" # Contains underscore and uppercase
"name": "my.env" # Contains period
"name": "MY-ENV" # Contains uppercase
# ✅ Valid names
"name": "my-env"
"name": "production-01"
"name": "dev-local"Problem: SSH private key not found at path
Error: Configuration validation failed
SSH private key not found at '~/.ssh/missing_key'
Solution: Verify SSH key paths exist:
# Check if keys exist
ls -la ~/.ssh/id_rsa*
# Generate new keys if needed
ssh-keygen -t rsa -b 4096 -f ~/.ssh/deployer_key
# Update configuration with correct paths
"private_key_path": "~/.ssh/deployer_key"
"public_key_path": "~/.ssh/deployer_key.pub"Problem: Environment 'my-env' already exists
Error: Environment creation failed
Environment 'my-env' already exists at './data/my-env'
Solution: Choose a different name or remove the existing environment:
# Option 1: Use different name
# Edit config.json and change environment.name
# Option 2: Remove existing environment first
torrust-tracker-deployer destroy my-env
# Then retry create
torrust-tracker-deployer create environment --env-file config.jsonProblem: Permission denied when creating directory
Error: Failed to create environment directory
Permission denied (os error 13): './data/my-env'
Solution: Ensure write permissions for the working directory:
# Check permissions
ls -ld ./data
# Create directory with correct permissions
mkdir -p ./data
chmod 755 ./data
# Or use a directory you own
torrust-tracker-deployer create environment \
--env-file config.json \
--working-dir ~/deploymentsProblem: Failed to parse configuration file
Error: Configuration parsing failed
expected `,` or `}` at line 5 column 3
Solution: Validate configuration file syntax:
# Validate JSON syntax
jq empty config.json
# Or regenerate from template
torrust-tracker-deployer create template config.json
# Edit with valid JSON syntax
nano config.jsonProblem: SSH key files exist but cannot be read
Error: Configuration validation failed
SSH private key exists but cannot be read: permission denied
Solution: Fix SSH key file permissions:
# SSH private keys should have restricted permissions
chmod 600 ~/.ssh/id_rsa
# SSH public keys can be more permissive
chmod 644 ~/.ssh/id_rsa.pub
# Verify permissions
ls -l ~/.ssh/id_rsa*Problem: Custom working directory doesn't exist
Error: Working directory '/opt/deployments' does not exist
Solution: Create the working directory first:
# Create directory with appropriate permissions
sudo mkdir -p /opt/deployments
sudo chown $(whoami):$(whoami) /opt/deployments
# Verify permissions
ls -ld /opt/deployments
# Then create environment
torrust-tracker-deployer create environment \
--env-file config.json \
--working-dir /opt/deploymentsIf environment creation fails, check the logs for detailed information:
# View logs with default file-only logging
cat data/logs/log.txt
# Or use file-and-stderr for real-time debugging
torrust-tracker-deployer create environment \
--env-file config.json \
--log-output file-and-stderr \
--log-stderr-format prettyThe logs will show:
- Configuration validation details
- File system operations
- Environment creation progress
- Detailed error messages with context
The create environment command is NOT idempotent:
- If an environment already exists, creation will fail
- You must destroy the existing environment before recreating
- This prevents accidental data loss
# First creation succeeds
torrust-tracker-deployer create environment --env-file config.json
# Second creation fails (environment exists)
torrust-tracker-deployer create environment --env-file config.json
# Error: Environment 'my-env' already exists
# Must destroy first to recreate
torrust-tracker-deployer destroy my-env
torrust-tracker-deployer create environment --env-file config.json0- Success (environment created successfully)1- Error (creation failed due to validation or file system errors)
After creating an environment, verify it was created successfully:
# Verify environment directory exists
ls -la ./data/my-environment/
# Check environment configuration
cat ./data/my-environment/environment.json# View creation logs
cat ./data/logs/log.txt
# Look for success message
grep "Environment created successfully" ./data/logs/log.txtcreate template- Generate configuration file templatedestroy- Remove environment and clean up resources- Command Index - Overview of all commands
After creating an environment, the typical workflow is:
-
Verify Creation: Check that environment directory and configuration exist
ls -la ./data/my-environment/ cat ./data/my-environment/environment.json
-
Provision Infrastructure: Deploy the infrastructure (future command)
# Future command - not yet implemented torrust-tracker-deployer provision my-environment -
Monitor Logs: Check logs for any issues
tail -f ./data/logs/log.txt
For convenience, use the create template command to generate a configuration template:
# Generate template with default name
torrust-tracker-deployer create template
# Generate template with custom name
torrust-tracker-deployer create template my-config.jsonThe template will contain placeholder values that you need to replace:
REPLACE_WITH_ENVIRONMENT_NAME- Choose a unique environment nameREPLACE_WITH_SSH_PRIVATE_KEY_PATH- Path to your SSH private keyREPLACE_WITH_SSH_PUBLIC_KEY_PATH- Path to your SSH public key
Edit the generated template and then use it to create your environment.
- Logging Guide - Configure logging output and formats