Skip to content

Instantly share code, notes, and snippets.

@yusukegoto
Last active August 29, 2015 14:17
Show Gist options
  • Save yusukegoto/d435cc410070936ed605 to your computer and use it in GitHub Desktop.
Save yusukegoto/d435cc410070936ed605 to your computer and use it in GitHub Desktop.
@rem .chef/bootstrapに配置する
@rem knife bootstrap windows winrm XX --template-file ./chef/bootstrap/windows-chef-client-msi.erb
@rem
@rem Author:: Seth Chisamore (<schisamo@opscode.com>)
@rem Copyright:: Copyright (c) 2011 Opscode, Inc.
@rem License:: Apache License, Version 2.0
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem Use delayed environment expansion so that ERRORLEVEL can be evaluated with the
@rem !ERRORLEVEL! syntax which evaluates at execution of the line of script, not when
@rem the line is read. See help for the /E switch from cmd.exe /? .
@setlocal ENABLEDELAYEDEXPANSION
<%= "SETX HTTP_PROXY \"#{knife_config[:bootstrap_proxy]}\"" if knife_config[:bootstrap_proxy] %>
@set BOOTSTRAP_DIRECTORY=<%= bootstrap_directory %>
@echo Checking for existing directory "%BOOTSTRAP_DIRECTORY%"...
@if NOT EXIST %BOOTSTRAP_DIRECTORY% (
@echo Existing directory not found, creating.
@mkdir %BOOTSTRAP_DIRECTORY%
) else (
@echo Existing directory found, skipping creation.
)
> <%= bootstrap_directory %>\wget.vbs (
<%= win_wget %>
)
> <%= bootstrap_directory %>\wget.ps1 (
<%= win_wget_ps %>
)
@rem Determine the version and the architecture
@FOR /F "usebackq tokens=1-8 delims=.[] " %%A IN (`ver`) DO (
@set WinMajor=%%D
@set WinMinor=%%E
@set WinBuild=%%F
)
@echo Detected Windows Version %WinMajor%.%WinMinor% Build %WinBuild%
@set LATEST_OS_VERSION_MAJOR=6
@set LATEST_OS_VERSION_MINOR=3
@if /i %WinMajor% GTR %LATEST_OS_VERSION_MAJOR% goto VersionUnknown
@if /i %WinMajor% EQU %LATEST_OS_VERSION_MAJOR% (
@if /i %WinMinor% GTR %LATEST_OS_VERSION_MINOR% goto VersionUnknown
)
goto Version%WinMajor%.%WinMinor%
:VersionUnknown
@rem If this is an unknown version of windows set the default
@set MACHINE_OS=2008r2
@echo Warning: Unknown version of Windows, assuming default of Windows %MACHINE_OS%
goto architecture_select
:Version6.0
@set MACHINE_OS=2008
goto architecture_select
:Version5.2
@set MACHINE_OS=2003r2
goto architecture_select
:Version6.1
@set MACHINE_OS=2008r2
goto architecture_select
:Version6.2
@set MACHINE_OS=2012
goto architecture_select
@rem Currently Windows Server 2012 R2 is treated as equivalent to Windows Server 2012
:Version6.3
goto Version6.2
:architecture_select
goto Architecture%PROCESSOR_ARCHITEW6432%
:Architecture
goto Architecture%PROCESSOR_ARCHITECTURE%
@rem If this is an unknown architecture set the default
@set MACHINE_ARCH=i686
goto install
:Architecturex86
@set MACHINE_ARCH=i686
goto install
:Architectureamd64
@set MACHINE_ARCH=x86_64
goto install
:install
@rem Install Chef using chef-client MSI installer
<% url = knife_config[:msi_url] -%>
@set "REMOTE_SOURCE_MSI_URL=<%= url %>"
@set "LOCAL_DESTINATION_MSI_PATH=<%= local_download_path %>"
@set "CHEF_CLIENT_MSI_LOG_PATH=%TEMP%\chef-client-msi%RANDOM%.log"
@set "FALLBACK_QUERY_STRING=&DownloadContext=PowerShell"
@rem Clear any pre-existing downloads
@echo Checking for existing downloaded package at "%LOCAL_DESTINATION_MSI_PATH%"
@if EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
@echo Found existing downloaded package, deleting.
@del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
@if ERRORLEVEL 1 (
echo Warning: Failed to delete pre-existing package with status code !ERRORLEVEL! > "&2"
)
) else (
echo No existing downloaded packages to delete.
)
@rem If there is somehow a name collision, remove pre-existing log
@if EXIST "%CHEF_CLIENT_MSI_LOG_PATH%" del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
@echo Attempting to download client package using cscript...
cscript /nologo <%= bootstrap_directory %>\wget.vbs /url:"%REMOTE_SOURCE_MSI_URL%" /path:"%LOCAL_DESTINATION_MSI_PATH%"
@rem Work around issues found in Windows Server 2012 around job objects not respecting WSMAN memory quotas
@rem that cause the MSI download process to exceed the quota even when it is increased by administrators.
@rem Retry the download using a more memory-efficient mechanism that only works if PowerShell is available.
@set DOWNLOAD_ERROR_STATUS=!ERRORLEVEL!
@if ERRORLEVEL 1 (
@echo Failed cscript download with status code !DOWNLOAD_ERROR_STATUS! > "&2"
@if !DOWNLOAD_ERROR_STATUS!==0 set DOWNLOAD_ERROR_STATUS=2
) else (
@rem Sometimes the error level is not set even when the download failed,
@rem so check for the file to be sure it is there -- if it is not, we will retry
@if NOT EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
echo Failed download: download completed, but downloaded file not found > "&2"
set DOWNLOAD_ERROR_STATUS=2
) else (
echo Download via cscript succeeded.
)
)
@if NOT %DOWNLOAD_ERROR_STATUS%==0 (
@echo Warning: Failed to download "%REMOTE_SOURCE_MSI_URL%" to "%LOCAL_DESTINATION_MSI_PATH%"
@echo Warning: Retrying download with PowerShell if available...
@if EXIST "%LOCAL_DESTINATION_MSI_PATH%" del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
@set powershell_download=powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File <%= bootstrap_directory %>\wget.ps1 "%REMOTE_SOURCE_MSI_URL%%FALLBACK_QUERY_STRING%" "%LOCAL_DESTINATION_MSI_PATH%"
@echo !powershell_download!
@call !powershell_download!
@if NOT ERRORLEVEL 1 (
echo Download via PowerShell succeeded.
) else (
echo Failed to download "%REMOTE_SOURCE_MSI_URL%" with status code !ERRORLEVEL!. > "&2"
echo Exiting without bootstrapping due to download failure. > "&2"
exit /b 1
)
)
@echo Installing downloaded client package...
<%= install_chef %>
@if ERRORLEVEL 1 (
echo Chef-client package failed to install with status code !ERRORLEVEL!. > "&2"
echo See installation log for additional detail: %CHEF_CLIENT_MSI_LOG_PATH%. > "&2"
) else (
@echo Installation completed successfully
del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
)
@endlocal
@echo off
echo Writing validation key...
> <%= bootstrap_directory %>\validation.pem (
<%= validation_key %>
)
echo Validation key written.
@echo on
<% if @config[:encrypted_data_bag_secret] -%>
> <%= bootstrap_directory %>\encrypted_data_bag_secret (
<%= encrypted_data_bag_secret %>
)
<% end -%>
<% unless trusted_certs.empty? -%>
mkdir <%= bootstrap_directory %>\trusted_certs
<%= trusted_certs %>
<% end -%>
<%# Generate Ohai Hints -%>
<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
mkdir <%= bootstrap_directory %>\ohai\hints
<% @chef_config[:knife][:hints].each do |name, hash| -%>
> <%= bootstrap_directory %>\ohai\hints\<%= name %>.json (
<%= escape_and_echo(hash.to_json) %>
)
<% end -%>
<% end -%>
> <%= bootstrap_directory %>\client.rb (
<%= config_content %>
)
> <%= bootstrap_directory %>\first-boot.json (
<%= first_boot %>
)
@echo Starting chef to bootstrap the node...
<%= start_chef %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment