knife-windows 0.5.14 → 0.6.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ N2UyYTQ5NDQwMTQ2Yzc2MTVkZDI3OGFiMTVjMjEyMmIxMmUxMWVkMw==
5
+ data.tar.gz: !binary |-
6
+ Nzk5OTk2NmYwYzBiNjQ3NGZiZmYyMjAxOGNjYmE1OWVhYzg4M2NiMw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzdlNDUwY2QyMGIwYzAzZmRlYjY0YmM4YjkxMmZjOGZkYjM0Yzg2YmQ5MWNh
10
+ NDkwOGFlODhiZDRlZDc1NjZjMjY3YTA4YTRiNjJkMDQ5YmFmY2I1MDVmZTNi
11
+ YTUyZGQ3M2Q4Njk4OWM0NGVhMDNhMTdlOTlmMjJjZjI4OGM0Y2M=
12
+ data.tar.gz: !binary |-
13
+ MDgzZWFlNzQ3OWI3MjNkOWEyZmNlNmYyYTM4NTA0ZjYzOTJmZjc4MTQwMGVi
14
+ ZWZkYTY2YmE5MmNhNThiYmY5MTZmMWM3NmM5Y2M2NmY3NGFlOTJhM2ExYjU1
15
+ NTBhZTUxYjVmNzEyYmY1OTRmYjA3MWIyNDhkYmNhNTg2ODYzMjY=
data/CHANGELOG.md ADDED
@@ -0,0 +1,33 @@
1
+ # knife-windows Change Log
2
+
3
+ ## Unreleased changes
4
+
5
+ None
6
+
7
+ ## Last Release: 0.6.0 (05/08/2014)
8
+
9
+ * [KNIFE-386](https://tickets.opscode.com/browse/KNIFE-386) Wait for a valid command response before bootstrapping over WinRM
10
+ * [KNIFE-394](https://tickets.opscode.com/browse/KNIFE-394) Update em-winrm dependency
11
+ * [KNIFE-450](https://tickets.opscode.com/browse/KNIFE-450) Set knife winrm command exit status on exception and command failure
12
+
13
+ **See source control commit history for earlier changes.**
14
+
15
+ ## Selected release notes
16
+ These are release notes from very early releases of the plugin. For recent
17
+ releases (2014 and later), see the RELEASE_NOTES.md file of each tagged release branch.
18
+
19
+ Release Notes - Knife Windows Plugin - Version 0.5.6
20
+
21
+ ** New Feature
22
+ * new default bootstrap template that installs Chef using official chef-client MSI installer
23
+
24
+ Release Notes - Knife Windows Plugin - Version 0.5.4
25
+
26
+ ** Bug
27
+ * [KNIFE\_WINDOWS-7] - Exception: NoMethodError: undefined method `env_namespace' for Savon:Module
28
+ * [KNIFE\_WINDOWS-8] - winrm based bootstrap fails with 'Bad HTTP response returned from server (500)'
29
+
30
+
31
+ ** New Feature
32
+ * [KNIFE\_WINDOWS-6] - default bootstrap template should support encrypted\_data\_bag\_secret
33
+
data/DOC_CHANGES.md ADDED
@@ -0,0 +1,42 @@
1
+ <!---
2
+ This file is reset every time a new release is done. This file describes changes that have not yet been released.
3
+
4
+ Example Doc Change:
5
+ ### Headline for the required change
6
+ Description of the required change.
7
+ -->
8
+
9
+ # knife-windows 0.6.0 doc changes:
10
+
11
+ ### `--auth-timeout` option for bootstrap
12
+ The `knife windows bootstrap` command has a new `--auth-timeout` option that
13
+ takes a number of minutes as an argument. After the command has detected that
14
+ the node to be bootstrapped has opened a port for the desired transport (e.g.
15
+ WinRM), the command then tries to successfully execute a remote test command
16
+ on the system to verify authentication and will retry for the number of minutes specified in the
17
+ `--auth-timeout` option if an authentication error is received. This is
18
+ particularly useful for nodes hosted by cloud providers that may open up ports before user
19
+ identities that can be authenticated are configured on the system, or if the
20
+ authentication subsystem's readiness lags that of transport readiness.
21
+
22
+ If the `--auth-timeout` option is not specified, the default retry period is
23
+ 25 minutes.
24
+
25
+ This value is currently only honored when bootstrapping using the WinRM
26
+ transport and is ignored when bootstrapping via ssh.
27
+
28
+ ### New configuration option for `knife winrm`: `:suppress_auth_failure`
29
+ The option `:suppress_auth_failure` may be configured in `knife.rb` to alter
30
+ the behavior of the `knife winrm` subcommand's behavior when authentication
31
+ fails:
32
+
33
+ * When this option is not specified (the default case) or is configured as `false`, the `knife winrm`
34
+ subcommand will return a process exit code of `100` if the command is able to
35
+ connect to the WinRM transport on the remote system but fails to authenticate.
36
+ * When this option is set to `true`, the exit status in the authentication
37
+ failure case is `0`.
38
+
39
+ The default behavior is retained for compatibility reasons. The ability to
40
+ override it via the `:suppress_auth_failure` option is useful for automation that uses the `knife winrm` subcommand
41
+ and needs to implement customized retry behavior when authentication fails.
42
+
data/README.rdoc CHANGED
@@ -69,20 +69,6 @@ This bootstrap template does the following:
69
69
 
70
70
  This is the default bootstrap template used by both the +windows+ +bootstrap+ subcommands.
71
71
 
72
- == windows-shell
73
-
74
- This bootstrap template does the following:
75
-
76
- * Installs Ruby 1.8.7 via the {Ruby Installer for Windows}[http://rubyinstaller.org/] which also includes the latest version of RubyGems
77
- * Installs the RubyInstaller Development Kit (DevKit). The RubyInstaller Development Kit (DevKit) is a MSYS/MinGW based toolkit than enables you to build many of the native C/C++ extensions available for Ruby.
78
- * Installs required Windows-related gems from RubyGems.org
79
- * Installs latest Chef version from RubyGems.org including {Ohai}[https://rubygems.org/gems/ohai] and {Chef}[https://rubygems.org/gems/chef]. The Chef version is configurable using the *--bootstrap-version* option.
80
- * Writes the validation.pem per the local knife configuration.
81
- * Writes a default config file for Chef (C:\chef\client.rb) using values from the +knife.rb+.
82
- * Creates a JSON attributes file containing the specified run list and run Chef.
83
-
84
- This should be considered a legacy bootstrap template and will most likely be removed in a future version.
85
-
86
72
  = REQUIREMENTS/SETUP:
87
73
 
88
74
  == Ruby
@@ -116,6 +102,14 @@ For development and testing purposes, unencrypted traffic with Basic authenticat
116
102
  C:\Users\Administrator> winrm set winrm/config/service @{AllowUnencrypted="true"}
117
103
  C:\Users\Administrator> winrm set winrm/config/service/auth @{Basic="true"}
118
104
 
105
+ === Troubleshooting
106
+
107
+ #### When I run the winrm command I get: "Error: Invalid use of command line. Type "winrm -?" for help."
108
+ - You're running the winrm command from powershell and you need to put the key/value pair in single quotes. For example:
109
+ ```
110
+ winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="512"}'
111
+ ```
112
+
119
113
  = CONTRIBUTING:
120
114
 
121
115
  Please file bugs against the KNIFE_WINDOWS project at http://tickets.opscode.com/browse/knife
data/RELEASE_NOTES.md ADDED
@@ -0,0 +1,40 @@
1
+ <!---
2
+ This file is reset every time a new release is done. The contents of this file are for the currently unreleased version.
3
+
4
+ Example Note:
5
+
6
+ ## Example Heading
7
+ Details about the thing that changed that needs to get included in the Release Notes in markdown.
8
+ -->
9
+ # knife-windows 0.6.0 release notes:
10
+ This release of knife-windows addresses issues with reliability of winrm
11
+ readiness detection during Windows bootstrap, exit codes in command execution
12
+ scenarios, and gem dependencies.
13
+
14
+ Special thanks to **Okezie Eze** for the helpful bug report in KNIFE-386.
15
+ Issues with `knife-windows` should be reported in the ticketing system at
16
+ https://tickets.opscode.com/browse/KNIFE. Learn more about how you can
17
+ contribute features and bug fixes to `knife-windows` at https://wiki.opscode.com/display/chef/How+to+Contribute.
18
+
19
+ ## Features added in knife-windows 0.6.0
20
+
21
+ ### `--auth-timeout` option for bootstrap
22
+ The `knife windows bootstrap` command has an `--auth-timeout` option that
23
+ takes as an argument a number of minutes that specifies the time period during
24
+ which to retry authentication attempts to WinRM when boostrapping a remote
25
+ node via WinRM. By default, the value is 25 minutes. This retry behavior during bootstrap addresses issues that arise on
26
+ some cloud providers where the authentication system is not ready until
27
+ some time after WinRM is ready.
28
+
29
+ ## knife-windows on RubyGems and Github
30
+ https://rubygems.org/gems/knife-windows
31
+ https://github.com/opscode/knife-windows
32
+
33
+ ## Issues fixed in knife-windows 0.6.0
34
+ * [KNIFE-386](https://tickets.opscode.com/browse/KNIFE-386) Wait for a valid command response before bootstrapping over WinRM
35
+ * [KNIFE-394](https://tickets.opscode.com/browse/KNIFE-394) Update em-winrm dependency
36
+ * [KNIFE-450](https://tickets.opscode.com/browse/KNIFE-450) Set knife winrm command exit status on exception and command failure
37
+
38
+ ## knife-windows breaking changes:
39
+
40
+ None.
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.description = s.summary
16
16
 
17
17
  s.required_ruby_version = ">= 1.9.1"
18
- s.add_dependency "em-winrm", "= 0.5.4"
18
+ s.add_dependency "em-winrm", "~> 0.5", ">= 0.5.4"
19
19
 
20
20
  s.files = `git ls-files`.split("\n")
21
21
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -42,7 +42,7 @@
42
42
 
43
43
  @rem Determine the version and the architecture
44
44
 
45
- @FOR /F "tokens=1-8 delims=.[] " %%A IN ('ver') DO (
45
+ @FOR /F "usebackq tokens=1-8 delims=.[] " %%A IN (`ver`) DO (
46
46
  @set WinMajor=%%D
47
47
  @set WinMinor=%%E
48
48
  @set WinBuild=%%F
@@ -82,7 +82,7 @@ goto architecture_select
82
82
  @set MACHINE_OS=2012
83
83
  goto architecture_select
84
84
 
85
- @rem Currently Windows Server 2012 R2 is treated as equivalent to Windows Server 2012
85
+ @rem Currently Windows Server 2012 R2 is treated as equivalent to Windows Server 2012
86
86
  :Version6.3
87
87
  goto Version6.2
88
88
 
@@ -126,7 +126,7 @@ goto install
126
126
  echo No existing downloaded packages to delete.
127
127
  )
128
128
 
129
- @rem If there's somehow a name collision, remove pre-existing log
129
+ @rem If there is somehow a name collision, remove pre-existing log
130
130
  @if EXIST "%CHEF_CLIENT_MSI_LOG_PATH%" del /f /q "%CHEF_CLIENT_MSI_LOG_PATH%"
131
131
 
132
132
  @echo Attempting to download client package using cscript...
@@ -141,7 +141,7 @@ cscript /nologo <%= bootstrap_directory %>\wget.vbs /url:"%REMOTE_SOURCE_MSI_URL
141
141
  @if !DOWNLOAD_ERROR_STATUS!==0 set DOWNLOAD_ERROR_STATUS=2
142
142
  ) else (
143
143
  @rem Sometimes the error level is not set even when the download failed,
144
- @rem so check for the file to be sure it is there -- if it's not, we'll retry
144
+ @rem so check for the file to be sure it is there -- if it is not, we will retry
145
145
  @if NOT EXIST "%LOCAL_DESTINATION_MSI_PATH%" (
146
146
  echo Failed download: download completed, but downloaded file not found > "&2"
147
147
  set DOWNLOAD_ERROR_STATUS=2
@@ -156,7 +156,7 @@ cscript /nologo <%= bootstrap_directory %>\wget.vbs /url:"%REMOTE_SOURCE_MSI_URL
156
156
 
157
157
  @if EXIST "%LOCAL_DESTINATION_MSI_PATH%" del /f /q "%LOCAL_DESTINATION_MSI_PATH%"
158
158
 
159
- @set powershell_download=powershell.exe -ExecutionPolicy Unrestricted -NoProfile -NonInteractive -command "& '<%= bootstrap_directory %>\wget.ps1' '%REMOTE_SOURCE_MSI_URL%%FALLBACK_QUERY_STRING%' '%LOCAL_DESTINATION_MSI_PATH%'"
159
+ @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%"
160
160
  @echo !powershell_download!
161
161
  @call !powershell_download!
162
162
  @if NOT ERRORLEVEL 1 (
@@ -198,6 +198,17 @@ echo Validation key written.
198
198
  )
199
199
  <% end -%>
200
200
 
201
+ <%# Generate Ohai Hints -%>
202
+ <% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
203
+ mkdir <%= bootstrap_directory %>\ohai\hints
204
+
205
+ <% @chef_config[:knife][:hints].each do |name, hash| -%>
206
+ > <%= bootstrap_directory %>\ohai\hints\<%= name %>.json (
207
+ <%= escape_and_echo(hash.to_json) %>
208
+ )
209
+ <% end -%>
210
+ <% end -%>
211
+
201
212
  > <%= bootstrap_directory %>\client.rb (
202
213
  <%= config_content %>
203
214
  )
@@ -83,7 +83,7 @@ class Chef
83
83
  :description => "A JSON string to be added to the first run of chef-client",
84
84
  :proc => lambda { |o| JSON.parse(o) },
85
85
  :default => {}
86
-
86
+
87
87
  option :encrypted_data_bag_secret,
88
88
  :short => "-s SECRET",
89
89
  :long => "--secret ",
@@ -94,6 +94,10 @@ class Chef
94
94
  :long => "--secret-file SECRET_FILE",
95
95
  :description => "A file containing the secret key to use to encrypt data bag item values. Will be rendered on the node at c:/chef/encrypted_data_bag_secret and set in the rendered client config."
96
96
 
97
+ option :auth_timeout,
98
+ :long => "--auth-timeout MINUTES",
99
+ :description => "The maximum time in minutes to wait to for authentication over the transport to the node to succeed. The default value is 25 minutes.",
100
+ :default => 25
97
101
  end
98
102
  end
99
103
 
@@ -135,7 +139,6 @@ class Chef
135
139
  end
136
140
 
137
141
  def bootstrap(proto=nil)
138
-
139
142
  validate_name_args!
140
143
 
141
144
  @node_name = Array(@name_args).first
@@ -144,12 +147,13 @@ class Chef
144
147
 
145
148
  STDOUT.sync = STDERR.sync = true
146
149
 
150
+ wait_for_remote_response( config[:auth_timeout].to_i )
147
151
  ui.info("Bootstrapping Chef on #{ui.color(@node_name, :bold)}")
148
152
  # create a bootstrap.bat file on the node
149
153
  # we have to run the remote commands in 2047 char chunks
150
154
  create_bootstrap_bat_command do |command_chunk, chunk_num|
151
155
  begin
152
- render_command_result = run_command("cmd.exe /C echo \"Rendering #{bootstrap_bat_file} chunk #{chunk_num}\" && #{command_chunk}")
156
+ render_command_result = run_command(%Q!cmd.exe /C echo "Rendering #{bootstrap_bat_file} chunk #{chunk_num}" && #{command_chunk}!)
153
157
  ui.error("Batch render command returned #{render_command_result}") if render_command_result != 0
154
158
  render_command_result
155
159
  rescue SystemExit => e
@@ -163,6 +167,12 @@ class Chef
163
167
  bootstrap_command_result
164
168
  end
165
169
 
170
+ protected
171
+
172
+ # Default implementation -- override only if required by the transport
173
+ def wait_for_remote_response(wait_max_minutes)
174
+ end
175
+
166
176
  def bootstrap_command
167
177
  @bootstrap_command ||= "cmd.exe /C #{bootstrap_bat_file}"
168
178
  end
@@ -53,9 +53,44 @@ class Chef
53
53
  winrm.config[:ca_trust_file] = Chef::Config[:knife][:ca_trust_file] if Chef::Config[:knife][:ca_trust_file]
54
54
  winrm.config[:manual] = true
55
55
  winrm.config[:winrm_port] = locate_config_value(:winrm_port)
56
+ winrm.config[:suppress_auth_failure] = true
57
+ winrm.config[:returns] = nil
56
58
  winrm.run
57
59
  end
58
60
 
61
+ protected
62
+
63
+ def wait_for_remote_response(wait_max_minutes)
64
+ wait_max_seconds = wait_max_minutes * 60
65
+ retry_interval_seconds = 10
66
+ retries_left = wait_max_seconds / retry_interval_seconds
67
+ print(ui.color("\nWaiting for remote response before bootstrap", :magenta))
68
+ wait_start_time = Time.now
69
+ begin
70
+ print(".")
71
+ # Return status of the command is non-zero, typically nil,
72
+ # for our simple echo command in cases where run_command
73
+ # swallows the exception, such as 401's. Treat such cases
74
+ # the same as the case where we encounter an exception.
75
+ status = run_command("echo . & echo Response received.")
76
+ raise RuntimeError, 'Command execution failed.' if status != 0
77
+ ui.info(ui.color("Remote node responded after #{elapsed_time_in_minutes(wait_start_time)} minutes.", :magenta))
78
+ return
79
+ rescue
80
+ retries_left -= 1
81
+ if retries_left <= 0 || (elapsed_time_in_minutes(wait_start_time) > wait_max_minutes)
82
+ ui.error("No response received from remote node after #{elapsed_time_in_minutes(wait_start_time)} minutes, giving up.")
83
+ raise
84
+ end
85
+ print '.'
86
+ sleep retry_interval_seconds
87
+ retry
88
+ end
89
+ end
90
+
91
+ def elapsed_time_in_minutes(start_time)
92
+ ((Time.now - start_time) / 60).round(2)
93
+ end
59
94
  end
60
95
  end
61
96
  end
@@ -93,20 +93,20 @@ Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
93
93
  Set wshShell = CreateObject( "WScript.Shell" )
94
94
  Set objUserVariables = wshShell.Environment("USER")
95
95
 
96
- 'http proxy is optional
97
- 'attempt to read from HTTP_PROXY env var first
96
+ rem http proxy is optional
97
+ rem attempt to read from HTTP_PROXY env var first
98
98
  On Error Resume Next
99
99
 
100
100
  If NOT (objUserVariables("HTTP_PROXY") = "") Then
101
101
  proxy = objUserVariables("HTTP_PROXY")
102
102
 
103
- 'fall back to named arg
103
+ rem fall back to named arg
104
104
  ElseIf NOT (WScript.Arguments.Named("proxy") = "") Then
105
105
  proxy = WScript.Arguments.Named("proxy")
106
106
  End If
107
107
 
108
108
  If NOT isNull(proxy) Then
109
- 'setProxy method is only available on ServerXMLHTTP 6.0+
109
+ rem setProxy method is only available on ServerXMLHTTP 6.0+
110
110
  Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
111
111
  objXMLHTTP.setProxy 2, proxy
112
112
  End If
@@ -140,7 +140,7 @@ param(
140
140
  [String] $localPath
141
141
  )
142
142
 
143
- $webClient = new-object System.Net.WebClient;
143
+ $webClient = new-object System.Net.WebClient;
144
144
 
145
145
  $webClient.DownloadFile($remoteUrl, $localPath);
146
146
  WGET_PS
@@ -161,9 +161,9 @@ WGET_PS
161
161
  end
162
162
 
163
163
  def first_boot
164
- first_boot_attributes_and_run_list = (@config[:first_boot_attributes] || {}).merge(:run_list => @run_list)
164
+ first_boot_attributes_and_run_list = (@config[:first_boot_attributes] || {}).merge(:run_list => @run_list)
165
165
  escape_and_echo(first_boot_attributes_and_run_list.to_json)
166
- end
166
+ end
167
167
 
168
168
  # escape WIN BATCH special chars
169
169
  # and prefixes each line with an
@@ -267,13 +267,23 @@ class Chef
267
267
  end
268
268
 
269
269
  session.close
270
+
271
+ # Knife seems to ignore the return value of this method,
272
+ # so we exit to force the process exit code for this
273
+ # subcommand if returns is set
274
+ exit @exit_code if @exit_code && @exit_code != 0
270
275
  @exit_code || 0
271
276
  end
272
277
  rescue WinRM::WinRMHTTPTransportError => e
273
278
  case e.message
274
279
  when /401/
275
- ui.error "Failed to authenticate to #{@name_args[0].split(" ")} as #{config[:winrm_user]}"
276
- ui.info "Response: #{e.message}"
280
+ if ! config[:suppress_auth_failure]
281
+ # Display errors if the caller hasn't opted to retry
282
+ ui.error "Failed to authenticate to #{@name_args[0].split(" ")} as #{config[:winrm_user]}"
283
+ ui.info "Response: #{e.message}"
284
+ raise e
285
+ end
286
+ @exit_code = 401
277
287
  else
278
288
  raise e
279
289
  end
@@ -18,6 +18,7 @@
18
18
 
19
19
  require 'chef/knife'
20
20
  require 'chef/encrypted_data_bag_item'
21
+ require 'kconv'
21
22
 
22
23
  class Chef
23
24
  class Knife
@@ -95,4 +96,4 @@ class Chef
95
96
 
96
97
  end
97
98
  end
98
- end
99
+ end
@@ -1,6 +1,6 @@
1
1
  module Knife
2
2
  module Windows
3
- VERSION = "0.5.14"
3
+ VERSION = "0.6.0"
4
4
  MAJOR, MINOR, TINY = VERSION.split('.')
5
5
  end
6
6
  end
@@ -64,7 +64,7 @@ describe 'Knife::Windows::Core msi download functionality for knife Windows winr
64
64
 
65
65
  describe "running on any version of the Windows OS", :windows_only do
66
66
  before do
67
- @mock_bootstrap_context = Chef::Knife::Core::WindowsBootstrapContext.new({ }, nil, { })
67
+ @mock_bootstrap_context = Chef::Knife::Core::WindowsBootstrapContext.new({ }, nil, { :knife => {} })
68
68
 
69
69
  # Stub the bootstrap context and prevent config related sections
70
70
  # from being populated, i.e. chef installation and first chef
@@ -89,9 +89,11 @@ describe 'Knife::Windows::Core msi download functionality for knife Windows winr
89
89
  end
90
90
 
91
91
  it "downloads the chef-client MSI during winrm bootstrap" do
92
+
92
93
  clean_test_case
93
94
 
94
95
  winrm_bootstrapper = Chef::Knife::BootstrapWindowsWinrm.new([ "127.0.0.1" ])
96
+ winrm_bootstrapper.stub(:wait_for_remote_response)
95
97
  winrm_bootstrapper.config[:template_file] = @template_file_path
96
98
 
97
99
  # Execute the commands locally that would normally be executed via WinRM
@@ -0,0 +1,85 @@
1
+ #
2
+ # Author:: Adam Edwards(<adamed@getchef.com>)
3
+ # Copyright:: Copyright (c) 2014 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'spec_helper'
20
+
21
+
22
+ describe Chef::Knife::BootstrapWindowsWinrm do
23
+ before(:all) do
24
+ Chef::Config.reset
25
+ end
26
+
27
+ before do
28
+ # Kernel.stub(:sleep).and_return 10
29
+ bootstrap.stub(:sleep).and_return 10
30
+ end
31
+
32
+ after do
33
+ # Kernel.unstub(:sleep)
34
+ bootstrap.stub(:sleep).and_return 10
35
+ end
36
+
37
+ let (:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new }
38
+ let(:initial_fail_count) { 4 }
39
+ it 'should retry if a 401 is received from WinRM' do
40
+ call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '401'}}
41
+ call_result_sequence.push(0)
42
+ bootstrap.stub(:run_command).and_return(*call_result_sequence)
43
+
44
+ bootstrap.should_receive(:run_command).exactly(call_result_sequence.length).times
45
+ bootstrap.send(:wait_for_remote_response, 2)
46
+ end
47
+
48
+ it 'should retry if something other than a 401 is received from WinRM' do
49
+ call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
50
+ call_result_sequence.push(0)
51
+ bootstrap.stub(:run_command).and_return(*call_result_sequence)
52
+
53
+ bootstrap.should_receive(:run_command).exactly(call_result_sequence.length).times
54
+ bootstrap.send(:wait_for_remote_response, 2)
55
+ end
56
+
57
+ it 'should have a wait timeout of 25 minutes by default' do
58
+ bootstrap.stub(:run_command).and_raise WinRM::WinRMHTTPTransportError
59
+ bootstrap.stub(:create_bootstrap_bat_command).and_raise SystemExit
60
+ bootstrap.should_receive(:wait_for_remote_response).with(25)
61
+ bootstrap.stub(:validate_name_args!).and_return(nil)
62
+ bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
63
+ expect { bootstrap.bootstrap }.to raise_error SystemExit
64
+ end
65
+
66
+ it 'should keep retrying at 10s intervals if the timeout in minutes has not elapsed' do
67
+ call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
68
+ call_result_sequence.push(0)
69
+ bootstrap.stub(:run_command).and_return(*call_result_sequence)
70
+
71
+ bootstrap.should_receive(:run_command).exactly(call_result_sequence.length).times
72
+ bootstrap.send(:wait_for_remote_response, 2)
73
+ end
74
+
75
+ it 'should stop retrying if more than 25 minutes has elapsed' do
76
+ times = [ Time.new(2014, 4, 1, 22, 25), Time.new(2014, 4, 1, 22, 51), Time.new(2014, 4, 1, 22, 52) ]
77
+ Time.stub(:now).and_return(*times)
78
+ run_command_result = lambda {raise WinRM::WinRMHTTPTransportError, '401'}
79
+ bootstrap.stub(:validate_name_args!).and_return(nil)
80
+ bootstrap.stub(:run_command).and_return(run_command_result)
81
+ bootstrap.should_receive(:run_command).exactly(1).times
82
+ bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
83
+ expect { bootstrap.bootstrap }.to raise_error RuntimeError
84
+ end
85
+ end
@@ -23,12 +23,12 @@ Chef::Knife::Winrm.load_deps
23
23
  describe Chef::Knife::Winrm do
24
24
  before(:all) do
25
25
  @original_config = Chef::Config.hash_dup
26
- @original_knife_config = Chef::Config[:knife].dup
26
+ @original_knife_config = Chef::Config[:knife].nil? ? nil : Chef::Config[:knife].dup
27
27
  end
28
28
 
29
29
  after(:all) do
30
30
  Chef::Config.configuration = @original_config
31
- Chef::Config[:knife] = @original_knife_config
31
+ Chef::Config[:knife] = @original_knife_config if @original_knife_config
32
32
  end
33
33
 
34
34
  before do
@@ -45,16 +45,16 @@ describe Chef::Knife::Winrm do
45
45
 
46
46
  describe "#configure_session" do
47
47
  before do
48
- @query = mock("Chef::Search::Query")
48
+ @query = double("Chef::Search::Query")
49
49
  end
50
50
 
51
51
  context "when there are some hosts found but they do not have an attribute to connect with" do
52
52
  before do
53
53
  @knife.config[:manual] = false
54
- @query.stub!(:search).and_return([[@node_foo, @node_bar]])
54
+ @query.stub(:search).and_return([[@node_foo, @node_bar]])
55
55
  @node_foo.automatic_attrs[:fqdn] = nil
56
56
  @node_bar.automatic_attrs[:fqdn] = nil
57
- Chef::Search::Query.stub!(:new).and_return(@query)
57
+ Chef::Search::Query.stub(:new).and_return(@query)
58
58
  end
59
59
 
60
60
  it "should raise a specific error (KNIFE-222)" do
@@ -67,16 +67,86 @@ describe Chef::Knife::Winrm do
67
67
  context "when there are nested attributes" do
68
68
  before do
69
69
  @knife.config[:manual] = false
70
- @query.stub!(:search).and_return([[@node_foo, @node_bar]])
71
- Chef::Search::Query.stub!(:new).and_return(@query)
70
+ @query.stub(:search).and_return([[@node_foo, @node_bar]])
71
+ Chef::Search::Query.stub(:new).and_return(@query)
72
72
  end
73
73
 
74
74
  it "should use nested attributes (KNIFE-276)" do
75
75
  @knife.config[:attribute] = "ec2.public_hostname"
76
- @knife.stub!(:session_from_list)
76
+ @knife.stub(:session_from_list)
77
77
  @knife.configure_session
78
78
 
79
79
  end
80
80
  end
81
+
82
+ describe Chef::Knife::Winrm do
83
+ context "when executing the run command which sets the process exit code" do
84
+ before(:each) do
85
+ Chef::Config[:knife] = {:winrm_transport => :http}
86
+ @winrm = Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', 'echo helloworld'])
87
+ end
88
+
89
+ after(:each) do
90
+ Chef::Config.configuration = @original_config
91
+ Chef::Config[:knife] = @original_knife_config if @original_knife_config
92
+ end
93
+
94
+ it "should return with 0 if the command succeeds" do
95
+ @winrm.stub(:winrm_command).and_return(0)
96
+ exit_code = @winrm.run
97
+ exit_code.should == 0
98
+ end
99
+
100
+ it "should exit the process with 0 if the command fails and returns config is not set" do
101
+ @winrm.stub(:winrm_command).and_return(1)
102
+ exit_code = @winrm.run
103
+ exit_code.should == 0
104
+ end
105
+
106
+ it "should exit the process with non-zero status if the command fails and returns config is set to 0" do
107
+ command_status = 1
108
+ @winrm.config[:returns] = [0,53]
109
+ Chef::Config[:knife][:returns] = [0,53]
110
+ @winrm.stub(:winrm_command).and_return(command_status)
111
+ EventMachine::WinRM::Session.any_instance.stub(:exit_codes).and_return({"thishost" => command_status})
112
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| e.status.should == command_status }
113
+ end
114
+
115
+ it "should exit the process with a zero status if the command returns an expected non-zero status" do
116
+ command_status = 53
117
+ Chef::Config[:knife][:returns] = [0,53]
118
+ @winrm.stub(:winrm_command).and_return(command_status)
119
+ EventMachine::WinRM::Session.any_instance.stub(:exit_codes).and_return({"thishost" => command_status})
120
+ exit_code = @winrm.run
121
+ exit_code.should == 0
122
+ end
123
+
124
+ it "should exit the process with a zero status if the command returns an expected non-zero status" do
125
+ command_status = 53
126
+ Chef::Config[:knife][:returns] = [0,53]
127
+ @winrm.stub(:winrm_command).and_return(command_status)
128
+ EventMachine::WinRM::Session.any_instance.stub(:exit_codes).and_return({"thishost" => command_status})
129
+ exit_code = @winrm.run
130
+ exit_code.should == 0
131
+ end
132
+
133
+ it "should exit the process with 100 if command execution raises an exception other than 401" do
134
+ @winrm.stub(:winrm_command).and_raise(WinRM::WinRMHTTPTransportError, '500')
135
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| e.status.should == 100 }
136
+ end
137
+
138
+ it "should exit the process with 100 if command execution raises a 401" do
139
+ @winrm.stub(:winrm_command).and_raise(WinRM::WinRMHTTPTransportError, '401')
140
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| e.status.should == 100 }
141
+ end
142
+
143
+ it "should exit the process with 0 if command execution raises a 401 and suppress_auth_failure is set to true" do
144
+ @winrm.config[:suppress_auth_failure] = true
145
+ @winrm.stub(:winrm_command).and_raise(WinRM::WinRMHTTPTransportError, '401')
146
+ exit_code = @winrm.run_with_pretty_exceptions
147
+ exit_code.should == 401
148
+ end
149
+ end
150
+ end
81
151
  end
82
152
  end
metadata CHANGED
@@ -1,30 +1,33 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-windows
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.14
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Seth Chisamore
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-07 00:00:00.000000000 Z
11
+ date: 2014-05-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: em-winrm
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - '='
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ - - ! '>='
20
21
  - !ruby/object:Gem::Version
21
22
  version: 0.5.4
22
23
  type: :runtime
23
24
  prerelease: false
24
25
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
26
  requirements:
27
- - - '='
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.5'
30
+ - - ! '>='
28
31
  - !ruby/object:Gem::Version
29
32
  version: 0.5.4
30
33
  description: Plugin that adds functionality to Chef's Knife CLI for configuring/interacting
@@ -40,16 +43,17 @@ files:
40
43
  - .gitignore
41
44
  - .rspec
42
45
  - .travis.yml
43
- - CHANGELOG
46
+ - CHANGELOG.md
47
+ - DOC_CHANGES.md
44
48
  - Gemfile
45
49
  - LICENSE
46
50
  - README.rdoc
51
+ - RELEASE_NOTES.md
47
52
  - Rakefile
48
53
  - features/knife_help.feature
49
54
  - features/support/env.rb
50
55
  - knife-windows.gemspec
51
56
  - lib/chef/knife/bootstrap/windows-chef-client-msi.erb
52
- - lib/chef/knife/bootstrap/windows-shell.erb
53
57
  - lib/chef/knife/bootstrap_windows_base.rb
54
58
  - lib/chef/knife/bootstrap_windows_ssh.rb
55
59
  - lib/chef/knife/bootstrap_windows_winrm.rb
@@ -61,30 +65,30 @@ files:
61
65
  - spec/functional/bootstrap_download_spec.rb
62
66
  - spec/spec_helper.rb
63
67
  - spec/unit/knife/bootstrap_template_spec.rb
68
+ - spec/unit/knife/bootstrap_windows_winrm_spec.rb
64
69
  - spec/unit/knife/winrm_spec.rb
65
70
  homepage: https://github.com/opscode/knife-windows
66
71
  licenses: []
72
+ metadata: {}
67
73
  post_install_message:
68
74
  rdoc_options: []
69
75
  require_paths:
70
76
  - lib
71
77
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
78
  requirements:
74
79
  - - ! '>='
75
80
  - !ruby/object:Gem::Version
76
81
  version: 1.9.1
77
82
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
83
  requirements:
80
84
  - - ! '>='
81
85
  - !ruby/object:Gem::Version
82
86
  version: '0'
83
87
  requirements: []
84
88
  rubyforge_project:
85
- rubygems_version: 1.8.23
89
+ rubygems_version: 2.1.11
86
90
  signing_key:
87
- specification_version: 3
91
+ specification_version: 4
88
92
  summary: Plugin that adds functionality to Chef's Knife CLI for configuring/interacting
89
93
  with nodes running Microsoft Windows
90
94
  test_files:
@@ -93,5 +97,6 @@ test_files:
93
97
  - spec/functional/bootstrap_download_spec.rb
94
98
  - spec/spec_helper.rb
95
99
  - spec/unit/knife/bootstrap_template_spec.rb
100
+ - spec/unit/knife/bootstrap_windows_winrm_spec.rb
96
101
  - spec/unit/knife/winrm_spec.rb
97
102
  has_rdoc: true
data/CHANGELOG DELETED
@@ -1,14 +0,0 @@
1
- Release Notes - Knife Windows Plugin - Version 0.5.6
2
-
3
- ** New Feature
4
- * new default bootstrap template that installs Chef using official chef-client MSI installer
5
-
6
- Release Notes - Knife Windows Plugin - Version 0.5.4
7
-
8
- ** Bug
9
- * [KNIFE_WINDOWS-7] - Exception: NoMethodError: undefined method `env_namespace' for Savon:Module
10
- * [KNIFE_WINDOWS-8] - winrm based bootstrap fails with 'Bad HTTP response returned from server (500)'
11
-
12
-
13
- ** New Feature
14
- * [KNIFE_WINDOWS-6] - default bootstrap template should support encrypted_data_bag_secret
@@ -1,68 +0,0 @@
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
- <%= "SETX HTTP_PROXY \"#{knife_config[:bootstrap_proxy]}\"" if knife_config[:bootstrap_proxy] %>
20
- mkdir C:\chef
21
-
22
- > C:\chef\wget.vbs (
23
- <%= win_wget %>
24
- )
25
-
26
- cscript /nologo C:\chef\wget.vbs /url:http://files.rubyforge.vm.bytemark.co.uk/rubyinstaller/rubyinstaller-1.8.7-p334.exe /path:%TEMP%\rubyinstaller.exe
27
- %TEMP%\rubyinstaller.exe /verysilent /dir="C:\ruby" /tasks="assocfiles,modpath"
28
-
29
- @rem Install the Ruby Dev Kit so we compile us some native gems
30
- cscript /nologo C:\chef\wget.vbs /url:http://cloud.github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.1-20101214-1400-sfx.exe /path:%TEMP%\rubydevkit.exe
31
- mkdir C:\DevKit
32
- copy %TEMP%\rubydevkit.exe C:\DevKit
33
- cmd.exe /C C:\DevKit\rubydevkit.exe -y
34
-
35
- @rem update path during bootstrap session
36
- SET PATH=%PATH%;C:\ruby\bin
37
-
38
- cmd.exe /C ruby c:/DevKit/dk.rb init
39
- cmd.exe /C ruby c:/DevKit/dk.rb install
40
-
41
- cmd.exe /C gem install win32-api win32-service --platform=mswin32
42
- cmd.exe /C gem install win32-open3 rdp-ruby-wmi windows-api windows-pr --no-rdoc --no-ri --verbose
43
-
44
- @rem Install Chef gems separately to prevent 'failed to allocate memory' errors
45
- cmd.exe /C gem install ohai --no-rdoc --no-ri --verbose
46
- cmd.exe /C gem install chef --no-rdoc --no-ri --verbose <%= bootstrap_version_string %>
47
-
48
- > C:\chef\validation.pem (
49
- <%= validation_key %>
50
- )
51
-
52
- <% if @config[:encrypted_data_bag_secret] -%>
53
- > C:\chef\encrypted_data_bag_secret (
54
- <%= encrypted_data_bag_secret %>
55
- )
56
- <% end -%>
57
-
58
- > C:\chef\client.rb (
59
- echo.require 'win32ole'
60
- echo.WIN32OLE.codepage = WIN32OLE::CP_UTF8
61
- <%= config_content %>
62
- )
63
-
64
- > C:\chef\first-boot.json (
65
- <%= run_list %>
66
- )
67
-
68
- <%= start_chef %>