Skip to content

Commit 1cef000

Browse files
committed
Reserve the CI environment in PowerShell startup
1 parent 10a2e2a commit 1cef000

1 file changed

Lines changed: 21 additions & 82 deletions

File tree

docker/start-environment.ps1

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ param(
44
[string] $EnvironmentName
55
)
66

7-
if ($args.Count -gt 0) {
8-
throw 'Only one environment name may be provided.'
9-
}
10-
7+
if ($args.Count -gt 0) { throw 'Only one environment name may be provided.' }
118
Set-StrictMode -Version Latest
129
$ErrorActionPreference = 'Stop'
1310

@@ -18,113 +15,55 @@ $maximumEnvironmentNameLength = 48
1815

1916
function Normalize-EnvironmentName {
2017
param([Parameter(Mandatory)][string] $Value)
21-
22-
$normalized = $Value.ToLowerInvariant()
23-
$normalized = [Regex]::Replace($normalized, '[^a-z0-9]+', '-')
24-
$normalized = $normalized.Trim('-')
25-
26-
if ([string]::IsNullOrWhiteSpace($normalized)) {
27-
throw "Environment name '$Value' does not contain any supported characters."
28-
}
29-
30-
if ($normalized.Length -gt $maximumEnvironmentNameLength) {
31-
$normalized = $normalized.Substring(0, $maximumEnvironmentNameLength).TrimEnd('-')
32-
}
33-
18+
$normalized = [Regex]::Replace($Value.ToLowerInvariant(), '[^a-z0-9]+', '-').Trim('-')
19+
if ([string]::IsNullOrWhiteSpace($normalized)) { throw "Environment name '$Value' does not contain any supported characters." }
20+
if ($normalized.Length -gt $maximumEnvironmentNameLength) { $normalized = $normalized.Substring(0, $maximumEnvironmentNameLength).TrimEnd('-') }
3421
return $normalized
3522
}
3623

3724
function Invoke-DockerCompose {
38-
param(
39-
[Parameter(Mandatory)][string[]] $Arguments,
40-
[switch] $CaptureOutput
41-
)
42-
25+
param([Parameter(Mandatory)][string[]] $Arguments, [switch] $CaptureOutput)
4326
$allArguments = $script:composeArguments + $Arguments
44-
if ($CaptureOutput) {
45-
$output = & docker @allArguments
46-
} else {
47-
& docker @allArguments
48-
}
49-
50-
if ($LASTEXITCODE -ne 0) {
51-
throw "Docker Compose failed with exit code $LASTEXITCODE."
52-
}
53-
54-
if ($CaptureOutput) {
55-
return $output
56-
}
27+
if ($CaptureOutput) { $output = & docker @allArguments } else { & docker @allArguments }
28+
if ($LASTEXITCODE -ne 0) { throw "Docker Compose failed with exit code $LASTEXITCODE." }
29+
if ($CaptureOutput) { return $output }
5730
}
5831

5932
function Get-AssignedPort {
60-
param(
61-
[Parameter(Mandatory)][string] $Service,
62-
[Parameter(Mandatory)][int] $ContainerPort
63-
)
64-
33+
param([Parameter(Mandatory)][string] $Service, [Parameter(Mandatory)][int] $ContainerPort)
6534
$endpoint = (Invoke-DockerCompose -Arguments @('port', $Service, $ContainerPort.ToString()) -CaptureOutput | Select-Object -Last 1).Trim()
6635
$match = [Regex]::Match($endpoint, ':(\d+)$')
67-
68-
if (-not $match.Success) {
69-
throw "Unable to determine the host port for ${Service}:${ContainerPort} from '$endpoint'."
70-
}
71-
36+
if (-not $match.Success) { throw "Unable to determine the host port for ${Service}:${ContainerPort} from '$endpoint'." }
7237
return $match.Groups[1].Value
7338
}
7439

75-
if ([string]::IsNullOrWhiteSpace($EnvironmentName)) {
76-
$EnvironmentName = Split-Path -Leaf $repositoryRoot
77-
}
78-
40+
if ([string]::IsNullOrWhiteSpace($EnvironmentName)) { $EnvironmentName = Split-Path -Leaf $repositoryRoot }
7941
$normalizedEnvironmentName = Normalize-EnvironmentName $EnvironmentName
80-
$portMode = if ($normalizedEnvironmentName -in @('debug', 'test')) { $normalizedEnvironmentName } else { 'dynamic' }
42+
$portMode = if ($normalizedEnvironmentName -in @('debug', 'test', 'ci')) { $normalizedEnvironmentName } else { 'dynamic' }
8143
$projectName = "$projectPrefix-$normalizedEnvironmentName"
82-
$baseComposeFile = Join-Path $scriptDirectory 'docker-compose.yml'
83-
$overrideComposeFile = Join-Path $scriptDirectory "docker-compose.$portMode.yml"
8444
$envFile = Join-Path $repositoryRoot '.env'
85-
$script:composeArguments = @(
86-
'compose',
87-
'--project-name', $projectName,
88-
'--file', $baseComposeFile,
89-
'--file', $overrideComposeFile
90-
)
45+
$script:composeArguments = @('compose', '--project-name', $projectName, '--file', (Join-Path $scriptDirectory 'docker-compose.yml'), '--file', (Join-Path $scriptDirectory "docker-compose.$portMode.yml"))
9146

