Class: KuberKit::Shell::SshShell
Constant Summary
AbstractShell::DirNotFoundError, AbstractShell::ShellError
Instance Method Summary
collapse
Methods inherited from LocalShell
#delete, #dir_exists?, #file_exists?, #recursive_list_files
#recursive_list_files
Instance Method Details
#connect(host:, user:, port:) ⇒ Object
11
12
13
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 11
def connect(host:, user:, port:)
@ssh_session = KuberKit::Shell::SshSession.new(host: host, user: user, port: port)
end
|
#connected? ⇒ Boolean
15
16
17
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 15
def connected?
@ssh_session && @ssh_session.connected?
end
|
#disconnect ⇒ Object
19
20
21
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 19
def disconnect
@ssh_session.disconnect if @ssh_session
end
|
#exec!(command, log_command: true) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 23
def exec!(command, log_command: true)
command_number = command_counter.get_number.to_s.rjust(2, "0")
if log_command
logger.info("#{ssh_session.host.green} > Execute: [#{command_number}]: #{command.to_s.cyan}")
end
result = ssh_session.exec!(command)
if result && result != "" && log_command
logger.info("#{ssh_session.host.green} > Finished [#{command_number}] with result: \n#{result.grey}")
end
result
rescue KuberKit::Shell::SshSession::SshSessionError => e
raise ShellError.new(e.message)
end
|
#read(file_path) ⇒ Object
49
50
51
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 49
def read(file_path)
exec!("cat #{file_path}")
end
|
#sync(local_path, remote_path, exclude: nil) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 41
def sync(local_path, remote_path, exclude: nil)
rsync_commands.rsync(
local_shell, local_path, remote_path,
target_host: "#{ssh_session.user}@#{ssh_session.host}",
exclude: exclude
)
end
|
#write(file_path, content) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/kuber_kit/shell/ssh_shell.rb', line 53
def write(file_path, content)
Tempfile.create do |file|
file << content
file.flush
sync(file.path, file_path)
end
logger.info("Created file #{file_path.to_s.cyan}\r\n#{content.grey}")
true
end
|