Sha256: ec7722e3aec28c98a4792121dd3ba8e0a43ea645627fcdf2e93473c41960d1a3

Contents?: true

Size: 1.82 KB

Versions: 7

Compression:

Stored size: 1.82 KB

Contents

require 'tempfile'

class KuberKit::Shell::SshShell < KuberKit::Shell::LocalShell
  include KuberKit::Import[
    "tools.logger",
    "shell.command_counter",
    "shell.rsync_commands",
    "shell.local_shell"
  ]

  def connect(host:, user:, port:)
    @ssh_session = KuberKit::Shell::SshSession.new(host: host, user: user, port: port)
  end
  
  def connected?
    @ssh_session && @ssh_session.connected?
  end

  def disconnect
    @ssh_session.disconnect if @ssh_session
  end

  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

  def sync(local_path, remote_path, exclude: nil, delete: true)
    rsync_commands.rsync(
      local_shell, local_path, remote_path, 
      target_host: "#{ssh_session.user}@#{ssh_session.host}",
      exclude:     exclude,
      delete:      delete
    )
  end

  def read(file_path)
    exec!("cat #{file_path}")
  end

  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

  private
    def ssh_session
      unless connected?
        raise ArgumentError, "ssh session is not created, please call #connect"
      end

      @ssh_session
    end

    def ensure_directory_exists(file_path)
      exec!("mkdir -p #{file_path}")
    end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kuber_kit-0.3.0 lib/kuber_kit/shell/ssh_shell.rb
kuber_kit-0.2.9 lib/kuber_kit/shell/ssh_shell.rb
kuber_kit-0.2.8 lib/kuber_kit/shell/ssh_shell.rb
kuber_kit-0.2.7 lib/kuber_kit/shell/ssh_shell.rb
kuber_kit-0.2.6 lib/kuber_kit/shell/ssh_shell.rb
kuber_kit-0.2.5 lib/kuber_kit/shell/ssh_shell.rb
kuber_kit-0.2.4 lib/kuber_kit/shell/ssh_shell.rb