Skip to content

Instantly share code, notes, and snippets.

@gildas
Created November 30, 2015 13:28
Show Gist options
  • Select an option

  • Save gildas/ac8f9158884aa8527d8b to your computer and use it in GitHub Desktop.

Select an option

Save gildas/ac8f9158884aa8527d8b to your computer and use it in GitHub Desktop.
Build packer on Windows machines
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[ValidateSet('386', 'amd64', 'arm')]
[string[]]$Arch=('386', 'amd64', 'arm'),
[Parameter(Position=1, Mandatory=$true)]
[ValidateSet('linux', 'darwin', 'windows', 'freesd', 'openbsd')]
[string[]]$OS=('linux', 'darwin', 'windows', 'freebsd', 'openbsd'),
[Parameter(Position=2, Mandatory=$false)]
[switch]$Deploy
)
begin
{
$Source = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Definition)
Write-Debug "Building in $Source"
Push-Location $Source
$GOPath = &go env GOPATH
$GOOS = &go env GOOS
$GOArch = &go env GOARCH
$GitCommit=&git rev-parse HEAD
if (! [string]::IsNullOrEmpty($(&git status --porcelain))) { $GitDirty="+CHANGES" }
Write-Verbose "Git commit: ${GitCommit}${GitDirty}"
}
process
{
Write-Verbose "==> Getting dependencies..."
go get -v -d ./...
Write-Verbose "==> Removing old directory..."
if (Test-Path bin) { Remove-Item bin -Recurse -Force }
if (Test-Path pkg) { Remove-Item pkg -Recurse -Force }
New-Item -Type Directory bin,pkg | Out-Null
Write-Verbose "==> Building for $OS and $Arch..."
gox -os="$OS" -arch="$Arch" -ldflags "-X main.GitCommit ${GitCommit}${GitDirty}" -output "pkg/{{.OS}}_{{.Arch}}/packer-{{.Dir}}" ./...
if (! $?)
{
Write-Error "Compilation failed"
exit 1
}
Get-ChildItem ./pkg -Directory -Depth 1 | ForEach {
if (Test-Path ./pkg/$_/packer-packer.exe) { Move-Item ./pkg/$_/packer-packer.exe ./pkg/$_/packer.exe }
if (Test-Path ./pkg/$_/packer-packer) { Move-Item ./pkg/$_/packer-packer ./pkg/$_/packer }
}
if (Test-Path ./pkg/"${GOOS}_${GOArch}")
{
Write-Verbose "==> Copying binaries for this platform..."
# This we are in a Powershell, we have to be on Windows... at least until now
Get-ChildItem ./pkg/"${GOOS}_${GOArch}" -Depth 1 -File | ForEach {
Write-Verbose " $_"
Copy-Item ./pkg/"${GOOS}_${GOArch}/$_" bin
Copy-Item ./pkg/"${GOOS}_${GOArch}/$_" $GOPath/bin
}
}
if ($Deploy)
{
$PackerRoot = (Split-Path (Get-Command packer.exe).Source -Parent)
if ($PackerRoot -like '*chocolatey*')
{
$PackerRoot = [System.IO.Path]::Combine((Split-Path $PackerRoot -Parent), 'lib', 'packer', 'tools')
}
if (! (Test-Path $PackerRoot))
{
Throw "Cannot deploy to $PackerRoot"
}
Write-Verbose "==> Deploying:"
Copy-Item bin/packer-builder-hyperv-iso.exe,bin/packer-post-processor-vagrant.exe $PackerRoot
}
Write-Verbose "==> Results:"
Get-ChildItem bin
}
end
{
Pop-Location
}
@gildas

gildas commented Nov 30, 2015

Copy link
Copy Markdown
Author

Save it in the scripts folder of the packer root source directory, and run it from that script folder.

i typically run:

.\build.ps1 -Arch amd64 -OS windows -Deploy -Verbose

To get my packer install updated.

@nzspambot

Copy link
Copy Markdown

looks good @gildas

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