From 123e3e06fb9075180697d3185656485b3d4323f2 Mon Sep 17 00:00:00 2001 From: Dustin Lee Date: Wed, 8 May 2019 16:37:08 -0400 Subject: [PATCH 1/5] Add -PackerOnly option Add same functionality as build.sh to PS script --- build.ps1 | 136 +++++++++++++++++++++++++++++------------------------- 1 file changed, 72 insertions(+), 64 deletions(-) diff --git a/build.ps1 b/build.ps1 index 67c7dbc..392dbbc 100644 --- a/build.ps1 +++ b/build.ps1 @@ -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" +} From 9029a95ee9ce79934b9a4598bd4c898de6730448 Mon Sep 17 00:00:00 2001 From: Chris Long Date: Fri, 10 May 2019 17:13:23 -0700 Subject: [PATCH 2/5] TLS1.2 Support for compact.bat --- Packer/scripts/compact.bat | 1 + 1 file changed, 1 insertion(+) diff --git a/Packer/scripts/compact.bat b/Packer/scripts/compact.bat index 4ab6261..f1d3672 100755 --- a/Packer/scripts/compact.bat +++ b/Packer/scripts/compact.bat @@ -1,3 +1,4 @@ +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 if not exist "C:\Windows\Temp\7z1900-x64.msi" ( powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z1900-x64.msi', 'C:\Windows\Temp\7z1900-x64.msi')" Date: Fri, 10 May 2019 17:13:54 -0700 Subject: [PATCH 3/5] TLS1.2 support for vm-guest-tools.bat --- Packer/scripts/vm-guest-tools.bat | 1 + 1 file changed, 1 insertion(+) diff --git a/Packer/scripts/vm-guest-tools.bat b/Packer/scripts/vm-guest-tools.bat index cacb016..b32fc18 100755 --- a/Packer/scripts/vm-guest-tools.bat +++ b/Packer/scripts/vm-guest-tools.bat @@ -1,3 +1,4 @@ +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 if not exist "C:\Windows\Temp\7z1900-x64.msi" ( powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z1900-x64.msi', 'C:\Windows\Temp\7z1900-x64.msi')" Date: Fri, 10 May 2019 17:18:06 -0700 Subject: [PATCH 4/5] Move powershell code in compact.bat --- Packer/scripts/compact.bat | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Packer/scripts/compact.bat b/Packer/scripts/compact.bat index f1d3672..47ac494 100755 --- a/Packer/scripts/compact.bat +++ b/Packer/scripts/compact.bat @@ -1,11 +1,10 @@ -[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 if not exist "C:\Windows\Temp\7z1900-x64.msi" ( - powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z1900-x64.msi', 'C:\Windows\Temp\7z1900-x64.msi')" Date: Fri, 10 May 2019 17:19:10 -0700 Subject: [PATCH 5/5] Move powershell code in vm-guest-tools.bat --- Packer/scripts/vm-guest-tools.bat | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Packer/scripts/vm-guest-tools.bat b/Packer/scripts/vm-guest-tools.bat index b32fc18..c27f16c 100755 --- a/Packer/scripts/vm-guest-tools.bat +++ b/Packer/scripts/vm-guest-tools.bat @@ -1,9 +1,8 @@ -[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 if not exist "C:\Windows\Temp\7z1900-x64.msi" ( - powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://www.7-zip.org/a/7z1900-x64.msi', 'C:\Windows\Temp\7z1900-x64.msi')"