Skip to content

Instantly share code, notes, and snippets.

@mikedougherty
Last active January 20, 2016 00:04
Show Gist options
  • Select an option

  • Save mikedougherty/edf592db2d8f055c2aff to your computer and use it in GitHub Desktop.

Select an option

Save mikedougherty/edf592db2d8f055c2aff to your computer and use it in GitHub Desktop.
packer file for a windows TP4 host on azure
# This is an approximately resolved version of the script generated above
mkdir c:\Packer
# Packer uploads the 'packer/files/windows' directory to 'c:\Packer' on the remote host
# Copy our authorized keys to the user directory
mkdir ~/.ssh
move c:\Packer\authorized_keys ~/.ssh/authorized_keys
# Install chocolatey
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
# Install Java, MinGW, Go, Process Explorer, Docker, Putty, and jq.
# procexp was for debugging some stuff while I was setting stuff up - can be removed if we don't care for it
# jq is used somewhere on jenkins, I forget where.
choco install -y jdk8 mingw golang procexp docker putty jq
# Install python2. Needed to run a script for importing localhost ssh key
# I forget why but it needs to be separate from the above install command.
choco install -y python2
# Install git. Separate because we need to specify args.
choco install -y git -ia '"/GitAndUnixToolsOnPath"'
# Install a fixed version of winsshd.install package.
# See https://github.com/dtgm/chocolatey-packages/issues/73
"C:\Program Files\Git\bin\bash.exe" -c "git clone https://github.com/dtgm/chocolatey-packages.git"
cd chocolatey-packages
"C:\Program Files\Git\bin\bash.exe" -c "git apply /c/Package/winssh-choco-fix.diff"
cd automatic\_output\winsshd.install\6.44
cpack
cd "%HOMEPATH%"
choco install -y winsshd -ia '"/startService"' -source chocolatey-packages\automatic\_output\winsshd.install\6.44
# Import our config and restart ssh
"C:\Program Files\Bitvise SSH Server\BssCfg.exe" settings importText "c:\Packer\bitvise-conf.txt" -blank
net stop BvSshServer
net start BvSshServer
# Get localhost key into registry so putty won't create a GUI prompt when ssh'ing to localhost
"C:\Program Files\Bitvise SSH Server\BssCfg.exe" keypair exportPubKey 1 -openssh localhost.pub
"C:\Program Files\Git\bin\bash.exe" -c "echo -n 'localhost ' > known_hosts; cat localhost.pub >> known_hosts"
"C:\Program Files\Git\bin\bash.exe" -c "curl -O https://raw.githubusercontent.com/Yasushi/putty/31a2ad775f393aad1c31a983b0baea205d48e219/contrib/kh2reg.py"
"C:\Program Files\Git\bin\bash.exe" -c "python kh2reg.py known_hosts > import_keys.reg"
reg.exe import import_keys.reg
# SSH to localhost one time so Bitvise SSH will read '~/.ssh/authorized_keys' and import them
# XXX: possible to do this without password?
"C:\Program Files\Git\bin\bash.exe" -c "echo true > tmp.sh; putty -ssh jenkins@localhost -pw Ch4Ng3Me -m tmp.sh"
# Disable password login
"C:\Program Files\Bitvise SSH Server\BssCfg.exe" settings importText "c:\Packer\bitvise-disable-password-conf.txt"
# Clean up files that are no longer necessary
rmdir -Recurse -Force c:\Packer
# Restart the host
# Run another script so packer will do its connect-fail-sleep-connect loop again
hostname
# After all steps are finished, packer runs sysprep:
Start-Process $env:windir\System32\Sysprep\sysprep.exe -NoNewWindow -Wait -Argument '/quiet /generalize /oobe /quit'
{
"variables": {
"image_name_prefix": "jenkins-windows",
"username": "jenkins",
"password": "Ch4Ng3Me",
"sn": "Microsoft Azure Sponsorship",
"ps": "{{ env `HOME` }}/.azure/engine-team@docker.com.publishsettings",
"sa": "packerbuild"
},
"builders": [
{
"type": "azure",
"publish_settings_path": "{{user `ps`}}",
"subscription_name": "{{user `sn`}}",
"storage_account": "{{user `sa`}}",
"storage_account_container": "images",
"os_type": "Windows",
"os_image_label": "Windows Server 2016 Technical Preview 4",
"location": "Central US",
"instance_size": "Small",
"user_image_label": "{{ user `image_name_prefix` }}_{{ isotime \"2006.01.02_15.04.05.0700\" }}"
}
],
"provisioners": [
{
"type": "powershell-azure",
"inline": ["mkdir c:\\Packer"]
},
{
"type": "file",
"source" :"packer/files/windows/",
"destination": "c:\\Packer"
},
{
"type": "powershell-azure",
"inline": [
"mkdir ~/.ssh",
"move c:\\Packer\\authorized_keys ~/.ssh/authorized_keys",
"iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))",
"choco install -y jdk8 mingw golang procexp docker putty jq",
"choco install -y python2",
"choco install -y git -ia '\"/GitAndUnixToolsOnPath\"'",
"\"C:\\Program Files\\Git\\bin\\bash.exe\" -c \"git clone https://github.com/dtgm/chocolatey-packages.git\"",
"cd chocolatey-packages",
"\"C:\\Program Files\\Git\\bin\\bash.exe\" -c \"git apply /c/Package/winssh-choco-fix.diff\"",
"cd automatic\\_output\\winsshd.install\\6.44",
"cpack",
"cd \"%HOMEPATH%\"",
"choco install -y winsshd -ia '\"/startService\"' -source chocolatey-packages\\automatic\\_output\\winsshd.install\\6.44",
"\"C:\\Program Files\\Bitvise SSH Server\\BssCfg.exe\" settings importText \"c:\\Packer\\bitvise-conf.txt\" -blank",
"net stop BvSshServer",
"net start BvSshServer",
"\"C:\\Program Files\\Bitvise SSH Server\\BssCfg.exe\" keypair exportPubKey 1 -openssh localhost.pub",
"\"C:\\Program Files\\Git\\bin\\bash.exe\" -c \"echo -n 'localhost ' > known_hosts; cat localhost.pub >> known_hosts\"",
"\"C:\\Program Files\\Git\\bin\\bash.exe\" -c \"curl -O https://raw.githubusercontent.com/Yasushi/putty/31a2ad775f393aad1c31a983b0baea205d48e219/contrib/kh2reg.py\"",
"\"C:\\Program Files\\Git\\bin\\bash.exe\" -c \"python kh2reg.py known_hosts > import_keys.reg\"",
"reg.exe import import_keys.reg",
"\"C:\\Program Files\\Git\\bin\\bash.exe\" -c \"echo true > tmp.sh; putty -ssh {{ user `username` }}@localhost -pw {{ user `password` }} -m tmp.sh\"",
"\"C:\\Program Files\\Bitvise SSH Server\\BssCfg.exe\" settings importText \"c:\\Packer\\bitvise-disable-password-conf.txt\"",
"rmdir -Recurse -Force c:\\Packer"
]
},
{
"type": "windows-restart",
"restart_timeout": "10m"
},
{
"type": "powershell-azure",
"inline": [
"hostname"
]
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment