class Kanrisuru::Remote::Host

Attributes

host[R]
keys[R]
password[R]
port[R]
username[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 14
def initialize(opts = {})
  @host = opts[:host]
  @username = opts[:username]
  @login_user = @username

  @port     = opts[:port] || 22
  @password = opts[:password] if opts[:password]
  @keys     = opts[:keys] if opts[:keys]
  @shell    = opts[:shell] || '/bin/bash'

  @current_dir = ''
end

Public Instance Methods

cd(path = '~') click to toggle source
# File lib/kanrisuru/remote/host.rb, line 55
def cd(path = '~')
  @current_dir = pwd.path if Kanrisuru::Util.blank?(@current_dir)

  @current_dir =
    if path == '~'
      realpath('~').path
    elsif path[0] == '.' || path[0] != '/'
      ## Use strip to preserve symlink directories
      realpath("#{@current_dir}/#{path}", strip: true).path
    else
      ## Use strip to preserve symlink directories
      realpath(path, strip: true).path
    end
end
chdir(path = '~') click to toggle source
# File lib/kanrisuru/remote/host.rb, line 51
def chdir(path = '~')
  cd(path)
end
cpu() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 70
def cpu
  @cpu ||= init_cpu
end
disconnect() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 111
def disconnect
  ssh.close
end
env() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 39
def env
  @env ||= init_env
end
execute(command) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 106
def execute(command)
  command = Kanrisuru::Command.new(command) if command.instance_of?(String)
  execute_with_retries(command)
end
execute_shell(command) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 95
def execute_shell(command)
  command = Kanrisuru::Command.new(command) if command.instance_of?(String)

  command.remote_user = remote_user
  command.remote_shell = @shell
  command.remote_path = @current_dir
  command.remote_env = env.to_s

  execute_with_retries(command)
end
file(path) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 82
def file(path)
  Kanrisuru::Remote::File.new(path, self)
end
fstab(file = '/etc/fstab') click to toggle source
# File lib/kanrisuru/remote/host.rb, line 47
def fstab(file = '/etc/fstab')
  @fstab ||= init_fstab(file)
end
hostname() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 31
def hostname
  @hostname ||= init_hostname
end
memory() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 74
def memory
  @memory ||= init_memory
end
os() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 35
def os
  @os ||= init_os
end
ping?() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 90
def ping?
  check = Net::Ping::External.new(@host)
  check.ping?
end
remote_user() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 27
def remote_user
  @remote_user ||= @username
end
ssh() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 86
def ssh
  @ssh ||= Net::SSH.start(@host, @username, keys: @keys, password: @password, port: @port)
end
su(user) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 78
def su(user)
  @remote_user = user
end
template(path, args = {}) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 43
def template(path, args = {})
  Kanrisuru::Template.new(path, args)
end

Private Instance Methods

execute_with_retries(command) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 117
def execute_with_retries(command)
  raise 'Invalid command type' unless command.instance_of?(Kanrisuru::Command)

  retry_attempts = 3

  Kanrisuru.logger.debug { "kanrisuru:~$ #{command.prepared_command}" }

  begin
    channel = ssh.open_channel do |ch|
      ch.exec(command.prepared_command) do |_, success|
        raise "could not execute command: #{command.prepared_command}" unless success

        ch.on_request('exit-status') do |_, data|
          command.handle_status(data.read_long)
        end

        ch.on_request('exit-signal') do |_, data|
          command.handle_signal(data.read_long)
        end

        ch.on_data do |_, data|
          command.handle_data(data)
        end

        ch.on_extended_data do |_, _type, data|
          command.handle_data(data)
        end
      end
    end

    channel.wait

    Kanrisuru.logger.debug { command.to_a }

    command
  rescue Net::SSH::ConnectionTimeout, Net::SSH::Timeout => e
    if retry_attempts > 1
      retry_attempts -= 1
      retry
    else
      disconnect
      raise e.class
    end
  end
end
init_cpu() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 186
def init_cpu
  Cpu.new(self)
end
init_env() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 170
def init_env
  Env.new
end
init_fstab(file) click to toggle source
# File lib/kanrisuru/remote/host.rb, line 174
def init_fstab(file)
  Fstab.new(self, file)
end
init_hostname() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 163
def init_hostname
  command = Kanrisuru::Command.new('hostname')
  execute(command)

  command.success? ? command.to_s : nil
end
init_memory() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 178
def init_memory
  Memory.new(self)
end
init_os() click to toggle source
# File lib/kanrisuru/remote/host.rb, line 182
def init_os
  Os.new(self)
end