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, logger: nil) ⇒ 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, logger: nil) ⇒ MachineControl
Returns a new instance of MachineControl.
10 11 12 13 14 |
# File 'lib/confctl/machine_control.rb', line 10 def initialize(machine, logger: nil) @machine = machine @extra_ssh_opts = [] @cmd = SystemCommand.new(logger:) 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
138 139 140 |
# File 'lib/confctl/machine_control.rb', line 138 def bash_script(script) run_cmd('bash', '--norc', input: script) end |
#execute {|out, err| ... } ⇒ TTY::Command::Result
Execute command, raises exception on error
124 125 126 |
# File 'lib/confctl/machine_control.rb', line 124 def execute(...) run_cmd(...) end |
#execute! {|out, err| ... } ⇒ TTY::Command::Result
Execute command, no exception raised on error
132 133 134 |
# File 'lib/confctl/machine_control.rb', line 132 def execute!(...) run_cmd!(...) end |
#interactive_shell ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/confctl/machine_control.rb', line 26 def interactive_shell if machine.localhost? Kernel.system(Etc.getpwuid(0).shell) else Kernel.system(*ssh_args) end end |
#read_file(path) ⇒ String
100 101 102 103 |
# File 'lib/confctl/machine_control.rb', line 100 def read_file(path) out, = run_cmd('cat', path) out end |
#read_realpath(path) ⇒ String
114 115 116 117 |
# File 'lib/confctl/machine_control.rb', line 114 def read_realpath(path) out, = run_cmd('realpath', path) out.strip end |
#read_symlink(path) ⇒ String
107 108 109 110 |
# File 'lib/confctl/machine_control.rb', line 107 def read_symlink(path) out, = run_cmd('readlink', path) out.strip end |
#reboot ⇒ Object
Reboot the machine
35 36 37 |
# File 'lib/confctl/machine_control.rb', line 35 def reboot execute!('reboot') end |
#reboot_and_wait(timeout: nil) {|state| ... } ⇒ Integer
Reboot the machine and wait for it to come back online
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 |
#test_connection ⇒ Object
Try to open SSH connection
18 19 20 21 22 23 24 |
# File 'lib/confctl/machine_control.rb', line 18 def test_connection with_ssh_opts( '-o', 'ConnectTimeout=3', '-o', 'ServerAliveInterval=3', '-o', 'ServerAliveCountMax=1' ) { uptime } end |
#timezone ⇒ Array<String>
93 94 95 96 |
# File 'lib/confctl/machine_control.rb', line 93 def timezone out, = run_cmd('date', '+%Z;%z') out.strip.split(';') end |
#uptime ⇒ Integer
Returns uptime in seconds.
88 89 90 |
# File 'lib/confctl/machine_control.rb', line 88 def uptime read_file('/proc/uptime').strip.split[0].to_f end |