test-kitchen 1.4.0.beta.2 → 1.4.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -0
  3. data/features/kitchen_diagnose_command.feature +32 -0
  4. data/lib/kitchen/cli.rb +3 -0
  5. data/lib/kitchen/command/diagnose.rb +6 -1
  6. data/lib/kitchen/configurable.rb +48 -1
  7. data/lib/kitchen/diagnostic.rb +29 -0
  8. data/lib/kitchen/driver/base.rb +25 -0
  9. data/lib/kitchen/driver/dummy.rb +4 -0
  10. data/lib/kitchen/driver/proxy.rb +3 -0
  11. data/lib/kitchen/instance.rb +17 -0
  12. data/lib/kitchen/provisioner/base.rb +30 -1
  13. data/lib/kitchen/provisioner/chef_base.rb +7 -3
  14. data/lib/kitchen/provisioner/chef_solo.rb +4 -0
  15. data/lib/kitchen/provisioner/chef_zero.rb +5 -1
  16. data/lib/kitchen/provisioner/dummy.rb +4 -0
  17. data/lib/kitchen/provisioner/shell.rb +5 -0
  18. data/lib/kitchen/shell_out.rb +6 -2
  19. data/lib/kitchen/transport/base.rb +25 -0
  20. data/lib/kitchen/transport/dummy.rb +4 -0
  21. data/lib/kitchen/transport/ssh.rb +22 -1
  22. data/lib/kitchen/transport/winrm.rb +61 -90
  23. data/lib/kitchen/verifier/base.rb +30 -1
  24. data/lib/kitchen/verifier/busser.rb +4 -0
  25. data/lib/kitchen/verifier/dummy.rb +4 -0
  26. data/lib/kitchen/version.rb +1 -1
  27. data/spec/kitchen/configurable_spec.rb +35 -0
  28. data/spec/kitchen/diagnostic_spec.rb +53 -3
  29. data/spec/kitchen/driver/dummy_spec.rb +8 -0
  30. data/spec/kitchen/driver/proxy_spec.rb +4 -0
  31. data/spec/kitchen/driver/ssh_base_spec.rb +4 -0
  32. data/spec/kitchen/instance_spec.rb +75 -0
  33. data/spec/kitchen/provisioner/base_spec.rb +32 -6
  34. data/spec/kitchen/provisioner/chef_base_spec.rb +3 -2
  35. data/spec/kitchen/provisioner/chef_solo_spec.rb +10 -2
  36. data/spec/kitchen/provisioner/chef_zero_spec.rb +24 -2
  37. data/spec/kitchen/provisioner/dummy_spec.rb +8 -0
  38. data/spec/kitchen/provisioner/shell_spec.rb +10 -0
  39. data/spec/kitchen/shell_out_spec.rb +7 -0
  40. data/spec/kitchen/transport/ssh_spec.rb +90 -1
  41. data/spec/kitchen/transport/winrm_spec.rb +91 -11
  42. data/spec/kitchen/verifier/base_spec.rb +32 -6
  43. data/spec/kitchen/verifier/busser_spec.rb +8 -0
  44. data/spec/kitchen/verifier/dummy_spec.rb +8 -0
  45. data/support/chef_base_install_command.sh +183 -100
  46. data/test-kitchen.gemspec +1 -2
  47. metadata +11 -48
  48. data/lib/kitchen/transport/winrm/command_executor.rb +0 -188
  49. data/lib/kitchen/transport/winrm/file_transporter.rb +0 -454
  50. data/lib/kitchen/transport/winrm/logging.rb +0 -50
  51. data/lib/kitchen/transport/winrm/template.rb +0 -74
  52. data/lib/kitchen/transport/winrm/tmp_zip.rb +0 -187
  53. data/spec/kitchen/transport/winrm/command_executor_spec.rb +0 -400
  54. data/spec/kitchen/transport/winrm/file_transporter_spec.rb +0 -876
  55. data/spec/kitchen/transport/winrm/logging_spec.rb +0 -92
  56. data/spec/kitchen/transport/winrm/template_spec.rb +0 -51
  57. data/spec/kitchen/transport/winrm/tmp_zip_spec.rb +0 -132
  58. data/support/check_files.ps1.erb +0 -48
  59. data/support/decode_files.ps1.erb +0 -62
