Class: VagrantPlugins::GANETI::Action::ReadState
- Inherits:
-
Object
- Object
- VagrantPlugins::GANETI::Action::ReadState
- Defined in:
- lib/vagrant-ganeti/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
- #call(env) ⇒ Object
-
#initialize(app, env) ⇒ ReadState
constructor
A new instance of ReadState.
- #is_pingable?(ip) ⇒ Boolean
-
#is_port_open?(ip, port) ⇒ Boolean
borrowed from stackoverflow.com/a/3473208/2388971.
- #read_state(machine) ⇒ Object
- #ssh_port_open?(ip) ⇒ Boolean
Constructor Details
#initialize(app, env) ⇒ ReadState
Returns a new instance of ReadState.
9 10 11 12 |
# File 'lib/vagrant-ganeti/action/read_state.rb', line 9 def initialize(app, env) @app = app @logger = Log4r::Logger.new("vagrant_ganeti::action::read_state") end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 18 |
# File 'lib/vagrant-ganeti/action/read_state.rb', line 14 def call(env) env[:machine_state_id] = read_state(env[:machine]) env[:ui].info "Read the machine state id #{env[:machine].id}" @app.call(env) end |
#is_pingable?(ip) ⇒ Boolean
47 48 49 50 51 52 53 |
# File 'lib/vagrant-ganeti/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
borrowed from stackoverflow.com/a/3473208/2388971
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vagrant-ganeti/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
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-ganeti/action/read_state.rb', line 20 def read_state(machine) return :not_created if machine.id.nil? ip_address = machine.id 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 #return machine.communicate.ready? ? :running : :not_reachable end |
#ssh_port_open?(ip) ⇒ Boolean
55 56 57 |
# File 'lib/vagrant-ganeti/action/read_state.rb', line 55 def ssh_port_open?(ip) is_port_open?(ip, 22) end |