Commit 8b3b8f55 authored by Pascal Beutels's avatar Pascal Beutels Committed by GitHub

Merge pull request #1 from StefanScherer/my

Sync fork
parents 0729f23c 51557e17
......@@ -13,6 +13,7 @@
*~
/output/
*.webm
tmp/
tmp
/issues/
*.iso
path-to.vmx
# Packer + Azure
Steps from the blog post http://blog.geuer-pollmann.de/blog/2016/09/21/azure-germany-loves-packer/
## Security Setup
### UsingPowerShell
Read https://david-obrien.net/2016/06/use-packer-with-azurerm/
### Using Azure CLI
Read https://www.packer.io/docs/builders/azure-setup.html
First, you create an app in Azure Active Directory:
```
azure ad app create --json \
--name "Service Principal Packer" \
--home-page "https://packer.io" \
--identifier-uris "https://packer.io" \
--key-type Password \
--password SuperLongPassword123.-
```
The output of this command shows you the application ID **appId**:
In the next step, we promote our app to be a "service principal", and we list
the service principals we have:
```
azure ad sp create --json -vv --applicationId 1326f47c-eaea-42aa-8aa8-ff99fbaf3da9
```
You will get the **appId** and the **objectId**.
Now lookup you Azure Active Directory TenantID with
```
azure account show --json | jq '.[].tenantId'
```
Now lookup your Azure Subscription ID with
```
azure account show --json | jq '.[].id'
```
As a last step of the security setup, you can assign your service principal ‘Contributor’ rights to your subscription (replace $spObjectId and $subscriptionId with proper values):
```
azure role assignment create \
--objectId $spObjectId \
--roleName Contributor \
--scope "/subscriptions/$subscriptionId"
```
## Pick a location
```
azure location list
```
## Pick a VM
First list the publishers, but normally we choose `MicrosoftWindowsServer` as publisher.
```
azure vm image list-publishers westeurope
```
Now list the images available for that publisher
```
azure vm image list -l westeurope MicrosoftWindowsServer
```
The interesting ones might be
```
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter Windows 2016.0.20161010 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter:2016.0.20161010
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter Windows 2016.0.20161108 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter:2016.0.20161108
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter Windows 2016.0.20161213 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter:2016.0.20161213
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter-with-Containers Windows 2016.0.20161012 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:2016.0.20161012
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter-with-Containers Windows 2016.0.20161025 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:2016.0.20161025
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter-with-Containers Windows 2016.0.20161108 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:2016.0.20161108
data: MicrosoftWindowsServer WindowsServer 2016-Datacenter-with-Containers Windows 2016.0.20161213 westeurope MicrosoftWindowsServer:WindowsServer:2016-Datacenter-with-Containers:2016.0.20161213
data: MicrosoftWindowsServer WindowsServer 2016-Nano-Server Windows 2016.0.20161012 westeurope MicrosoftWindowsServer:WindowsServer:2016-Nano-Server:2016.0.20161012
data: MicrosoftWindowsServer WindowsServer 2016-Nano-Server Windows 2016.0.20161109 westeurope MicrosoftWindowsServer:WindowsServer:2016-Nano-Server:2016.0.20161109
data: MicrosoftWindowsServer WindowsServer 2016-Nano-Server Windows 2016.0.20170113 westeurope MicrosoftWindowsServer:WindowsServer:2016-Nano-Server:2016.0.20170113
```
**TODO** Describe how to create the resource group and storage account needed by packer build. I've used an existing resource group and storage account.
## Create Resource Group
```
azure group create myaccount westeurope
```
## Create a storage account
```
azure storage account create --sku-name LRS --location westeurope --kind BlobStorage --access-tier Cool --resource-group myaccount myaccount
```
## Store secrets in pass
I use `pass` for my secrets.
```
export PACKER_AZURE_AD_TENANT_ID=xxx
export PACKER_AZURE_SUBSCRIPTION_ID=xxx
export PACKER_AZURE_OBJECT_ID=xxx
export PACKER_AZURE_APP_ID=xxx
export PACKER_AZURE_CLIENT_SECRET='xxx'
export PACKER_AZURE_RESOURCE_GROUP=myaccount
export PACKER_AZURE_STORAGE_ACCOUNT=myaccount
```
## Build
Load your secrets and run the packer build
```
eval $(pass packer-azure)
packer build windows_2016_docker_azure.json
```
## Copy vhd
### Create a public container
Create a public container, eg. `vhds`
### Copy blob
```
azure storage blob copy start https://myaccount.blob.core.windows.net/system/Microsoft.Compute/Images/images/WindowsServer2016Docker-osDisk.vhd vhds
azure storage blob copy start https://myaccount.blob.core.windows.net/system/Microsoft.Compute/Images/images/WindowsServer2016Docker-osDisk.vhd --dest-container vhds --dest-blob WindowsServer2016Docker.20170122-osDisk.vhd
```
......@@ -12,4 +12,19 @@ Replace it with
"shutdown_command": "c:/windows/system32/sysprep/sysprep.exe /generalize /oobe /quiet /shutdown /unattend:a:/unattend.xml",
```
and on the first `vagrant up` the box will boot with an out-of-box-experience (OOBE)
Also make sure to add the `./scripts/unattend.xml` file to the `floppy_files`.
On the first `vagrant up` the box will boot with an out-of-box-experience (OOBE)
## Windows Server 2016
On newer systems like Windows 10 and Windows Server 2016 this shutdown_command is not enough.
We also have to stop the "tiledatamodelsvc" service to make sysprep work. So we use a small cmd script.
```json
"shutdown_command": "a:/sysprep.bat"
```
Also make sure to add the files `./scripts/unattend.xml` and `./scripts/sysprep.bat` to the `floppy_files`.
On the first `vagrant up` the box will boot with an out-of-box-experience (OOBE)
# My Retina Windows Templates for Packer
[![Build status](https://ci.appveyor.com/api/projects/status/76pea1oexae5ca05?svg=true)](https://ci.appveyor.com/project/StefanScherer/packer-windows)
### Introduction
......@@ -14,7 +15,7 @@ Some of my enhancements are:
### Packer Version
[Packer](https://github.com/mitchellh/packer/blob/master/CHANGELOG.md) `0.10.0` or greater is required.
[Packer](https://github.com/mitchellh/packer/blob/master/CHANGELOG.md) `0.12.2` or greater is required.
### Windows Versions
......@@ -91,17 +92,7 @@ Doing so will give you hours back in your day, which is a good thing.
These boxes use WinRM. There is no OpenSSH installed.
### Hyper-V Support
If you are running Windows 10, then you can also use these packerfiles to build a Hyper-V virtual machine:
This requires @taliesens [fork of Packer](https://github.com/mitchellh/packer/pull/2576) with Hyper-V support. The linked PR also has binaries available ex: https://dl.bintray.com/taliesins/Packer/Packer.1.0.0.104-HyperV.nupkg
Then run Packer:
```
packer build -var 'hyperv_switchname=Wifi' .\windows_2016.json
```
Be sure to set hyperv_switchname to an external Hyper-V switch.
I have the ISO already downloaded to save time, and only have Hyper-V installed on my laptop, so I run:
If you are running Windows 10, then you can also use these packerfiles to build a Hyper-V virtual machine. I have the ISO already downloaded to save time, and only have Hyper-V installed on my laptop, so I run:
```
packer build --only hyperv-iso -var 'hyperv_switchname=Ethernet' -var 'iso_url=./server2016.iso' .\windows_2016_docker.json
```
......
......@@ -84,6 +84,12 @@
</component>
</settings>
<settings pass="oobeSystem">
<component name="Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<InputLocale>en-US</InputLocale>
<SystemLocale>en-US</SystemLocale>
<UILanguage>en-US</UILanguage>
<UserLocale>en-US</UserLocale>
</component>
<component xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS">
<UserAccounts>
<AdministratorPassword>
......@@ -183,6 +189,16 @@
<Order>12</Order>
<Description>Disable password expiration for vagrant user</Description>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>%SystemRoot%\System32\reg.exe ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d "vagrant" /f</CommandLine>
<Order>13</Order>
<Description>Enable AutoLogon</Description>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>%SystemRoot%\System32\reg.exe ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1 /f</CommandLine>
<Order>14</Order>
<Description>Enable AutoLogon</Description>
</SynchronousCommand>
<!-- WITHOUT WINDOWS UPDATES -->
<!--
<SynchronousCommand wcm:action="add">
......@@ -198,6 +214,12 @@
<Order>98</Order>
<Description>Enable Microsoft Updates</Description>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\disable-screensaver.ps1</CommandLine>
<Description>Disable Screensaver</Description>
<Order>99</Order>
<RequiresUserInput>true</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\win-updates.ps1</CommandLine>
<Description>Install Windows Updates</Description>
......
This diff is collapsed.
......@@ -195,6 +195,12 @@
<Order>98</Order>
<Description>Enable Microsoft Updates</Description>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\disable-screensaver.ps1</CommandLine>
<Description>Disable Screensaver</Description>
<Order>99</Order>
<RequiresUserInput>true</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\win-updates.ps1</CommandLine>
<Description>Install Windows Updates</Description>
......
......@@ -176,6 +176,12 @@
<Order>98</Order>
<Description>Enable Microsoft Updates</Description>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\disable-screensaver.ps1</CommandLine>
<Description>Disable Screensaver</Description>
<Order>99</Order>
<RequiresUserInput>true</RequiresUserInput>
</SynchronousCommand>
<SynchronousCommand wcm:action="add">
<CommandLine>cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File a:\win-updates.ps1</CommandLine>
<Description>Install Windows Updates</Description>
......
This diff is collapsed.
This diff is collapsed.
version: 1.0.{build}
image: Visual Studio 2017
install:
- choco feature disable --name showDownloadProgress
- choco install -y packer
- ps: Install-WindowsFeature Hyper-V-PowerShell
build: off
test_script:
- ps: .\test.ps1
......@@ -4,6 +4,8 @@
# --var iso_checksum=99fd8082a609997ae97a514dca22becf20420891 `
# --var autounattend=./tmp/10/Autounattend.xml `
# windows_10.json
# bigger disk for hyperv
packer build --only=vmware-iso `
--var iso_url=C:/packer_cache/14393.0.160715-1616.RS1_RELEASE_CLIENTENTERPRISEEVAL_OEMRET_X64FRE_EN-US.ISO `
--var disk_size=136400 `
windows_10.json
#!/bin/bash
#packer build --only=vmware-iso windows_10.json
# my own license
packer build --only=vmware-iso --var iso_url=~/packer_cache/Win10_1607_English_x64.iso --var iso_checksum=99fd8082a609997ae97a514dca22becf20420891 --var autounattend=./tmp/10/Autounattend.xml windows_10.json
# Windows 10 Pro own license
#packer build --only=vmware-iso --var iso_url=~/packer_cache/my/Win10_1607_English_x64.iso --var iso_checksum=99fd8082a609997ae97a514dca22becf20420891 --var autounattend=./tmp/10_pro/Autounattend.xml windows_10.json
# Windows 10 Insider
packer build --only=vmware-iso --var iso_url=~/packer_cache/connect/16281.1000.170829-1438.rs3_release_CLIENT_BUSINESS_x64FRE_en-us.iso --var iso_checksum=1424eee844683d5e0206f94a034f3ddb80f13f65add5bf838c8608f247a99bd9 windows_10_insider.json
# Windows 10 Enterprise MSDN
#packer build --only=vmware-iso --var iso_url=~/packer_cache/msdn/en_windows_10_enterprise_version_1607_updated_jan_2017_x64_dvd_9714415.iso --var iso_checksum=97164DD5C1C933BAEF89A4BDE93D544256134FE4 --var iso_checksum_type=sha1 --var autounattend=./tmp/10/Autounattend.xml windows_10.json
# packer build --only=vmware-iso --var iso_url=~/packer_cache/msdn/en_windows_10_enterprise_version_1703_updated_march_2017_x64_dvd_10189290.iso --var iso_checksum=77D5E7C91B5DBBEE410FB6C57CB944238BF7176A --var iso_checksum_type=sha1 --var autounattend=./tmp/10/Autounattend.xml windows_10.json
# Windows 10 Client 15031
#packer build --only=vmware-iso \
# --var iso_url=~/connect/2017-02-08-windows10-15031-rs2/15031.0.170204-1546.RS2_RELEASE_CLIENTPRO-CORE_OEMRET_X64FRE_EN-US.ISO \
# --var iso_checksum=d35a1bc67c4cf0226a4e7381752e81a0ab081356 \
# --var autounattend=./tmp/10_pro_msdn/Autounattend.xml \
# windows_10.json
#!/bin/bash
# Windows 10 Insider 15031 + Docker 17.03.0-ce
packer build --only=vmware-vmx --var source_path=~/.vagrant.d/boxes/windows_10/0/vmware_desktop/windows_10.vmx windows_10_docker.json
#!/bin/bash
# packer build --only=vmware-iso --var iso_url=~/packer_cache/14393.0.160715-1616.RS1_RELEASE_SERVER_EVAL_X64FRE_EN-US.ISO windows_2016_docker.json
# MSDN ISO
packer build --only=vmware-iso --var iso_url=~/packer_cache/msdn/en_windows_server_2016_x64_dvd_9718492.iso --var iso_checksum=F185197AF68FAE4F0E06510A4579FC511BA27616 --var iso_checksum_type=sha1 --var autounattend=./tmp/2016/Autounattend.xml windows_2016_core.json
#!/bin/bash
#packer build --only=vmware-iso --var iso_url=~/packer_cache/14393.0.160715-1616.RS1_RELEASE_SERVER_EVAL_X64FRE_EN-US.ISO windows_2016_docker.json
packer build --only=vmware-iso --var iso_url=~/packer_cache/en_windows_server_2016_x64_dvd_9327751.iso --var iso_checksum=91d7b2ebcff099b3557570af7a8a5cd6 --var autounattend=./tmp/2016_core/Autounattend.xml windows_2016_dc.json
#packer build --only=vmware-iso `
# --var disk_size=102400 `
# --var iso_url=C:/packer_cache/Win10_1607_English_x64.iso `
# --var iso_checksum=99fd8082a609997ae97a514dca22becf20420891 `
# --var autounattend=./tmp/10/Autounattend.xml `
# windows_10.json
packer build windows_2016_docker.json
#!/bin/bash
# packer build --only=vmware-iso --var iso_url=~/packer_cache/14393.0.160715-1616.RS1_RELEASE_SERVER_EVAL_X64FRE_EN-US.ISO windows_2016_docker.json
packer build --only=vmware-iso --var iso_url=~/packer_cache/en_windows_server_2016_x64_dvd_9327751.iso --var iso_checksum=91d7b2ebcff099b3557570af7a8a5cd6 --var autounattend=./tmp/2016/Autounattend.xml windows_2016_docker.json
# MSDN ISO
packer build --only=vmware-iso --var iso_url=~/packer_cache/msdn/en_windows_server_2016_x64_dvd_9718492.iso --var iso_checksum=F185197AF68FAE4F0E06510A4579FC511BA27616 --var iso_checksum_type=sha1 --var autounattend=./tmp/2016/Autounattend.xml windows_2016_docker.json
packer build --only=hyperv-iso --var hyperv_switchname=ext windows_2016_docker.json
#!/bin/bash
# Microsoft Connect ISO
# packer build --only=vmware-iso --var iso_url=~/packer_cache/connect/16278.1000.170825-1441.rs3_release_SERVER_ACORE_VOL_x64FRE_en-us.iso --var iso_checksum=f87ebde2f4dc7094d2e01819d370cb8d06d2bcde520d3bdf9e8ce7e70a8ea5da --var iso_checksum_type=sha256 --var autounattend=./tmp/2016_connect/Autounattend.xml windows_2016_insider.json
# Windows Insider ISO
packer build --only=vmware-iso --var iso_url=~/packer_cache/insider/Windows_InsiderPreview_Server_16278.iso windows_2016_insider.json
......@@ -23,6 +23,8 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provision "reload"
config.vm.provision "shell", path: "scripts/install-packer.ps1", privileged: false
config.vm.provision "shell", path: "scripts/install-vagrant.ps1", privileged: false
config.vm.provision "shell", path: "scripts/install-docker.ps1", privileged: false
config.vm.provision "shell", path: "scripts/install-docker-machine.ps1", privileged: false
["vmware_fusion", "vmware_workstation"].each do |provider|
config.vm.provider provider do |v, override|
......
# install chocolatey
iex (wget 'https://chocolatey.org/install.ps1' -UseBasicParsing)
rm $PROFILE
choco install -y docker-machine
Write-Host Installing Docker client
choco install -y docker
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
Install-WindowsFeature Hyper-V-Tools
Install-WindowsFeature Hyper-V-PowerShell
cd $env:TEMP
wget -outfile Packer.zip -uri https://dl.bintray.com/taliesins/Packer/Packer.1.0.0.127-Develop.nupkg -UseBasicParsing
Expand-Archive packer.zip
copy packer\packer.exe $env:ChocolateyInstall\bin\packer.exe
choco install -y packer
choco install -y vagrant -version 1.8.5
choco install -y vagrant -version 1.9.1
terraform.tf*
.terraform/
<FirstLogonCommands>
<SynchronousCommand>
<CommandLine>cmd /c "copy C:\AzureData\CustomData.bin C:\provision.ps1"</CommandLine
><Description>CopyScript</Description>
<Order>11</Order>
</SynchronousCommand>
<SynchronousCommand>
<CommandLine>powershell.exe -sta -ExecutionPolicy Unrestricted -file C:\provision.ps1</CommandLine
><Description>RunScript</Description>
<Order>12</Order>
</SynchronousCommand>
</FirstLogonCommands>
# Packer Hyper-V builder in Azure
This is a Terraform template to spin up a VM in Azure that has nested Hyper-V
activated and tools like Git, Packer and Vagrant installed.
Now you are able to build Vagrant base boxes for Hyper-V in the Cloud with Packer.
## Stage 1: Spin up the Azure VM with Terraform
### Install Terraform
```
brew install terraform
```
### Secrets
Get your Azure ID's and secret with `pass`
```
eval $(pass azure-terraform)
```
You will need these environment variables for terraform
```
export ARM_SUBSCRIPTION_ID="uuid"
export ARM_CLIENT_ID="uuid"
export ARM_CLIENT_SECRET="secret"
export ARM_TENANT_ID="uuid"
```
### Configure
Adjust the file `variables.tf` to your needs to choose
- location / region
- DNS prefix and suffix
- size of the VM's, default is `Standard_E2s_v3` which is needed for nested virtualization
- username and password
### Plan
```bash
terraform plan
```
### Create / Apply
Create the Azure VM with. After 5 minutes the VM should be up and running, and the provision.ps1 script will run inside the VM to install Packer, Vagrant, Hyper-V and then reboots the VM and adds the internal virtual switch 'packer-hyperv-iso' and DHCP server.
```bash
terraform apply
```
If you want more than one Packer VM, then use eg. `terraform apply -var count=3`.
## Stage 2: Packer build
Now RDP into the Azure VM `pckr-01.westeurope.cloudapp.azure.com` (the dns_prefix is specified in `variables.tf`). Open a PowerShell terminal and clone my packer-windows repo or any other repo with a Packer template for Hyper-V.
```
git clone https://github.com/StefanScherer/packer-windows
cd packer-windows
packer build --only=hyperv-iso windows_2016_docker.json
```
Packer uses the internal Hyper-V virtual switch with name "packer-hyperv-iso" which was creating during the provisioning of the Azure VM. Packer now downloads the eval ISO file and boots a Hyper-V VM to run the whole packer build configuration.
## Stage 3: Vagrant up
You could also try to run it in this Azure VM with
```
vagrant box add windows_2016_docker windows_2016_docker_hyperv.box
cd ..
git clone https://github.com/StefanScherer/docker-windows-box
cd docker-windows-box
vagrant up
vagrant rdp
```
### packer push
Now you can push the Vagrant box to Vagrant Cloud (https://app.vagrantup.com).
# Configure the Microsoft Azure Provider
provider "azurerm" {}
# Create a resource group
resource "azurerm_resource_group" "global" {
location = "${var.location}"
name = "${var.account}"
}
# Create a storage account
resource "azurerm_storage_account" "global" {
account_type = "Standard_LRS" # Only locally redundant
location = "${var.location}"
name = "${var.account}"
resource_group_name = "${azurerm_resource_group.global.name}"
}
Start-Transcript -Path C:\provision.log -Append
Function SetupPhase1 {
Cscript $env:WinDir\System32\SCregEdit.wsf /AU 1
Net stop wuauserv
Net start wuauserv
Set-MpPreference -DisableRealtimeMonitoring $true
New-ItemProperty -Path HKCU:\Software\Microsoft\ServerManager -Name DoNotOpenServerManagerAtLogon -PropertyType DWORD -Value "1" -Force
Write-Host "Installing Chocolatey"
iex (wget 'https://chocolatey.org/install.ps1' -UseBasicParsing)
choco feature disable --name showDownloadProgress
choco install -y git
choco install -y packer
choco install -y vagrant
Write-Host "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 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 "Rebooting"
Restart-Computer
}
Function SetupPhase2 {
Write-Host "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 "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 "Allow Packer http server"
New-NetFirewallRule -DisplayName "Allow Packer" -Direction Inbound -Program "C:\ProgramData\chocolatey\lib\packer\tools\packer.exe" -RemoteAddress LocalSubnet -Action Allow
Write-Host "Disabling autologon"
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoAdminLogon -PropertyType DWORD -Value "0" -Force
Write-Host "Removing scheduled job"
Unregister-ScheduledJob -Name NewServerSetupResume -Force
}
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
}
# Settings
variable "account" {
default = "pckr"
}
variable "dns_prefix" {
default = "pckr"
}
variable "location" {
default = "westeurope"
}
variable "azure_dns_suffix" {
description = "Azure DNS suffix for the Public IP"
default = "cloudapp.azure.com"
}
variable "admin_username" {
default = "vagrant"
}
variable "admin_password" {
default = "Password1234!"
}
variable "count" {
default = 1
}
variable "vm_size" {
default = "Standard_E2s_v3"
}
resource "azurerm_virtual_network" "windows" {
name = "windows-virtnet"
address_space = ["10.0.0.0/16"]
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.global.name}"
}
resource "azurerm_subnet" "windows" {
name = "windows-${format("%02d", count.index + 1)}-sn"
resource_group_name = "${azurerm_resource_group.global.name}"
virtual_network_name = "${azurerm_virtual_network.windows.name}"
address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "windows" {
count = "${var.count}"
name = "windows-${format("%02d", count.index + 1)}-nic"
location = "${var.location}"
resource_group_name = "${azurerm_resource_group.global.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.windows.id}"
public_ip_address_id = "${element(azurerm_public_ip.windows.*.id, count.index)}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_public_ip" "windows" {
count = "${var.count}"
domain_name_label = "${var.dns_prefix}-${format("%02d", count.index + 1)}"
idle_timeout_in_minutes = 30
location = "${var.location}"
name = "windows-${format("%02d", count.index + 1)}-publicip"
public_ip_address_allocation = "dynamic"
resource_group_name = "${azurerm_resource_group.global.name}"
}
resource "azurerm_storage_container" "windows" {
container_access_type = "private"
count = "${var.count}"
name = "windows-${format("%02d", count.index + 1)}-storage"
resource_group_name = "${azurerm_resource_group.global.name}"
storage_account_name = "${azurerm_storage_account.global.name}"