|
| 1 | +# manage.ps1 |
| 2 | + |
| 3 | +[CmdletBinding(DefaultParameterSetName='Action')] |
| 4 | +param( |
| 5 | + [Parameter(Mandatory=$false, Position=0, ParameterSetName='Action')] |
| 6 | + [ValidateSet('start','stop','destroy','build','force-build','backup','migrate')] |
| 7 | + [string]$Action, |
| 8 | + |
| 9 | + [Parameter(Mandatory=$false, ParameterSetName='Help')] |
| 10 | + [switch]$Help |
| 11 | +) |
| 12 | + |
| 13 | +function Write-Color($color, $message) { |
| 14 | + Write-Host $message -ForegroundColor $color |
| 15 | +} |
| 16 | + |
| 17 | +function Show-Help { |
| 18 | + $helpText = @" |
| 19 | +Use: .\manage.ps1 [-Action] <action> [-Help] |
| 20 | +
|
| 21 | +Available actions: |
| 22 | + start Start all services |
| 23 | + stop Stop all services |
| 24 | + destroy Delete services and volumes (data loss!) |
| 25 | + build Build services images |
| 26 | + force-build Build services images without using the cache |
| 27 | + backup Back the database up in a .sql file |
| 28 | + migrate Migrate the current database to a newer schema version |
| 29 | +
|
| 30 | +Options: |
| 31 | + -Help Displays some help |
| 32 | +
|
| 33 | +Examples: |
| 34 | + .\manage.ps1 start |
| 35 | + .\manage.ps1 backup |
| 36 | + .\manage.ps1 -Action migrate |
| 37 | +"@ |
| 38 | + Write-Host $helpText |
| 39 | +} |
| 40 | + |
| 41 | +# Help or no action specified |
| 42 | +if ($Help -or [string]::IsNullOrEmpty($Action)) { |
| 43 | + Show-Help |
| 44 | + exit 0 |
| 45 | +} |
| 46 | + |
| 47 | +function Test-DockerRunning { |
| 48 | + try { |
| 49 | + $output = docker info 2>&1 |
| 50 | + if ($LASTEXITCODE -eq 0) { |
| 51 | + return $true |
| 52 | + } else { |
| 53 | + Write-Color Red "Docker cannot be accessed. Please ensure Docker Desktop has been started beforehand." |
| 54 | + return $false |
| 55 | + } |
| 56 | + } |
| 57 | + catch { |
| 58 | + Write-Color Red "Could not run 'docker info' : $_" |
| 59 | + return $false |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +# Check if Docker is running |
| 64 | +if (-not (Test-DockerRunning)) { |
| 65 | + exit 1 |
| 66 | +} |
| 67 | + |
| 68 | +# Load environment variables from .env |
| 69 | +if (Test-Path .env) { |
| 70 | + Get-Content .env | ForEach-Object { |
| 71 | + if ($_ -match '^([^#][^=]+)=(.*)$') { |
| 72 | + [Environment]::SetEnvironmentVariable($matches[1], $matches[2], 'Process') |
| 73 | + } |
| 74 | + } |
| 75 | +} else { |
| 76 | + Write-Color Red "Environment file .env has not been found!" |
| 77 | + exit 1 |
| 78 | +} |
| 79 | + |
| 80 | +switch ($Action) { |
| 81 | + 'start' { |
| 82 | + Write-Color Green "Starting containers..." |
| 83 | + docker-compose up -d |
| 84 | + } |
| 85 | + 'stop' { |
| 86 | + Write-Color Green "Stopping containers..." |
| 87 | + docker-compose down |
| 88 | + } |
| 89 | + 'destroy' { |
| 90 | + Write-Color Red "Deletion of containers and volumes (all data will be lost!)" |
| 91 | + $confirmation = Read-Host "Proceed? (y/N)" |
| 92 | + if ($confirmation -eq 'y' -or $confirmation -eq 'Y') { |
| 93 | + docker-compose down -v |
| 94 | + } else { |
| 95 | + Write-Host "Operation cancelled." |
| 96 | + } |
| 97 | + } |
| 98 | + 'build' { |
| 99 | + Write-Color Green "Building services..." |
| 100 | + docker-compose build |
| 101 | + } |
| 102 | + 'force-build' { |
| 103 | + Write-Color Green "Building services (no cache)..." |
| 104 | + docker-compose build --no-cache |
| 105 | + } |
| 106 | + 'backup' { |
| 107 | + Write-Color Green "Database backup in progress..." |
| 108 | + |
| 109 | + $backupDir = "db\backups" |
| 110 | + if (-not (Test-Path $backupDir)) { |
| 111 | + New-Item -ItemType Directory -Path $backupDir -Force | Out-Null |
| 112 | + } |
| 113 | + |
| 114 | + $backupFile = "$backupDir\backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').sql" |
| 115 | + $mysqlContainer = docker-compose ps -q mysql |
| 116 | + if (-not $mysqlContainer) { |
| 117 | + Write-Color Red "MySQL service has not been found. Please ensure it has been started beforehand." |
| 118 | + exit 1 |
| 119 | + } |
| 120 | + docker exec $mysqlContainer mysqldump -u root -p"${env:MYSQL_ROOT_PASSWORD}" efes_db > $backupFile |
| 121 | + if ($LASTEXITCODE -eq 0) { |
| 122 | + Write-Color Green "Backup has been successful: $backupFile" |
| 123 | + } else { |
| 124 | + Write-Color Red "An error occurred!" |
| 125 | + } |
| 126 | + } |
| 127 | + 'migrate' { |
| 128 | + Write-Color Green "Migrations are being applied..." |
| 129 | + docker-compose run --rm flyway |
| 130 | + } |
| 131 | + default { |
| 132 | + Write-Color Red "Unknown argument. Please use -Help for more info." |
| 133 | + exit 1 |
| 134 | + } |
| 135 | +} |
0 commit comments