-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
134 lines (109 loc) · 4.12 KB
/
Copy pathsetup.ps1
File metadata and controls
134 lines (109 loc) · 4.12 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
<#PSScriptInfo
.VERSION 1.1.0
.GUID bed24bcf-8230-4909-8619-50d5bc381721
.AUTHOR Guided Setup Author
.DESCRIPTION Prepares helm and K8s resource files and optionally runs deployment
#>
param (
[string] $workDir = "$HOME/.k8s-mariadb",
[string] $kubeContextName='dev',
[string] $namespace = "db",
[string] $releaseName = "mariadb",
[int] $replicaCount = 1,
[int] $binaryLogExpirationSeconds = 5,
[string] $rootPwd = ([guid]::newguid().guid.replace('-','')),
[string] $replicatorPwd = ([guid]::newguid().guid.replace('-','')),
[string] $characterSet = 'utf8',
[string] $collation = 'utf8_general_ci',
[int] $lowerCaseTableNames = 0,
[string[]] $extraValuesFiles = @(),
[int] $helmDeployWaitTimeSeconds = 600,
[string] $primaryName = 'primary',
[string] $replicaName = 'secondary',
[switch] $resourceFilesOnly
)
$ErrorActionPreference = 'Stop'
$VerbosePreference = 'Continue'
Set-PSDebug -Strict
### Import dependencies
'../.install-guided-setup-module.ps1', # install currently required guided-setup version
'./setup/validate.ps1',
'./setup/directory.ps1',
'./setup/resources.ps1',
'./setup/helm.ps1' | ForEach-Object {
$path = join-path $PSScriptRoot $_
if (-not (Test-Path $path)) {
Write-Error "Unable to find file script dependency at $path. Please download all files and rerun the downloaded copy of this script."
}
. $path
}
### parameter validation
Write-Verbose 'Validating script parameters...'
$prereqMessages = @()
if (-not (Test-SetupParams ` ([ref]$prereqMessages) `
'workDir','kubeContextName','namespace','releaseName','rootPwd','replicatorPwd','characterSet','collation' `
'replicaCount','binaryLogExpirationSeconds','helmDeployWaitTimeSeconds' `
$rootPwd `
$replicatorPwd `
$replicaCount `
$lowerCaseTableNames `
$workDir `
$extraValuesFiles)) {
Write-Error ([string]::join("`n", $prereqMessages))
}
### work directory config
Write-Verbose 'Configuring work directory...'
New-SetupWorkDirectory $workDir $namespace $releaseName
### namespace config
Write-Verbose 'Configuring namespace...'
New-MariaDBNamespace $namespace -resourceFileOnly:$resourceFilesOnly
### K8s secret config
Write-Verbose 'Configuring MariaDB credential K8s Secret...'
$dbCredentialsSecretName = 'db-credentials'
New-MariaDBCredentialSecret $namespace $dbCredentialsSecretName $rootPwd $replicatorPwd -resourceFileOnly:$resourceFilesOnly
### K8s configmap config
$dbOptionsConfigMapName = 'db-config'
Write-Verbose 'Configuring MariaDB options K8s ConfigMap...'
New-MariaDBOptionConfigMap $namespace $dbOptionsConfigMapName $characterSet $collation $lowerCaseTableNames $binaryLogExpirationSeconds -resourceFileOnly:$resourceFilesOnly
### helm values config
$valuesFile = './helm-values.yaml'
Write-Verbose 'Generating helm values file...'
New-MariaDBValuesFile $dbCredentialsSecretName $dbOptionsConfigMapName $primaryName $replicaName $replicaCount $valuesFile
### gather all values files
$valuesPaths = @((Get-ChildItem $valuesFile))
$extraValuesFiles | ForEach-Object {
$valuesPaths += (Get-ChildItem $_)
}
if ($resourceFilesOnly) {
### Save recommended helm install/upgrade command
Write-Verbose 'Generating helm command...'
New-HelmCommand `
$namespace `
$releaseName `
'bitnami/mariadb' `
$valuesPaths
Write-Verbose 'Done'
return
}
### configure helm repository
Write-Verbose 'Configuring helm repo...'
Add-HelmRepo 'bitnami' 'https://charts.bitnami.com/bitnami'
### invoking helm
Write-Verbose 'Invoking helm...'
Invoke-HelmCommand 'MariaDB' `
$helmDeployWaitTimeSeconds `
$namespace `
$releaseName `
'bitnami/mariadb' `
$valuesPaths
### waiting for statefulsets, showing deployment progress
$statefulSetName = $releaseName
if ($releaseName -ne 'mariadb') {
$statefulSetName = "$releaseName-mariadb"
}
if ($replicaCount -gt 0) {
Wait-StatefulSet "StatefulSet: $primaryName" $helmDeployWaitTimeSeconds $namespace "$statefulSetName-$primaryName" 1
Wait-StatefulSet "StatefulSet: $replicaName" $helmDeployWaitTimeSeconds $namespace "$statefulSetName-$replicaName" $replicaCount
} else {
Wait-StatefulSet "StatefulSet: $releaseName" $helmDeployWaitTimeSeconds $namespace $statefulSetName 1
}