vagrant-chef-zero 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ module VagrantPlugins
21
21
  end
22
22
 
23
23
  def destroy(hook)
24
- hook.after(VagrantPlugins::ProviderVirtualBox::Action::DestroyUnusedNetworkInterfaces, VagrantPlugins::ChefZero::Action.chef_zero_destroy)
24
+ hook.before(VagrantPlugins::ProviderVirtualBox::Action::DestroyUnusedNetworkInterfaces, VagrantPlugins::ChefZero::Action.chef_zero_destroy)
25
25
  end
26
26
 
27
27
  end
@@ -5,28 +5,86 @@ module VagrantPlugins
5
5
  def start_chef_zero(env)
6
6
  require 'chef_zero/server'
7
7
 
8
- unless env[:chef_zero].server && env[:chef_zero].server.running?
9
- port = get_port(env)
10
- host = get_host(env)
8
+ port = get_port(env)
9
+ host = get_host(env)
11
10
 
12
- env[:chef_zero].server = ::ChefZero::Server.new(host: host, port: port)
13
- env[:chef_zero].server.start_background
14
- env[:chef_zero].ui.info("Starting Chef Zero at http://#{host}:#{port}")
11
+ unless chef_zero_server_running?(host, port)
12
+ vagrant_gems = ENV['GEM_PATH'].split(':').select { |gp| gp.include?('vagrant.d')}.first
13
+ chef_zero_binary = ::File.join(vagrant_gems, "bin", "chef-zero")
14
+ fork_process(chef_zero_binary, host, port, env)
15
+ wait_for_server_to_start(host, port, env)
16
+ end
17
+ end
18
+
19
+ def fork_process(command, host, port, env)
20
+ IO.popen("#{command} --host #{host} --port #{port} 2>&1 > /dev/null")
21
+ env[:chef_zero].ui.info("Starting Chef Zero at http://#{host}:#{port}")
22
+ end
23
+
24
+ def wait_for_server_to_start(host, port, env)
25
+ until chef_zero_server_running?(host, port)
26
+ sleep 1
27
+ env[:chef_zero].ui.warn("Waiting for Chef Zero to start")
28
+ end
29
+ end
30
+
31
+ def chef_zero_server_running?(host, port)
32
+ timeout = 0.5 # Seconds
33
+ Timeout::timeout(timeout) do
34
+ return port_open?(host, port) && is_a_chef_zero_server?(host, port)
35
+ end
36
+ rescue Timeout::Error
37
+ false
38
+ end
39
+
40
+ def is_a_chef_zero_server?(host, port)
41
+ require "net/http"
42
+ require "uri"
43
+ # Seems silly to rebuild the URI after deconstructing it earlier
44
+ uri = URI::HTTP.build({ :host => host, :port => port.to_i, :path => "/"})
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.open_timeout = 0.5 # Seconds
47
+ http.read_timeout = 0.5 # Seconds
48
+ request = Net::HTTP::Get.new(uri.request_uri)
49
+ response = http.request(request)
50
+ return response['server'] == "chef-zero"
51
+ end
15
52
 
16
- until env[:chef_zero].server.running?
17
- sleep 1
18
- env[:chef_zero].ui.warn("Waiting for Chef Zero to start")
53
+ # Taken from SO, http://stackoverflow.com/questions/517219/ruby-see-if-a-port-is-open
54
+ def port_open?(ip, port, seconds=0.5)
55
+ begin
56
+ TCPSocket.new(ip, port).close
57
+ true
58
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
59
+ false
60
+ end
61
+ end
62
+
63
+ def stop_chef_zero(env)
64
+ host = get_host(env)
65
+ port = get_port(env)
66
+ if chef_zero_server_running?(host, port)
67
+ pid = get_chef_zero_server_pid(port)
68
+ if pid
69
+ kill_process(env, pid)
19
70
  end
20
71
  end
21
-
22
72
  end
23
73
 
