Module: CloudProviders::Connections

Included in:
CloudProviderInstance
Defined in:
lib/cloud_providers/connections.rb

Instance Method Summary collapse

Instance Method Details

#host(n = nil) ⇒ Object

hostname or ip to use when running remote commands



15
16
17
18
19
20
21
# File 'lib/cloud_providers/connections.rb', line 15

def host(n=nil)
  if n.nil? 
    @host ||= dns_name
  else
    @host = n
  end
end

#rsync(opts = {}) ⇒ Object

Raises:

  • (StandardError)


51
52
53
54
55
56
57
58
# File 'lib/cloud_providers/connections.rb', line 51

def rsync( opts={} )
  raise StandardError.new("You must pass a :source=>uri option to rsync") unless opts[:source]
  destination_path = opts[:destination] || opts[:source]
  rsync_opts = opts[:rsync_opts] || '-va'
  cmd_string =  "rsync -L -e 'ssh #{ssh_options}' #{rsync_opts} #{opts[:source]}  #{user}@#{host}:#{destination_path}"
  out = system_run(cmd_string)
  out
end

#run(commands, o = {}) ⇒ Object



23
24
25
# File 'lib/cloud_providers/connections.rb', line 23

def run(commands, o={})
  ssh(commands)
end

#scp(opts = {}) ⇒ Object

Raises:

  • (StandardError)


60
61
62
63
64
65
66
67
68
# File 'lib/cloud_providers/connections.rb', line 60

def scp(opts={})
  source = opts[:source]
  destination_path = opts[:destination] || opts[:source]
  raise StandardError.new("You must pass a local_file to scp") unless source
  scp_opts = opts[:scp_opts] || ""
  cmd_string = "scp #{ssh_options(scp_opts)} #{source} #{user}@#{host}:#{destination_path}"
  out = system_run(cmd_string)
  out
end

#ssh(commands = [], extra_ssh_ops = {}) ⇒ Object

Simply shell out and call ssh, simple, reliable and fewest dependencies, but slow



28
29
30
31
32
33
34
35
36
37
# File 'lib/cloud_providers/connections.rb', line 28

def ssh( commands=[], extra_ssh_ops={})
  commands = commands.compact.join(' && ') if commands.is_a?(Array)
  cmd_string = "ssh #{user}@#{host} #{ssh_options(extra_ssh_ops)} "
  if commands.empty?
    #TODO: replace this with a IO.popen call with read_nonblocking to show progress, and accept input
    Kernel.system(cmd_string)
  else
    system_run(cmd_string+"'#{commands}'")
  end
end

#ssh_options(opts = {}) ⇒ Object

Take a hash of options and join them into a string, combined with default options. Default options are -o StrictHostKeyChecking=no -i keypair.full_filepath -l user {‘-i’=>‘keyfile, ’-l’ => ‘fred’ } would become “-i keyfile -o StrictHostKeyChecking=no -i keypair.to_s -l fred”



43
44
45
46
47
48
49
# File 'lib/cloud_providers/connections.rb', line 43

def ssh_options(opts={})
  return @ssh_options if @ssh_options && opts.empty?
  ssh_options = {"-i" => keypair.full_filepath,
       "-o" =>"StrictHostKeyChecking=no"
       }.merge(opts)
  @ssh_options = ssh_options.collect{ |k,v| "#{k} #{v}"}.join(' ')
end

#user(n = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/cloud_providers/connections.rb', line 6

def user(n=nil)
  if n.nil? 
    @user ||= 'root'
  else
    @user = n
  end
end