-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepoizer.sh
executable file
·398 lines (334 loc) · 9.59 KB
/
repoizer.sh
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/bash
if ! [ -x "$(command -v az)" ] || ! [ -x "$(command -v terraform)" ] || ! [ -x "$(command -v terragrunt)" ] || ! [ -x "$(command -v git)" ]
then
echo "You must have Azure Cli, Terraform, Terragrunt, and Git installed to use this utility";
return;
fi
repoName=''
serviceName=''
ownerEmail=''
hasMigrations=false
repoType=''
complexity=''
collegiatelinkConnection=true
crossDomainMappings=false
team='Engage'
schema=''
az account show >/dev/null 2>&1
status=$?
if [ "$status" != "0" ]
then
az login --use-device-code --tenant 809fd6c8-b876-47a9-abe2-8be2888f4a55
fi
az account show >/dev/null 2>&1
status=$?
if [ "$status" != "0" ]
then
echo 'Unable to login to azure';
return;
fi
promptQuestion() {
local response;
while true; do
read -erp "$1: " response
if [[ ! -z $response ]]; then
break
fi
done
echo "$response"
}
repoName=$(promptQuestion "Repo Name")
ownerEmail=$(promptQuestion "Owner Email")
read -p "Team [Engage]: " team
team=${team:-Engage}
read -p "Repo Type [Pillar]: " repoType
repoType=${repoType:-Pillar}
read -p "Complexity [Low]: " complexity
complexity=${complexity:-Low}
read -p "Has Collegiatelink Connection [true]: " collegiatelinkConnection
collegiatelinkConnection=${collegiatelinkConnection:-true}
read -p "Has Migrations [false]: " hasMigrations
hasMigrations=${hasMigrations:-false}
read -p "Has Cross Domain Mappings [false]: " crossDomainMappings
crossDomainMappings=${crossDomainMappings:-false}
serviceName=$(echo "$repoName" | sed -e 's/[^a-zA-Z]/-/g' -e 's/-{2,}/-/g' | awk '{print tolower($0)}')
mkdir $repoName
cd $repoName
curl -o .gitignore https://www.toptal.com/developers/gitignore/api/vscode,intellij,rider,windows,osx,dotnetcore,terraform,terragrunt,visualstudio
echo >> .gitignore
echo ".terraform.lock.hcl" >> .gitignore
git init -b main
git add .gitignore
git commit -m "Add a gitignore."
mkdir -p build
mkdir -p lib/terraform/live/non-prod/_global/devops
touch README.md
cat <<EOF > README.md
---
service_name: $serviceName
owner_email_addresses:
- $ownerEmail
project_details:
repo_name: $repoName
has_migrations: $hasMigrations
type: $repoType
complexity: $complexity
is_legacy: false
has_collegiatelink_connection: $collegiatelinkConnection
has_cross_domain_mappings: $crossDomainMappings
team: Engage
---
# $repoName
Welcome.
EOF
touch build/ci.yml
cat <<"EOF" > build/ci.yml
name: $(Year:yyyy).$(Month).$(DayOfMonth)-beta$(Rev:r)
parameters:
- name: onlySwapTest
displayName: Only Swap Test
type: boolean
default: false
trigger:
branches:
include:
- main
paths:
include:
- src
- test
pool:
vmImage: ubuntu-latest
resources:
repositories:
- repository: self
- repository: templates
type: git
name: Infra.PipelineTemplates
stages:
- stage: Build
condition: ne(${{ parameters.onlySwapTest }}, true)
jobs:
- job: Build
steps:
- template: shared/engage-ci-setup.yml@templates
- template: netcore/restore.yml@templates
- template: shared/snyk-scan.yml@templates
- template: netcore/build.yml@templates
- template: netcore/test.yml@templates
- template: netcore/pack-contracts.yml@templates
- template: netcore/publish-web.yml@templates
- template: shared/copy-artifacts.yml@templates
- stage: Deploy
dependsOn:
- Build
condition: and(succeeded(), ne(${{ parameters.onlySwapTest }}, true))
jobs:
- template: shared/deploy-slot-matrix.yml@templates
parameters:
azureSubscription: CollegiateLink Dev/Test
environment: NonProd
artifact: api
package: '*.zip'
- template: shared/deploy-slot-matrix.yml@templates
parameters:
azureSubscription: CollegiateLink Production
environment: Prod
artifact: api
package: '*.zip'
- stage: Swap
dependsOn:
- Deploy
condition: or(eq(${{ parameters.onlySwapTest }}, true), and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main')))
jobs:
- template: shared/swap-slot-matrix.yml@templates
parameters:
azureSubscription: CollegiateLink Dev/Test
environment: NonProd
artifact: api
healthCheckPath: /healthcheck
- ${{ if ne(parameters.onlySwapTest, true) }}:
- template: shared/swap-slot-matrix.yml@templates
parameters:
azureSubscription: CollegiateLink Production
environment: Prod
artifact: api
healthCheckPath: /healthcheck
- stage: Packages
dependsOn:
- Build
condition: and(succeeded(), ne(${{ parameters.onlySwapTest }}, true))
jobs:
- template: nuget/push-notify.yml@templates
parameters:
pillar: Events
defaultBranchName: main
EOF
if [ "$hasMigrations" = true ] ; then
read -p "Schema [dbo]: " schema
schema=${schema:-dbo}
mkdir -p lib/migrations/sql
touch build/migtations.yml
cat <<"EOF" > build/migtations.yml
name: $(Year:yyyy).$(Month).$(DayOfMonth).$(Rev:r)
trigger:
branches:
include:
- main
paths:
include:
- lib/migrations
pool:
vmImage: ubuntu-latest
resources:
repositories:
- repository: self
- repository: templates
type: git
name: Infra.PipelineTemplates
stages:
- stage: Migrate
jobs:
- template: database/flyway-migrations.yml@templates
parameters:
EOF
cat <<EOF >> build/migtations.yml
sqlSchemas: $schema
EOF
touch lib/migrations/flyway.sh
cat <<"EOF" > lib/migrations/flyway.sh
#!/usr/bin/env zsh
command="$1"
if [[ -z "$command" ]]; then
command="info"
fi
case "$2" in
"olddev")
server="azrdev"
database="collegiatelink-dev"
;;
"test")
server="sql-se-infra-monolithdatabase-test-001"
database="sqldb-se-infra-monolithdatabase-test-001"
;;
"usa")
server="collegiatelink"
database="collegiatelink"
;;
"can")
server="collegiatelink-can"
database="collegiatelink-can"
;;
"sea")
server="sql-se-infra-monolithdatabase-sea-001"
database="sqldb-se-infra-monolithdatabase-sea-001"
;;
*)
server="sql-se-infra-monolithdatabase-dev-001"
database="sqldb-se-infra-monolithdatabase-dev-001"
isDev=1
;;
esac
flyway $command \
-url="jdbc:sqlserver://$server.database.windows.net:1433;database=$database;encrypt=true" \
-jdbcProperties.accessToken="$(az account get-access-token --resource https://database.windows.net/ --query accessToken --output tsv)" \
-user="" \
-password="" \
EOF
cat <<EOF >> lib/migrations/flyway.sh
-schemas="$schema" \\
EOF
cat <<"EOF" >> lib/migrations/flyway.sh
-table="FlywayMigrationHistory" \
-placeholders.isDev=$isDev \
-baselineVersion=0003
EOF
fi
touch build/terraform.yml
cat <<"EOF" > build/terraform.yml
name: $(Year:yyyy).$(Month).$(DayOfMonth).$(Rev:r)
trigger:
branches:
include:
- main
paths:
include:
- lib/terraform
pool:
vmImage: ubuntu-latest
resources:
repositories:
- repository: self
- repository: templates
type: git
name: Infra.PipelineTemplates
stages:
- stage: Terraform
jobs:
- template: terraform/terragrunt-all.yml@templates
parameters:
azureSubscription: CollegiateLink Dev/Test Terraform
azureSubscriptionType: NonProd
terragruntWorkingDirectory: live/non-prod
- template: terraform/terragrunt-all.yml@templates
parameters:
azureSubscription: CollegiateLink Production Terraform
azureSubscriptionType: Prod
terragruntWorkingDirectory: live/prod
EOF
touch lib/terraform/live/root.hcl
cat <<"EOF" > lib/terraform/live/root.hcl
locals {
global_inputs = merge(
{ business_unit = "se" },
yamldecode(trim(regex("---(?s:.+)---", file(find_in_parent_folders("README.md"))), "---"))
)
account_hcl = read_terragrunt_config(find_in_parent_folders("account.hcl"))
env_hcl = try(read_terragrunt_config(find_in_parent_folders("env.hcl")), {})
region_hcl = try(read_terragrunt_config(find_in_parent_folders("region.hcl")), {})
}
remote_state {
backend = "azurerm"
config = {
resource_group_name = local.account_hcl.locals.tfstate_resource_group_name
storage_account_name = local.account_hcl.locals.tfstate_storage_account_name
container_name = local.account_hcl.locals.tfstate_container_name
key = format("%s/%s/terraform.tfstate", lower(local.global_inputs.project_details.repo_name), path_relative_to_include())
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}
inputs = merge(
local.global_inputs,
coalesce(local.account_hcl.inputs, {}),
try(local.env_hcl.inputs, {}),
try(local.region_hcl.inputs, {}),
)
EOF
touch lib/terraform/live/non-prod/account.hcl
cat <<"EOF" > lib/terraform/live/non-prod/account.hcl
locals {
tfstate_resource_group_name = "rg-se-terraform-test-001"
tfstate_storage_account_name = "stseterraformtest001"
tfstate_container_name = "tfstate"
}
EOF
touch lib/terraform/live/non-prod/_global/devops/terragrunt.hcl
cat <<"EOF" > lib/terraform/live/non-prod/_global/devops/terragrunt.hcl
terraform {
source = "git::https://[email protected]/campuslabs/Student%20Engagement%20Terraform/_git/azuredevops_engage_repository//?ref=main"
}
include {
path = find_in_parent_folders("root.hcl")
}
EOF
pushd lib/terraform/live/non-prod/_global/devops
terragrunt apply
popd
git remote add origin https://dev.azure.com/campuslabs/CollegiateLink/_git/$repoName && \
git add . && \
git commit -m "Initial Commit" && \
git push origin main
echo "All Done!"