Class: Octosh::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/octosh/shell.rb

Instance Method Summary collapse

Constructor Details

#initialize(hosts, options) ⇒ Shell

Returns a new instance of Shell.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/octosh/shell.rb', line 16

def initialize(hosts, options)
  colors = Octosh::COLORS::COLORS.dup
  @workers = []
  hosts.each do |host|
    prompt_for_password(options[:password_prompt], options[:uniform_password])
    exec_user,hostname = ""
    if host.include? '@'
      # USer defined with host, override provided user
      exec_user,hostname = host.split('@')
    else
      exec_user = options[:default_user]
      hostname = host
    end
    worker_options = options.dup
    worker_options[:color] = colors.shift
    colors << worker_options[:color]
    worker = Octosh::Worker.new(hostname, exec_user, @password, worker_options)
    worker.connect
    @workers << worker
  end
end

Instance Method Details

#disconnectObject



83
84
85
86
87
88
89
# File 'lib/octosh/shell.rb', line 83

def disconnect
  @workers.each do |worker|
    print "Closing connection to #{worker.host} . . . ".colorize(worker.options[:color].to_sym)
    worker.disconnect
    puts "OK".colorize(worker.options[:color].to_sym)
  end
end

#preprocess_command(command) ⇒ Object



76
77
78
79
80
81
# File 'lib/octosh/shell.rb', line 76

def preprocess_command(command)
  if command.downcase == "exit"
    disconnect
    exit
  end
end

#prompt_for_password(password_prompt, uniform_password, host = "current host") ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/octosh/shell.rb', line 38

def prompt_for_password(password_prompt, uniform_password, host="current host")
  if password_prompt
    # Password authentication
    if uniform_password and @password.nil?
      # One password for all hosts
      @password = Octosh::Helper.password_prompt("Password: ")
    elsif not uniform_password
      # One password for each host
      @password = Octosh::Helper.password_prompt("Password for #{host}: ")
    end
  end
end

#startObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/octosh/shell.rb', line 51

def start
  
  puts "Starting Octoshell connected to #{@workers.length} hosts"
  @workers.each do |worker|
    puts worker.host.colorize(worker.options[:color].to_sym)
  end
  
  while true
    print ">> "
    command = ""
    begin
      command = gets.chomp!
    rescue Interrupt
      disconnect
      exit
    end
    preprocess_command(command || "")
    Parallel.each(@workers, :in_threads => @workers.length) do |worker|
      output = worker.exec(command) do |output|
        print output.colorize(worker.options[:color].to_sym)
      end
    end
  end
end