Class: KuberKit::Shell::LocalShell

Inherits:
AbstractShell show all
Defined in:
lib/kuber_kit/shell/local_shell.rb

Direct Known Subclasses

SshShell

Constant Summary collapse

MAX_LINES_TO_PRINT =
50

Constants inherited from AbstractShell

AbstractShell::DirNotFoundError, AbstractShell::ShellError

Instance Method Summary collapse

Instance Method Details

#delete(file_path) ⇒ Object



94
95
96
# File 'lib/kuber_kit/shell/local_shell.rb', line 94

def delete(file_path)
  exec!("rm #{file_path}", merge_stderr: true)
end

#dir_exists?(dir_path) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/kuber_kit/shell/local_shell.rb', line 102

def dir_exists?(dir_path)
  exec!("test -d #{dir_path} && echo 'true' || echo 'false'", log_command: false, merge_stderr: true) == 'true'
end

#exec!(command, log_command: true, merge_stderr: false) ⇒ Object

@merge_stderr: Merge STDERR to the resulting stream. Could be helpful, if we don’t want it printed to STDERR.

Should be false, if we want to read & use the result of the command.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kuber_kit/shell/local_shell.rb', line 14

def exec!(command, log_command: true, merge_stderr: false)
  command_number = command_counter.get_number.to_s.rjust(2, "0")
  
  if log_command
    ui.print_debug("LocalShell", "Execute: [#{command_number}]: #{command.to_s.cyan}")
  end

  result = nil
  
  options = merge_stderr ? {err: [:child, :out]} : {}
  IO.popen(wrap_command_with_pid(command), **options) do |io|
    result = io.read.chomp.strip
  end

  if result && result != "" && log_command
    print_result = result
    print_result_lines = print_result.split("\n")

    if print_result_lines.count >= MAX_LINES_TO_PRINT
      print_result = [
        "[Result is too long, showing only first and last items]".yellow, 
        print_result_lines.first, 
        "[#{print_result_lines.count - 2} lines not showing]".yellow, 
        print_result_lines.last
    ].join("\n")
    end

    ui.print_debug("LocalShell", "Finished [#{command_number}] with result: \n  ----\n#{print_result.grey}\n  ----")
  end

  if $?.exitstatus != 0
    raise ShellError, "Shell command failed: #{command}\r\n#{result}"
  end

  result
end

#file_exists?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/kuber_kit/shell/local_shell.rb', line 98

def file_exists?(file_path)
  exec!("test -f #{file_path} && echo 'true' || echo 'false'", log_command: false, merge_stderr: true) == 'true'
end

#interactive!(command, log_command: true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kuber_kit/shell/local_shell.rb', line 51

def interactive!(command, log_command: true)
  command_number = command_counter.get_number.to_s.rjust(2, "0")
  
  if log_command
    ui.print_debug("LocalShell", "Interactive: [#{command_number}]: #{command.to_s.cyan}")
  end

  result = system(wrap_command_with_pid(command))

  if !$?.success?
    raise ShellError, "Shell command failed: #{command}\r\n#{result}"
  end
end

#list_dirs(path) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/kuber_kit/shell/local_shell.rb', line 118

def list_dirs(path)
  command = %Q{find -L #{path} -maxdepth 0 -type d}
  exec!(command, merge_stderr: true).split(/[\r\n]+/)
rescue => e
  if e.message.include?("No such file or directory")
    raise DirNotFoundError.new("Dir not found: #{path}")
  else
    raise e
  end
end

#read(file_path) ⇒ Object



80
81
82
# File 'lib/kuber_kit/shell/local_shell.rb', line 80

def read(file_path)
  File.read(file_path)
end

#recursive_list_files(path, name: nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kuber_kit/shell/local_shell.rb', line 106

def recursive_list_files(path, name: nil)
  command = %Q{find -L #{path}  -type f}
  command += " -name '#{name}'" if name
  exec!(command, merge_stderr: true).split(/[\r\n]+/)
rescue => e
  if e.message.include?("No such file or directory")
    raise DirNotFoundError.new("Dir not found: #{path}")
  else
    raise e
  end
end

#replace!(shell_name: nil, env: [], log_command: true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/kuber_kit/shell/local_shell.rb', line 65

def replace!(shell_name: nil, env: [], log_command: true)
  shell_name ||= "$SHELL --login"
  command = (env + [shell_name]).join(" ")

  if log_command
    ui.print_debug("LocalShell", "Replace Shell: #{command.to_s.cyan}")
  end

  system_exec(command)
end

#sync(local_path, remote_path, exclude: nil, delete: true) ⇒ Object



76
77
78
# File 'lib/kuber_kit/shell/local_shell.rb', line 76

def sync(local_path, remote_path, exclude: nil, delete: true)
  rsync_commands.rsync(self, local_path, remote_path, exclude: exclude, delete: delete)
end

#wrap_command_with_pid(command) ⇒ Object



129
130
131
# File 'lib/kuber_kit/shell/local_shell.rb', line 129

def wrap_command_with_pid(command)
  "KIT=#{Process.pid} #{command}"
end

#write(file_path, content) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/kuber_kit/shell/local_shell.rb', line 84

def write(file_path, content)
  ensure_directory_exists(file_path)

  File.write(file_path, content)

  ui.print_debug("LocalShell", "Created file #{file_path.to_s.cyan}\r\n  ----\r\n#{content.grey}\r\n  ----")

  true
end