-
Notifications
You must be signed in to change notification settings - Fork 1
/
Deploy.ps1
260 lines (200 loc) · 7.97 KB
/
Deploy.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
param(
# Define all parameters that might be used
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$BUCKET,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$BUCKETKEY,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$BUCKETREGION,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$BUCKETENDPOINT,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$VMNAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$AZREGION,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$VAULTNAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$VAULTGROUP,
# [Parameter(ValueFromPipelineByPropertyName = $true)]
# [string]$SCRIPTURL,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$DOMAINNAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$DOMAINSUFFIX,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$EC2NAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$EC2SIZE,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$SSH_PUBLIC_KEY_LOCATION,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$USERNAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[string]$WINDOWSUSERNAME,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$genKeyVault,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$wsl,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$switch,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[switch]$approve,
[Parameter(ValueFromPipelineByPropertyName = $true)]
[bool]$s3enabled = $false
)
# Define a helper function to run each script
# function Run-ScriptWithParams {
# param($ScriptPath, $Params)
# & $ScriptPath @Params
# }
# Prepare parameter sets for each script
$ParamsKeyVault = @{
VAULTNAME = $VAULTNAME
VAULTGROUP = $VAULTGROUP
}
$ParamsBackend = @{
BUCKET = $BUCKET
BUCKETKEY = $BUCKETKEY
BUCKETREGION = $BUCKETREGION
BUCKETENDPOINT = $BUCKETENDPOINT
}
$ParamsMain = @{
DOMAINNAME = $DOMAINNAME
DOMAINSUFFIX = $DOMAINSUFFIX
EC2NAME = $EC2NAME
EC2SIZE = $EC2SIZE
SSH_PUBLIC_KEY_LOCATION = $SSH_PUBLIC_KEY_LOCATION
}
$ParamsProviders = @{
BUCKETREGION = $BUCKETREGION
VAULTNAME = $VAULTNAME
VAULTGROUP = $VAULTGROUP
}
# $ParamsVariables = @{
# SCRIPTURL = $SCRIPTURL
# }
$ParamsInventory = @{
EC2NAME = $EC2NAME
USERNAME = $USERNAME
DOMAINNAME = $DOMAINNAME
DOMAINSUFFIX = $DOMAINSUFFIX
}
$ParamsOutputs = @{
DOMAINNAME = $DOMAINNAME
EC2NAME = $EC2NAME
}
function Run-ScriptWithParams {
param($ScriptPath, $Params)
Write-Output "Running $ScriptPath."
& $ScriptPath @Params
}
function Replace-PlaceholderInFile {
param($filePath, $placeholder, $value)
Write-Output "Replacing $placeholder with $value"
(Get-Content $filePath) -replace "\(\(\($placeholder\)\)\)", $value | Set-Content $filePath
}
# Run the scripts with the correct parameter sets
if ($genKeyVault) {
Run-ScriptWithParams ".\Gen-KeyVault.ps1" $ParamsKeyVault
}
if($s3enabled) {
Run-ScriptWithParams ".\Gen-Backend.ps1" $ParamsBackend
}
# Clean and Create Deploy directory
$deployPath = ".\Deploy"
if(Test-Path $deployPath) { Remove-Item $deployPath -Recurse -Force}
if (-not (Test-Path -Path $deployPath)) {
New-Item -ItemType Directory -Path $deployPath -Force
}
Run-ScriptWithParams ".\Gen-Main.ps1" $ParamsMain
Run-ScriptWithParams ".\Gen-Providers.ps1" $ParamsProviders
Run-ScriptWithParams ".\Gen-Variables.ps1" $ParamsVariables
Run-ScriptWithParams ".\Gen-Outputs.ps1" $ParamsOutputs
Run-ScriptWithParams ".\Gen-Inventory.ps1" $ParamsInventory
# Change directory to .\Deploy and run terraform init
Set-Location -Path $deployPath
Copy-Item -Path "..\templates\roles" -Destination . -Recurse -Force
Copy-Item -Path "..\templates\modules" -Destination . -Recurse -Force
Copy-Item -Path "..\templates\postconfig.yml" -Destination . -Recurse -Force
# Copy-Item -Path ".\templates\ami-search" -Destination $deployPath -Recurse -Force
if($s3enabled)
{
Write-Output "Placeholder for backend version"
Prompt for DigitalOcean Spaces credentials
$doAccessKey = Read-Host "Enter your DigitalOcean Spaces Access Key" -AsSecureString
$doSecretKey = Read-Host "Enter your DigitalOcean Spaces Secret Key" -AsSecureString
# Convert Secure Strings to Plain Text
$ptr1 = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($doAccessKey)
$doAccessKeyPlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr1)
[System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($ptr1)
$ptr2 = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($doSecretKey)
$doSecretKeyPlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr2)
[System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($ptr2)
# Replace placeholders in deploy\backend.tf
$backendFilePath = ".\backend.tf"
Replace-PlaceholderInFile $backendFilePath "DOSPACEACCESS" $doAccessKeyPlainText
Replace-PlaceholderInFile $backendFilePath "DOSPACEKEY" $doSecretKeyPlainText
# # Set up AWS CLI profile for DigitalOcean Spaces
# aws configure set aws_access_key_id $doAccessKeyPlainText --profile digitalocean
# aws configure set aws_secret_access_key $doSecretKeyPlainText --profile digitalocean
# aws configure set region $BUCKETREGION --profile digitalocean
# $doAccessKey = Read-Host "Enter your DigitalOcean Spaces Access Key"
# $doSecretKey = Read-Host "Enter your DigitalOcean Spaces Secret Key"
# # Set up AWS CLI profile for DigitalOcean Spaces
# aws configure set aws_access_key_id $doAccessKey --profile digitalocean
# aws configure set aws_secret_access_key $doSecretKey --profile digitalocean
# aws configure set region $BUCKETREGION --profile digitalocean
# # Use the Profile with Terraform
# $Env:AWS_PROFILE = "digitalocean"
# terraform init -migrate-state
terraform init -reconfigure
$Env:AWS_PROFILE = "default" # or your regular AWS profile name
terraform plan
terraform apply
# Clearing Environment Variable
# Remove-Variable -Name AWS_PROFILE -Scope Local
# Reset to default AWS profile if necessary
# $Env:AWS_PROFILE = "default"
}
else{
# Run it with a normal backend, only way supported at the moment
# $Env:AWS_PROFILE = "default"
terraform init -reconfigure
terraform plan
if($approve){
terraform apply --auto-approve
}else{
terraform apply
}
if($wsl){
Write-Output "RUnning ansible with WSL. Uses sudo, will ask for a password multiple times."
wsl sudo cp /mnt/c/Users/$WINDOWSUSERNAME/.ssh/id_rsa /root/.ssh/id_rsa
wsl sudo chmod 600 /root/.ssh/id_rsa
wsl sudo ansible-galaxy install --roles-path ~/roles -r requirements.yml
wsl export ANSIBLE_CONFIG=ansible.cfg
wsl sudo ansible-playbook -i ./inventory.yml deploy.yml
wsl sudo ansible-playbook -i ./inventory.yml postconfig.yml
}
}
# Usage
# Define your parameters in a hashtable with placeholder values
# $params = @{
# BUCKET = "your-bucket-name"
# BUCKETKEY = "your-bucket-key"
# BUCKETREGION = "your-bucket-region"
# BUCKETENDPOINT = "your-bucket-endpoint"
# EC2NAME = "your-ec2-instance-name"
# EC2SIZE = "your-ec2-instance-size"
# USERNAME = "your-username"
# AZREGION = "your-azure-region"
# VAULTNAME = "your-vault-name"
# VAULTGROUP = "your-vault-group"
# genKeyVault = $true # Set to $false if you don't want to generate Key Vault
# DOMAINNAME = "your-domain-name"
# DOMAINSUFFIX = "your-domain-suffix"
# SSH_PUBLIC_KEY_LOCATION = "path-to-your-ssh-public-key"
# WSL =
# }
# # Run the Deploy script with the parameters from the hashtable
# .\Deploy.ps1 @params