24
- def stop_chef_zero(env)
25
- if env[:chef_zero].server && env[:chef_zero].server.running?
26
- env[:chef_zero].ui.info("Stopping Chef Zero")
27
- env[:chef_zero].server.stop
74
+ def kill_process(env, pid)
75
+ env[:chef_zero].ui.info("Stopping Chef Zero")
76
+ system("kill -s SIGTERM #{pid}")
77
+ end
78
+
79
+ def get_chef_zero_server_pid(port)
80
+ pid = %x[ lsof -i tcp:#{port} | grep -E 'ruby|chef-zero' | awk '{print $2}' ]
81
+ if pid && pid != ""
82
+ return pid
83
+ else
84
+ return false
28
85
  end
29
86
  end
87
+
30
88
  end
31
89
  end
32
90
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module ChefZero
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
@@ -10,11 +10,6 @@ describe VagrantPlugins::ChefZero::ServerHelpers do
10
10
  class DummyChefZeroEnv < VagrantPlugins::ChefZero::Env
11
11
  end
12
12
 
13
- before do
14
- # We need to prevent ChefZero::Server from actually binding to any addresses
15
- ::Puma::Server.any_instance.stub(:add_tcp_listener)
16
- end
17
-
18
13
  let(:d) { DummyServerHelpers.new }
19
14
  let(:env) {
20
15
  h = Hash.new
@@ -28,15 +23,18 @@ describe VagrantPlugins::ChefZero::ServerHelpers do
28
23
  before do
29
24
  d.stub(:get_port) { "4000" }
30
25
  d.stub(:get_host) { "127.0.0.1" }
26
+ d.stub(:fork_process) { }
27
+ d.stub(:wait_for_server_to_start) { true }
28
+ ENV['GEM_PATH'] += ":/foo/bar/vagrant.d/bin"
31
29
  end
32
30
 
33
31
  context "when chef-zero server is already running" do
34
32
 
35
33
  it "should take no action" do
36
- env[:chef_zero].stub_chain(:server, :running?) { true }
37
- env[:chef_zero].stub_chain(:server, :start_background)
34
+ d.stub(:port_open?) { true }
35
+ d.stub(:is_a_chef_zero_server?) { true }
38
36
  d.start_chef_zero(env)
39
- env[:chef_zero].server.should_not have_received(:start_background)
37
+ d.should_not have_received(:fork_process)
40
38
  end
41
39
 
42
40
  end
@@ -44,12 +42,8 @@ describe VagrantPlugins::ChefZero::ServerHelpers do
44
42
  context "when chef-zero server is not running" do
45
43
 
46
44
  it "should start the server in the background" do
47
- env[:chef_zero].stub_chain(:server, :running?) { false }
48
- env[:chef_zero].stub_chain(:server, :start_background) do
49
- env[:chef_zero].stub_chain(:server, :running?) { true }
50
- end
51
45
  d.start_chef_zero(env)
52
- env[:chef_zero].server.should have_received(:start_background)
46
+ d.should have_received(:fork_process)
53
47
  end
54
48
 
55
49
  end
@@ -58,13 +52,17 @@ describe VagrantPlugins::ChefZero::ServerHelpers do
58
52
  describe "#stop_chef_zero" do
59
53
 
60
54
  before do
61
- env[:chef_zero].stub_chain(:server, :running?) { true }
55
+ d.stub(:get_host) { "127.0.0.1" }
56
+ d.stub(:get_port) { "4000" }
57
+ d.stub(:kill_process) { }
58
+ d.stub(:port_open?) { true }
59
+ d.stub(:get_chef_zero_server_pid) { 1234 }
60
+ d.stub(:is_a_chef_zero_server?) { true }
62
61
  end
63
62
 
64
63
  it "should stop server" do
65
- env[:chef_zero].stub_chain(:server, :stop)
66
64
  d.stop_chef_zero(env)
67
- env[:chef_zero].server.should have_received(:stop)
65
+ d.should have_received(:kill_process)
68
66
  end
69
67
 
70
68
  end # describe #stop_chef_zero
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-chef-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-19 00:00:00.000000000 Z
12
+ date: 2013-09-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef-zero
@@ -185,7 +185,6 @@ extra_rdoc_files: []
185
185
  files:
186
186
  - CHANGELOG.md
187
187
  - Gemfile
188
- - lib/vagrant-chef-zero/action/default.rb
189
188
  - lib/vagrant-chef-zero/action/reconfig.rb
190
189
  - lib/vagrant-chef-zero/action/start.rb
191
190
  - lib/vagrant-chef-zero/action/stop.rb
@@ -200,6 +199,7 @@ files:
200
199
  - lib/vagrant-chef-zero.rb
201
200
  - LICENSE.txt
202
201
  - Makefile
202
+ - pkg/vagrant-chef-zero-0.4.0.gem
203
203
  - Rakefile
204
204
  - README.md
205
205
  - spec/lib/config_spec.rb
@@ -245,7 +245,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
245
245
  version: '0'
246
246
  segments:
247
247
  - 0
248
- hash: -1000113792365389957
248
+ hash: 3074438575813103530
249
249
  required_rubygems_version: !ruby/object:Gem::Requirement
250
250
  none: false
251
251
  requirements:
@@ -1 +0,0 @@
1
- default.rb