lib/vagrant-notify/action/start_server.rb in vagrant-notify-0.4.0 vs lib/vagrant-notify/action/start_server.rb in vagrant-notify-0.5.0

- old
+ new

@@ -1,8 +1,7 @@ require "vagrant/util/is_port_open" -require_relative '../server' module Vagrant module Notify module Action class StartServer @@ -14,13 +13,23 @@ def call(env) @env = env port = next_available_port - env[:notify_data][:pid] = Server.run(env, port) - env[:notify_data][:port] = port + id = env[:machine].id + dir = File.expand_path('../../', __FILE__) + if which('ruby') + env[:notify_data][:pid] = Process.spawn("ruby #{dir}/server.rb #{id} #{port}") + env[:notify_data][:port] = port + + env[:machine].ui.success("Started vagrant-notify-server pid: #{env[:notify_data][:pid]}") + sleep 5 + else + env[:machine].ui.error("Unable to spawn TCPServer daemon, ruby not found in $PATH") + end + @app.call env end def next_available_port # Determine a list of usable ports for us to use @@ -53,9 +62,23 @@ # Ignore anything but forwarded ports next if type != :forwarded_port yield options end + end + + # http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby + # Cross-platform way of finding an executable in the $PATH. + # which('ruby') #=> /usr/bin/ruby + def which(cmd) + exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| + exts.each { |ext| + exe = File.join(path, "#{cmd}#{ext}") + return exe if File.executable?(exe) && !File.directory?(exe) + } + end + return nil end end end end end