Class: VagrantPlugins::ManagedServers::Action::ReadState

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-managed-servers/action/read_state.rb

Overview

This action reads the state of the machine and puts it in the ‘:machine_state_id` key in the environment.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ReadState

Returns a new instance of ReadState.



9
10
11
12
# File 'lib/vagrant-managed-servers/action/read_state.rb', line 9

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_managed_servers::action::read_state")
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
# File 'lib/vagrant-managed-servers/action/read_state.rb', line 14

def call(env)
  env[:machine_state_id] = read_state(env[:machine])
  @app.call(env)
end

#is_pingable?(ip) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/vagrant-managed-servers/action/read_state.rb', line 47

def is_pingable?(ip)
  if Vagrant::Util::Platform.windows?
    system("ping -n 1 #{ip}")
  else
    system("ping -q -c 1 #{ip}")
  end
end

#is_port_open?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-managed-servers/action/read_state.rb', line 62

def is_port_open?(ip, port)
  require 'socket'
  s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  sa = Socket.sockaddr_in(port, ip)
  begin
    s.connect_nonblock(sa)
  rescue Errno::EINPROGRESS
    if IO.select(nil, [s], nil, 1)
      begin
        s.connect_nonblock(sa)
      rescue Errno::EISCONN
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  end
  return false
end

#read_state(machine) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vagrant-managed-servers/action/read_state.rb', line 19

def read_state(machine)
  return :not_linked if machine.id.nil?

  ip_address = machine.id
=begin
  if machine.communicate.ready?
    @logger.info "#{ip_address} is reachable and SSH login OK"
    return :reachable
  else
    if ssh_port_open? ip_address
      @logger.info "#{ip_address} is reachable, SSH port open (but login failed)"
      return :reachable
    else 
      if is_pingable? ip_address
        @logger.info "#{ip_address} is pingable (but SSH failed)"
        return :reachable
      end
    end
  end

  # host is not reachable at all...
  return :not_reachable
=end

  return machine.communicate.ready? ? :running : :not_reachable
end

#ssh_port_open?(ip) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/vagrant-managed-servers/action/read_state.rb', line 55

def ssh_port_open?(ip)
  is_port_open?(ip, 22)
end