Unverified Commit 26ddfc6b authored by Stefan Scherer's avatar Stefan Scherer
Browse files

Build a packer build environment in Azure

parent ba4cd895
Start-Transcript -Path C:\provision.log
function Get-HostToIP($hostname) {
$result = [system.Net.Dns]::GetHostByName($hostname)
$result.AddressList | ForEach-Object {$_.IPAddressToString }
}
Write-Host "provision.ps1"
Write-Host "HostName = $($HostName)"
Write-Host Windows Updates to manual
Cscript $env:WinDir\System32\SCregEdit.wsf /AU 1
Net stop wuauserv
Net start wuauserv
Start-Transcript -Path C:\provision.log -Append
Write-Host Disable Windows Defender
Set-MpPreference -DisableRealtimeMonitoring $true
Function SetupPhase1 {
Cscript $env:WinDir\System32\SCregEdit.wsf /AU 1
Net stop wuauserv
Net start wuauserv
Write-Host Do not open Server Manager at logon
New-ItemProperty -Path HKCU:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value "1" -Force
Set-MpPreference -DisableRealtimeMonitoring $true
Write-Host Install Chocolatey
iex (wget 'https://chocolatey.org/install.ps1' -UseBasicParsing)
New-ItemProperty -Path HKCU:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value "1" -Force
Write-Host Install editors
choco install -y atom
Write-Output "Installing Chocolatey"
iex (wget 'https://chocolatey.org/install.ps1' -UseBasicParsing)
choco feature disable --name showDownloadProgress
choco install -y git
choco install -y packer
Write-Host Install Git
choco install -y git
Write-Output "Installing Hyper-V"
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
Install-WindowsFeature Hyper-V-Tools
Install-WindowsFeature Hyper-V-PowerShell
Write-Host Install Packer
choco install -y packer
#Write-Output Install all Windows Updates
#Get-Content C:\windows\system32\en-us\WUA_SearchDownloadInstall.vbs | ForEach-Object {
# $_ -replace 'confirm = msgbox.*$', 'confirm = vbNo'
#} | Out-File $env:TEMP\WUA_SearchDownloadInstall.vbs
#"a`na" | cscript $env:TEMP\WUA_SearchDownloadInstall.vbs
Write-Host Install Vagrant
choco install -y vagrant
Write-Output "Rebooting"
Restart-Computer
}
Write-Host Install Docker
choco install -y docker
Function SetupPhase2 {
Write-Host Install HyperV
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
Install-WindowsFeature Hyper-V-Tools
Install-WindowsFeature Hyper-V-PowerShell
Write-Output "Adding NAT"
New-VMSwitch -SwitchName "packer-hyperv-iso" -SwitchType Internal
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceIndex (Get-NetAdapter -name "vEthernet (packer-hyperv-iso)").ifIndex
New-NetNat -Name MyNATnetwork -InternalIPInterfaceAddressPrefix 192.168.0.0/24
Write-Host Disable autologon
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -PropertyType DWORD -Value "0" -Force
Write-Output "Adding DHCP scope"
Install-WindowsFeature DHCP -IncludeManagementTools
Add-DhcpServerv4Scope -Name "Internal" -StartRange 192.168.0.10 -EndRange 192.168.0.250 -SubnetMask 255.255.255.0 -Description "Internal Network"
Set-DhcpServerv4OptionValue -ScopeID 192.168.0 -DNSServer 8.8.8.8 -Router 192.168.0.1
Write-Host Install all Windows Updates
Get-Content C:\windows\system32\en-us\WUA_SearchDownloadInstall.vbs | ForEach-Object {
$_ -replace 'confirm = msgbox.*$', 'confirm = vbNo'
} | Out-File $env:TEMP\WUA_SearchDownloadInstall.vbs
"a`na" | cscript $env:TEMP\WUA_SearchDownloadInstall.vbs
Write-Output "Disabling autologon"
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -PropertyType DWORD -Value "0" -Force
Write-Host Cleaning up
Remove-Item C:\provision.ps1
Write-Output "Removing scheduled job"
Unregister-ScheduledJob -Name NewServerSetupResume -Force
}
Write-Host Restarting computer
Restart-Computer
if (!(Test-Path c:\ProgramData\chocolatey)) {
$pwd = ConvertTo-SecureString -String $Password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username, $pwd)
$AtStartup = New-JobTrigger -AtStartup
Register-ScheduledJob -Name NewServerSetupResume `
-Credential $cred `
-Trigger $AtStartup `
-ScriptBlock { c:\provision.ps1 }
SetupPhase1
} else {
SetupPhase2
}
......@@ -56,7 +56,7 @@ resource "azurerm_virtual_machine" "windows" {
storage_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter-with-Containers"
sku = "2016-Datacenter"
version = "latest"
}
......@@ -71,7 +71,7 @@ resource "azurerm_virtual_machine" "windows" {
computer_name = "${var.dns_prefix}-win-${format("%02d", count.index + 1)}"
admin_username = "${var.admin_username}"
admin_password = "${var.admin_password}"
custom_data = "${base64encode("Param($HostName = \"${var.dns_prefix}-win-${format("%02d", count.index + 1)}.${var.location}.${var.azure_dns_suffix}\") ${file("./provision.ps1")}")}"
custom_data = "${base64encode("Param($HostName = \"${var.dns_prefix}-win-${format("%02d", count.index + 1)}.${var.location}.${var.azure_dns_suffix}\", $Username=\"${var.admin_username}\", $Password=\"${var.admin_password}\") ${file("./provision.ps1")}")}"
}
os_profile_windows_config {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment