@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 

 

@set BOOTSTRAP_DIRECTORY=C:\chef 
@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. 
) 

> C:\chef\wget.vbs ( 
 echo.url = WScript.Arguments.Named^("url"^)
echo.path = WScript.Arguments.Named^("path"^)
echo.proxy = null
echo.'* Vaguely attempt to handle file:// scheme urls by url unescaping and switching all
echo.'* / into .  Also assume that file:/// is a local absolute path and that file://^<foo^>
echo.'* is possibly a network file path.
echo.If InStr^(url, "file://"^) = 1 Then
echo.url = Unescape^(url^)
echo.If InStr^(url, "file:///"^) = 1 Then
echo.sourcePath = Mid^(url, Len^("file:///"^) + 1^)
echo.Else
echo.sourcePath = Mid^(url, Len^("file:"^) + 1^)
echo.End If
echo.sourcePath = Replace^(sourcePath, "/", "\"^)
echo.
echo.Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
echo.If objFSO.Fileexists^(path^) Then objFSO.DeleteFile path
echo.objFSO.CopyFile sourcePath, path, true
echo.Set objFSO = Nothing
echo.
echo.Else
echo.Set objXMLHTTP = CreateObject^("MSXML2.ServerXMLHTTP"^)
echo.Set wshShell = CreateObject^( "WScript.Shell" ^)
echo.Set objUserVariables = wshShell.Environment^("USER"^)
echo.
echo.rem http proxy is optional
echo.rem attempt to read from HTTP_PROXY env var first
echo.On Error Resume Next
echo.
echo.If NOT ^(objUserVariables^("HTTP_PROXY"^) = ""^) Then
echo.proxy = objUserVariables^("HTTP_PROXY"^)
echo.
echo.rem fall back to named arg
echo.ElseIf NOT ^(WScript.Arguments.Named^("proxy"^) = ""^) Then
echo.proxy = WScript.Arguments.Named^("proxy"^)
echo.End If
echo.
echo.If NOT isNull^(proxy^) Then
echo.rem setProxy method is only available on ServerXMLHTTP 6.0+
echo.Set objXMLHTTP = CreateObject^("MSXML2.ServerXMLHTTP.6.0"^)
echo.objXMLHTTP.setProxy 2, proxy
echo.End If
echo.
echo.On Error Goto 0
echo.
echo.objXMLHTTP.open "GET", url, false
echo.objXMLHTTP.send^(^)
echo.If objXMLHTTP.Status = 200 Then
echo.Set objADOStream = CreateObject^("ADODB.Stream"^)
echo.objADOStream.Open
echo.objADOStream.Type = 1
echo.objADOStream.Write objXMLHTTP.ResponseBody
echo.objADOStream.Position = 0
echo.Set objFSO = Createobject^("Scripting.FileSystemObject"^)
echo.If objFSO.Fileexists^(path^) Then objFSO.DeleteFile path
echo.Set objFSO = Nothing
echo.objADOStream.SaveToFile path
echo.objADOStream.Close
echo.Set objADOStream = Nothing
echo.End If
echo.Set objXMLHTTP = Nothing
echo.End If
 
) 

> C:\chef\wget.ps1 ( 
 echo.param^(
echo.   [String] $remoteUrl,
echo.   [String] $localPath
echo.^)
echo.
echo.$webClient = new-object System.Net.WebClient;
echo.
echo.$webClient.DownloadFile^($remoteUrl, $localPath^);
 
) 

@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 If user has provided the custom installation command for chef-client then execute it 
  chef-client -o recipe[cbk1::rec2] 

@endlocal 

@echo off 


echo Writing validation key... 


echo Validation key written. 
@echo on 




> C:\chef\client.rb ( 
 echo.log_level        :info
echo.log_location     STDOUT
echo.
echo.chef_server_url  "https://localhost:443"
echo.validation_client_name "chef-validator"
echo.
echo.file_cache_path   "c:/chef/cache"
echo.file_backup_path  "c:/chef/backup"
echo.cache_options     ^({:path =^> "c:/chef/cache/checksums", :skip_expires =^> true}^)
echo.
echo.# Using default node name ^(fqdn^)
 
) 

> C:\chef\first-boot.json ( 
 echo.{"run_list":null} 
) 

@echo Starting chef to bootstrap the node... 
SET "PATH=%PATH%;C:\ruby\bin;C:\opscode\chef\bin;C:\opscode\chef\embedded\bin"
chef-client -c c:/chef/client.rb -j c:/chef/first-boot.json -E _default
 
