-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdocker-dev-services.ps1
executable file
·140 lines (112 loc) · 4.36 KB
/
docker-dev-services.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env pwsh
Param(
[ValidateNotNullOrEmpty()]
[ValidateSet('Start', 'Stop')]
[string]$Action = 'Start',
[ValidateNotNullOrEmpty()]
[string]$Set,
[ValidateNotNullOrEmpty()]
[ValidateSet('docker', 'podman')]
[string]$Engine = 'docker',
[switch]$Force,
[switch]$Verbose
)
$rootDir = $PSScriptRoot
#######################################################################
# SINGLE INSTANCE
$activeSetFile = Join-Path $rootDir '.active-set'
if ($(Test-Path $activeSetFile)) {
$activeSetContent = Get-Content $activeSetFile
$activeSetParts = $activeSetContent -Split ':'
if ($activeSetParts.Length -eq 2) {
$activeEngine = $activeSetParts[0]
$activeSet = $activeSetParts[1]
}
}
if ($activeSet) {
if ($($Action -eq 'Start') -and $($activeSet -ne $Set)) {
Write-Host "Already have active service set: $activeSet"
if (-Not $Force) { Exit 1 }
}
if ($Action -eq 'Stop') {
$Set = $activeSet
$Engine = $activeEngine
}
}
else {
if ($Action -eq 'Stop') {
Write-Host "No service sets are active"
if (-Not $Force) { Exit 1 }
}
}
#######################################################################
# SHARED VARIABLES
$serviceSetDefinition = Join-Path $rootDir 'service-sets' "$Set.yaml"
Write-Host -ForegroundColor Green "Docker Dev Services: $Set $Action"
if ($Verbose) {
Write-Host -ForegroundColor DarkGray "Root directory: $rootDir"
Write-Host -ForegroundColor DarkGray "Service set definition file: $serviceSetDefinition"
}
#######################################################################
# PARAMETERS VALIDATION
if (-Not $(Test-Path $serviceSetDefinition)) {
Write-Host -ForegroundColor Red "Service set definition does not exist: $serviceSetDefinition"
Exit 1
}
#######################################################################
# READ THE SET DEFINITION
if (-not $(Get-Module | Where-Object -Property Name -eq -Value 'powershell-yaml')) {
if (-not $(Get-InstalledModule | Where-Object -Property Name -eq -Value 'powershell-yaml')) {
Install-Module powershell-yaml -Force
}
Import-Module powershell-yaml -Force
}
$services = Get-Content $serviceSetDefinition -Raw | ConvertFrom-Yaml
if ($Verbose) {
Write-Host -ForegroundColor DarkGray "Service set definition content:"
Write-Host -ForegroundColor DarkGray $($services | ConvertTo-Yaml)
}
if (-not $services.set) {
Write-Host -ForegroundColor Red "Service set name is not defined in $serviceSetDefinition"
Exit 1
}
if (-not $($services.services.Length -gt 0)) {
Write-Host -ForegroundColor Red "No services defined in $serviceSetDefinition"
Exit 1
}
#######################################################################
# PREPARE THE DOCKER-COMPOSE PARAMETERS
$composeArguments = 'compose', '-p', $services.set, '-f', 'services/network.yaml'
foreach ($x in $services.services) {
$composeArguments = $composeArguments + '-f' + "services/$x/service.yaml"
}
#######################################################################
# ENSURE .ENV FILES EXIST
$localEnvDir = Join-Path $rootDir '.env'
if (-not $(Test-Path $localEnvDir)) {
if ($Verbose) { Write-Host -ForegroundColor DarkGray "Creating $localEnvDir" }
New-Item -Path $localEnvDir -ItemType Directory | Out-Null
}
foreach ($x in $services.services) {
$localServiceEnv = Join-Path $localEnvDir "$x.env"
if (-not $(Test-Path $localServiceEnv)) {
if ($Verbose) { Write-Host -ForegroundColor DarkGray "Creating $localServiceEnv" }
New-Item -Path $localServiceEnv -ItemType File | Out-Null
}
}
#######################################################################
# ENTRY POINT
if ($Action -eq 'Start') { $composeArguments = $composeArguments + 'up' + '-d' }
if ($Action -eq 'Stop') { $composeArguments = $composeArguments + 'down' }
$currentLocation = Get-Location
try {
Set-Location $rootDir
if ($Verbose) { Write-Host -ForegroundColor DarkGray "compose $composeArguments" }
if ($Action -eq 'Start') { Set-Content -Path $activeSetFile $Set }
Start-Process -FilePath $Engine -ArgumentList $composeArguments -WorkingDirectory $rootDir -NoNewWindow -Wait
if ($Action -eq 'Start') { Set-Content -Path $activeSetFile "${Engine}:${Set}" }
if ($Action -eq 'Stop') { Set-Content -Path $activeSetFile '' }
}
finally {
Set-Location $currentLocation
}