Class: KuberKit::Shell::LocalShell

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

Direct Known Subclasses

SshShell

Constant Summary

Constants inherited from AbstractShell

AbstractShell::DirNotFoundError, AbstractShell::ShellError

Instance Method Summary collapse

Instance Method Details

#delete(file_path) ⇒ Object



51
52
53
# File 'lib/kuber_kit/shell/local_shell.rb', line 51

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

#dir_exists?(dir_path) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/kuber_kit/shell/local_shell.rb', line 59

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kuber_kit/shell/local_shell.rb', line 10

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

  result = nil
  IO.popen(command, err: [:child, :out]) do |io|
    result = io.read.chomp.strip
  end

  if result && result != "" && log_command
    logger.info("Finished [#{command_number}] with result: \n#{result.grey}")
  end

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

  result
end

#file_exists?(file_path) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/kuber_kit/shell/local_shell.rb', line 55

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

#read(file_path) ⇒ Object



37
38
39
# File 'lib/kuber_kit/shell/local_shell.rb', line 37

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

#recursive_list_files(path, name: nil) ⇒ Object



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

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

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



33
34
35
# File 'lib/kuber_kit/shell/local_shell.rb', line 33

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

#write(file_path, content) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/kuber_kit/shell/local_shell.rb', line 41

def write(file_path, content)
  ensure_directory_exists(file_path)

  File.write(file_path, content)

  logger.info("Created file #{file_path.to_s.cyan}\r\n#{content.grey}")

  true
end