|
| 1 | +# PowerShell script to build and install the Terraform IIS provider locally |
| 2 | + |
| 3 | +param( |
| 4 | + [string]$Version = "0.1.0", |
| 5 | + [string]$GoOS = "windows", |
| 6 | + [string]$GoArch = "amd64", |
| 7 | + [switch]$Install, |
| 8 | + [switch]$DevSetup, |
| 9 | + [switch]$Clean, |
| 10 | + [switch]$Help |
| 11 | +) |
| 12 | + |
| 13 | +$ProviderName = "terraform-provider-iis" |
| 14 | +$TerraformPluginsDir = "$env:APPDATA\terraform.d\plugins" |
| 15 | +$LocalProviderPath = "terraform.local\maxjoehnk\iis\$Version\${GoOS}_$GoArch" |
| 16 | + |
| 17 | +function Show-Help { |
| 18 | + Write-Host @" |
| 19 | +Terraform IIS Provider Build Script |
| 20 | +
|
| 21 | +Usage: .\build.ps1 [OPTIONS] |
| 22 | +
|
| 23 | +Options: |
| 24 | + -Version <string> Provider version (default: 0.1.0) |
| 25 | + -GoOS <string> Target OS (default: windows) |
| 26 | + -GoArch <string> Target architecture (default: amd64) |
| 27 | + -Install Install provider locally after building |
| 28 | + -DevSetup Build and setup for local development (recommended) |
| 29 | + -Clean Clean build artifacts |
| 30 | + -Help Show this help message |
| 31 | +
|
| 32 | +Examples: |
| 33 | + .\build.ps1 # Build provider |
| 34 | + .\build.ps1 -DevSetup # Build and setup for development (recommended) |
| 35 | + .\build.ps1 -Install # Build and install provider locally |
| 36 | + .\build.ps1 -Version "0.2.0" # Build specific version |
| 37 | + .\build.ps1 -Clean # Clean build artifacts |
| 38 | + .\build.ps1 -GoOS linux -GoArch amd64 # Build for Linux |
| 39 | +
|
| 40 | +Environment Variables (for proxy testing): |
| 41 | + IIS_HOST - IIS server URL |
| 42 | + IIS_ACCESS_KEY - Access key for authentication |
| 43 | + IIS_PROXY_URL - Proxy URL (e.g., http://proxy.company.com:8080) |
| 44 | + IIS_INSECURE - Skip TLS verification (true/false) |
| 45 | +"@ |
| 46 | +} |
| 47 | + |
| 48 | +function Build-Provider { |
| 49 | + Write-Host "Building $ProviderName for $GoOS/$GoArch..." -ForegroundColor Green |
| 50 | + |
| 51 | + # Create bin directory if it doesn't exist |
| 52 | + if (!(Test-Path "bin")) { |
| 53 | + New-Item -ItemType Directory -Name "bin" | Out-Null |
| 54 | + } |
| 55 | + |
| 56 | + $env:GOOS = $GoOS |
| 57 | + $env:GOARCH = $GoArch |
| 58 | + |
| 59 | + # For dev_overrides, we need the binary named exactly "terraform-provider-iis" |
| 60 | + $outputName = if ($GoOS -eq "windows") { |
| 61 | + "bin\terraform-provider-iis.exe" |
| 62 | + } |
| 63 | + else { |
| 64 | + "bin/terraform-provider-iis" |
| 65 | + } |
| 66 | + |
| 67 | + $buildCmd = "go build -ldflags `"-w -s`" -o `"$outputName`" ." |
| 68 | + Write-Host "Running: $buildCmd" -ForegroundColor Yellow |
| 69 | + |
| 70 | + Invoke-Expression $buildCmd |
| 71 | + |
| 72 | + if ($LASTEXITCODE -eq 0) { |
| 73 | + Write-Host "Build completed successfully: $outputName" -ForegroundColor Green |
| 74 | + |
| 75 | + # Also create versioned binary for registry installation |
| 76 | + $versionedName = if ($GoOS -eq "windows") { |
| 77 | + "bin\${ProviderName}_v$Version.exe" |
| 78 | + } |
| 79 | + else { |
| 80 | + "bin/${ProviderName}_v$Version" |
| 81 | + } |
| 82 | + Copy-Item $outputName $versionedName -Force |
| 83 | + Write-Host "Also created versioned binary: $versionedName" -ForegroundColor Green |
| 84 | + |
| 85 | + return $outputName |
| 86 | + } |
| 87 | + else { |
| 88 | + Write-Host "Build failed!" -ForegroundColor Red |
| 89 | + exit 1 |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function Install-ProviderLocally { |
| 94 | + param([string]$BinaryPath) |
| 95 | + |
| 96 | + Write-Host "Installing provider locally..." -ForegroundColor Green |
| 97 | + |
| 98 | + $fullInstallPath = "$TerraformPluginsDir\$LocalProviderPath" |
| 99 | + |
| 100 | + # Create directory structure |
| 101 | + if (!(Test-Path $fullInstallPath)) { |
| 102 | + New-Item -ItemType Directory -Path $fullInstallPath -Force | Out-Null |
| 103 | + Write-Host "Created directory: $fullInstallPath" -ForegroundColor Yellow |
| 104 | + } |
| 105 | + |
| 106 | + # Copy binary |
| 107 | + $destinationFile = "$fullInstallPath\${ProviderName}_v$Version.exe" |
| 108 | + Copy-Item $BinaryPath $destinationFile -Force |
| 109 | + |
| 110 | + Write-Host "Provider installed successfully!" -ForegroundColor Green |
| 111 | + Write-Host "Location: $destinationFile" -ForegroundColor Yellow |
| 112 | + |
| 113 | + # Show terraform configuration hint |
| 114 | + Write-Host @" |
| 115 | +
|
| 116 | +To use this provider in your Terraform configuration, add: |
| 117 | +
|
| 118 | +terraform { |
| 119 | + required_providers { |
| 120 | + iis = { |
| 121 | + source = "terraform.local/maxjoehnk/iis" |
| 122 | + version = "~> $Version" |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +
|
| 127 | +provider "iis" { |
| 128 | + host = "https://your-iis-server.com:8443" |
| 129 | + access_key = "your-access-key" |
| 130 | +
|
| 131 | + # Optional proxy configuration |
| 132 | + proxy_url = "http://proxy.company.com:8080" |
| 133 | + insecure = false |
| 134 | +} |
| 135 | +"@ -ForegroundColor Cyan |
| 136 | +} |
| 137 | + |
| 138 | +function Setup-DevEnvironment { |
| 139 | + Write-Host "Setting up development environment..." -ForegroundColor Green |
| 140 | + |
| 141 | + # Get current directory and convert backslashes to forward slashes for HCL |
| 142 | + $currentDir = Get-Location |
| 143 | + $binPath = Join-Path $currentDir "bin" |
| 144 | + $binPath = $binPath -replace '\\', '/' |
| 145 | + |
| 146 | + # Create terraform.rc content for dev overrides |
| 147 | + $terraformRcContent = @" |
| 148 | +provider_installation { |
| 149 | + dev_overrides { |
| 150 | + "terraform.local/maxjoehnk/iis" = "$binPath" |
| 151 | + } |
| 152 | +
|
| 153 | + # For all other providers, install them directly as normal. |
| 154 | + direct {} |
| 155 | +} |
| 156 | +"@ |
| 157 | + |
| 158 | + # Determine terraform.rc location |
| 159 | + $terraformRcPath = "$env:APPDATA\terraform.rc" |
| 160 | + |
| 161 | + # Backup existing terraform.rc if it exists |
| 162 | + if (Test-Path $terraformRcPath) { |
| 163 | + $backupPath = "$terraformRcPath.backup.$(Get-Date -Format 'yyyyMMdd-HHmmss')" |
| 164 | + Copy-Item $terraformRcPath $backupPath |
| 165 | + Write-Host "Backed up existing terraform.rc to: $backupPath" -ForegroundColor Yellow |
| 166 | + } |
| 167 | + |
| 168 | + # Write new terraform.rc |
| 169 | + Set-Content -Path $terraformRcPath -Value $terraformRcContent |
| 170 | + Write-Host "Created terraform.rc with dev overrides: $terraformRcPath" -ForegroundColor Green |
| 171 | + |
| 172 | + Write-Host @" |
| 173 | +
|
| 174 | +Development environment configured! |
| 175 | +
|
| 176 | +The terraform.rc file has been set up to use your local provider binary. |
| 177 | +You can now run Terraform commands in the examples directory without needing |
| 178 | +to install the provider to the plugins directory. |
| 179 | +
|
| 180 | +Next steps: |
| 181 | +1. cd examples |
| 182 | +2. terraform init |
| 183 | +3. terraform plan |
| 184 | +
|
| 185 | +To restore normal provider behavior, delete or rename: $terraformRcPath |
| 186 | +"@ -ForegroundColor Cyan |
| 187 | +} |
| 188 | + |
| 189 | +function Clean-BuildArtifacts { |
| 190 | + Write-Host "Cleaning build artifacts..." -ForegroundColor Green |
| 191 | + |
| 192 | + if (Test-Path "bin") { |
| 193 | + Remove-Item "bin" -Recurse -Force |
| 194 | + Write-Host "Removed bin directory" -ForegroundColor Yellow |
| 195 | + } |
| 196 | + |
| 197 | + Write-Host "Clean completed!" -ForegroundColor Green |
| 198 | +} |
| 199 | + |
| 200 | +function Test-Environment { |
| 201 | + Write-Host "Checking environment..." -ForegroundColor Green |
| 202 | + |
| 203 | + # Check if Go is installed |
| 204 | + try { |
| 205 | + $goVersion = go version |
| 206 | + Write-Host "Go: $goVersion" -ForegroundColor Yellow |
| 207 | + } |
| 208 | + catch { |
| 209 | + Write-Host "Error: Go is not installed or not in PATH" -ForegroundColor Red |
| 210 | + exit 1 |
| 211 | + } |
| 212 | + |
| 213 | + # Check if Terraform is installed |
| 214 | + try { |
| 215 | + $tfVersion = terraform version |
| 216 | + Write-Host "Terraform: $($tfVersion.Split("`n")[0])" -ForegroundColor Yellow |
| 217 | + } |
| 218 | + catch { |
| 219 | + Write-Host "Warning: Terraform is not installed or not in PATH" -ForegroundColor Yellow |
| 220 | + } |
| 221 | + |
| 222 | + Write-Host "Environment check completed!" -ForegroundColor Green |
| 223 | +} |
| 224 | + |
| 225 | +# Main script logic |
| 226 | +if ($Help) { |
| 227 | + Show-Help |
| 228 | + exit 0 |
| 229 | +} |
| 230 | + |
| 231 | +if ($Clean) { |
| 232 | + Clean-BuildArtifacts |
| 233 | + exit 0 |
| 234 | +} |
| 235 | + |
| 236 | +Test-Environment |
| 237 | + |
| 238 | +$binaryPath = Build-Provider |
| 239 | + |
| 240 | +if ($DevSetup) { |
| 241 | + Setup-DevEnvironment |
| 242 | +} |
| 243 | +elseif ($Install) { |
| 244 | + Install-ProviderLocally -BinaryPath $binaryPath |
| 245 | +} |
| 246 | + |
| 247 | +Write-Host "Done!" -ForegroundColor Green |
0 commit comments