Class: ConfCtl::MachineControl

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/machine_control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ MachineControl

Returns a new instance of MachineControl.

Parameters:



9
10
11
12
13
# File 'lib/confctl/machine_control.rb', line 9

def initialize(machine)
  @machine = machine
  @extra_ssh_opts = []
  @cmd = SystemCommand.new
end

Instance Attribute Details

#machineMachine (readonly)

Returns:



6
7
8
# File 'lib/confctl/machine_control.rb', line 6

def machine
  @machine
end

Instance Method Details

#bash_script(script) ⇒ TTY::Command::Result

Parameters:

  • script (String)

Returns:

  • (TTY::Command::Result)


137
138
139
# File 'lib/confctl/machine_control.rb', line 137

def bash_script(script)
  run_cmd('bash', '--norc', input: script)
end

#execute {|out, err| ... } ⇒ TTY::Command::Result

Execute command, raises exception on error

Yield Parameters:

  • out (String)
  • err (String)

Returns:

  • (TTY::Command::Result)

Raises:

  • (TTY::Command::ExitError)


123
124
125
# File 'lib/confctl/machine_control.rb', line 123

def execute(...)
  run_cmd(...)
end

#execute! {|out, err| ... } ⇒ TTY::Command::Result

Execute command, no exception raised on error

Yield Parameters:

  • out (String)
  • err (String)

Returns:

  • (TTY::Command::Result)


131
132
133
# File 'lib/confctl/machine_control.rb', line 131

def execute!(...)
  run_cmd!(...)
end

#interactive_shellObject



25
26
27
28
29
30
31
# File 'lib/confctl/machine_control.rb', line 25

def interactive_shell
  if machine.localhost?
    Kernel.system(Etc.getpwuid(0).shell)
  else
    Kernel.system(*ssh_args)
  end
end

#read_file(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


99
100
101
102
# File 'lib/confctl/machine_control.rb', line 99

def read_file(path)
  out, = run_cmd('cat', path)
  out
end

#read_realpath(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


113
114
115
116
# File 'lib/confctl/machine_control.rb', line 113

def read_realpath(path)
  out, = run_cmd('realpath', path)
  out.strip
end

Parameters:

  • path (String)

Returns:

  • (String)


106
107
108
109
# File 'lib/confctl/machine_control.rb', line 106

def read_symlink(path)
  out, = run_cmd('readlink', path)
  out.strip
end

#rebootObject

Reboot the machine



34
35
36
# File 'lib/confctl/machine_control.rb', line 34

def reboot
  execute!('reboot')
end

#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



42
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
# File 'lib/confctl/machine_control.rb', line 42

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

#test_connectionObject

Try to open SSH connection

Raises:

  • (TTY::Command::ExitError)


17
18
19
20
21
22
23
# File 'lib/confctl/machine_control.rb', line 17

def test_connection
  with_ssh_opts(
    '-o', 'ConnectTimeout=3',
    '-o', 'ServerAliveInterval=3',
    '-o', 'ServerAliveCountMax=1'
  ) { uptime }
end

#timezoneArray<String>

Returns:

  • (Array<String>)


92
93
94
95
# File 'lib/confctl/machine_control.rb', line 92

def timezone
  out, = run_cmd('date', '+%Z;%z')
  out.strip.split(';')
end

#uptimeInteger

Returns uptime in seconds.

Returns:

  • (Integer)

    uptime in seconds



87
88
89
# File 'lib/confctl/machine_control.rb', line 87

def uptime
  read_file('/proc/uptime').strip.split[0].to_f
end