Class: Kanrisuru::Remote::Host

Inherits:
Object
  • Object
show all
Extended by:
OsPackage::Include
Defined in:
lib/kanrisuru/core.rb,
lib/kanrisuru/remote/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OsPackage::Include

os_include

Constructor Details

#initialize(opts = {}) ⇒ Host

Returns a new instance of Host.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kanrisuru/remote/host.rb', line 15

def initialize(opts = {})
  @opts = 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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



13
14
15
# File 'lib/kanrisuru/remote/host.rb', line 13

def host
  @host
end

#keysObject (readonly)

Returns the value of attribute keys.



13
14
15
# File 'lib/kanrisuru/remote/host.rb', line 13

def keys
  @keys
end

#passwordObject (readonly)

Returns the value of attribute password.



13
14
15
# File 'lib/kanrisuru/remote/host.rb', line 13

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



13
14
15
# File 'lib/kanrisuru/remote/host.rb', line 13

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



13
14
15
# File 'lib/kanrisuru/remote/host.rb', line 13

def username
  @username
end

Instance Method Details

#cd(path = '~') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kanrisuru/remote/host.rb', line 58

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 = '~') ⇒ Object



54
55
56
# File 'lib/kanrisuru/remote/host.rb', line 54

def chdir(path = '~')
  cd(path)
end

#cpuObject



73
74
75
# File 'lib/kanrisuru/remote/host.rb', line 73

def cpu
  @cpu ||= init_cpu
end

#disconnectObject



118
119
120
121
# File 'lib/kanrisuru/remote/host.rb', line 118

def disconnect
  ssh.close
  proxy&.close(ssh.transport.port)
end

#envObject



42
43
44
# File 'lib/kanrisuru/remote/host.rb', line 42

def env
  @env ||= init_env
end

#execute(command) ⇒ Object



113
114
115
116
# File 'lib/kanrisuru/remote/host.rb', line 113

def execute(command)
  command = Kanrisuru::Command.new(command) if command.instance_of?(String)
  execute_with_retries(command)
end

#execute_shell(command) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/kanrisuru/remote/host.rb', line 102

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) ⇒ Object



85
86
87
# File 'lib/kanrisuru/remote/host.rb', line 85

def file(path)
  Kanrisuru::Remote::File.new(path, self)
end

#fstab(file = '/etc/fstab') ⇒ Object



50
51
52
# File 'lib/kanrisuru/remote/host.rb', line 50

def fstab(file = '/etc/fstab')
  @fstab ||= init_fstab(file)
end

#hostnameObject



34
35
36
# File 'lib/kanrisuru/remote/host.rb', line 34

def hostname
  @hostname ||= init_hostname
end

#memoryObject



77
78
79
# File 'lib/kanrisuru/remote/host.rb', line 77

def memory
  @memory ||= init_memory
end

#osObject



38
39
40
# File 'lib/kanrisuru/remote/host.rb', line 38

def os
  @os ||= init_os
end

#ping?Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/kanrisuru/remote/host.rb', line 97

def ping?
  check = Net::Ping::External.new(@host)
  check.ping?
end

#proxyObject



93
94
95
# File 'lib/kanrisuru/remote/host.rb', line 93

def proxy
  @proxy ||= init_proxy
end

#remote_userObject



30
31
32
# File 'lib/kanrisuru/remote/host.rb', line 30

def remote_user
  @remote_user ||= @username
end

#sshObject



89
90
91
# File 'lib/kanrisuru/remote/host.rb', line 89

def ssh
  @ssh ||= init_ssh
end

#su(user) ⇒ Object



81
82
83
# File 'lib/kanrisuru/remote/host.rb', line 81

def su(user)
  @remote_user = user
end

#template(path, args = {}) ⇒ Object



46
47
48
# File 'lib/kanrisuru/remote/host.rb', line 46

def template(path, args = {})
  Kanrisuru::Template.new(path, args)
end