Skip to content

Instantly share code, notes, and snippets.

@sneal
Last active May 13, 2023 15:01
Show Gist options
  • Save sneal/b3db4d2fdbd3c4edff2d to your computer and use it in GitHub Desktop.
Save sneal/b3db4d2fdbd3c4edff2d to your computer and use it in GitHub Desktop.
Windows 2012R2 PCF Cell Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Get the IP of the PCF Dev instance and put the Windows cell on the same subnet
# PCFDev creates a host only network in a predictable pattern
subnet = `vboxmanage list hostonlyifs`.split("\n").select { |i| i.start_with?('Name') }.map { |i| i.split(' ')[1] }.sort.reverse![0][7].to_i * 11 + 11
pcfdev_public_ip = ENV['PCFDEV_IP'] || "192.168.#{subnet}.11"
wincell_public_ip = ENV['WIN_PCFDEV_IP'] || "#{pcfdev_public_ip}1"
# Configure the PCFDev instance to support a Windows cell
#
# 1. Grab the Consul certs and keys
# 2. Reconfigure all services to bind to the host only adapter
# 3. Add a windows2012R2 PCF stack
# 4. Provision/start the PCFDev services
if ARGV[0] == 'up' && !File.exist?('.winpcfdev')
File.open(File.expand_path('~/.ssh/known_hosts'), 'a') { |f| f.write("#{pcfdev_public_ip} ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSd7iU/g74EEfoV7RCAD7aQ+tIHMxIfCW3w+W11iUTuuHoQczu3oyVerD967qAA0njASxpC7Qgh37OCJpl8cXOcxa4gvW+NzfIOlpIZ8N5ZHzSlOD+5JVSDPUz2c11QxgVXNAq8B9hV4gJq0IoGxMC/AYzHNnrmYbnFrh156YYznxyo9piLe/xTFGkVowzDyK9j0fGnONOMH/bQ1MoHBFL97huY6LAv1J8U22zhgSLKiqNDYxjLE6AV59ZM9X848+EnE1kjjsn5Lm7hTDvFxAJ9pfu0GuEDBvzpanXiUy7A55Dd9goa688KiMTaWYmjoBPNS/SZ0DD2xUroHsxExe1") }
puts "Starting PCFDev VM @ #{pcfdev_public_ip}"
puts `cf dev start -n`
puts `ssh -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip} "sudo ruby -e \\\"require 'json'; e = JSON.parse(IO.read('/var/vcap/jobs/consul_agent/config/config.json'))['encrypt']; IO.write('/var/vcap/jobs/consul_agent/config/certs/consul_encrypt.key', e)\\\""`
puts `scp -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip}:/var/vcap/jobs/consul_agent/config/certs/consul_encrypt.key consul_encrypt.key`
puts `scp -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip}:/var/vcap/jobs/consul_agent/config/certs/ca.crt consul_ca.crt`
puts `scp -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip}:/var/vcap/jobs/consul_agent/config/certs/agent.crt consul_agent.crt`
puts `scp -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip}:/var/vcap/jobs/consul_agent/config/certs/agent.key consul_agent.key`
puts `ssh -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip} "echo -e \\\"default: cflinuxfs2\\\\\\nstacks:\\\\\\n- description: Cloud Foundry Linux-based filesystem\\\\\\n name: cflinuxfs2\\\\\\n- description: Windows Server 2012 R2\\\\\\n name: windows2012R2\\\" | sudo tee /var/vcap/jobs/cloud_controller_ng/config/stacks.yml"`
puts `ssh -i ~/.vagrant.d/insecure_private_key vagrant@#{pcfdev_public_ip} "sudo sed -i.bak \\\"s#^ip=.*#ip=\\\\\\$(ifconfig eth1 | awk -F ' *|:' '/inet addr/{print \\\\\\$4}')#g\\\" /var/pcfdev/run"`
puts `cf dev start -p`
IO.write('.winpcfdev', 'Done')
end
# Cleanup after ourselves on destroy
File.delete('.winpcfdev') if ARGV[0] == 'destroy' && File.exist?('.winpcfdev')
# Finally provision the Windows Cell
Vagrant.configure(2) do |config|
config.vm.box = 'mwrock/Windows2012R2'
config.vm.network 'private_network', ip: wincell_public_ip
config.vm.provider 'virtualbox' do |v|
v.customize ['modifyvm', :id, '--memory', 2048]
v.customize ['modifyvm', :id, '--cpus', 2]
v.gui = false
end
# Ensure UAC is disabled so the MSI installers run non-interactively
config.vm.provision 'shell' do |s|
s.inline = <<-SCRIPT
if ((Get-ItemProperty HKLM:Software/Microsoft/Windows/CurrentVersion/policies/system).EnableLUA -ne 0) {
New-ItemProperty -Path HKLM:Software/Microsoft/Windows/CurrentVersion/policies/system -Name EnableLUA -PropertyType DWord -Value 0 -Force
shutdown /r /f /t 5
}
SCRIPT
end
# Install Windows Diego/Garden
config.vm.provision 'shell' do |s|
s.inline = <<-SCRIPT
function Check-Service-Running($svcName) {
Write-Output "Checking $svcName service is running"
$svc = Get-Service -Name $svcName -ErrorAction SilentlyContinue
if ($svc.Status -ne "Running") {
Write-Output "$svcName service is not started"
exit 1
}
}
function Download-File($src, $target, $retryCount=0) {
Write-Output "Downloading $src"
(New-Object System.Net.WebClient).DownloadFile($src, $target)
if ($? -ne $true) {
Write-Output "$src download failed"
if ($retryCount -gt 3) {
exit 1
} else {
Start-Sleep -s 5
$retryCount = $retryCount + 1
Download-File $src $target $retryCount
}
}
}
function Service-Stop($svc, $retryCount=0) {
Write-Output "Stopping $svc"
net stop $svc
Start-Sleep -s 5
if ($LastExitCode -ne 0) {
Write-Output "Stop $svc failed"
if ($retryCount -lt 3) {
$retryCount = $retryCount + 1
Service-Stop $svc $retryCount
}
}
}
function Service-Start($svc) {
Write-Output "Starting $svc"
Start-Service $svc
}
Write-Output "Starting and configuring Windows Time service"
Service-Start 'w32time'
Set-Service w32time -startuptype "automatic"
W32tm /config /manualpeerlist:pool.ntp.org /syncfromflags:MANUAL
W32tm /config /update
Download-File 'https://github.com/cloudfoundry/garden-windows-release/releases/download/v0.149/setup.ps1' 'C:/Windows/Temp/setup.ps1'
Write-Output "Executing setup.ps1 script"
powershell.exe -File C:/Windows/Temp/setup.ps1 -quiet
if ($LastExitCode -ne 0) {
Write-Output "setup.ps1 failed"
exit 1
}
Download-File 'https://github.com/cloudfoundry/diego-windows-release/releases/download/v0.400/DiegoWindows.msi' 'C:/Windows/Temp/DiegoWindows.msi'
Write-Output "Installing DiegoWindows"
msiexec /passive /norestart /i C:\\Windows\\Temp\\DiegoWindows.msi CONSUL_IPS=#{pcfdev_public_ip} CONSUL_DOMAIN=cf.internal CF_ETCD_CLUSTER=http://#{pcfdev_public_ip}:4001 STACK=windows2012R2 REDUNDANCY_ZONE=windows LOGGREGATOR_SHARED_SECRET=loggregator-secret MACHINE_IP=#{wincell_public_ip} CONSUL_ENCRYPT_FILE=C:\\vagrant\\consul_encrypt.key CONSUL_CA_FILE=C:\\vagrant\\consul_ca.crt CONSUL_AGENT_CERT_FILE=C:\\vagrant\\consul_agent.crt CONSUL_AGENT_KEY_FILE=C:\\vagrant\\consul_agent.key /log C:\\Windows\\Temp\\diegowindows.log
Download-File 'https://github.com/cloudfoundry/garden-windows-release/releases/download/v0.149/GardenWindows.msi' 'C:/Windows/Temp/GardenWindows.msi'
Write-Output "Installing GardenWindows"
msiexec /passive /norestart /i C:\\Windows\\Temp\\GardenWindows.msi MACHINE_IP=#{wincell_public_ip} /log C:\\Windows\\Temp\\gardenwindows.log
# Replace the Diego installed rep.exe and RepService.exe with our special forked version
# which supports a configurable listenAddr via MACHINE_IP
Service-Stop 'RepService'
Download-File 'https://github.com/sneal/rep/releases/download/NAT/rep.exe' 'C:/Program Files/CloudFoundry/DiegoWindows/rep.exe'
Download-File 'https://github.com/sneal/diego-windows-release/releases/download/NAT/RepService.exe' 'C:/Program Files/CloudFoundry/DiegoWindows/RepService.exe'
Service-Start 'RepService'
# Ensure all the CloudFoundry Windows services are installed and running
Check-Service-Running "ConsulService"
Check-Service-Running "ContainerizerService"
Check-Service-Running "GardenWindowsService"
Check-Service-Running "MetronService"
Check-Service-Running "RepService"
SCRIPT
end
end
@sneal
Copy link
Author

sneal commented Oct 6, 2016

Its best to cf pcfdev destroy before running this. Consul can become confused when rebinding to a different IP and the Windows cell will not register.

@PrashanthIBM
Copy link

@sneal I have followed the steps you have mentioned but when i push the .Net App, i am getting NOCOMPATIABLE CELL.

I am not sure how to proceed.

Can you please help me out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment