Method: ConfCtl::MachineControl#reboot_and_wait

Defined in:
lib/confctl/machine_control.rb

#reboot_and_wait(timeout: nil) {|state| ... } ⇒ Integer

Reboot the machine and wait for it to come back online

Parameters:

  • timeout (Integer, nil) (defaults to: nil)

Yield Parameters:

  • state (:reboot, :went_down, :is_down, :is_up, :timeout)

Returns:

  • (Integer)

    seconds it took to reboot the machine



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/confctl/machine_control.rb', line 43

def reboot_and_wait(timeout: nil)
  initial_uptime = uptime
  t = Time.now
  went_down = false
  reboot
  yield :reboot, timeout if block_given?

  loop do
    state = nil

    begin
      current_uptime = with_ssh_opts(
        '-o', 'ConnectTimeout=3',
        '-o', 'ServerAliveInterval=3',
        '-o', 'ServerAliveCountMax=1'
      ) { uptime }

      if current_uptime < initial_uptime
        yield :is_up, nil
        return Time.now - t
      end
    rescue TTY::Command::ExitError
      if went_down
        state = :is_down
      else
        state = :went_down
        went_down = true
      end
    end

    if timeout
      timeleft = (t + timeout) - Time.now

      raise 'timeout' if timeleft <= 0

      yield state, timeleft if block_given?
    elsif block_given?
      yield state, nil
    end

    sleep(0.3)
  end
end