Module: VagrantPlugins::ChefZero::ServerHelpers

Included in:
Action::Reconfig, Action::Start, Action::Stop, Action::Upload
Defined in:
lib/vagrant-chef-zero/server_helpers.rb

Instance Method Summary collapse

Instance Method Details

#chef_zero_server_running?(host, port) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 75

def chef_zero_server_running?(host, port)
  timeout = 0.5 # Seconds
  Timeout::timeout(timeout) do
    return port_open?(host, port) && is_a_chef_zero_server?(host, port)
  end
rescue Timeout::Error
  false
end

#find_chef_zero_binary(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 46

def find_chef_zero_binary(path)
  # Assuming a path from Gem.path
  if path == nil
    return nil
  end
  gems_path = ::File.join(path, "gems")
  chef_zero_gem = Dir["#{gems_path}/*"].select { |gp| gp.include?('/chef-zero-')}.first
  if chef_zero_gem
    return ::File.join(chef_zero_gem, "bin", "chef-zero")
  else
    return nil
  end
end

#fork_process(command, host, port, env) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 60

def fork_process(command, host, port, env)
  ruby_bin = Pathname.new(Vagrant::Util::Which.which("ruby")).to_s
  pid = Process.spawn("#{ruby_bin} #{command} --host #{host} --port #{port}",
                      :out=>File::NULL, :err=>File::NULL)
  Process.detach pid
  env[:chef_zero].ui.info("Starting Chef Zero at http://#{host}:#{port}")
end

#get_chef_zero_binary(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 20

def get_chef_zero_binary(env)
  # We want to prefer the vagrant.d path, but for development
  # we want to find any valid path that has chef-zero
  paths = Gem.path
  vagrant_path = paths.select { |gp| gp.include?('vagrant.d')}.first
  if has_chef_zero_binary?(vagrant_path)
    return find_chef_zero_binary(vagrant_path)
  end
  paths.each do |path|
    if has_chef_zero_binary?(path)
      return find_chef_zero_binary(path)
    end
  end
  env[:chef_zero].ui.warn("Could not find Chef Zero binary in any path in #{Gem.path}")
  raise
end

#get_chef_zero_server_pid(port) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 123

def get_chef_zero_server_pid(port)
  pid = %x[ lsof -i tcp:#{port} | grep -E 'ruby|chef-zero' | awk '{print $2}' ]
  if pid && pid != ""
    return pid
  else
    return false
  end
end

#has_chef_zero_binary?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 37

def has_chef_zero_binary?(path)
  potential_binary = find_chef_zero_binary(path)
  if potential_binary
    return ::File.exists?(potential_binary)
  else
    return false
  end
end

#is_a_chef_zero_server?(host, port) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 84

def is_a_chef_zero_server?(host, port)
  require "net/http"
  require "uri"
  # Seems silly to rebuild the URI after deconstructing it earlier
  uri = URI::HTTP.build({ :host => host, :port => port.to_i, :path => "/"})
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = 0.5 # Seconds
  http.read_timeout = 0.5 # Seconds
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  return response['server'] == "chef-zero"
end

#kill_process(env, pid) ⇒ Object



118
119
120
121
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 118

def kill_process(env, pid)
  env[:chef_zero].ui.info("Stopping Chef Zero")
  system("kill -s TERM #{pid}")
end

#port_open?(ip, port, seconds = 0.5) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 98

def port_open?(ip, port, seconds=0.5)
  begin
    TCPSocket.new(ip, port).close
    true
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH
    false
  end
end

#start_chef_zero(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 7

def start_chef_zero(env)
  require 'chef_zero/server'

  port = get_port(env)
  host = get_host(env)

  unless chef_zero_server_running?(host, port)
    chef_zero_binary = get_chef_zero_binary(env)
    fork_process(chef_zero_binary, host, port, env)
    wait_for_server_to_start(host, port, env)
  end
end

#stop_chef_zero(env) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 107

def stop_chef_zero(env)
  host = get_host(env)
  port = get_port(env)
  if chef_zero_server_running?(host, port)
    pid = get_chef_zero_server_pid(port)
    if pid
      kill_process(env, pid)
    end
  end
end

#wait_for_server_to_start(host, port, env) ⇒ Object



68
69
70
71
72
73
# File 'lib/vagrant-chef-zero/server_helpers.rb', line 68

def wait_for_server_to_start(host, port, env)
  until chef_zero_server_running?(host, port)
    sleep 1
    env[:chef_zero].ui.warn("Waiting for Chef Zero to start")
  end
end