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



90
91
92
# File 'lib/kuber_kit/shell/local_shell.rb', line 90

def delete(file_path)
  exec!("rm #{file_path}")
end

#dir_exists?(dir_path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



12
13
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
# File 'lib/kuber_kit/shell/local_shell.rb', line 12

def exec!(command, log_command: true)
  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
  IO.popen(wrap_command_with_pid(command), err: [:child, :out]) 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)


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

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kuber_kit/shell/local_shell.rb', line 47

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



114
115
116
117
118
119
120
121
122
123
# File 'lib/kuber_kit/shell/local_shell.rb', line 114

def list_dirs(path)
  command = %Q{find -L #{path} -maxdepth 0 -type d}
  exec!(command).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



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

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

#recursive_list_files(path, name: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kuber_kit/shell/local_shell.rb', line 102

def recursive_list_files(path, name: nil)
  command = %Q{find -L #{path}  -type f}
  command += " -name '#{name}'" if name
  exec!(command).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



61
62
63
64
65
66
67
68
69
70
# File 'lib/kuber_kit/shell/local_shell.rb', line 61

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



72
73
74
# File 'lib/kuber_kit/shell/local_shell.rb', line 72

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



125
126
127
# File 'lib/kuber_kit/shell/local_shell.rb', line 125

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

#write(file_path, content) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/kuber_kit/shell/local_shell.rb', line 80

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