Class: ConfCtl::MachineControl
- Inherits:
-
Object
- Object
- ConfCtl::MachineControl
- Defined in:
- lib/confctl/machine_control.rb
Instance Attribute Summary collapse
- #machine ⇒ Machine readonly
Instance Method Summary collapse
- #bash_script(script) ⇒ TTY::Command::Result
-
#execute {|out, err| ... } ⇒ TTY::Command::Result
Execute command, raises exception on error.
-
#execute! {|out, err| ... } ⇒ TTY::Command::Result
Execute command, no exception raised on error.
-
#initialize(machine) ⇒ MachineControl
constructor
A new instance of MachineControl.
- #interactive_shell ⇒ Object
- #read_file(path) ⇒ String
- #read_realpath(path) ⇒ String
- #read_symlink(path) ⇒ String
-
#reboot ⇒ Object
Reboot the machine.
-
#reboot_and_wait(timeout: nil) {|state| ... } ⇒ Integer
Reboot the machine and wait for it to come back online.
-
#test_connection ⇒ Object
Try to open SSH connection.
- #timezone ⇒ Array<String>
-
#uptime ⇒ Integer
Uptime in seconds.
Constructor Details
#initialize(machine) ⇒ MachineControl
Returns a new instance of MachineControl.
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
#machine ⇒ Machine (readonly)
6 7 8 |
# File 'lib/confctl/machine_control.rb', line 6 def machine @machine end |
Instance Method Details
#bash_script(script) ⇒ 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
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
131 132 133 |
# File 'lib/confctl/machine_control.rb', line 131 def execute!(...) run_cmd!(...) end |
#interactive_shell ⇒ Object
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
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
113 114 115 116 |
# File 'lib/confctl/machine_control.rb', line 113 def read_realpath(path) out, = run_cmd('realpath', path) out.strip end |
#read_symlink(path) ⇒ 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 |
#reboot ⇒ Object
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
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_connection ⇒ Object
Try to open SSH connection
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 |
#timezone ⇒ 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 |
#uptime ⇒ Integer
Returns 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 |