@@ -49,6 +49,16 @@ describe Kitchen::Provisioner::Shell do
49
49
  }.new(config).finalize_config!(instance)
50
50
  end
51
51
 
52
+ it "provisioner api_version is 2" do
53
+ provisioner = Kitchen::Provisioner::Shell.new(config).finalize_config!(instance)
54
+ provisioner.diagnose_plugin[:api_version].must_equal 2
55
+ end
56
+
57
+ it "plugin_version is set to Kitchen::VERSION" do
58
+ provisioner = Kitchen::Provisioner::Shell.new(config).finalize_config!(instance)
59
+ provisioner.diagnose_plugin[:version].must_equal Kitchen::VERSION
60
+ end
61
+
52
62
  describe "configuration" do
53
63
 
54
64
  describe "for bourne shells" do
@@ -112,6 +112,13 @@ describe Kitchen::ShellOut do
112
112
  subject.run_command("yo", :use_sudo => true)
113
113
  end
114
114
 
115
+ it "prepends with custom :sudo_command if :use_sudo is truthy" do
116
+ Mixlib::ShellOut.unstub(:new)
117
+ Mixlib::ShellOut.expects(:new).with("wat yo", opts).returns(command)
118
+
119
+ subject.run_command("yo", :use_sudo => true, :sudo_command => "wat")
120
+ end
121
+
115
122
  it "logs a debug BEGIN message" do
116
123
  subject.run_command("echo whoopa\ndoopa\ndo")
117
124
 
@@ -127,6 +127,14 @@ describe Kitchen::Transport::Ssh do
127
127
  Kitchen::Transport::Ssh.new(config).finalize_config!(instance)
128
128
  end
129
129
 
130
+ it "provisioner api_version is 1" do
131
+ transport.diagnose_plugin[:api_version].must_equal 1
132
+ end
133
+
134
+ it "plugin_version is set to Kitchen::VERSION" do
135
+ transport.diagnose_plugin[:version].must_equal Kitchen::VERSION
136
+ end
137
+
130
138
  describe "default_config" do
131
139
 
132
140
  it "sets :port to 22 by default" do
@@ -137,6 +145,33 @@ describe Kitchen::Transport::Ssh do
137
145
  transport[:username].must_equal "root"
138
146
  end
139
147
 
