Closed spuder closed 5 years ago
Different approaches for detecting if chef-client is installed using batch
Trying to run chef-client.bat —version would be compatible even with really old shells, however it also adds several seconds to the bootstrap process.
You could try using my PR #435. It will still download The client, but will continue straight after because I replaced the 5 minute wait for a blocking, non-parallel MSI run.
Also skipping the download won’t be difficult to add to that PR but that would make it a bit messy. It would help if someone merged it.
Brain dump for myself:
Trying to use WHERE since it is native to batch
@echo Checking for existing chef installation
@set chef_installed=WHERE chef-client
@echo !chef_installed!
@call !chef_installed!
@set CHEF_CLIENT_ERROR=!ERRORLEVEL!
@if ERRORLEVEL 0 (
@echo "Detected existing chef install, skipping download"
) else (
10.254.130.165 Detected Windows Version 10.0 Build 14393
10.254.130.165 Warning: Unknown version of Windows, assuming default of Windows 2008r2
10.254.130.165
10.254.130.165 C:\Users\vagrant>goto architecture_select
10.254.130.165
10.254.130.165 C:\Users\vagrant>IF "AMD64" == "x86" IF not defined PROCESSOR_ARCHITEW6432
10.254.130.165
10.254.130.165 C:\Users\vagrant>goto install
10.254.130.165 Checking for existing downloaded package at "C:\Users\vagrant\AppData\Local\Temp\chef-client-latest.msi"
10.254.130.165 No existing downloaded packages to delete.
10.254.130.165 Checking for existing chef installation
10.254.130.165 WHERE chef-client
10.254.130.165 C:\opscode\chef\bin\chef-client
10.254.130.165 ( was unexpected at this time.
10.254.130.165
10.254.130.165 C:\opscode\chef\bin\chef-client.bat
10.254.130.165
10.254.130.165 C:\Users\vagrant>@if NOT ==0 (
There is some issue with delayed evaluation that I have yet to figure out.
Also trying to not save the WHERE command to a variable and execute it directly
@echo Checking for existing chef installation
WHERE chef-client
@set CHEF_CLIENT_STATUS=!ERRORLEVEL!
@if !CHEF_CLIENT_STATUS!==0 (
@echo "Detected existing chef install, skipping download"
) else (
@echo Attempting to download client package using PowerShell if available...
Trying to make a new 'function' ( I think that is what batch calls them)
<% else %>
@set MACHINE_ARCH=x86_64
IF "%PROCESSOR_ARCHITECTURE%"=="x86" IF not defined PROCESSOR_ARCHITEW6432 @set MACHINE_ARCH=i686
<% end %>
goto chef_installed
:chef_installed
@echo Checking for existing chef installation
WHERE chef-client
If !ERRORLEVEL!==0 (
@echo "Detected existing chef install, skipping download"
) else (
goto install
)
no longer errors, but somehow still enters the :install code
C:\Users\vagrant>IF "AMD64" == "x86" IF not defined PROCESSOR_ARCHITEW6432
10.254.130.113
10.254.130.113 C:\Users\vagrant>goto chef_installed
10.254.130.113 Checking for existing chef installation
10.254.130.113
10.254.130.113 C:\Users\vagrant>WHERE chef-client
10.254.130.113 C:\opscode\chef\bin\chef-client
10.254.130.113
10.254.130.113 C:\opscode\chef\bin\chef-client.bat
10.254.130.113
10.254.130.113 C:\Users\vagrant>If !ERRORLEVEL! == 0 () else (goto install )
10.254.130.113 "Detected existing chef install, skipping download"
10.254.130.113 Checking for existing downloaded package at "C:\Users\vagrant\AppData\Local\Temp\chef-client-latest.msi"
10.254.130.113 No existing downloaded packages to delete.
10.254.130.113 Attempting to download client package using PowerShell if available...
10.254.130.113 powershell.exe -ExecutionPolicy Unrestricted -InputFormat None -NoProfile -NonInteractive -File C:\chef\wget.ps1 "https://www.chef.io/chef/download?p=windows&pv=2008r2&m=x86_64&DownloadContext=PowerShell&v=12" "C:\Users\vagrant\AppData\Local\Temp\chef-client-latest.msi"
10.254.130.113 Download via PowerShell succeeded.
10.254.130.113 Installing downloaded client package...
Chef client 12.20.21 Windows 2012r2
Problem
If you install chef on your golden image, the install.ps1 script will not detect it, and will re-download chef when bootstrapping.
Additional Information
We use packer to create windows 2012R2 golden images, (based off of the bento boxes and Matt Wrocks images) One of the packer steps installs chef using omnibus
The golden image has chef installed
Yet, every machine that gets bootstrapped, reinstalls chef. When spinning up dozens or hundreds of windows vms, this adds a lot of unnecessary delay. The windows
install.ps1
script should behave more like the linuxinstall.sh
script and intelligently detect existing chef installs.Here is the intelligence in the linux script
https://github.com/chef/chef/blob/db57131ad383076391b9df32d5e9989cfb312d58/lib/chef/knife/bootstrap/templates/chef-full.erb#L165-L176
It runs
Here is the windows install.ps1 script
https://github.com/chef/knife-windows/blob/master/lib/chef/knife/bootstrap/windows-chef-client-msi.erb
The two scripts would be more similar if the
install.ps1
did something like:Or a more batch friendly command