Class: Bosh::Bootstrap::Commander::RemoteServer

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh-bootstrap/commander/remote_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, private_key_path, logfile = STDERR) ⇒ RemoteServer

Returns a new instance of RemoteServer.



11
12
13
14
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 11

def initialize(host, private_key_path, logfile=STDERR)
  @host, @private_key_path, @logfile = host, private_key_path, logfile
  @default_ssh_username = "vcap" # unless overridden by a Command (before vcap exists)
end

Instance Attribute Details

#default_ssh_usernameObject (readonly)

Returns the value of attribute default_ssh_username.



8
9
10
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 8

def default_ssh_username
  @default_ssh_username
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 6

def host
  @host
end

#logfileObject (readonly)

Returns the value of attribute logfile.



9
10
11
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 9

def logfile
  @logfile
end

#private_key_pathObject (readonly)

Returns the value of attribute private_key_path.



7
8
9
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 7

def private_key_path
  @private_key_path
end

Instance Method Details

#run(commands) ⇒ Object

Execute the Command objects, in sequential order upon the local server commands is a Commands container

Returns false once any subcommand fails to execute successfully



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 21

def run(commands)
  commands.commands.each do |command|
    puts command.full_present_tense
    if command.perform(self)
      Thor::Base.shell.new.say "Successfully #{command.full_past_tense}", :green
    else
      Thor::Base.shell.new.say_status "error", "#{command.full_present_tense}", :red
      return false
    end
  end
end

#run_script(command, script, options = {}) ⇒ Object

Run a script

Uploads it to the remote server, makes it executable, then executes Stores the last line of stripped STDOUT/STDERR into a settings field,

if :settings & :save_output_to_settings_key => "x.y.z" provided


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 44

def run_script(command, script, options={})
  ssh_username = options[:ssh_username] || default_ssh_username
  run_as_root  = options[:run_as_root]
  settings     = options[:settings]
  settings_key = options[:save_output_to_settings_key]

  remote_path = remote_tmp_script_path(command)
  upload_file(command, remote_path, script, ssh_username)
  output, status = run_remote_script(remote_path, ssh_username, run_as_root)
  output =~ /^(.*)\Z/
  last_line = $1
  # store output into a settings field, if requested
  # TODO replace this with SettingsSetting#setting(settings_key, last_line.strip)

  puts "Last line of script: #{last_line}"
  puts "Storing to settings_key #{settings_key}" if settings_key
  if settings_key
    settings_key_portions = settings_key.split(".")
    parent_key_portions, final_key = settings_key_portions[0..-2], settings_key_portions[-1]
    target_settings_field = settings
    parent_key_portions.each do |key_portion|
      target_settings_field[key_portion] ||= {}
      target_settings_field = target_settings_field[key_portion]
    end
    target_settings_field[final_key] = last_line.strip
  end

  status
rescue StandardError => e
  logfile.puts e.message
  false
end

#upload_file(command, remote_path, contents, ssh_username = nil) ⇒ Object

Upload a file (put a file into the remote server’s filesystem)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bosh-bootstrap/commander/remote_server.rb', line 78

def upload_file(command, remote_path, contents, ssh_username=nil)
  upload_as_user = ssh_username || default_ssh_username
  run_remote_command("mkdir -p #{File.dirname(remote_path)}", upload_as_user)
  Tempfile.open("remote_script") do |file|
    file << contents
    file.flush
    logfile.puts "uploading #{remote_path} to Inception VM"
    Net::SCP.upload!(host, upload_as_user, file.path, remote_path, ssh: { keys: private_keys })
  end
  true
rescue StandardError => e
  logfile.puts "ERROR running upload_file(#{command.class}, '#{remote_path}', ...)"
  logfile.puts e.message
  logfile.puts e.backtrace
  false
end