Merge pull request #274 from dlee35/master

Add -PackerOnly option
This commit is contained in:
Chris Long
2019-05-10 22:50:40 -07:00
committed by GitHub

136
build.ps1
View File

@@ -25,6 +25,9 @@
.PARAMETER PackerPath
The full path to the packer executable. Default is C:\Hashicorp\packer.exe
.PARAMETER PackerOnly
This switch skips deploying boxes with vagrant after being built by packer
.PARAMETER VagrantOnly
This switch skips building packer boxes and instead downloads from Vagrant Cloud
@@ -48,6 +51,7 @@ Param(
[ValidateSet('virtualbox', 'vmware_desktop')]
[string]$ProviderName,
[string]$PackerPath = 'C:\Hashicorp\packer.exe',
[switch]$PackerOnly,
[switch]$VagrantOnly
)
@@ -186,52 +190,54 @@ function preflight_checks {
break
}
}
Write-Host '[preflight_checks] Checking if Vagrant is installed'
check_vagrant
if (!($PackerOnly)) {
Write-Host '[preflight_checks] Checking if Vagrant is installed'
check_vagrant
Write-Host '[preflight_checks] Checking for pre-existing boxes..'
if ((Get-ChildItem "$DL_DIR\Boxes\*.box").Count -gt 0) {
Write-Host 'You seem to have at least one .box file present in the Boxes directory already. If you would like fresh boxes downloaded, please remove all files from the Boxes directory and re-run this script.'
}
# Check to see that no vagrant instances exist
Write-Host '[preflight_checks] Checking for vagrant instances..'
$CurrentDir = Get-Location
Set-Location "$DL_DIR\Vagrant"
if (($(vagrant status) | Select-String -Pattern "not[ _]created").Count -ne 4) {
Write-Error 'You appear to have already created at least one Vagrant instance. This script does not support already created instances. Please either destroy the existing instances or follow the build steps in the README to continue.'
break
}
Set-Location $CurrentDir
# Check available disk space. Recommend 80GB free, warn if less
Write-Host '[preflight_checks] Checking disk space..'
$drives = Get-PSDrive | Where-Object {$_.Provider -like '*FileSystem*'}
$drivesList = @()
forEach ($drive in $drives) {
if ($drive.free -lt 80GB) {
$DrivesList = $DrivesList + $drive
Write-Host '[preflight_checks] Checking for pre-existing boxes..'
if ((Get-ChildItem "$DL_DIR\Boxes\*.box").Count -gt 0) {
Write-Host 'You seem to have at least one .box file present in the Boxes directory already. If you would like fresh boxes downloaded, please remove all files from the Boxes directory and re-run this script.'
}
}
if ($DrivesList.Count -gt 0) {
Write-Output "The following drives have less than 80GB of free space. They should not be used for deploying DetectionLab"
forEach ($drive in $DrivesList) {
Write-Output "[*] $($drive.Name)"
}
Write-Output "You can safely ignore this warning if you are deploying DetectionLab to a different drive."
}
# Ensure the vagrant-reload plugin is installed
Write-Host '[preflight_checks] Checking if vagrant-reload is installed..'
if (-Not (vagrant plugin list | Select-String 'vagrant-reload')) {
Write-Output 'The vagrant-reload plugin is required and not currently installed. This script will attempt to install it now.'
(vagrant plugin install 'vagrant-reload')
if ($LASTEXITCODE -ne 0) {
Write-Error 'Unable to install the vagrant-reload plugin. Please try to do so manually and re-run this script.'
# Check to see that no vagrant instances exist
Write-Host '[preflight_checks] Checking for vagrant instances..'
$CurrentDir = Get-Location
Set-Location "$DL_DIR\Vagrant"
if (($(vagrant status) | Select-String -Pattern "not[ _]created").Count -ne 4) {
Write-Error 'You appear to have already created at least one Vagrant instance. This script does not support already created instances. Please either destroy the existing instances or follow the build steps in the README to continue.'
break
}
Set-Location $CurrentDir
# Check available disk space. Recommend 80GB free, warn if less
Write-Host '[preflight_checks] Checking disk space..'
$drives = Get-PSDrive | Where-Object {$_.Provider -like '*FileSystem*'}
$drivesList = @()
forEach ($drive in $drives) {
if ($drive.free -lt 80GB) {
$DrivesList = $DrivesList + $drive
}
}
if ($DrivesList.Count -gt 0) {
Write-Output "The following drives have less than 80GB of free space. They should not be used for deploying DetectionLab"
forEach ($drive in $DrivesList) {
Write-Output "[*] $($drive.Name)"
}
Write-Output "You can safely ignore this warning if you are deploying DetectionLab to a different drive."
}
# Ensure the vagrant-reload plugin is installed
Write-Host '[preflight_checks] Checking if vagrant-reload is installed..'
if (-Not (vagrant plugin list | Select-String 'vagrant-reload')) {
Write-Output 'The vagrant-reload plugin is required and not currently installed. This script will attempt to install it now.'
(vagrant plugin install 'vagrant-reload')
if ($LASTEXITCODE -ne 0) {
Write-Error 'Unable to install the vagrant-reload plugin. Please try to do so manually and re-run this script.'
break
}
}
}
Write-Host '[preflight_checks] Finished.'
}
@@ -262,11 +268,11 @@ function move_boxes {
Write-Host "[move_boxes] Running.."
Move-Item -Path $DL_DIR\Packer\*.box -Destination $DL_DIR\Boxes
if (-Not (Test-Path "$DL_DIR\Boxes\windows_10_$PackerProvider.box")) {
Write-Error "Windows 10 box is missing from the Boxes directory. Qutting."
Write-Error "Windows 10 box is missing from the Boxes directory. Quitting."
break
}
if (-Not (Test-Path "$DL_DIR\Boxes\windows_2016_$PackerProvider.box")) {
Write-Error "Windows 2016 box is missing from the Boxes directory. Qutting."
Write-Error "Windows 2016 box is missing from the Boxes directory. Quitting."
break
}
Write-Host "[move_boxes] Finished."
@@ -387,27 +393,29 @@ if (!($VagrantOnly)) {
move_boxes
}
# Vagrant up each box and attempt to reload one time if it fails
forEach ($VAGRANT_HOST in $LAB_HOSTS) {
Write-Host "[main] Running vagrant_up_host for: $VAGRANT_HOST"
$result = vagrant_up_host -VagrantHost $VAGRANT_HOST
Write-Host "[main] vagrant_up_host finished. Exitcode: $result"
if ($result -eq '0') {
Write-Output "Good news! $VAGRANT_HOST was built successfully!"
}
else {
Write-Warning "Something went wrong while attempting to build the $VAGRANT_HOST box."
Write-Output "Attempting to reload and reprovision the host..."
Write-Host "[main] Running vagrant_reload_host for: $VAGRANT_HOST"
$retryResult = vagrant_reload_host -VagrantHost $VAGRANT_HOST
if ($retryResult -ne 0) {
Write-Error "Failed to bring up $VAGRANT_HOST after a reload. Exiting"
break
if (!($PackerOnly)) {
# Vagrant up each box and attempt to reload one time if it fails
forEach ($VAGRANT_HOST in $LAB_HOSTS) {
Write-Host "[main] Running vagrant_up_host for: $VAGRANT_HOST"
$result = vagrant_up_host -VagrantHost $VAGRANT_HOST
Write-Host "[main] vagrant_up_host finished. Exitcode: $result"
if ($result -eq '0') {
Write-Output "Good news! $VAGRANT_HOST was built successfully!"
}
else {
Write-Warning "Something went wrong while attempting to build the $VAGRANT_HOST box."
Write-Output "Attempting to reload and reprovision the host..."
Write-Host "[main] Running vagrant_reload_host for: $VAGRANT_HOST"
$retryResult = vagrant_reload_host -VagrantHost $VAGRANT_HOST
if ($retryResult -ne 0) {
Write-Error "Failed to bring up $VAGRANT_HOST after a reload. Exiting"
break
}
}
Write-Host "[main] Finished for: $VAGRANT_HOST"
}
Write-Host "[main] Finished for: $VAGRANT_HOST"
}
Write-Host "[main] Running post_build_checks"
post_build_checks
Write-Host "[main] Finished post_build_checks"
Write-Host "[main] Running post_build_checks"
post_build_checks
Write-Host "[main] Finished post_build_checks"
}