148
+ it "sets :compression to zlib by default" do
149
+ transport[:compression].must_equal "zlib"
150
+ end
151
+
152
+ it "sets :compression to none if set to none" do
153
+ config[:compression] = "none"
154
+
155
+ transport[:compression].must_equal "none"
156
+ end
157
+
158
+ it "raises a UserError if :compression is set to a bogus value" do
159
+ config[:compression] = "boom"
160
+
161
+ err = proc { transport }.must_raise Kitchen::UserError
162
+ err.message.must_match(%r{value may only be set to `none' or `zlib'})
163
+ end
164
+
165
+ it "sets :compression_level to 6 by default" do
166
+ transport[:compression_level].must_equal 6
167
+ end
168
+
169
+ it "sets :compression_level to 0 if :compression is set to none" do
170
+ config[:compression] = "none"
171
+
172
+ transport[:compression_level].must_equal 0
173
+ end
174
+
140
175
  it "sets :keepalive to true by default" do
141
176
  transport[:keepalive].must_equal true
142
177
  end
@@ -160,6 +195,17 @@ describe Kitchen::Transport::Ssh do
160
195
  it "sets :max_wait_until_ready to 600 by default" do
161
196
  transport[:max_wait_until_ready].must_equal 600
162
197
  end
198
+
199
+ it "sets :ssh_key to nil by default" do
200
+ transport[:ssh_key].must_equal nil
201
+ end
202
+
203
+ it "expands :ssh_path path if set" do
204
+ config[:kitchen_root] = "/rooty"
205
+ config[:ssh_key] = "my_key"
206
+
207
+ transport[:ssh_key].must_equal "/rooty/my_key"
208
+ end
163
209
  end
164
210
 
165
211
  describe "#connection" do
@@ -259,6 +305,48 @@ describe Kitchen::Transport::Ssh do
259
305
  make_connection
260
306
  end
261
307
 
308
+ it "sets :compression from config" do
309
+ config[:compression] = "none"
310
+
311
+ klass.expects(:new).with do |hash|
312
+ hash[:compression] == "none"
313
+ end
314
+
315
+ make_connection
316
+ end
317
+
318
+ it "sets :compression from state over config data" do
319
+ state[:compression] = "none"
320
+ config[:compression] = "zlib"
321
+
322
+ klass.expects(:new).with do |hash|
323
+ hash[:compression] == "none"
324
+ end
325
+
326
+ make_connection
327
+ end
328
+
329
+ it "sets :compression_level from config" do
330
+ config[:compression_level] = 9999
331
+
332
+ klass.expects(:new).with do |hash|
333
+ hash[:compression_level] == 9999
334
+ end
335
+
336
+ make_connection
337
+ end
338
+
339
+ it "sets :compression_level from state over config data" do
340
+ state[:compression_level] = 9999
341
+ config[:compression_level] = 1111
342
+
343
+ klass.expects(:new).with do |hash|
344
+ hash[:compression_level] == 9999
345
+ end
346
+
347
+ make_connection
348
+ end
349
+
262
350
  it "sets :timeout from :connection_timeout in config" do
263
351
  config[:connection_timeout] = "timeout_from_config"
264
352
 
@@ -407,10 +495,11 @@ describe Kitchen::Transport::Ssh do
407
495
  end
408
496
 
409
497
  it "sets :keys to an array if :ssh_key is set in config" do
498
+ config[:kitchen_root] = "/r"
410
499
  config[:ssh_key] = "ssh_key_from_config"
411
500
 
412
501
  klass.expects(:new).with do |hash|
413
- hash[:keys] == ["ssh_key_from_config"]
502
+ hash[:keys] == ["/r/ssh_key_from_config"]
414
503
  end
415
504
 
416
505
  make_connection
@@ -19,6 +19,10 @@
19
19
  require_relative "../../spec_helper"
20
20
 
21
21
  require "kitchen/transport/winrm"
22
+ require "winrm"
23
+ require "winrm/transport/command_executor"
24
+ require "winrm/transport/shell_closer"
25
+ require "winrm/transport/file_transporter"
22
26
 
23
27
  describe Kitchen::Transport::Winrm do
24
28
 
@@ -35,6 +39,14 @@ describe Kitchen::Transport::Winrm do
35
39
  Kitchen::Transport::Winrm.new(config).finalize_config!(instance)
36
40
  end
37
41
 
42
+ it "provisioner api_version is 1" do
43
+ transport.diagnose_plugin[:api_version].must_equal 1
44
+ end
45
+
46
+ it "plugin_version is set to Kitchen::VERSION" do
47
+ transport.diagnose_plugin[:version].must_equal Kitchen::VERSION
48
+ end
49
+
38
50
  describe "default_config" do
39
51
 
40
52
  it "sets :port to 5985 by default" do
@@ -345,9 +357,77 @@ describe Kitchen::Transport::Winrm do
345
357
  end
346
358
  end
347
359
 
360
+ describe "#load_needed_dependencies" do
361
+
362
+ before do
363
+ # force loading of winrm-transport to get the version constant
364
+ require "winrm/transport/version"
365
+ end
366
+
367
+ it "logs a message to debug that code will be loaded" do
368
+ transport
369
+
370
+ logged_output.string.must_match debug_line_with(
371
+ "Winrm Transport requested, loading WinRM::Transport gem")
372
+ end
373
+
374
+ it "logs a message to debug when library is initially loaded" do
375
+ transport = Kitchen::Transport::Winrm.new(config)
376
+ transport.stubs(:require)
377
+ transport.stubs(:require).with("winrm/transport/version").returns(true)
378
+ transport.finalize_config!(instance)
379
+
380
+ logged_output.string.must_match(
381
+ %r{^D, .* : WinRM::Transport [^\s]+ library loaded$}
382
+ )
383
+ end
384
+
385
+ it "logs a message to debug when library is previously loaded" do
386
+ transport = Kitchen::Transport::Winrm.new(config)
387
+ transport.stubs(:require)
388
+ transport.stubs(:require).with("winrm/transport/version").returns(false)
389
+ transport.finalize_config!(instance)
390
+
391
+ logged_output.string.must_match(
392
+ %r{^D, .* : WinRM::Transport [^\s]+ previously loaded$}
393
+ )
394
+ end
395
+
396
+ it "logs a message to fatal when libraries cannot be loaded" do
397
+ transport = Kitchen::Transport::Winrm.new(config)
398
+ transport.stubs(:require)
399
+ transport.stubs(:require).with("winrm/transport/version").
400
+ raises(LoadError, "uh oh")
401
+ begin
402
+ transport.finalize_config!(instance)
403
+ rescue # rubocop:disable Lint/HandleExceptions
404
+ # we are interested in the log output, not this exception
405
+ end
406
+
407
+ logged_output.string.must_match fatal_line_with(
408
+ "The `winrm-transport' gem is missing and must be installed")
409
+ end
410
+
411
+ it "raises a UserError when libraries cannot be loaded" do
412
+ transport = Kitchen::Transport::Winrm.new(config)
413
+ transport.stubs(:require)
414
+ transport.stubs(:require).with("winrm/transport/version").
415
+ raises(LoadError, "uh oh")
416
+
417
+ err = proc {
418
+ transport.finalize_config!(instance)
419
+ }.must_raise Kitchen::UserError
420
+ err.message.must_match(/^Could not load or activate WinRM::Transport /)
421
+ end
422
+ end
423
+
348
424
  def debug_line_with(msg)
349
425
  %r{^D, .* : #{Regexp.escape(msg)}}
350
426
  end
427
+
428
+ def fatal_line_with(msg)
429
+ %r{^F, .* : #{Regexp.escape(msg)}}
430
+ end
351
431
  end
352
432
 
353
433
  describe Kitchen::Transport::Winrm::Connection do
@@ -375,7 +455,7 @@ describe Kitchen::Transport::Winrm::Connection do
375
455
 
376
456
  let(:executor) do
377
457
  s = mock("command_executor")
378
- s.responds_like_instance_of(Kitchen::Transport::Winrm::CommandExecutor)
458
+ s.responds_like_instance_of(WinRM::Transport::CommandExecutor)
379
459
  s
380
460
  end
381
461
 
@@ -397,7 +477,7 @@ describe Kitchen::Transport::Winrm::Connection do
397
477
  end
398
478
 
399
479
  before do
400
- Kitchen::Transport::Winrm::CommandExecutor.stubs(:new).returns(executor)
480
+ WinRM::Transport::CommandExecutor.stubs(:new).returns(executor)
401
481
  # disable finalizer as service is a fake anyway
402
482
  ObjectSpace.stubs(:define_finalizer).
403
483
  with { |obj, _| obj.class == Kitchen::Transport::Winrm::Connection }
@@ -433,7 +513,7 @@ describe Kitchen::Transport::Winrm::Connection do
433
513
  describe "#execute" do
434
514
 
435
515
  before do
436
- Kitchen::Transport::Winrm::CommandExecutor.stubs(:new).returns(executor)
516
+ WinRM::Transport::CommandExecutor.stubs(:new).returns(executor)
437
517
  # disable finalizer as service is a fake anyway
438
518
  ObjectSpace.stubs(:define_finalizer).
439
519
  with { |obj, _| obj.class == Kitchen::Transport::Winrm::Connection }
@@ -871,7 +951,7 @@ MSG
871
951
 
872
952
  let(:transporter) do
873
953
  t = mock("file_transporter")
874
- t.responds_like_instance_of(Kitchen::Transport::Winrm::FileTransporter)
954
+ t.responds_like_instance_of(WinRM::Transport::FileTransporter)
875
955
  t
876
956
  end
877
957
 
@@ -880,10 +960,10 @@ MSG
880
960
  ObjectSpace.stubs(:define_finalizer).
881
961
  with { |obj, _| obj.class == Kitchen::Transport::Winrm::Connection }
882
962
 
883
- Kitchen::Transport::Winrm::CommandExecutor.stubs(:new).returns(executor)
963
+ WinRM::Transport::CommandExecutor.stubs(:new).returns(executor)
884
964
  executor.stubs(:open)
885
965
 
886
- Kitchen::Transport::Winrm::FileTransporter.stubs(:new).
966
+ WinRM::Transport::FileTransporter.stubs(:new).
887
967
  with(executor, logger).returns(transporter)
888
968
  transporter.stubs(:upload)
889
969
  end
@@ -891,18 +971,18 @@ MSG
891
971
  # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
892
972
  def self.common_specs_for_upload
893
973
  it "builds a Winrm::FileTransporter" do
894
- Kitchen::Transport::Winrm::FileTransporter.unstub(:new)
974
+ WinRM::Transport::FileTransporter.unstub(:new)
895
975
 
896
- Kitchen::Transport::Winrm::FileTransporter.expects(:new).
976
+ WinRM::Transport::FileTransporter.expects(:new).
897
977
  with(executor, logger).returns(transporter)
898
978
 
899
979
  upload
900
980
  end
901
981
 
902
982
  it "reuses the Winrm::FileTransporter" do
903
- Kitchen::Transport::Winrm::FileTransporter.unstub(:new)
983
+ WinRM::Transport::FileTransporter.unstub(:new)
904
984
 
905
- Kitchen::Transport::Winrm::FileTransporter.expects(:new).
985
+ WinRM::Transport::FileTransporter.expects(:new).
906
986
  with(executor, logger).returns(transporter).once
907
987
 
908
988
  upload
@@ -934,7 +1014,7 @@ MSG
934
1014
  describe "#wait_until_ready" do
935
1015
 
936
1016
  before do
937
- Kitchen::Transport::Winrm::CommandExecutor.stubs(:new).returns(executor)
1017
+ WinRM::Transport::CommandExecutor.stubs(:new).returns(executor)
938
1018
  # disable finalizer as service is a fake anyway
939
1019
  ObjectSpace.stubs(:define_finalizer).
940
1020
  with { |obj, _| obj.class == Kitchen::Transport::Winrm::Connection }
@@ -102,6 +102,10 @@ describe Kitchen::Verifier::Base do
102
102
  verifier[:sudo].must_equal true
103
103
  end
104
104
 
105
+ it ":sudo_command defaults to sudo -E" do
106
+ verifier[:sudo_command].must_equal "sudo -E"
107
+ end
108
+
105
109
  it ":root_path defaults to '/tmp/verifier'" do
106
110
  verifier[:root_path].must_equal "/tmp/verifier"
107
111
  end
@@ -115,6 +119,10 @@ describe Kitchen::Verifier::Base do
115
119
  verifier[:sudo].must_equal nil
116
120
  end
117
121
 
122
+ it ":sudo_command defaults to nil" do
123
+ verifier[:sudo_command].must_equal nil
124
+ end
125
+
118
126
  it ":root_path defaults to $env:TEMP\\verifier" do
119
127
  verifier[:root_path].must_equal "$env:TEMP\\verifier"
120
128
  end
@@ -295,16 +303,34 @@ describe Kitchen::Verifier::Base do
295
303
 
296
304
  describe "#sudo" do
297
305
 
298
- it "if :sudo is set, prepend sudo command" do
299
- config[:sudo] = true
306
+ describe "with :sudo set" do
307
+
308
+ before { config[:sudo] = true }
309
+
310
+ it "prepends sudo command" do
311
+ verifier.send(:sudo, "wakka").must_equal("sudo -E wakka")
312
+ end
313
+
314
+ it "customizes sudo when :sudo_command is set" do
315
+ config[:sudo_command] = "blueto -Ohai"
300
316
 
301
- verifier.send(:sudo, "wakka").must_equal("sudo -E wakka")
317
+ verifier.send(:sudo, "wakka").must_equal("blueto -Ohai wakka")
318
+ end
302
319
  end
303
320
 
304
- it "if :sudo is falsy, do not include sudo command" do
305
- config[:sudo] = false
321
+ describe "with :sudo falsey" do
306
322
 
307
- verifier.send(:sudo, "wakka").must_equal("wakka")
323
+ before { config[:sudo] = false }
324
+
325
+ it "does not include sudo command" do
326
+ verifier.send(:sudo, "wakka").must_equal("wakka")
327
+ end
328
+
329
+ it "does not include sudo command, even when :sudo_command is set" do
330
+ config[:sudo_command] = "blueto -Ohai"
331
+
332
+ verifier.send(:sudo, "wakka").must_equal("wakka")
333
+ end
308
334
  end
309
335
  end
310
336
  end
@@ -89,6 +89,14 @@ describe Kitchen::Verifier::Busser do
89
89
  # }.must_raise Kitchen::UserError
90
90
  # end
91
91
 
92
+ it "verifier api_version is 1" do
93
+ verifier.diagnose_plugin[:api_version].must_equal 1
94
+ end
95
+
96
+ it "plugin_version is set to Kitchen::VERSION" do
97
+ verifier.diagnose_plugin[:version].must_equal Kitchen::VERSION
98
+ end
99
+
92
100
  describe "configuration" do
93
101
 
94
102
  describe "for unix operating systems" do
@@ -49,6 +49,14 @@ describe Kitchen::Verifier::Dummy do
49
49
  Kitchen::Verifier::Dummy.new(config).finalize_config!(instance)
50
50
  end
51
51
 
52
+ it "verifier api_version is 1" do
53
+ verifier.diagnose_plugin[:api_version].must_equal 1
54
+ end
55
+
56
+ it "plugin_version is set to Kitchen::VERSION" do
57
+ verifier.diagnose_plugin[:version].must_equal Kitchen::VERSION
58
+ end
59
+
52
60
  describe "configuration" do
53
61
 
54
62
  it "sets :sleep to 0 by default" do
@@ -1,137 +1,220 @@
1
- # Check whether a command exists - returns 0 if it does, 1 if it does not
2
- exists() {
3
- if command -v $1 >/dev/null 2>&1
4
- then
5
- return 0
6
- else
7
- return 1
1
+ tmp_stderr="/tmp/stderr";
2
+
3
+ # capture_tmp_stderr SOURCE
4
+ capture_tmp_stderr() {
5
+ # spool up $tmp_stderr from all the commands we called
6
+ if test -f "$tmp_stderr"; then
7
+ output="`cat $tmp_stderr`";
8
+ stderr_results="${stderr_results}\nSTDERR from $1:\n\n${output}\n";
9
+ rm $tmp_stderr;
8
10
  fi
9
11
  }
10
12
 
11
- # do_wget URL FILENAME
12
- do_wget() {
13
- echo "trying wget..."
14
- wget -O "$2" "$1" 2>/tmp/stderr
15
- # check for bad return status
16
- test $? -ne 0 && return 1
17
- # check for 404 or empty file
18
- grep "ERROR 404" /tmp/stderr 2>&1 >/dev/null
19
- if test $? -eq 0 || test ! -s "$2"; then
20
- return 1
21
- fi
22
- return 0
23
- }
24
-
25
13
  # do_curl URL FILENAME
26
14
  do_curl() {
27
- echo "trying curl..."
28
- curl -L "$1" > "$2"
29
- # check for bad return status
30
- [ $? -ne 0 ] && return 1
31
- # check for bad output or empty file
32
- grep "The specified key does not exist." "$2" 2>&1 >/dev/null
33
- if test $? -eq 0 || test ! -s "$2"; then
34
- return 1
35
- fi
36
- return 0
15
+ echo "Trying curl...";
16
+ curl -sL -D "$tmp_stderr" "$1" > "$2";
17
+ ec=$?;
18
+ # check for 404
19
+ grep "404 Not Found" "$tmp_stderr" 2>&1 >/dev/null;
20
+ if test $? -eq 0; then
21
+ http_404_error "$1";
22
+ fi
23
+
24
+ # check for bad return status or empty output
25
+ if test $ec -ne 0 || test ! -s "$2"; then
26
+ capture_tmp_stderr "curl";
27
+ return 1;
28
+ else
29
+ echo "Download complete.";
30
+ return 0;
31
+ fi
32
+ }
33
+
34
+ # do_download URL FILENAME
35
+ do_download() {
36
+ echo "Downloading ${1} to file ${2}";
37
+
38
+ exists wget;
39
+ if test $? -eq 0; then
40
+ do_wget "$1" "$2" && return 0;
41
+ fi
42
+
43
+ exists curl;
44
+ if test $? -eq 0; then
45
+ do_curl "$1" "$2" && return 0;
46
+ fi
47
+
48
+ exists fetch;
49
+ if test $? -eq 0; then
50
+ do_fetch "$1" "$2" && return 0;
51
+ fi
52
+
53
+ exists python;
54
+ if test $? -eq 0; then
55
+ do_python "$1" "$2" && return 0;
56
+ fi
57
+
58
+ exists perl;
59
+ if test $? -eq 0; then
60
+ do_perl "$1" "$2" && return 0;
61
+ fi
62
+
63
+ unable_to_download "$1" "$2";
37
64
  }
38
65
 
39
66
  # do_fetch URL FILENAME
40
67
  do_fetch() {
41
- echo "trying fetch..."
42
- fetch -o "$2" "$1" 2>/tmp/stderr
43
- # check for bad return status
44
- test $? -ne 0 && return 1
45
- return 0
68
+ echo "Trying fetch...";
69
+ fetch -o "$2" "$1" 2>"$tmp_stderr";
70
+ ec=$?;
71
+ # check for 404
72
+ grep "Not Found" "$tmp_stderr" 2>&1 >/dev/null;
73
+ if test $? -eq 0; then
74
+ http_404_error "$1";
75
+ fi
76
+
77
+ # check for bad return status or empty output
78
+ if test $ec -ne 0 || test ! -s "$2"; then
79
+ capture_tmp_stderr "fetch";
80
+ return 1;
81
+ else
82
+ echo "Download complete.";
83
+ return 0;
84
+ fi
46
85
  }
47
86
 
48
87
  # do_perl URL FILENAME
49
88
  do_perl() {
50
- echo "trying perl..."
51
- perl -e "use LWP::Simple; getprint($ARGV[0]);" "$1" > "$2"
52
- # check for bad return status
53
- test $? -ne 0 && return 1
54
- # check for bad output or empty file
55
- # grep "The specified key does not exist." "$2" 2>&1 >/dev/null
56
- # if test $? -eq 0 || test ! -s "$2"; then
57
- # unable_to_retrieve_package
58
- # fi
59
- return 0
89
+ echo "Trying perl...";
90
+ perl -e "use LWP::Simple; getprint(\$ARGV[0]);" "$1" > "$2" 2>"$tmp_stderr";
91
+ ec=$?;
92
+ # check for 404
93
+ grep "404 Not Found" "$tmp_stderr" 2>&1 >/dev/null;
94
+ if test $? -eq 0; then
95
+ http_404_error "$1";
96
+ fi
97
+
98
+ # check for bad return status or empty output
99
+ if test $ec -ne 0 || test ! -s "$2"; then
100
+ capture_tmp_stderr "perl";
101
+ return 1;
102
+ else
103
+ echo "Download complete.";
104
+ return 0;
105
+ fi
60
106
  }
61
107
 
62
108
  # do_python URL FILENAME
63
109
  do_python() {
64
- echo "trying python..."
65
- python -c "import sys,urllib2 ; sys.stdout.write(urllib2.urlopen(sys.argv[1]).read())" "$1" > "$2"
66
- # check for bad return status
67
- test $? -ne 0 && return 1
68
- # check for bad output or empty file
69
- #grep "The specified key does not exist." "$2" 2>&1 >/dev/null
70
- #if test $? -eq 0 || test ! -s "$2"; then
71
- # unable_to_retrieve_package
72
- #fi
73
- return 0
74
- }
75
-
76
- # do_download URL FILENAME
77
- do_download() {
78
- PATH=/opt/local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
79
- export PATH
80
-
81
- echo "downloading $1"
82
- echo " to file $2"
83
-
84
- # we try all of these until we get success.
85
- # perl, in particular may be present but LWP::Simple may not be installed
86
-
87
- if exists wget; then
88
- do_wget $1 $2 && return 0
110
+ echo "Trying python...";
111
+ python -c "import sys,urllib2 ; sys.stdout.write(urllib2.urlopen(sys.argv[1]).read())" "$1" > "$2" 2>"$tmp_stderr";
112
+ ec=$?;
113
+ # check for 404
114
+ grep "HTTP Error 404" "$tmp_stderr" 2>&1 >/dev/null;
115
+ if test $? -eq 0; then
116
+ http_404_error "$1";
89
117
  fi
90
118
 
91
- if exists curl; then
92
- do_curl $1 $2 && return 0
119
+ # check for bad return status or empty output
120
+ if test $ec -ne 0 || test ! -s "$2"; then
121
+ capture_tmp_stderr "python";
122
+ return 1;
123
+ else
124
+ echo "Download complete.";
125
+ return 0;
93
126
  fi
127
+ }
94
128
 
95
- if exists fetch; then
96
- do_fetch $1 $2 && return 0
129
+ # do_wget URL FILENAME
130
+ do_wget() {
131
+ echo "Trying wget...";
132
+ wget -O "$2" "$1" 2>"$tmp_stderr";
133
+ ec=$?;
134
+ # check for 404
135
+ grep "ERROR 404" "$tmp_stderr" 2>&1 >/dev/null;
136
+ if test $? -eq 0; then
137
+ http_404_error "$1";
97
138
  fi
98
139
 
99
- if exists perl; then
100
- do_perl $1 $2 && return 0
140
+ # check for bad return status or empty output
141
+ if test $ec -ne 0 || test ! -s "$2"; then
142
+ capture_tmp_stderr "wget";
143
+ return 1;
144
+ else
145
+ echo "Download complete.";
146
+ return 0;
101
147
  fi
148
+ }
102
149
 
103
- if exists python; then
104
- do_python $1 $2 && return 0
150
+ # exists COMMAND
151
+ exists() {
152
+ if command -v "$1" >/dev/null 2>&1; then
153
+ return 0;
154
+ else
155
+ return 1;
105
156
  fi
157
+ }
106
158
 
107
- echo ">>>>>> wget, curl, fetch, perl or python not found on this instance."
108
- return 16
159
+ # http_404_error URL
160
+ http_404_error() {
161
+ echo ">>>>>> Downloading ${1} resulted in an HTTP/404, aborting";
162
+ exit 40;
109
163
  }
110
164
 
111
- # @param $1 the omnibus root directory
112
- # @param $2 the requested version of omnibus package
113
- # @return 0 if omnibus needs to be installed, non-zero otherwise
165
+ # should_update_chef ROOT VERSION
114
166
  should_update_chef() {
115
- if test ! -d "$1" ; then return 0
116
- elif test "$2" = "true" ; then return 1
117
- elif test "$2" = "latest" ; then return 0
167
+ if test ! -d "$1"; then
168
+ return 0;
169
+ elif test "$2" = "true"; then
170
+ return 1;
171
+ elif test "$2" = "latest"; then
172
+ return 0;
173
+ fi
174
+
175
+ chef_version="`${1}/bin/chef-solo -v | cut -d \" \" -f 2`";
176
+
177
+ echo "$chef_version" | grep "^${2}" 2>&1 >/dev/null;
178
+ if test $? -eq 0; then
179
+ return 1;
180
+ else
181
+ return 0;
118
182
  fi
183
+ }
119
184
 
120
- local version="`$1/bin/chef-solo -v | cut -d " " -f 2`"
185
+ # unable_to_download URL FILE
186
+ unable_to_download() {
187
+ echo "Unable to download $1 to $2, aborting";
188
+
189
+ if test "x${stderr_results}" != "x"; then
190
+ echo "\nDEBUG OUTPUT FOLLOWS:\n${stderr_results}";
191
+ fi
121
192
 
122
- echo "$version" | grep "^$2" 2>&1 >/dev/null
123
- if test $? -eq 0 ; then
124
- return 1
193
+ exit 10;
194
+ }
195
+
196
+ # main
197
+ main() {
198
+ should_update_chef "$chef_omnibus_root" "$version"
199
+ if test $? -eq 0; then
200
+ echo "-----> Installing Chef Omnibus (${pretty_version})";
201
+
202
+ # solaris 10 lacks recent enough credentials, so http url is used
203
+ platform="`/usr/bin/uname -s 2>/dev/null`";
204
+ platform_version="`/usr/bin/uname -r 2>/dev/null`";
205
+ if test "x${platform}" = "xSunOS" && test "x${platform_version}" = "x5.10"; then
206
+ chef_omnibus_url=`echo "$chef_omnibus_url" | sed -e "s/https/http/"`;
207
+ fi
208
+
209
+ do_download "$chef_omnibus_url" /tmp/install.sh;
210
+ $sudo_sh /tmp/install.sh $install_flags;
125
211
  else
126
- return 0
212
+ echo "-----> Chef Omnibus installation detected (${pretty_version})";
127
213
  fi
128
214
  }
129
215
 
130
- should_update_chef "$chef_omnibus_root" "$version"
131
- if test $? -eq 0 ; then
132
- echo "-----> Installing Chef Omnibus ($pretty_version)"
133
- do_download "$chef_omnibus_url" /tmp/install.sh
134
- $sudo_sh /tmp/install.sh $install_flags
135
- else
136
- echo "-----> Chef Omnibus installation detected ($pretty_version)"
137
- fi
216
+ # augment path in an attempt to find a download program
217
+ PATH="${PATH}:/opt/local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/sfw/bin";
218
+ export PATH;
219
+
220
+ main