knife-windows 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -5
  3. data/.travis.yml +26 -26
  4. data/CHANGELOG.md +112 -108
  5. data/DOC_CHANGES.md +14 -14
  6. data/Gemfile +12 -12
  7. data/LICENSE +201 -201
  8. data/README.md +391 -385
  9. data/RELEASE_NOTES.md +34 -34
  10. data/Rakefile +21 -21
  11. data/appveyor.yml +42 -42
  12. data/ci.gemfile +15 -15
  13. data/features/knife_help.feature +20 -20
  14. data/features/support/env.rb +5 -5
  15. data/knife-windows.gemspec +25 -25
  16. data/lib/chef/knife/bootstrap/windows-chef-client-msi.erb +233 -247
  17. data/lib/chef/knife/bootstrap_windows_base.rb +449 -415
  18. data/lib/chef/knife/bootstrap_windows_ssh.rb +115 -115
  19. data/lib/chef/knife/bootstrap_windows_winrm.rb +95 -95
  20. data/lib/chef/knife/core/windows_bootstrap_context.rb +372 -366
  21. data/lib/chef/knife/knife_windows_base.rb +33 -33
  22. data/lib/chef/knife/windows_cert_generate.rb +155 -155
  23. data/lib/chef/knife/windows_cert_install.rb +68 -68
  24. data/lib/chef/knife/windows_helper.rb +36 -36
  25. data/lib/chef/knife/windows_listener_create.rb +107 -107
  26. data/lib/chef/knife/winrm.rb +122 -122
  27. data/lib/chef/knife/winrm_base.rb +117 -117
  28. data/lib/chef/knife/winrm_knife_base.rb +305 -303
  29. data/lib/chef/knife/winrm_session.rb +88 -87
  30. data/lib/chef/knife/winrm_shared_options.rb +47 -47
  31. data/lib/chef/knife/wsman_endpoint.rb +44 -44
  32. data/lib/chef/knife/wsman_test.rb +117 -117
  33. data/lib/knife-windows/path_helper.rb +234 -234
  34. data/lib/knife-windows/version.rb +6 -6
  35. data/spec/assets/win_template_rendered_with_bootstrap_install_command.txt +217 -217
  36. data/spec/assets/win_template_rendered_with_bootstrap_install_command_on_12_5_client.txt +217 -217
  37. data/spec/assets/win_template_rendered_without_bootstrap_install_command.txt +329 -329
  38. data/spec/assets/win_template_rendered_without_bootstrap_install_command_on_12_5_client.txt +329 -329
  39. data/spec/assets/win_template_unrendered.txt +246 -246
  40. data/spec/functional/bootstrap_download_spec.rb +241 -234
  41. data/spec/spec_helper.rb +94 -93
  42. data/spec/unit/knife/bootstrap_options_spec.rb +155 -155
  43. data/spec/unit/knife/bootstrap_template_spec.rb +98 -92
  44. data/spec/unit/knife/bootstrap_windows_winrm_spec.rb +341 -295
  45. data/spec/unit/knife/core/windows_bootstrap_context_spec.rb +177 -177
  46. data/spec/unit/knife/windows_cert_generate_spec.rb +90 -90
  47. data/spec/unit/knife/windows_cert_install_spec.rb +51 -51
  48. data/spec/unit/knife/windows_listener_create_spec.rb +76 -76
  49. data/spec/unit/knife/winrm_session_spec.rb +65 -65
  50. data/spec/unit/knife/winrm_spec.rb +516 -516
  51. data/spec/unit/knife/wsman_test_spec.rb +202 -202
  52. metadata +23 -4
