-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathNew-AcceleratorFolderStructure.ps1
More file actions
144 lines (127 loc) · 7.27 KB
/
New-AcceleratorFolderStructure.ps1
File metadata and controls
144 lines (127 loc) · 7.27 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
function New-AcceleratorFolderStructure {
[CmdletBinding(SupportsShouldProcess = $true)]
param (
[Parameter(
Mandatory = $false,
HelpMessage = "[REQUIRED] The infrastructure as code type for the accelerator. Options are 'terraform', 'bicep' or 'bicep-classic'"
)]
[string] $iacType = "terraform",
[Parameter(
Mandatory = $false,
HelpMessage = "[REQUIRED] The version of the accelerator to use for the bootstrap and starter configuration files"
)]
[string] $versionControl = "github",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] The scenario number to use for the starter configuration files"
)]
[int] $scenarioNumber = 1,
[Parameter(
Mandatory = $false,
HelpMessage = "[REQUIRED] The target folder to create the accelerator bootstrap and platform landing zone configuration files in"
)]
[string] $targetFolderPath = "~/accelerator",
[Parameter(
Mandatory = $false,
HelpMessage = "[OPTIONAL] Force recreate of the target folder if it already exists"
)]
[switch] $force
)
if ($PSCmdlet.ShouldProcess("Accelerator folder create", "modify")) {
$currentPath = Get-Location
# Normalize target folder path
if($targetFolderPath.StartsWith("~/" )) {
$targetFolderPath = Join-Path $HOME $targetFolderPath.Replace("~/", "")
}
if(Test-Path -Path $targetFolderPath) {
if($force.IsPresent) {
Write-Host "Force flag is set, removing existing target folder at $targetFolderPath"
Remove-Item -Recurse -Force -Path $targetFolderPath | Write-Verbose | Out-Null
} else {
throw "Target folder $targetFolderPath already exists. Please specify a different folder path or remove the existing folder."
}
}
Write-Host "Creating target folder at $targetFolderPath"
New-Item -ItemType "directory" -Path $targetFolderPath -Force | Write-Verbose | Out-Null
$targetFolderPath = (Resolve-Path -Path $targetFolderPath).Path
# Create target folder structure
$outputFolder = Join-Path $targetFolderPath "output"
Write-Host "Creating output folder at $outputFolder"
New-Item -ItemType "directory" $outputFolder -Force | Write-Verbose | Out-Null
# Create temp folder
$tempFolderPath = Join-Path $targetFolderPath "temp"
Write-Host "Creating temp folder at $tempFolderPath"
New-Item -ItemType "directory" $tempFolderPath -Force | Write-Verbose | Out-Null
# Map the repo
$repos = @{
"terraform" = @{
repoName = "alz-terraform-accelerator"
folderToClone = "templates/platform_landing_zone"
libraryFolderPath = "lib"
exampleFolderPath = "examples"
bootstrapExampleFolderPath = "bootstrap"
hasScenarios = $true
hasLibrary = $true
}
"bicep" = @{
repoName = "alz-bicep-accelerator"
folderToClone = ""
libraryFolderPath = ""
exampleFolderPath = "examples"
bootstrapExampleFolderPath = "bootstrap"
hasScenarios = $false
hasLibrary = $false
platformLandingZoneFilePath = "platform-landing-zone.yaml"
}
"bicep-classic" = @{
repoName = "alz-bicep"
folderToClone = "accelerator"
libraryFolderPath = ""
exampleFolderPath = "examples"
bootstrapExampleFolderPath = "bootstrap"
hasScenarios = $false
hasLibrary = $false
platformLandingZoneFilePath = ""
}
}
# Clone the repo and copy the bootstrap and starter configuration files
$repo = $repos[$iacType]
Write-Host "Cloning repo $($repo.repoName)"
git clone --depth=1 "https://github.com/Azure/$($repo.repoName)" "$tempFolderPath" | Write-Verbose | Out-Null
Set-Location $tempFolderPath
Set-Location $currentPath
$exampleFolderPath = "$($repo.folderToClone)/$($repo.exampleFolderPath)"
$bootstrapExampleFolderPath = "$exampleFolderPath/$($repo.bootstrapExampleFolderPath)"
$configFolderPath = Join-Path $targetFolderPath "config"
Write-Host "Creating config folder at $configFolderPath"
New-Item -ItemType "directory" $configFolderPath -Force | Write-Verbose | Out-Null
# Copy the bootstrap configuration file
Write-Host "Copying bootstrap configuration file to $($targetFolderPath)/config/inputs.yaml"
Copy-Item -Path "$tempFolderPath/$bootstrapExampleFolderPath/inputs-$versionControl.yaml" -Destination "$targetFolderPath/config/inputs.yaml" -Force | Write-Verbose | Out-Null
if ($repo.hasLibrary) {
$libFolderPath = "$($repo.folderToClone)/$($repo.libraryFolderPath)"
Write-Host "Copying library files to $($targetFolderPath)/config"
Copy-Item -Path "$tempFolderPath/$libFolderPath" -Destination "$targetFolderPath/config" -Recurse -Force | Write-Verbose | Out-Null
}
# Copy the platform landing zone configuration files based on scenario number or specific file path
if ($repo.hasScenarios) {
$scenarios = @{
1 = "full-multi-region/hub-and-spoke-vnet.tfvars"
2 = "full-multi-region/virtual-wan.tfvars"
3 = "full-multi-region-nva/hub-and-spoke-vnet.tfvars"
4 = "full-multi-region-nva/virtual-wan.tfvars"
5 = "management-only/management.tfvars"
6 = "full-single-region/hub-and-spoke-vnet.tfvars"
7 = "full-single-region/virtual-wan.tfvars"
8 = "full-single-region-nva/hub-and-spoke-vnet.tfvars"
9 = "full-single-region-nva/virtual-wan.tfvars"
}
Write-Host "Copying platform landing zone configuration file for scenario $scenarioNumber to $($targetFolderPath)/config/platform-landing-zone.tfvars"
Copy-Item -Path "$tempFolderPath/$exampleFolderPath/$($scenarios[$scenarioNumber])" -Destination "$targetFolderPath/config/platform-landing-zone.tfvars" -Force | Write-Verbose | Out-Null
} elseif ($repo.platformLandingZoneFilePath -ne "") {
Write-Host "Copying platform landing zone configuration file to $($targetFolderPath)/config/platform-landing-zone.yaml"
Copy-Item -Path "$tempFolderPath/$exampleFolderPath/$($repo.platformLandingZoneFilePath)" -Destination "$targetFolderPath/config/platform-landing-zone.yaml" -Force | Write-Verbose | Out-Null
}
Remove-Item -Path $tempFolderPath -Recurse -Force | Write-Verbose | Out-Null
}
}