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'.freeze
HELP_KEY =
'\\?'.freeze
LIST_KEY =
'\\l'.freeze
DANGER_CMD =
"rm\s+-r|rm\s+-f|rm\s+-fr|rm\s+-rf|rm".freeze

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



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

def action
  input = get_input

  if exit?(input)
    close_ssh_connections
    return
  end

  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



88
89
90
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 88

def close_ssh_connections
  node.close_ssh_connections
end

#command_may_be_unwise?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#doObject



13
14
15
16
17
18
19
20
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 13

def do
  puts ''
  open_ssh_connections
  show_menu
  puts ''
  list_machines
  action
end

#execute_on_machines(user_input) ⇒ Object



105
106
107
108
109
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 105

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

#exit?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def exit?(input)
  input == END_SESSION_KEY
end

#handle_the_unwise(input) ⇒ Object



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

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] > '.error)
    execute_on_machines(input)
  end
end

#indicate_failed_nodes(unconnected_nodes) ⇒ Object



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

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

#list_machinesObject



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

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

#list_machines?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def list_machines?(input)
  input == LIST_KEY
end

#machinesObject



9
10
11
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 9

def machines
  node.server? ? [node] : node.machines
end

#open_ssh_connectionsObject



83
84
85
86
# File 'lib/objects/interactive/session_item/transparent_ssh.rb', line 83

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

#prompt_for_confirmation(message) ⇒ Object



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

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

#show_menuObject



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

def show_menu
  warning = "\n\sCommands entered here will be executed on " + 'EVERY'.underline + ' machine in your selection.'
  second_warning = "\n\n\s" + 'Use with CAUTION.'.warning
  info = "\n\n\s\\l list machines\n\s\\q to quit\n\s\\? this message".informational
  puts "#{warning}#{second_warning}#{info}\n"
end

#show_menu?(input) ⇒ Boolean

Returns:

  • (Boolean)


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

def show_menu?(input)
  input == HELP_KEY
end

#start_messageObject



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

def start_message; end

#terminal_promptObject



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

def terminal_prompt
  ">\s".bold.terminal_prompt
end