@@ -1,329 +1,329 @@
1
- @rem
2
- @rem Author:: Seth Chisamore (<schisamo@opscode.com>)
3
- @rem Copyright:: Copyright (c) 2011 Opscode, Inc.
4
- @rem License:: Apache License, Version 2.0
5
- @rem
6
- @rem Licensed under the Apache License, Version 2.0 (the "License");
7
- @rem you may not use this file except in compliance with the License.
8
- @rem You may obtain a copy of the License at
9
- @rem
10
- @rem http://www.apache.org/licenses/LICENSE-2.0
11
- @rem
12
- @rem Unless required by applicable law or agreed to in writing, software
13
- @rem distributed under the License is distributed on an "AS IS" BASIS,
14
- @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- @rem See the License for the specific language governing permissions and
16
- @rem limitations under the License.
17
- @rem
18
-
19
- @rem Use delayed environment expansion so that ERRORLEVEL can be evaluated with the
20
- @rem !ERRORLEVEL! syntax which evaluates at execution of the line of script, not when
21
- @rem the line is read. See help for the /E switch from cmd.exe /? .
22
- @setlocal ENABLEDELAYEDEXPANSION
23
-
24
-
25
-
26
- @set BOOTSTRAP_DIRECTORY=C:\chef
27
- @echo Checking for existing directory "%BOOTSTRAP_DIRECTORY%"...
28
- @if NOT EXIST %BOOTSTRAP_DIRECTORY% (
29
- @echo Existing directory not found, creating.
30
- @mkdir %BOOTSTRAP_DIRECTORY%
31
- ) else (
32
- @echo Existing directory found, skipping creation.
33
- )
34
-
35
- > C:\chef\wget.vbs (
36
- echo.url = WScript.Arguments.Named^("url"^)
37
- echo.path = WScript.Arguments.Named^("path"^)
38
- echo.proxy = null
39
- echo.'* Vaguely attempt to handle file:// scheme urls by url unescaping and switching all
40
- echo.'* / into . Also assume that file:/// is a local absolute path and that file://^<foo^>
41
- echo.'* is possibly a network file path.
42
- echo.If InStr^(url, "file://"^) = 1 Then
43
- echo.url = Unescape^(url^)
44
- echo.If InStr^(url, "file:///"^) = 1 Then
45
- echo.sourcePath = Mid^(url, Len^("file:///"^) + 1^)
46
- echo.Else
47
- echo.sourcePath = Mid^(url, Len^("file:"^) + 1^)
48
- echo.End If
49
- echo.sourcePath = Replace^(sourcePath, "/", "\"^)
50
- echo.
51
- echo.Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
52
- echo.If objFSO.Fileexists^(path^) Then objFSO.DeleteFile path
53
- echo.objFSO.CopyFile sourcePath, path, true
54
- echo.Set objFSO = Nothing
55
- echo.
56
- echo.Else
57
- echo.Set objXMLHTTP = CreateObject^("MSXML2.ServerXMLHTTP"^)
58
- echo.Set wshShell = CreateObject^( "WScript.Shell" ^)
59
- echo.Set objUserVariables = wshShell.Environment^("USER"^)
60
- echo.
61
- echo.rem http proxy is optional
62
- echo.rem attempt to read from HTTP_PROXY env var first
63
- echo.On Error Resume Next
64
- echo.
65
- echo.If NOT ^(objUserVariables^("HTTP_PROXY"^) = ""^) Then
66
- echo.proxy = objUserVariables^("HTTP_PROXY"^)
67
- echo.
68
- echo.rem fall back to named arg
69
- echo.ElseIf NOT ^(WScript.Arguments.Named^("proxy"^) = ""^) Then
70
- echo.proxy = WScript.Arguments.Named^("proxy"^)
71
- echo.End If
72
- echo.
73
- echo.If NOT isNull^(proxy^) Then
74
- echo.rem setProxy method is only available on ServerXMLHTTP 6.0+
75
- echo.Set objXMLHTTP = CreateObject^("MSXML2.ServerXMLHTTP.6.0"^)
76
- echo.objXMLHTTP.setProxy 2, proxy
77
- echo.End If
78
- echo.
79
- echo.On Error Goto 0
80
- echo.
81
- echo.objXMLHTTP.open "GET", url, false
82
- echo.objXMLHTTP.send^(^)
83
- echo.If objXMLHTTP.Status = 200 Then
84
- echo.Set objADOStream = CreateObject^("ADODB.Stream"^)
85
- echo.objADOStream.Open
86
- echo.objADOStream.Type = 1
87
- echo.objADOStream.Write objXMLHTTP.ResponseBody
88
- echo.objADOStream.Position = 0
89
- echo.Set objFSO = Createobject^("Scripting.FileSystemObject"^)
90
- echo.If objFSO.Fileexists^(path^) Then objFSO.DeleteFile path
91
- echo.Set objFSO = Nothing
92
- echo.objADOStream.SaveToFile path
93
- echo.objADOStream.Close
94
- echo.Set objADOStream = Nothing
95
- echo.End If
96
- echo.Set objXMLHTTP = Nothing
97
- echo.End If
98
-
99
- )
100
-
101
- > C:\chef\wget.ps1 (
102
- echo.param^(
103
- echo. [String] $remoteUrl,
104
- echo. [String] $localPath
105
- echo.^)
106
- echo.
107
- echo.$webClient = new-object System.Net.WebClient;
108
- echo.
109
- echo.$webClient.DownloadFile^($remoteUrl, $localPath^);
110
-
111
- )
112
-
113
- @rem Determine the version and the architecture
114
-
115
- @FOR /F "usebackq tokens=1-8 delims=.[] " %%A IN (`ver`) DO (
116
- @set WinMajor=%%D
117
- @set WinMinor=%%E
118
- @set WinBuild=%%F
119
- )
120
-
121
- @echo Detected Windows Version %WinMajor%.%WinMinor% Build %WinBuild%
122
-
123
- @set LATEST_OS_VERSION_MAJOR=6
124
- @set LATEST_OS_VERSION_MINOR=3
125
-
126
- @if /i %WinMajor% GTR %LATEST_OS_VERSION_MAJOR% goto VersionUnknown
127
- @if /i %WinMajor% EQU %LATEST_OS_VERSION_MAJOR% (
128
- @if /i %WinMinor% GTR %LATEST_OS_VERSION_MINOR% goto VersionUnknown
129
- )
130
-
131
- goto Version%WinMajor%.%WinMinor%
132
-
133
- :VersionUnknown
134
- @rem If this is an unknown version of windows set the default
135
- @set MACHINE_OS=2008r2
136
- @echo Warning: Unknown version of Windows, assuming default of Windows %MACHINE_OS%
137
- goto architecture_select
138
-
139
- :Version6.0
140
- @set MACHINE_OS=2008
141
- goto architecture_select
142
-
143
- :Version5.2
144
- @set MACHINE_OS=2003r2
145
- goto architecture_select
146
-
147
- :Version6.1
148
- @set MACHINE_OS=2008r2
149
- goto architecture_select
150
-
151
- :Version6.2
152
- @set MACHINE_OS=2012
153
- goto architecture_select
154
-
155
- @rem Currently Windows Server 2012 R2 is treated as equivalent to Windows Server 2012
156
- :Version6.3
157
- goto Version6.2
158
-
159
- :architecture_select
160
- goto Architecture%PROCESSOR_ARCHITEW6432%
161
-
162
- :Architecture
163
- goto Architecture%PROCESSOR_ARCHITECTURE%
164
-
165
- @rem If this is an unknown architecture set the default
166
- @set MACHINE_ARCH=i686
167
- goto install
168
-
169
- :Architecturex86
170
- @set MACHINE_ARCH=i686
171
- goto install
172
-
173
- :Architectureamd64
174
- @set MACHINE_ARCH=x86_64
175
- goto install
176
-
177
- :install
178
- @rem If user has provided the custom installation command for chef-client then execute it
179
- @rem Install Chef using chef-client MSI installer
180
-
181
- @set "LOCAL_DESTINATION_MSI_PATH=%TEMP%\chef-client-latest.msi"
182
- @set "CHEF_CLIENT_MSI_LOG_PATH=%TEMP%\chef-client-msi%RANDOM%.log"
183
-
184
- @rem Clear any pre-existing downloads
185
- @echo Checking for existing downloaded package at "%LOCAL_DESTINATION_MSI_PATH%"
186
- @if EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
187
- @echo Found existing downloaded package, deleting.
188
- @del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
189
- @if ERRORLEVEL 1 (
190
- echo Warning: Failed to delete pre-existing package with status code !ERRORLEVEL! > "&2"
191
- )
192
- ) else (
193
- echo No existing downloaded packages to delete.
194
- )
195
-
196
- @rem If there is somehow a name collision, remove pre-existing log
197
- @if EXIST "%CHEF_CLIENT_MSI_LOG_PATH%" del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
198
-
199
- @echo Attempting to download client package using PowerShell if available...
200
- @set "REMOTE_SOURCE_MSI_URL=https://www.chef.io/chef/download?p=windows&pv=%MACHINE_OS%&m=%MACHINE_ARCH%&DownloadContext=PowerShell&v=12"
201
- @set powershell_download=powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File C:\chef\wget.ps1 "%REMOTE_SOURCE_MSI_URL%" "%LOCAL_DESTINATION_MSI_PATH%"
202
- @echo !powershell_download!
203
- @call !powershell_download!
204
-
205
- @set DOWNLOAD_ERROR_STATUS=!ERRORLEVEL!
206
-
207
- @if ERRORLEVEL 1 (
208
- @echo Failed PowerShell download with status code !DOWNLOAD_ERROR_STATUS! > "&2"
209
- @if !DOWNLOAD_ERROR_STATUS!==0 set DOWNLOAD_ERROR_STATUS=2
210
- ) else (
211
- @rem Sometimes the error level is not set even when the download failed,
212
- @rem so check for the file to be sure it is there -- if it is not, we will retry
213
- @if NOT EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
214
- echo Failed download: download completed, but downloaded file not found > "&2"
215
- set DOWNLOAD_ERROR_STATUS=2
216
- ) else (
217
- echo Download via PowerShell succeeded.
218
- )
219
- )
220
-
221
- @if NOT %DOWNLOAD_ERROR_STATUS%==0 (
222
- @echo Warning: Failed to download "%REMOTE_SOURCE_MSI_URL%" to "%LOCAL_DESTINATION_MSI_PATH%"
223
- @echo Warning: Retrying download with cscript ...
224
-
225
- @if EXIST "%LOCAL_DESTINATION_MSI_PATH%" del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
226
-
227
- @set "REMOTE_SOURCE_MSI_URL=https://www.chef.io/chef/download?p=windows&pv=%MACHINE_OS%&m=%MACHINE_ARCH%&v=12"
228
- cscript /nologo C:\chef\wget.vbs /url:"%REMOTE_SOURCE_MSI_URL%" /path:"%LOCAL_DESTINATION_MSI_PATH%"
229
-
230
- @if NOT ERRORLEVEL 1 (
231
- @rem Sometimes the error level is not set even when the download failed,
232
- @rem so check for the file to be sure it is there.
233
- @if NOT EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
234
- echo Failed download: download completed, but downloaded file not found > "&2"
235
- echo Exiting without bootstrapping due to download failure. > "&2"
236
- exit /b 1
237
- ) else (
238
- echo Download via cscript succeeded.
239
- )
240
- ) else (
241
- echo Failed to download "%REMOTE_SOURCE_MSI_URL%" with status code !ERRORLEVEL!. > "&2"
242
- echo Exiting without bootstrapping due to download failure. > "&2"
243
- exit /b 1
244
- )
245
- )
246
-
247
- @echo Installing downloaded client package...
248
-
249
- msiexec /qn /log "%CHEF_CLIENT_MSI_LOG_PATH%" /i "%LOCAL_DESTINATION_MSI_PATH%"
250
- @set MSIERRORCODE=!ERRORLEVEL!
251
- @if ERRORLEVEL 1 (
252
- @echo WARNING: Failed to install Chef Client MSI package in remote context with status code !MSIERRORCODE!.
253
- @echo WARNING: This may be due to a defect in operating system update KB2918614: http://support.microsoft.com/kb/2918614
254
- @set OLDLOGLOCATION="%CHEF_CLIENT_MSI_LOG_PATH%-fail.log"
255
- @move "%CHEF_CLIENT_MSI_LOG_PATH%" "!OLDLOGLOCATION!" > NUL
256
- @echo WARNING: Saving installation log of failure at !OLDLOGLOCATION!
257
- @echo WARNING: Retrying installation with local context...
258
- @schtasks /create /f /sc once /st 00:00:00 /tn chefclientbootstraptask /ru SYSTEM /rl HIGHEST /tr "cmd /c msiexec /qn /log '%CHEF_CLIENT_MSI_LOG_PATH%' /i '%LOCAL_DESTINATION_MSI_PATH%' & sleep 2 & waitfor /s %computername% /si chefclientinstalldone"
259
-
260
- @if ERRORLEVEL 1 (
261
- @echo ERROR: Failed to create Chef Client installation scheduled task with status code !ERRORLEVEL! > "&2"
262
- ) else (
263
- @echo Successfully created scheduled task to install Chef Client.
264
- @schtasks /run /tn chefclientbootstraptask
265
- @if ERRORLEVEL 1 (
266
- @echo ERROR: Failed to execut Chef Client installation scheduled task with status code !ERRORLEVEL!. > "&2"
267
- ) else (
268
- @echo Successfully started Chef Client installation scheduled task.
269
- @echo Waiting for installation to complete -- this may take a few minutes...
270
- waitfor chefclientinstalldone /t 600
271
- if ERRORLEVEL 1 (
272
- @echo ERROR: Timed out waiting for Chef Client package to install
273
- ) else (
274
- @echo Finished waiting for Chef Client package to install.
275
- )
276
- @schtasks /delete /f /tn chefclientbootstraptask > NUL
277
- )
278
- )
279
- ) else (
280
- @echo Successfully installed Chef Client package.
281
- )
282
-
283
-
284
- @if ERRORLEVEL 1 (
285
- echo Chef-client package failed to install with status code !ERRORLEVEL!. > "&2"
286
- echo See installation log for additional detail: %CHEF_CLIENT_MSI_LOG_PATH%. > "&2"
287
- ) else (
288
- @echo Installation completed successfully
289
- del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
290
- )
291
-
292
-
293
- @endlocal
294
-
295
- @echo off
296
-
297
-
298
- echo Writing validation key...
299
-
300
-
301
- echo Validation key written.
302
- @echo on
303
-
304
-
305
-
306
-
307
- > C:\chef\client.rb (
308
- echo.log_level :info
309
- echo.log_location STDOUT
310
- echo.
311
- echo.chef_server_url "https://localhost:443"
312
- echo.validation_client_name "chef-validator"
313
- echo.
314
- echo.file_cache_path "c:/chef/cache"
315
- echo.file_backup_path "c:/chef/backup"
316
- echo.cache_options ^({:path =^> "c:/chef/cache/checksums", :skip_expires =^> true}^)
317
- echo.
318
- echo.# Using default node name ^(fqdn^)
319
-
320
- )
321
-
322
- > C:\chef\first-boot.json (
323
- echo.{"run_list":null}
324
- )
325
-
326
- @echo Starting chef to bootstrap the node...
327
- SET "PATH=%PATH%;C:\ruby\bin;C:\opscode\chef\bin;C:\opscode\chef\embedded\bin"
328
- chef-client -c c:/chef/client.rb -j c:/chef/first-boot.json -E _default
329
-
1
+ @rem
2
+ @rem Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ @rem Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ @rem License:: Apache License, Version 2.0
5
+ @rem
6
+ @rem Licensed under the Apache License, Version 2.0 (the "License");
7
+ @rem you may not use this file except in compliance with the License.
8
+ @rem You may obtain a copy of the License at
9
+ @rem
10
+ @rem http://www.apache.org/licenses/LICENSE-2.0
11
+ @rem
12
+ @rem Unless required by applicable law or agreed to in writing, software
13
+ @rem distributed under the License is distributed on an "AS IS" BASIS,
14
+ @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ @rem See the License for the specific language governing permissions and
16
+ @rem limitations under the License.
17
+ @rem
18
+
19
+ @rem Use delayed environment expansion so that ERRORLEVEL can be evaluated with the
20
+ @rem !ERRORLEVEL! syntax which evaluates at execution of the line of script, not when
21
+ @rem the line is read. See help for the /E switch from cmd.exe /? .
22
+ @setlocal ENABLEDELAYEDEXPANSION
23
+
24
+
25
+
26
+ @set BOOTSTRAP_DIRECTORY=C:\chef
27
+ @echo Checking for existing directory "%BOOTSTRAP_DIRECTORY%"...
28
+ @if NOT EXIST %BOOTSTRAP_DIRECTORY% (
29
+ @echo Existing directory not found, creating.
30
+ @mkdir %BOOTSTRAP_DIRECTORY%
31
+ ) else (
32
+ @echo Existing directory found, skipping creation.
33
+ )
34
+
35
+ > C:\chef\wget.vbs (
36
+ echo.url = WScript.Arguments.Named^("url"^)
37
+ echo.path = WScript.Arguments.Named^("path"^)
38
+ echo.proxy = null
39
+ echo.'* Vaguely attempt to handle file:// scheme urls by url unescaping and switching all
40
+ echo.'* / into . Also assume that file:/// is a local absolute path and that file://^<foo^>
41
+ echo.'* is possibly a network file path.
42
+ echo.If InStr^(url, "file://"^) = 1 Then
43
+ echo.url = Unescape^(url^)
44
+ echo.If InStr^(url, "file:///"^) = 1 Then
45
+ echo.sourcePath = Mid^(url, Len^("file:///"^) + 1^)
46
+ echo.Else
47
+ echo.sourcePath = Mid^(url, Len^("file:"^) + 1^)
48
+ echo.End If
49
+ echo.sourcePath = Replace^(sourcePath, "/", "\"^)
50
+ echo.
51
+ echo.Set objFSO = CreateObject^("Scripting.FileSystemObject"^)
52
+ echo.If objFSO.Fileexists^(path^) Then objFSO.DeleteFile path
53
+ echo.objFSO.CopyFile sourcePath, path, true
54
+ echo.Set objFSO = Nothing
55
+ echo.
56
+ echo.Else
57
+ echo.Set objXMLHTTP = CreateObject^("MSXML2.ServerXMLHTTP"^)
58
+ echo.Set wshShell = CreateObject^( "WScript.Shell" ^)
59
+ echo.Set objUserVariables = wshShell.Environment^("USER"^)
60
+ echo.
61
+ echo.rem http proxy is optional
62
+ echo.rem attempt to read from HTTP_PROXY env var first
63
+ echo.On Error Resume Next
64
+ echo.
65
+ echo.If NOT ^(objUserVariables^("HTTP_PROXY"^) = ""^) Then
66
+ echo.proxy = objUserVariables^("HTTP_PROXY"^)
67
+ echo.
68
+ echo.rem fall back to named arg
69
+ echo.ElseIf NOT ^(WScript.Arguments.Named^("proxy"^) = ""^) Then
70
+ echo.proxy = WScript.Arguments.Named^("proxy"^)
71
+ echo.End If
72
+ echo.
73
+ echo.If NOT isNull^(proxy^) Then
74
+ echo.rem setProxy method is only available on ServerXMLHTTP 6.0+
75
+ echo.Set objXMLHTTP = CreateObject^("MSXML2.ServerXMLHTTP.6.0"^)
76
+ echo.objXMLHTTP.setProxy 2, proxy
77
+ echo.End If
78
+ echo.
79
+ echo.On Error Goto 0
80
+ echo.
81
+ echo.objXMLHTTP.open "GET", url, false
82
+ echo.objXMLHTTP.send^(^)
83
+ echo.If objXMLHTTP.Status = 200 Then
84
+ echo.Set objADOStream = CreateObject^("ADODB.Stream"^)
85
+ echo.objADOStream.Open
86
+ echo.objADOStream.Type = 1
87
+ echo.objADOStream.Write objXMLHTTP.ResponseBody
88
+ echo.objADOStream.Position = 0
89
+ echo.Set objFSO = Createobject^("Scripting.FileSystemObject"^)
90
+ echo.If objFSO.Fileexists^(path^) Then objFSO.DeleteFile path
91
+ echo.Set objFSO = Nothing
92
+ echo.objADOStream.SaveToFile path
93
+ echo.objADOStream.Close
94
+ echo.Set objADOStream = Nothing
95
+ echo.End If
96
+ echo.Set objXMLHTTP = Nothing
97
+ echo.End If
98
+
99
+ )
100
+
101
+ > C:\chef\wget.ps1 (
102
+ echo.param^(
103
+ echo. [String] $remoteUrl,
104
+ echo. [String] $localPath
105
+ echo.^)
106
+ echo.
107
+ echo.$webClient = new-object System.Net.WebClient;
108
+ echo.
109
+ echo.$webClient.DownloadFile^($remoteUrl, $localPath^);
110
+
111
+ )
112
+
113
+ @rem Determine the version and the architecture
114
+
115
+ @FOR /F "usebackq tokens=1-8 delims=.[] " %%A IN (`ver`) DO (
116
+ @set WinMajor=%%D
117
+ @set WinMinor=%%E
118
+ @set WinBuild=%%F
119
+ )
120
+
121
+ @echo Detected Windows Version %WinMajor%.%WinMinor% Build %WinBuild%
122
+
123
+ @set LATEST_OS_VERSION_MAJOR=6
124
+ @set LATEST_OS_VERSION_MINOR=3
125
+
126
+ @if /i %WinMajor% GTR %LATEST_OS_VERSION_MAJOR% goto VersionUnknown
127
+ @if /i %WinMajor% EQU %LATEST_OS_VERSION_MAJOR% (
128
+ @if /i %WinMinor% GTR %LATEST_OS_VERSION_MINOR% goto VersionUnknown
129
+ )
130
+
131
+ goto Version%WinMajor%.%WinMinor%
132
+
133
+ :VersionUnknown
134
+ @rem If this is an unknown version of windows set the default
135
+ @set MACHINE_OS=2008r2
136
+ @echo Warning: Unknown version of Windows, assuming default of Windows %MACHINE_OS%
137
+ goto architecture_select
138
+
139
+ :Version6.0
140
+ @set MACHINE_OS=2008
141
+ goto architecture_select
142
+
143
+ :Version5.2
144
+ @set MACHINE_OS=2003r2
145
+ goto architecture_select
146
+
147
+ :Version6.1
148
+ @set MACHINE_OS=2008r2
149
+ goto architecture_select
150
+
151
+ :Version6.2
152
+ @set MACHINE_OS=2012
153
+ goto architecture_select
154
+
155
+ @rem Currently Windows Server 2012 R2 is treated as equivalent to Windows Server 2012
156
+ :Version6.3
157
+ goto Version6.2
158
+
159
+ :architecture_select
160
+ goto Architecture%PROCESSOR_ARCHITEW6432%
161
+
162
+ :Architecture
163
+ goto Architecture%PROCESSOR_ARCHITECTURE%
164
+
165
+ @rem If this is an unknown architecture set the default
166
+ @set MACHINE_ARCH=i686
167
+ goto install
168
+
169
+ :Architecturex86
170
+ @set MACHINE_ARCH=i686
171
+ goto install
172
+
173
+ :Architectureamd64
174
+ @set MACHINE_ARCH=x86_64
175
+ goto install
176
+
177
+ :install
178
+ @rem If user has provided the custom installation command for chef-client then execute it
179
+ @rem Install Chef using chef-client MSI installer
180
+
181
+ @set "LOCAL_DESTINATION_MSI_PATH=%TEMP%\chef-client-latest.msi"
182
+ @set "CHEF_CLIENT_MSI_LOG_PATH=%TEMP%\chef-client-msi%RANDOM%.log"
183
+
184
+ @rem Clear any pre-existing downloads
185
+ @echo Checking for existing downloaded package at "%LOCAL_DESTINATION_MSI_PATH%"
186
+ @if EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
187
+ @echo Found existing downloaded package, deleting.
188
+ @del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
189
+ @if ERRORLEVEL 1 (
190
+ echo Warning: Failed to delete pre-existing package with status code !ERRORLEVEL! > "&2"
191
+ )
192
+ ) else (
193
+ echo No existing downloaded packages to delete.
194
+ )
195
+
196
+ @rem If there is somehow a name collision, remove pre-existing log
197
+ @if EXIST "%CHEF_CLIENT_MSI_LOG_PATH%" del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
198
+
199
+ @echo Attempting to download client package using PowerShell if available...
200
+ @set "REMOTE_SOURCE_MSI_URL=https://www.chef.io/chef/download?p=windows&pv=%MACHINE_OS%&m=%MACHINE_ARCH%&DownloadContext=PowerShell&v=12"
201
+ @set powershell_download=powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -File C:\chef\wget.ps1 "%REMOTE_SOURCE_MSI_URL%" "%LOCAL_DESTINATION_MSI_PATH%"
202
+ @echo !powershell_download!
203
+ @call !powershell_download!
204
+
205
+ @set DOWNLOAD_ERROR_STATUS=!ERRORLEVEL!
206
+
207
+ @if ERRORLEVEL 1 (
208
+ @echo Failed PowerShell download with status code !DOWNLOAD_ERROR_STATUS! > "&2"
209
+ @if !DOWNLOAD_ERROR_STATUS!==0 set DOWNLOAD_ERROR_STATUS=2
210
+ ) else (
211
+ @rem Sometimes the error level is not set even when the download failed,
212
+ @rem so check for the file to be sure it is there -- if it is not, we will retry
213
+ @if NOT EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
214
+ echo Failed download: download completed, but downloaded file not found > "&2"
215
+ set DOWNLOAD_ERROR_STATUS=2
216
+ ) else (
217
+ echo Download via PowerShell succeeded.
218
+ )
219
+ )
220
+
221
+ @if NOT %DOWNLOAD_ERROR_STATUS%==0 (
222
+ @echo Warning: Failed to download "%REMOTE_SOURCE_MSI_URL%" to "%LOCAL_DESTINATION_MSI_PATH%"
223
+ @echo Warning: Retrying download with cscript ...
224
+
225
+ @if EXIST "%LOCAL_DESTINATION_MSI_PATH%" del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
226
+
227
+ @set "REMOTE_SOURCE_MSI_URL=https://www.chef.io/chef/download?p=windows&pv=%MACHINE_OS%&m=%MACHINE_ARCH%&v=12"
228
+ cscript /nologo C:\chef\wget.vbs /url:"%REMOTE_SOURCE_MSI_URL%" /path:"%LOCAL_DESTINATION_MSI_PATH%"
229
+
230
+ @if NOT ERRORLEVEL 1 (
231
+ @rem Sometimes the error level is not set even when the download failed,
232
+ @rem so check for the file to be sure it is there.
233
+ @if NOT EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
234
+ echo Failed download: download completed, but downloaded file not found > "&2"
235
+ echo Exiting without bootstrapping due to download failure. > "&2"
236
+ exit /b 1
237
+ ) else (
238
+ echo Download via cscript succeeded.
239
+ )
240
+ ) else (
241
+ echo Failed to download "%REMOTE_SOURCE_MSI_URL%" with status code !ERRORLEVEL!. > "&2"
242
+ echo Exiting without bootstrapping due to download failure. > "&2"
243
+ exit /b 1
244
+ )
245
+ )
246
+
247
+ @echo Installing downloaded client package...
248
+
249
+ msiexec /qn /log "%CHEF_CLIENT_MSI_LOG_PATH%" /i "%LOCAL_DESTINATION_MSI_PATH%"
250
+ @set MSIERRORCODE=!ERRORLEVEL!
251
+ @if ERRORLEVEL 1 (
252
+ @echo WARNING: Failed to install Chef Client MSI package in remote context with status code !MSIERRORCODE!.
253
+ @echo WARNING: This may be due to a defect in operating system update KB2918614: http://support.microsoft.com/kb/2918614
254
+ @set OLDLOGLOCATION="%CHEF_CLIENT_MSI_LOG_PATH%-fail.log"
255
+ @move "%CHEF_CLIENT_MSI_LOG_PATH%" "!OLDLOGLOCATION!" > NUL
256
+ @echo WARNING: Saving installation log of failure at !OLDLOGLOCATION!
257
+ @echo WARNING: Retrying installation with local context...
258
+ @schtasks /create /f /sc once /st 00:00:00 /tn chefclientbootstraptask /ru SYSTEM /rl HIGHEST /tr "cmd /c msiexec /qn /log '%CHEF_CLIENT_MSI_LOG_PATH%' /i '%LOCAL_DESTINATION_MSI_PATH%' & sleep 2 & waitfor /s %computername% /si chefclientinstalldone"
259
+
260
+ @if ERRORLEVEL 1 (
261
+ @echo ERROR: Failed to create Chef Client installation scheduled task with status code !ERRORLEVEL! > "&2"
262
+ ) else (
263
+ @echo Successfully created scheduled task to install Chef Client.
264
+ @schtasks /run /tn chefclientbootstraptask
265
+ @if ERRORLEVEL 1 (
266
+ @echo ERROR: Failed to execut Chef Client installation scheduled task with status code !ERRORLEVEL!. > "&2"
267
+ ) else (
268
+ @echo Successfully started Chef Client installation scheduled task.
269
+ @echo Waiting for installation to complete -- this may take a few minutes...
270
+ waitfor chefclientinstalldone /t 600
271
+ if ERRORLEVEL 1 (
272
+ @echo ERROR: Timed out waiting for Chef Client package to install
273
+ ) else (
274
+ @echo Finished waiting for Chef Client package to install.
275
+ )
276
+ @schtasks /delete /f /tn chefclientbootstraptask > NUL
277
+ )
278
+ )
279
+ ) else (
280
+ @echo Successfully installed Chef Client package.
281
+ )
282
+
283
+
284
+ @if ERRORLEVEL 1 (
285
+ echo Chef-client package failed to install with status code !ERRORLEVEL!. > "&2"
286
+ echo See installation log for additional detail: %CHEF_CLIENT_MSI_LOG_PATH%. > "&2"
287
+ ) else (
288
+ @echo Installation completed successfully
289
+ del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
290
+ )
291
+
292
+
293
+ @endlocal
294
+
295
+ @echo off
296
+
297
+
298
+ echo Writing validation key...
299
+
300
+
301
+ echo Validation key written.
302
+ @echo on
303
+
304
+
305
+
306
+
307
+ > C:\chef\client.rb (
308
+ echo.log_level :info
309
+ echo.log_location STDOUT
310
+ echo.
311
+ echo.chef_server_url "https://localhost:443"
312
+ echo.validation_client_name "chef-validator"
313
+ echo.
314
+ echo.file_cache_path "c:/chef/cache"
315
+ echo.file_backup_path "c:/chef/backup"
316
+ echo.cache_options ^({:path =^> "c:/chef/cache/checksums", :skip_expires =^> true}^)
317
+ echo.
318
+ echo.# Using default node name ^(fqdn^)
319
+
320
+ )
321
+
322
+ > C:\chef\first-boot.json (
323
+ echo.{"run_list":null}
324
+ )
325
+
326
+ @echo Starting chef to bootstrap the node...
327
+ SET "PATH=%PATH%;C:\ruby\bin;C:\opscode\chef\bin;C:\opscode\chef\embedded\bin"
328
+ chef-client -c c:/chef/client.rb -j c:/chef/first-boot.json -E _default
329
+