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.



14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#keysObject (readonly)

Returns the value of attribute keys.



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

def keys
  @keys
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#cd(path = '~') ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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 = '~') ⇒ Object



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

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

#cpuObject



70
71
72
# File 'lib/kanrisuru/remote/host.rb', line 70

def cpu
  @cpu ||= init_cpu
end

#disconnectObject



111
112
113
# File 'lib/kanrisuru/remote/host.rb', line 111

def disconnect
  ssh.close
end

#envObject



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

def env
  @env ||= init_env
end

#execute(command) ⇒ Object



106
107
108
109
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



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

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

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



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

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

#hostnameObject



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

def hostname
  @hostname ||= init_hostname
end

#memoryObject



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

def memory
  @memory ||= init_memory
end

#osObject



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

def os
  @os ||= init_os
end

#ping?Boolean

Returns:

  • (Boolean)


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

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

#remote_userObject



27
28
29
# File 'lib/kanrisuru/remote/host.rb', line 27

def remote_user
  @remote_user ||= @username
end

#sshObject



86
87
88
# 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) ⇒ Object



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

def su(user)
  @remote_user = user
end

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



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

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