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

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

Constant Summary collapse

END_SESSION_KEY =
'\\q'
HELP_KEY =
'\\?'
LIST_KEY =
'\\l'
RECONNECT_CMD =
'\\r'
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, #get_input, #has_start_message?, #initialize, #node, #options, #set_response_on_session

Constructor Details

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

Instance Method Details

#actionObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 26

def action
  input = get_input

  return 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
  action
end

#close_ssh_connectionsObject



86
87
88
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 86

def close_ssh_connections
  node.close_ssh_connections
end

#command_may_be_unwise?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#doObject



16
17
18
19
20
21
22
23
24
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 16

def do
  puts ''
  open_ssh_connections
  puts "\nINTERACTIVE COMMAND SESSION".underline
  show_menu
  puts ''
  list_machines
  action
end

#execute_on_machines(user_input) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 103

def execute_on_machines(user_input)
  machines.pmap do |machine|
    begin
      machine.run(user_input)
    rescue IOError => e
      puts "Reopening connection to\s".informational + machine.identifier
      machine.reopen_ssh_connection
      machine.run(user_input)
    rescue Exception => e
      puts "Error connecting to #{machine.identifier} (#{e.message})".error
    end
  end
end

#exit?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def exit?(input)
  input == END_SESSION_KEY
end

#handle_the_unwise(input) ⇒ Object



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

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

#indicate_failed_nodes(unconnected_nodes) ⇒ Object



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

def indicate_failed_nodes(unconnected_nodes)
  unconnected_nodes.each do |node|
    puts "\s\s - #{node.namespace}"
  end
end

#list_machinesObject



96
97
98
99
100
101
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 96

def list_machines
  puts "\n"
  machines.each do |machine|
    puts "- #{machine.namespace}"
  end
end

#list_machines?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def list_machines?(input)
  input == LIST_KEY
end

#machinesObject



11
12
13
14
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 11

def machines
  skip_for_hidden = true
  node.server? ? [node] : node.machines(skip_for_hidden)
end

#open_ssh_connectionsObject



81
82
83
84
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 81

def open_ssh_connections
  ::Bcome::Ssh::Connector.connect(node, show_progress: true)
  # system('clear')
end

#prompt_for_confirmation(message) ⇒ Object



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

def prompt_for_confirmation(message)
  answer = get_input(message)
  prompt_for_confirmation(message) unless %w[Y N].include?(answer)
  answer == 'Y'
end

#show_menuObject



43
44
45
46
47
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 43

def show_menu
  warning = "\nCommands entered here will be executed on" + "\severy\s".warning + "machine in your selection. \n\nUse with caution or hit \\q if you're unsure what this is."
  info = "\n\n\\l list machines\n\\q to quit\n\\? this message".informational
  puts warning + "#{info}\n"
end

#show_menu?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def show_menu?(input)
  input == HELP_KEY
end

#start_messageObject



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

def start_message; end

#terminal_promptObject



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

def terminal_prompt
  "enter a command>\s"
end