Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit d3e9ed5

Browse files
committed
refactor: simplify deployment script configuration file handling
Streamlines the deployment process by directly copying configuration files from local machine to persistent volume, eliminating intermediate storage steps: - Remove complex storage backup/restore logic during repository updates - Eliminate intermediate configuration file copying through VM storage folder - Copy generated config files directly from local to /var/lib/torrust/ via SCP - Simplify repository replacement logic (no storage preservation needed) - Reduce deployment complexity and potential error points Benefits: - 63 lines of code removed (103 removed, 40 added) - Cleaner deployment flow: Local generation → Direct transfer → Service start - Better separation of concerns between repository and persistent data - Fewer file operations and reduced chance of deployment failures - Easier to debug and maintain All E2E tests pass confirming the deployment works correctly with the simplified approach.
1 parent 9877fa6 commit d3e9ed5

File tree

1 file changed

+40
-103
lines changed

1 file changed

+40
-103
lines changed

infrastructure/scripts/deploy-app.sh

Lines changed: 40 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -287,31 +287,13 @@ release_stage() {
287287
# Create target directory structure
288288
vm_exec "${vm_ip}" "mkdir -p /home/torrust/github/torrust" "Creating directory structure"
289289

290-
# Check if we need to preserve storage before removing repository
291-
storage_exists=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "torrust@${vm_ip}" "
292-
if [ -d /home/torrust/github/torrust/torrust-tracker-demo/application/storage ]; then
293-
echo 'true'
294-
else
295-
echo 'false'
296-
fi
297-
" 2>/dev/null || echo "false")
298-
299-
if [[ "${storage_exists}" == "true" ]]; then
300-
log_warning "Preserving existing storage folder with persistent data"
301-
fi
302-
303-
# Handle existing repository - preserve storage folder if it exists
290+
# Handle existing repository
304291
vm_exec "${vm_ip}" "
305292
if [ -d /home/torrust/github/torrust/torrust-tracker-demo ]; then
306-
if [ -d /home/torrust/github/torrust/torrust-tracker-demo/application/storage ]; then
307-
# Move storage folder to temporary location
308-
mv /home/torrust/github/torrust/torrust-tracker-demo/application/storage /tmp/torrust-storage-backup-\$(date +%s) || true
309-
fi
310-
311-
# Remove the repository directory (excluding storage)
293+
# Remove the repository directory
312294
rm -rf /home/torrust/github/torrust/torrust-tracker-demo
313295
fi
314-
" "Removing existing repository (preserving storage)"
296+
" "Removing existing repository"
315297

316298
# Copy archive to VM
317299
if ! scp -o StrictHostKeyChecking=no "${temp_archive}" "torrust@${vm_ip}:/tmp/"; then
@@ -320,70 +302,12 @@ release_stage() {
320302
exit 1
321303
fi
322304

323-
# Also copy generated configuration files (not in git archive)
324-
log_info "Copying generated configuration files to VM..."
325-
326-
# Create a temporary archive of just the generated files
327-
local config_archive
328-
config_archive="/tmp/torrust-config-$(date +%s).tar.gz"
329-
330-
if [[ -d "application/storage" ]]; then
331-
tar -czf "${config_archive}" -C "${PROJECT_ROOT}" application/storage/ 2>/dev/null || true
332-
333-
# Copy configuration archive to VM
334-
if scp -o StrictHostKeyChecking=no "${config_archive}" "torrust@${vm_ip}:/tmp/" 2>/dev/null; then
335-
log_info "Configuration files copied successfully"
336-
else
337-
log_warning "No configuration files to copy (this is normal for first deployment)"
338-
fi
339-
340-
# Clean up local config archive
341-
rm -f "${config_archive}"
342-
else
343-
log_warning "No application/storage directory found - configuration will be generated on VM"
344-
fi
345-
346305
# Extract archive on VM
347306
vm_exec "${vm_ip}" "cd /home/torrust/github/torrust && mkdir -p torrust-tracker-demo" "Creating repository directory"
348307
vm_exec "${vm_ip}" "cd /home/torrust/github/torrust/torrust-tracker-demo && tar -xzf /tmp/$(basename "${temp_archive}")" "Extracting repository"
349308

350-
# Extract configuration files if they were copied
351-
vm_exec "${vm_ip}" "
352-
config_archive=\$(ls /tmp/torrust-config-*.tar.gz 2>/dev/null | head -1 || echo '')
353-
if [ -n \"\$config_archive\" ] && [ -f \"\$config_archive\" ]; then
354-
cd /home/torrust/github/torrust/torrust-tracker-demo
355-
tar -xzf \"\$config_archive\"
356-
rm -f \"\$config_archive\"
357-
echo 'Configuration files extracted successfully'
358-
else
359-
echo 'No configuration archive found - will generate on VM'
360-
fi
361-
" "Extracting configuration files"
362-
363309
vm_exec "${vm_ip}" "rm -f /tmp/$(basename "${temp_archive}")" "Cleaning up temp files"
364310

365-
# Restore storage folder if it was backed up
366-
vm_exec "${vm_ip}" "
367-
storage_backup=\$(ls /tmp/torrust-storage-backup-* 2>/dev/null | head -1 || echo '')
368-
if [ -n \"\$storage_backup\" ] && [ -d \"\$storage_backup\" ]; then
369-
rm -rf /home/torrust/github/torrust/torrust-tracker-demo/application/storage
370-
mv \"\$storage_backup\" /home/torrust/github/torrust/torrust-tracker-demo/application/storage
371-
fi
372-
" "Restoring preserved storage folder"
373-
374-
# Check if storage was restored and log appropriately
375-
storage_restored=$(ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "torrust@${vm_ip}" "
376-
if [ -d /home/torrust/github/torrust/torrust-tracker-demo/application/storage/mysql ] || [ -d /home/torrust/github/torrust/torrust-tracker-demo/application/storage/tracker ]; then
377-
echo 'true'
378-
else
379-
echo 'false'
380-
fi
381-
" 2>/dev/null || echo "false")
382-
383-
if [[ "${storage_restored}" == "true" ]]; then
384-
log_info "Storage folder restored with existing persistent data"
385-
fi
386-
387311
# Clean up local temp file
388312
rm -f "${temp_archive}"
389313

@@ -392,7 +316,7 @@ release_stage() {
392316

393317
log_success "Local repository deployed successfully"
394318

395-
# Set up persistent data volume and directory structure (using locally generated files)
319+
# Set up persistent data volume and copy locally generated configuration files directly
396320
vm_exec "${vm_ip}" "
397321
cd /home/torrust/github/torrust/torrust-tracker-demo
398322
@@ -404,32 +328,45 @@ release_stage() {
404328
# Ensure persistent storage directories exist
405329
sudo mkdir -p /var/lib/torrust/{tracker/{lib/database,log,etc},prometheus/{data,etc},proxy/{webroot,etc/nginx-conf},certbot/{etc,lib},dhparam,mysql/init,compose}
406330
407-
# Copy generated configuration files to persistent storage
408-
# These files are generated locally and need to be in the persistent volume
409-
if [ -f application/storage/tracker/etc/tracker.toml ]; then
410-
sudo cp application/storage/tracker/etc/tracker.toml /var/lib/torrust/tracker/etc/
411-
fi
412-
if [ -f application/storage/prometheus/etc/prometheus.yml ]; then
413-
sudo cp application/storage/prometheus/etc/prometheus.yml /var/lib/torrust/prometheus/etc/
414-
fi
415-
if [ -f application/storage/proxy/etc/nginx-conf/nginx.conf ]; then
416-
sudo cp application/storage/proxy/etc/nginx-conf/nginx.conf /var/lib/torrust/proxy/etc/nginx-conf/
417-
fi
418-
419-
# Copy .env file to persistent storage
420-
# This file is generated locally and needs to be in the persistent volume
421-
if [ -f application/storage/compose/.env ]; then
422-
sudo cp application/storage/compose/.env /var/lib/torrust/compose/.env
423-
else
424-
echo 'ERROR: No .env file found at application/storage/compose/.env'
425-
echo 'Configuration should have been generated locally before deployment'
426-
exit 1
427-
fi
428-
429-
# Ensure torrust user owns all persistent data
331+
# Ensure torrust user owns all persistent data directories
430332
sudo chown -R torrust:torrust /var/lib/torrust
431333
" "Setting up persistent data volume directory structure"
432334

335+
# Copy locally generated configuration files directly to persistent volume
336+
log_info "Copying locally generated configuration files to persistent volume..."
337+
338+
# Copy tracker configuration
339+
if [[ -f "${PROJECT_ROOT}/application/storage/tracker/etc/tracker.toml" ]]; then
340+
log_info "Copying tracker configuration..."
341+
scp -o StrictHostKeyChecking=no "${PROJECT_ROOT}/application/storage/tracker/etc/tracker.toml" "torrust@${vm_ip}:/tmp/tracker.toml"
342+
vm_exec "${vm_ip}" "sudo mv /tmp/tracker.toml /var/lib/torrust/tracker/etc/tracker.toml && sudo chown torrust:torrust /var/lib/torrust/tracker/etc/tracker.toml"
343+
fi
344+
345+
# Copy prometheus configuration
346+
if [[ -f "${PROJECT_ROOT}/application/storage/prometheus/etc/prometheus.yml" ]]; then
347+
log_info "Copying prometheus configuration..."
348+
scp -o StrictHostKeyChecking=no "${PROJECT_ROOT}/application/storage/prometheus/etc/prometheus.yml" "torrust@${vm_ip}:/tmp/prometheus.yml"
349+
vm_exec "${vm_ip}" "sudo mv /tmp/prometheus.yml /var/lib/torrust/prometheus/etc/prometheus.yml && sudo chown torrust:torrust /var/lib/torrust/prometheus/etc/prometheus.yml"
350+
fi
351+
352+
# Copy nginx configuration
353+
if [[ -f "${PROJECT_ROOT}/application/storage/proxy/etc/nginx-conf/nginx.conf" ]]; then
354+
log_info "Copying nginx configuration..."
355+
scp -o StrictHostKeyChecking=no "${PROJECT_ROOT}/application/storage/proxy/etc/nginx-conf/nginx.conf" "torrust@${vm_ip}:/tmp/nginx.conf"
356+
vm_exec "${vm_ip}" "sudo mv /tmp/nginx.conf /var/lib/torrust/proxy/etc/nginx-conf/nginx.conf && sudo chown torrust:torrust /var/lib/torrust/proxy/etc/nginx-conf/nginx.conf"
357+
fi
358+
359+
# Copy Docker Compose .env file
360+
if [[ -f "${PROJECT_ROOT}/application/storage/compose/.env" ]]; then
361+
log_info "Copying Docker Compose environment file..."
362+
scp -o StrictHostKeyChecking=no "${PROJECT_ROOT}/application/storage/compose/.env" "torrust@${vm_ip}:/tmp/compose.env"
363+
vm_exec "${vm_ip}" "sudo mv /tmp/compose.env /var/lib/torrust/compose/.env && sudo chown torrust:torrust /var/lib/torrust/compose/.env"
364+
else
365+
log_error "No .env file found at ${PROJECT_ROOT}/application/storage/compose/.env"
366+
log_error "Configuration should have been generated locally before deployment"
367+
exit 1
368+
fi
369+
433370
log_success "Release stage completed"
434371
}
435372

0 commit comments

Comments
 (0)