Created
November 30, 2015 13:28
-
-
Save gildas/ac8f9158884aa8527d8b to your computer and use it in GitHub Desktop.
Build packer on Windows machines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save it in the scripts folder of the packer root source directory, and run it from that script folder.
i typically run:
To get my packer install updated.