9247
& docker compose version *> $null
93-
if ($LASTEXITCODE -ne 0) {
94-
throw "Docker Compose v2 is required. Ensure 'docker compose' is available."
95-
}
48+
if ($LASTEXITCODE -ne 0) { throw "Docker Compose v2 is required. Ensure 'docker compose' is available." }
9649

9750
Write-Host "Starting '$projectName' using $portMode ports..."
9851
Invoke-DockerCompose -Arguments @('up', '--detach', '--wait')
99-
100-
Invoke-DockerCompose -Arguments @(
101-
'exec', '-T', 'sqlexpress',
102-
'/opt/mssql-tools/bin/sqlcmd',
103-
'-S', 'localhost',
104-
'-U', 'sa',
105-
'-P', 'Password1',
106-
'-Q', "IF DB_ID(N'NEventStore') IS NULL CREATE DATABASE [NEventStore];"
107-
)
52+
$databasePassword = (Invoke-DockerCompose -Arguments @('exec', '-T', 'sqlexpress', 'printenv', 'MSSQL_SA_PASSWORD') -CaptureOutput | Select-Object -Last 1).Trim()
53+
Invoke-DockerCompose -Arguments @('exec', '-T', 'sqlexpress', '/opt/mssql-tools/bin/sqlcmd', '-S', 'localhost', '-U', 'sa', '-P', $databasePassword, '-Q', "IF DB_ID(N'NEventStore') IS NULL CREATE DATABASE [NEventStore];")
10854

10955
$sqlServerPort = Get-AssignedPort -Service 'sqlexpress' -ContainerPort 1433
11056
$mySqlPort = Get-AssignedPort -Service 'mysql' -ContainerPort 3306
11157
$postgreSqlPort = Get-AssignedPort -Service 'postgres' -ContainerPort 5432
11258
$oraclePort = Get-AssignedPort -Service 'oracle' -ContainerPort 1521
113-
11459
$connectionStrings = @(
11560
"# Generated by docker/start-environment.ps1 for environment '$normalizedEnvironmentName'.",
116-
"NEventStore.MsSql=Server=127.0.0.1,$sqlServerPort;Database=NEventStore;User Id=sa;Password=Password1;TrustServerCertificate=True;",
117-
"NEventStore.MySql=Server=127.0.0.1;Port=$mySqlPort;Database=NEventStore;Uid=sa;Pwd=Password1;AutoEnlist=false;",
118-
"NEventStore.PostgreSql=Server=127.0.0.1;Port=$postgreSqlPort;Database=NEventStore;Uid=sa;Pwd=Password1;Enlist=false;",
119-
"NEventStore.Oracle=Data Source=127.0.0.1:$oraclePort/XE;User Id=system;Password=Password1;Persist Security Info=True;"
61+
"NEventStore.MsSql=Server=127.0.0.1,$sqlServerPort;Database=NEventStore;User Id=sa;Password=$databasePassword;TrustServerCertificate=True;",
62+
"NEventStore.MySql=Server=127.0.0.1;Port=$mySqlPort;Database=NEventStore;Uid=sa;Pwd=$databasePassword;AutoEnlist=false;",
63+
"NEventStore.PostgreSql=Server=127.0.0.1;Port=$postgreSqlPort;Database=NEventStore;Uid=sa;Pwd=$databasePassword;Enlist=false;",
64+
"NEventStore.Oracle=Data Source=127.0.0.1:$oraclePort/XE;User Id=system;Password=$databasePassword;Persist Security Info=True;"
12065
)
121-
122-
$utf8WithoutBom = [System.Text.UTF8Encoding]::new($false)
123-
[System.IO.File]::WriteAllText($envFile, ($connectionStrings -join [Environment]::NewLine) + [Environment]::NewLine, $utf8WithoutBom)
124-
125-
Write-Host
66+
[System.IO.File]::WriteAllText($envFile, ($connectionStrings -join [Environment]::NewLine) + [Environment]::NewLine, [System.Text.UTF8Encoding]::new($false))
12667
Write-Host "Environment: $normalizedEnvironmentName"
12768
Write-Host "Compose project: $projectName"
12869
Write-Host "Connection strings written to: $envFile"
129-
Write-Host
130-
$connectionStrings | Select-Object -Skip 1 | ForEach-Object { Write-Host $_ }

0 commit comments

Comments
 (0)