Class: Bcome::Interactive::SessionItem::TransparentSsh

Inherits:
Base
  • Object
show all
Defined in:
lib/interactive/session_item/transparent_ssh.rb

Constant Summary collapse

END_SESSION_KEY =
"\\q"
HELP_KEY =
"\\?"
LIST_KEY =
"\\l"

Instance Method Summary collapse

Methods inherited from Base

#bcome_identifier, #initialize, #irb_session, #options, #set_response_on_session

Constructor Details

This class inherits a constructor from Bcome::Interactive::SessionItem::Base

Instance Method Details

#doObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/interactive/session_item/transparent_ssh.rb', line 8

def do
  input = get_input
  raise ::Bcome::Interactive::SessionHalt.new if exit?(input)
  if show_menu?(input)
    show_menu
  elsif list_machines?(input)
    list_machines
  else
    execute_on_machines(input)
  end
  send(:do)
end

#execute_on_machines(user_input) ⇒ Object



58
59
60
61
62
# File 'lib/interactive/session_item/transparent_ssh.rb', line 58

def execute_on_machines(user_input)
  machines.pmap {|machine|
    machine.run(user_input)
  }
end

#exit?(input) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/interactive/session_item/transparent_ssh.rb', line 42

def exit?(input)
  input == END_SESSION_KEY
end

#get_inputObject



54
55
56
# File 'lib/interactive/session_item/transparent_ssh.rb', line 54

def get_input
  return ::Readline.readline("\n#{terminal_prompt}", true).squeeze(" " ).to_s
end

#has_selected_machines?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/interactive/session_item/transparent_ssh.rb', line 73

def has_selected_machines?
  selected_machines.is_a?(Array) && selected_machines.any?
end

#list_machinesObject



64
65
66
67
# File 'lib/interactive/session_item/transparent_ssh.rb', line 64

def list_machines
  listed = "\n#{machines.collect(&:identifier).join(", ")}\n"
  print listed.friendly
end

#list_machines?(input) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/interactive/session_item/transparent_ssh.rb', line 50

def list_machines?(input)
  input == LIST_KEY 
end

#machinesObject



69
70
71
# File 'lib/interactive/session_item/transparent_ssh.rb', line 69

def machines
  @machines ||= has_selected_machines? ? selected_machines : @irb_session.resources.collect(&:node)
end

#selected_machinesObject



77
78
79
# File 'lib/interactive/session_item/transparent_ssh.rb', line 77

def selected_machines
  @selections ||= @irb_session.instance_variable_get(:@objects)
end

#show_menuObject



21
22
23
# File 'lib/interactive/session_item/transparent_ssh.rb', line 21

def show_menu
  system("clear") ; print start_message
end

#show_menu?(input) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/interactive/session_item/transparent_ssh.rb', line 46

def show_menu?(input)
  input == HELP_KEY
end

#start_messageObject



25
26
27
28
29
30
31
# File 'lib/interactive/session_item/transparent_ssh.rb', line 25

def start_message
  warning = "\nCommands entered here will be executed on every machine in your selection.".danger
  second_warning = "\nUse with CAUTION.".danger
  info = "\n\n\\l list machines\n\\q to quit\n\\? this message".informational                                                             

  return "#{warning}#{second_warning}#{info}\n"
end

#terminal_promptObject



33
34
35
# File 'lib/interactive/session_item/transparent_ssh.rb', line 33

def terminal_prompt
  "#{bcome_identifier}>\s" + "interactive\s>\s".command   # high voltage
end

#valid_response(response) ⇒ Object



37
38
39
40
# File 'lib/interactive/session_item/transparent_ssh.rb', line 37

def valid_response(response)
  a = response.gsub("\s", "").downcase
  valid_responses.include?(response)
end