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"
DANGER_CMD =
"rm\s+-r|rm\s+-f|rm\s+-fr|rm\s+-rf|rm"

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

#command_may_be_unwise?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def command_may_be_unwise?(input)
  input =~ /#{DANGER_CMD}/
end

#doObject



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

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
  elsif command_may_be_unwise?(input)
    handle_the_unwise(input)   
  else
    execute_on_machines(input)
  end
  send(:do)
end

#execute_on_machines(user_input) ⇒ Object



78
79
80
81
82
# File 'lib/interactive/session_item/transparent_ssh.rb', line 78

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

#exit?(input) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/interactive/session_item/transparent_ssh.rb', line 62

def exit?(input)
  input == END_SESSION_KEY
end

#flat_list_of_machinesObject



96
97
98
# File 'lib/interactive/session_item/transparent_ssh.rb', line 96

def flat_list_of_machines
  resources.collect{|resource| resource.node.machines }.flatten
end

#get_input(message = terminal_prompt) ⇒ Object



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

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

#handle_the_unwise(input) ⇒ Object



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

def handle_the_unwise(input)
  if prompt_for_confirmation("Command may be dangerous to run on all machines. Are you sure you want to proceed? [Y|N] > ".danger)
    execute_on_machines(input)
  end
end

#has_selected_machines?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/interactive/session_item/transparent_ssh.rb', line 104

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

#list_machinesObject



84
85
86
87
88
89
90
# File 'lib/interactive/session_item/transparent_ssh.rb', line 84

def list_machines
  puts "\n" + machines.collect {|m| 
    env = m.environment
    platform = env.platform
   "* #{platform.identifier.cyan}:#{env.identifier.orange}:#{m.identifier.yellow}"  
  }.join("\n")
end

#list_machines?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def list_machines?(input)
  input == LIST_KEY 
end

#machinesObject



92
93
94
# File 'lib/interactive/session_item/transparent_ssh.rb', line 92

def machines
  @machines ||= has_selected_machines? ? selected_machines : flat_list_of_machines
end

#prompt_for_confirmation(message) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/interactive/session_item/transparent_ssh.rb', line 38

def prompt_for_confirmation(message)
  answer = get_input(message)
  unless ["Y", "N"].include?(answer)
    prompt_for_confirmation(message)
  end 
  return answer == "Y" ? true : false 
end

#resourcesObject



100
101
102
# File 'lib/interactive/session_item/transparent_ssh.rb', line 100

def resources
  @irb_session.resources  
end

#selected_machinesObject



108
109
110
# File 'lib/interactive/session_item/transparent_ssh.rb', line 108

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

#show_menuObject



24
25
26
# File 'lib/interactive/session_item/transparent_ssh.rb', line 24

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

#show_menu?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def show_menu?(input)
  input == HELP_KEY
end

#start_messageObject



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

def start_message
  warning = "\nCommands entered here will be executed on EVERY machine in your selection.".danger
  second_warning = "\n\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



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

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

#valid_response(response) ⇒ Object



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

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