Class: GoDeploy::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/go_deploy/console.rb

Overview

Console cli linux

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Console

Returns a new instance of Console.



6
7
8
# File 'lib/go_deploy/console.rb', line 6

def initialize(config)
  @config = config
end

Instance Method Details

#exec(ssh:, command:) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/go_deploy/console.rb', line 15

def exec(ssh:, command:)
  log(command: command)
  ssh.open_channel do |channel|
    channel.exec(command) do |_ch, success|
      show_remote_name(is_success: success)
    end
  end
end

#file_exists?(ssh:, path:) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/go_deploy/console.rb', line 24

def file_exists?(ssh:, path:)
  results = []
  ssh.exec!("if [ -e '#{path}' ]; then echo -n 'true'; fi") do |_ch, _stream, out|
    results << (out == 'true')
  end

  results.all?
end

#log(command:, is_show_remote_name: true, is_show_color: true) ⇒ Object



10
11
12
13
# File 'lib/go_deploy/console.rb', line 10

def log(command:, is_show_remote_name: true, is_show_color: true)
  puts is_show_color ? "\t#{command}".yellow : "\t#{command}"
  show_remote_name if is_show_remote_name
end

#show_remote_name(is_success: true) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/go_deploy/console.rb', line 50

def show_remote_name(is_success: true)
  if is_success
    puts "#{@config['user']}@#{@config['host']}".green
  else
    puts "#{@config['user']}@#{@config['host']}".red
  end
end

#sudo_exec(ssh:, command:, is_show_color: true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/go_deploy/console.rb', line 33

def sudo_exec(ssh:, command:, is_show_color: true)
  ssh.open_channel do |channel|
    channel.request_pty do |_c, success|
      raise 'Could not request pty' unless success

      channel.exec(command)
      channel.on_data do |_c, cmd|
        log(command: cmd, is_show_remote_name: false, is_show_color: is_show_color)

        channel.send_data "#{@config['password']}\n" if cmd[/\[sudo\]|Password/i]
      end
    end
  end

  ssh.loop
end