Class: Chef::Knife::Core::WindowsBootstrapContext
Overview
Instances of BootstrapContext are the context objects (i.e., self
) for bootstrap templates. For backwards compatibility, they must
set the following instance variables:
Instance Attribute Summary collapse
#client_pem
Instance Method Summary
collapse
#bootstrap_environment, #client_d, #license_available?, #trusted_certs, #version_to_install
Constructor Details
#initialize(config, run_list, chef_config, secret = nil) ⇒ WindowsBootstrapContext
Returns a new instance of WindowsBootstrapContext.
37
38
39
40
41
42
43
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 37
def initialize(config, run_list, chef_config, secret = nil)
@config = config
@run_list = run_list
@chef_config = chef_config
@secret = secret
super(config, run_list, chef_config, secret)
end
|
Instance Attribute Details
#chef_config ⇒ Object
Returns the value of attribute chef_config.
34
35
36
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 34
def chef_config
@chef_config
end
|
#config ⇒ Object
Returns the value of attribute config.
33
34
35
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 33
def config
@config
end
|
#secret ⇒ Object
Returns the value of attribute secret.
35
36
37
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 35
def secret
@secret
end
|
Instance Method Details
#bootstrap_directory ⇒ Object
322
323
324
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 322
def bootstrap_directory
ChefConfig::Config.etc_chef_dir(windows: true)
end
|
#clean_etc_chef_file(path) ⇒ Object
314
315
316
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 314
def clean_etc_chef_file(path)
ChefConfig::PathHelper.cleanpath(etc_chef_file(path), windows: true)
end
|
#config_content ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 61
def config_content
client_rb = " chef_server_url \"\#{chef_config[:chef_server_url]}\"\n validation_client_name \"\#{chef_config[:validation_client_name]}\"\n file_cache_path \"\#{ChefConfig::PathHelper.escapepath(chef_config[:windows_bootstrap_file_cache_path] || \"\")}\"\n file_backup_path \"\#{ChefConfig::PathHelper.escapepath(chef_config[:windows_bootstrap_file_backup_path] || \"\")}\"\n cache_options ({:path => \"\#{ChefConfig::PathHelper.escapepath(ChefConfig::Config.etc_chef_dir(windows: true))}\\\\\\\\cache\\\\\\\\checksums\", :skip_expires => true})\n CONFIG\n\n unless chef_config[:chef_license].nil?\n client_rb << \"chef_license \\\"\#{chef_config[:chef_license]}\\\"\\n\"\n end\n\n if config[:chef_node_name]\n client_rb << %Q{node_name \"\#{config[:chef_node_name]}\"\\n}\n else\n client_rb << \"# Using default node name (fqdn)\\n\"\n end\n\n if chef_config[:config_log_level]\n client_rb << %Q{log_level :\#{chef_config[:config_log_level]}\\n}\n else\n client_rb << \"log_level :auto\\n\"\n end\n\n client_rb << \"log_location \#{get_log_location}\"\n\n # We configure :verify_api_cert only when it's overridden on the CLI\n # or when specified in the knife config.\n if !config[:node_verify_api_cert].nil? || config.key?(:verify_api_cert)\n value = config[:node_verify_api_cert].nil? ? config[:verify_api_cert] : config[:node_verify_api_cert]\n client_rb << %Q{verify_api_cert \#{value}\\n}\n end\n\n # We configure :ssl_verify_mode only when it's overridden on the CLI\n # or when specified in the knife config.\n if config[:node_ssl_verify_mode] || config.key?(:ssl_verify_mode)\n value = case config[:node_ssl_verify_mode]\n when \"peer\"\n :verify_peer\n when \"none\"\n :verify_none\n when nil\n config[:ssl_verify_mode]\n else\n nil\n end\n\n if value\n client_rb << %Q{ssl_verify_mode :\#{value}\\n}\n end\n end\n\n if config[:ssl_verify_mode]\n client_rb << %Q{ssl_verify_mode :\#{config[:ssl_verify_mode]}\\n}\n end\n\n if config[:bootstrap_proxy]\n client_rb << \"\\n\"\n client_rb << %Q{http_proxy \"\#{config[:bootstrap_proxy]}\"\\n}\n client_rb << %Q{https_proxy \"\#{config[:bootstrap_proxy]}\"\\n}\n client_rb << %Q{no_proxy \"\#{config[:bootstrap_no_proxy]}\"\\n} if config[:bootstrap_no_proxy]\n end\n\n if config[:bootstrap_no_proxy]\n client_rb << %Q{no_proxy \"\#{config[:bootstrap_no_proxy]}\"\\n}\n end\n\n if secret\n client_rb << %Q{encrypted_data_bag_secret \"\#{ChefConfig::PathHelper.escapepath(ChefConfig::Config.etc_chef_dir(windows: true))}\\\\\\\\encrypted_data_bag_secret\"\\n}\n end\n\n unless trusted_certs_script.empty?\n client_rb << %Q{trusted_certs_dir \"\#{ChefConfig::PathHelper.escapepath(ChefConfig::Config.etc_chef_dir(windows: true))}\\\\\\\\trusted_certs\"\\n}\n end\n\n if chef_config[:fips]\n client_rb << \"fips true\\n\"\n end\n\n escape_and_echo(client_rb)\nend\n"
|
#encrypted_data_bag_secret ⇒ Object
53
54
55
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 53
def encrypted_data_bag_secret
escape_and_echo(@secret)
end
|
#escape_and_echo(file_contents) ⇒ Object
escape WIN BATCH special chars and prefixes each line with an echo
354
355
356
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 354
def escape_and_echo(file_contents)
file_contents.gsub(/^(.*)$/, 'echo.\1').gsub(/([(<|>)^])/, '^\1')
end
|
#etc_chef_file(path) ⇒ Object
318
319
320
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 318
def etc_chef_file(path)
"#{bootstrap_directory}/#{path}"
end
|
#first_boot ⇒ Object
347
348
349
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 347
def first_boot
escape_and_echo(super.to_json)
end
|
#get_log_location ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 153
def get_log_location
if chef_config[:config_log_location].equal?(:win_evt)
%Q{:#{chef_config[:config_log_location]}\n}
elsif chef_config[:config_log_location].equal?(:syslog)
raise "syslog is not supported for log_location on Windows OS\n"
elsif chef_config[:config_log_location].equal?(STDOUT)
"STDOUT\n"
elsif chef_config[:config_log_location].equal?(STDERR)
"STDERR\n"
elsif chef_config[:config_log_location].nil? || chef_config[:config_log_location].empty?
"STDOUT\n"
elsif chef_config[:config_log_location]
%Q{"#{chef_config[:config_log_location]}"\n}
else
"STDOUT\n"
end
end
|
#install_chef ⇒ Object
308
309
310
311
312
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 308
def install_chef
install_command('"') + "\n" + fallback_install_task_command
end
|
#local_download_path ⇒ Object
326
327
328
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 326
def local_download_path
"%TEMP%\\#{ChefUtils::Dist::Infra::CLIENT}-latest.msi"
end
|
#msi_url(machine_os = nil, machine_arch = nil, download_context = nil) ⇒ Object
Build a URL that will redirect to the correct Chef Infra msi download.
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 331
def msi_url(machine_os = nil, machine_arch = nil, download_context = nil)
if config[:msi_url].nil? || config[:msi_url].empty?
url = if config[:license_id] && !config[:omnitruck_url].empty?
format(config[:omnitruck_url], config[:channel] + "/chef/download") + "&p=windows"
else
"https://omnitruck.chef.io/chef/download?p=windows&channel=#{config[:channel]}"
end
url += "&pv=#{machine_os}" unless machine_os.nil?
url += "&m=#{machine_arch}" unless machine_arch.nil?
url += "&DownloadContext=#{download_context}" unless download_context.nil?
url += "&v=#{version_to_install}"
else
config[:msi_url]
end
end
|
#start_chef ⇒ Object
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 171
def start_chef
c_opscode_dir = ChefConfig::PathHelper.cleanpath(ChefConfig::Config.c_opscode_dir, windows: true)
client_rb = clean_etc_chef_file("client.rb")
first_boot = clean_etc_chef_file("first-boot.json")
bootstrap_environment_option = bootstrap_environment.nil? ? "" : " -E #{bootstrap_environment}"
start_chef = "SET \"PATH=%SYSTEM32%;%SystemRoot%;%SYSTEM32%\\Wbem;%SYSTEM32%\\WindowsPowerShell\\v1.0\\;C:\\ruby\\bin;#{c_opscode_dir}\\bin;#{c_opscode_dir}\\embedded\\bin\;%PATH%\"\n"
start_chef << "#{ChefUtils::Dist::Infra::CLIENT} -c #{client_rb} -j #{first_boot}#{bootstrap_environment_option}\n"
end
|
#trusted_certs_script ⇒ Object
57
58
59
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 57
def trusted_certs_script
@trusted_certs_script ||= trusted_certs_content
end
|
#validation_key ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 45
def validation_key
if File.exist?(File.expand_path(chef_config[:validation_key]))
IO.read(File.expand_path(chef_config[:validation_key]))
else
false
end
end
|
#win_wget ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 182
def win_wget
win_wget = " url = WScript.Arguments.Named(\"url\")\n path = WScript.Arguments.Named(\"path\")\n proxy = null\n '* Vaguely attempt to handle file:// scheme urls by url unescaping and switching all\n '* / into \\. Also assume that file:/// is a local absolute path and that file://<foo>\n '* is possibly a network file path.\n If InStr(url, \"file://\") = 1 Then\n url = Unescape(url)\n If InStr(url, \"file:///\") = 1 Then\n sourcePath = Mid(url, Len(\"file:///\") + 1)\n Else\n sourcePath = Mid(url, Len(\"file:\") + 1)\n End If\n sourcePath = Replace(sourcePath, \"/\", \"\\\\\")\n\n Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n If objFSO.Fileexists(path) Then objFSO.DeleteFile path\n objFSO.CopyFile sourcePath, path, true\n Set objFSO = Nothing\n\n Else\n Set objXMLHTTP = CreateObject(\"MSXML2.ServerXMLHTTP\")\n Set wshShell = CreateObject( \"WScript.Shell\" )\n Set objUserVariables = wshShell.Environment(\"USER\")\n\n rem http proxy is optional\n rem attempt to read from HTTP_PROXY env var first\n On Error Resume Next\n\n If NOT (objUserVariables(\"HTTP_PROXY\") = \"\") Then\n proxy = objUserVariables(\"HTTP_PROXY\")\n\n rem fall back to named arg\n ElseIf NOT (WScript.Arguments.Named(\"proxy\") = \"\") Then\n proxy = WScript.Arguments.Named(\"proxy\")\n End If\n\n If NOT isNull(proxy) Then\n rem setProxy method is only available on ServerXMLHTTP 6.0+\n Set objXMLHTTP = CreateObject(\"MSXML2.ServerXMLHTTP.6.0\")\n objXMLHTTP.setProxy 2, proxy\n End If\n\n On Error Goto 0\n\n objXMLHTTP.open \"GET\", url, false\n objXMLHTTP.send()\n If objXMLHTTP.Status = 200 Then\n Set objADOStream = CreateObject(\"ADODB.Stream\")\n objADOStream.Open\n objADOStream.Type = 1\n objADOStream.Write objXMLHTTP.ResponseBody\n objADOStream.Position = 0\n Set objFSO = Createobject(\"Scripting.FileSystemObject\")\n If objFSO.Fileexists(path) Then objFSO.DeleteFile path\n Set objFSO = Nothing\n objADOStream.SaveToFile path\n objADOStream.Close\n Set objADOStream = Nothing\n ElseIf objXMLHTTP.Status = 400 Then\n errorBody = objXMLHTTP.ResponseText\n WScript.Echo \"Error: 400 BadRequest\"\n WScript.Echo \"Error Body:\"\n WScript.Echo errorBody\n Else\n WScript.Echo \"An error occurred while downloading the file:\"\n errorBody = objXMLHTTP.ResponseText\n WScript.Echo \"Status: \"\n WScript.Echo objXMLHTTP.Status\n WScript.Echo \"Status Text: \"\n WScript.Echo errorBody\n End If\n Set objXMLHTTP = Nothing\n End If\n WGET\n escape_and_echo(win_wget)\nend\n"
|
#win_wget_ps ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 264
def win_wget_ps
win_wget_ps = " param(\n [String] $remoteUrl,\n [String] $localPath\n )\n\n [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12\n\n $ProxyUrl = $env:http_proxy;\n $webClient = new-object System.Net.WebClient;\n\n if ($ProxyUrl -ne '') {\n $WebProxy = New-Object System.Net.WebProxy($ProxyUrl,$true)\n $WebClient.Proxy = $WebProxy\n }\n\n try {\n $webClient.DownloadFile($remoteUrl, $localPath);\n\n Write-Host \"Download complete. The file has been saved to $localPath.\"\n } catch [System.Net.WebException] {\n $response = $_.Exception.Response\n\n if ($response.StatusCode -eq [System.Net.HttpStatusCode]::BadRequest) {\n $streamReader = New-Object System.IO.StreamReader($response.GetResponseStream())\n $errorBody = $streamReader.ReadToEnd()\n $streamReader.Dispose()\n\n Write-Host \"Error: 400 BadRequest\"\n Write-Host \"Error Body:\"\n Write-Host $errorBody\n }\n else {\n Write-Host \"An error occurred while downloading the file:\"\n Write-Host $_.Exception.Message\n }\n Exit 1\n }\n WGET_PS\n\n escape_and_echo(win_wget_ps)\nend\n"
|