Class: Bcome::Ssh::ScriptExec

Inherits:
Object
  • Object
show all
Defined in:
lib/objects/ssh/script_exec.rb

Constant Summary collapse

SCRIPTS_PATH =
'bcome/scripts'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, script_name) ⇒ ScriptExec

Returns a new instance of ScriptExec.



12
13
14
15
16
# File 'lib/objects/ssh/script_exec.rb', line 12

def initialize(server, script_name)
  @server = server
  @script_name = script_name
  @ssh_driver = server.ssh_driver
end

Class Method Details

.execute(server, script_name) ⇒ Object



6
7
8
9
# File 'lib/objects/ssh/script_exec.rb', line 6

def execute(server, script_name)
  executor = new(server, script_name)
  executor.execute
end

Instance Method Details

#executeObject



18
19
20
21
22
# File 'lib/objects/ssh/script_exec.rb', line 18

def execute
  command = execute_command
  pretty_print(command)
  command
end

#execute_commandObject



24
25
26
27
28
29
30
# File 'lib/objects/ssh/script_exec.rb', line 24

def execute_command
  local_path_to_script = "#{SCRIPTS_PATH}/#{@script_name}.sh"
  raise Bcome::Exception::OrchestrationScriptDoesNotExist, local_path_to_script unless File.exist?(local_path_to_script)
  execute_script_command = "#{@ssh_driver.ssh_command} \"bash -s\" < #{local_path_to_script}"
  command = ::Bcome::Command::Local.run(execute_script_command)
  command
end

#output_append(output_string) ⇒ Object



39
40
41
# File 'lib/objects/ssh/script_exec.rb', line 39

def output_append(output_string)
  @output_string = "#{@output_string}#{output_string}"
end

#pretty_print(command) ⇒ Object



32
33
34
35
36
37
# File 'lib/objects/ssh/script_exec.rb', line 32

def pretty_print(command)
  output_append("\n(#{@server.namespace})$".terminal_prompt + "> ./#{SCRIPTS_PATH}/#{@script_name}.sh - \s#{command.pretty_result}\n")
  output_append(command.stdout) # append stderr
  output_append "\nSTDERR: #{command.stderr}" if command.failed?
  puts "\n\n#{@output_string}\n\n"
end