Module: Bcome::InstanceCommand

Included in:
Node::Instance, Stack::Instance
Defined in:
lib/helpers/instance_command.rb

Instance Method Summary collapse

Instance Method Details

#direct_rsync(local_path, remote_path) ⇒ Object



32
33
34
# File 'lib/helpers/instance_command.rb', line 32

def direct_rsync(local_path, remote_path)
  "rsync #{rsync_is_sudo}-av #{local_path} #{self.ssh_user}@#{self.ip_address}:#{remote_path}"
end

#direct_rsync_bootstrap(local_path, remote_path) ⇒ Object



40
41
42
# File 'lib/helpers/instance_command.rb', line 40

def direct_rsync_bootstrap(local_path, remote_path)   
  "rsync -i #{bootstrap_settings[:key]} #{rsync_is_sudo}-av #{local_path} #{bootstrap_settings[:ssh_user]}@#{self.ip_address}:#{remote_path}"
end

#execute_command(commands, bootstrap = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/helpers/instance_command.rb', line 8

def execute_command(commands, bootstrap = false)
  begin
    return @environment.execute_command(commands, self, bootstrap)
  rescue
    unless bootstrap && bootstrap_settings  ### Try bootstrap fallback
      puts "\n BOOTSTRAP MODE".informational  
      bootstrap = true
      return @environment.execute_command(commands, self, bootstrap)
    end
  end
end

#get(remote_path, local_path = local_download_path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/helpers/instance_command.rb', line 48

def get(remote_path, local_path = local_download_path)
  raise "No local path specified" unless local_path
  raise "No remote_path specified" unless remote_path

  if @environment.ssh_mode_type == ::SSH_DIRECT_MODE_IDENTIFIER
    rsync_command = "rsync #{rsync_is_sudo} -chavzP #{self.ssh_user}@#{self.ip_address}:#{remote_path} #{local_path}"
  else
    rsync_command = "rsync -chavzP -av -e \"ssh -A #{self.nat_user}@#{@environment.bastion_ip_address} ssh -o StrictHostKeyChecking=no\" #{rsync_is_sudo}#{self.ssh_user}@#{self.ip_address}:#{remote_path} #{local_path}"
  end

  silent = true
  local("mkdir -p #{local_path}", silent)
  run_local(rsync_command)
  puts "done: files copied to #{local_path}".informational
end

#jump_host_rsync(local_path, remote_path) ⇒ Object



36
37
38
# File 'lib/helpers/instance_command.rb', line 36

def jump_host_rsync(local_path, remote_path)
  "rsync -av -e \"ssh -A #{self.nat_user}@#{@environment.bastion_ip_address} ssh -o StrictHostKeyChecking=no\" #{rsync_is_sudo}#{local_path} #{self.ssh_user}@#{self.ip_address}:#{remote_path}"
end

#jump_host_rsync_bootstrap(local_path, remote_path) ⇒ Object



44
45
46
# File 'lib/helpers/instance_command.rb', line 44

def jump_host_rsync_bootstrap(local_path, remote_path) 
  "rsync -av -e \"ssh -i #{bootstrap_settings[:key]} -A #{bootstrap_settings[:ssh_user]}@#{@environment.bastion_ip_address} ssh -o StrictHostKeyChecking=no\" #{rsync_is_sudo}#{local_path} #{bootstrap_settings[:ssh_user]}@#{self.ip_address}:#{remote_path}"  
end

#put(local_path, remote_path, bootstrap = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/helpers/instance_command.rb', line 20

def put(local_path, remote_path, bootstrap = false)
  puts "rsync> #{self.identifier}".informational
  if @environment.ssh_mode_type == ::SSH_DIRECT_MODE_IDENTIFIER
    ## DIRECT MODE
    rsync_command = bootstrap ? direct_rsync_bootstrap(local_path, remote_path) : direct_rsync(local_path, remote_path)
  else
    ## JUMP HOST MODE
    rsync_command = bootstrap ? jump_host_rsync_bootstrap(local_path, remote_path)  : jump_host_rsync(local_path, remote_path)
  end
  run_local(rsync_command)
end

#rsync_is_sudoObject



64
65
66
# File 'lib/helpers/instance_command.rb', line 64

def rsync_is_sudo
  is_sudo? ? " --rsync-path=\"sudo rsync\" " : ""
end

#run(raw_commands, bootstrap = false) ⇒ Object



3
4
5
6
# File 'lib/helpers/instance_command.rb', line 3

def run(raw_commands, bootstrap = false)
  commands = raw_commands.is_a?(String) ? [raw_commands] : raw_commands
  return execute_command(commands, bootstrap)
end