This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathspawn.ps1
More file actions
executable file
·171 lines (136 loc) · 6.25 KB
/
spawn.ps1
File metadata and controls
executable file
·171 lines (136 loc) · 6.25 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
$env:SPAWN_ACCOUNT_IMAGE_NAME="demo-account:latest"
$env:SPAWN_TODO_IMAGE_NAME="demo-todo:latest"
function Get-GitBranchName {
$branchName=((git rev-parse --abbrev-ref HEAD).Trim() -replace "\W")
return $branchName
}
function Get-DataContainerName {
param (
[Parameter(Mandatory=$true)]
[string]$database
)
$branchName=Get-GitBranchName
switch ($database)
{
"account" {
$imageWithoutTag=$env:SPAWN_ACCOUNT_IMAGE_NAME.SubString(0, $env:SPAWN_ACCOUNT_IMAGE_NAME.IndexOf(":"))
return "$imageWithoutTag-$branchName"
}
"todo" {
$imageWithoutTag=$env:SPAWN_TODO_IMAGE_NAME.SubString(0, $env:SPAWN_TODO_IMAGE_NAME.IndexOf(":"))
return "$imageWithoutTag-$branchName"
}
}
}
function Write-SpawnMessage() {
param (
[Parameter(Mandatory=$true)]
[string]$Message,
[bool]$Exit
)
if ($true -eq $exit) {
throw "$message"
}
Write-Host -ForegroundColor Green "$message"
}
function Assert-DataImagesExist() {
if ($null -eq $env:SPAWN_TODO_IMAGE_NAME) {
Write-SpawnMessage -Exit $true "No spawn 'Todo' database image specified in environment variable SPAWN_TODO_IMAGE_NAME. Please specify an image id."
}
if ($null -eq $env:SPAWN_ACCOUNT_IMAGE_NAME) {
Write-SpawnMessage -Exit $true "No spawn 'Account' database image specified in environment variable SPAWN_ACCOUNT_IMAGE_NAME. Please specify an image id."
}
Invoke-Expression -Command "spawnctl get data-image $env:SPAWN_TODO_IMAGE_NAME 2>&1" | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-SpawnMessage -Exit $true "Could not find spawn image with id '$env:SPAWN_TODO_IMAGE_NAME'. Please ensure you have created the image."
}
Invoke-Expression -Command "spawnctl get data-image $env:SPAWN_ACCOUNT_IMAGE_NAME 2>&1" | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-SpawnMessage -Exit $true "Could not find spawn image with id '$env:SPAWN_ACCOUNT_IMAGE_NAME'. Please ensure you have created the image."
}
}
function Assert-DataContainersExist() {
param(
[Parameter(Mandatory=$true)]
[string]$TodoContainer,
[Parameter(Mandatory=$true)]
[string]$AccountContainer
)
Write-SpawnMessage "Checking if Spawn containers already exist"
Invoke-Expression -Command "spawnctl get data-container $TodoContainer 2>&1" | Out-Null
if ($LASTEXITCODE -ne 0) {
return $false
}
Invoke-Expression -Command "spawnctl get data-container $AccountContainer 2>&1" | Out-Null
if ($LASTEXITCODE -ne 0) {
return $false
}
return $true
}
function Edit-AppSettingsDatabaseConnectionStrings () {
param(
[Parameter(Mandatory=$true)]
[string]$todoContainerName,
[Parameter(Mandatory=$true)]
[string]$accountcontainerName
)
$appSettingsFilePath=(Join-Path $PSScriptRoot "/api/Spawn.Demo.WebApi/appsettings.Development.Database.json")
Write-SpawnMessage "Updating '$appSettingsFilePath' with data container connection strings"
$todoContainerDetails=(Invoke-Expression -Command "spawnctl get data-container -o json $todoContainerName" | ConvertFrom-Json)
$accountContainerDetails=(Invoke-Expression -Command "spawnctl get data-container -o json $accountContainerName" | ConvertFrom-Json)
$todoHost=$todoContainerDetails.Host
$todoPort=$todoContainerDetails.Port
$todoUser=$todoContainerDetails.User
$todoPassword=$todoContainerDetails.Password
$accountHost=$accountContainerDetails.Host
$accountPort=$accountContainerDetails.Port
$accountUser=$accountContainerDetails.User
$accountPassword=$accountContainerDetails.Password
$todoConnString="Host=$todoHost;Port=$todoPort;Database=spawndemotodo;User Id=$todoUser;Password=$todoPassword;"
$accountConnString="Server=$accountHost,$accountPort;Database=spawndemoaccount;User Id=$accountUser;Password=$accountPassword;"
$connStringObj=[PSCustomObject]@{
TodoDatabaseConnectionString = $todoConnString
AccountDatabaseConnectionString = $accountConnString
}
$json=($connStringObj | ConvertTo-Json)
Set-Content -Path $appSettingsFilePath -Value $json
}
function New-DataContainers() {
$todoContainerName=(Get-DataContainerName "todo")
$accountContainerName=(Get-DataContainerName "account")
if (((Assert-DataContainersExist $todoContainerName $accountContainerName) -eq $true) -and ($null -eq $env:NEW_CONTAINERS)) {
Write-SpawnMessage "Containers found - reusing existing Spawn containers"
} else {
Write-SpawnMessage "No containers found - creating new Spawn containers"
Write-Host
Write-SpawnMessage "Creating 'Todo' Spawn container from image '$env:SPAWN_TODO_IMAGE_NAME'"
Invoke-Expression -Command "spawnctl create data-container --image $env:SPAWN_TODO_IMAGE_NAME --name '$todoContainerName' -q" | Out-Null
Write-SpawnMessage "Spawn 'Todo' container '$todoContainerName' created from image '$SPAWN_TODO_IMAGE_NAME'"
Write-Host
Write-SpawnMessage "Creating 'Account' Spawn container from image '$env:SPAWN_ACCOUNT_IMAGE_NAME'"
Invoke-Expression -Command "spawnctl create data-container --image $env:SPAWN_ACCOUNT_IMAGE_NAME --name '$accountContainerName' -q" | Out-Null
Write-SpawnMessage "Spawn 'Account' container '$accountContainerName' created from image '$SPAWN_ACCOUNT_IMAGE_NAME'"
Write-Host
}
Edit-AppSettingsDatabaseConnectionStrings $todoContainerName $accountContainerName
Write-Host
Write-Host
Write-SpawnMessage "Successfully provisioned Spawn containers. Ready to start app"
}
function Sync-DatabasesWithMigrationScripts {
$todoContainerName=(Get-DataContainerName "todo")
$accountContainerName=(Get-DataContainerName "account")
$todoContainerDetails=(Invoke-Expression -Command "spawnctl get data-container -o json $todoContainerName" | ConvertFrom-Json)
$accountContainerDetails=(Invoke-Expression -Command "spawnctl get data-container -o json $accountContainerName" | ConvertFrom-Json)
$todoHost=$todoContainerDetails.Host
$todoPort=$todoContainerDetails.Port
$todoUser=$todoContainerDetails.User
$todoPassword=$todoContainerDetails.Password
$accountHost=$accountContainerDetails.Host
$accountPort=$accountContainerDetails.Port
$accountUser=$accountContainerDetails.User
$accountPassword=$accountContainerDetails.Password
.\migrate-db.ps1 $todoHost $todoPort $todoUser $todoPassword $accountHost $accountPort $accountUser $accountPassword
}
New-DataContainers
Sync-DatabasesWithMigrationScripts