Class: Fog::Compute::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/compute/models/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Instance Attribute Details

#private_keyObject



18
19
20
# File 'lib/fog/compute/models/server.rb', line 18

def private_key
  @private_key ||= private_key_path && File.read(private_key_path)
end

#private_key_pathObject



13
14
15
16
# File 'lib/fog/compute/models/server.rb', line 13

def private_key_path
  @private_key_path ||= Fog.credentials[:private_key_path]
  @private_key_path &&= File.expand_path(@private_key_path)
end

#public_keyObject



27
28
29
# File 'lib/fog/compute/models/server.rb', line 27

def public_key
  @public_key ||= public_key_path && File.read(public_key_path)
end

#public_key_pathObject



22
23
24
25
# File 'lib/fog/compute/models/server.rb', line 22

def public_key_path
  @public_key_path ||= Fog.credentials[:public_key_path]
  @public_key_path &&= File.expand_path(@public_key_path)
end

#ssh_optionsObject



61
62
63
64
65
66
67
68
69
# File 'lib/fog/compute/models/server.rb', line 61

def ssh_options
  @ssh_options ||= {}
  ssh_options = @ssh_options.merge({:port => ssh_port})
  if private_key
    ssh_options[:key_data] = [private_key]
    ssh_options[:auth_methods] = ["publickey"]
  end
  ssh_options
end

#ssh_portInteger

Note:

By default this returns 22

Port used for ssh/scp interactions with server.

Returns:

  • (Integer)

    IP port



34
35
36
# File 'lib/fog/compute/models/server.rb', line 34

def ssh_port
  @ssh_port ||= 22
end

#usernameObject



9
10
11
# File 'lib/fog/compute/models/server.rb', line 9

def username
  @username ||= 'root'
end

Instance Method Details

#scp(local_path, remote_path, upload_options = {}) ⇒ Object Also known as: scp_upload



71
72
73
74
75
76
# File 'lib/fog/compute/models/server.rb', line 71

def scp(local_path, remote_path, upload_options = {})
  require 'net/scp'
  requires :ssh_ip_address, :username

  Fog::SCP.new(ssh_ip_address, username, ssh_options).upload(local_path, remote_path, upload_options)
end

#scp_download(remote_path, local_path, download_options = {}) ⇒ Object



80
81
82
83
84
85
# File 'lib/fog/compute/models/server.rb', line 80

def scp_download(remote_path, local_path, download_options = {})
  require 'net/scp'
  requires :ssh_ip_address, :username

  Fog::SCP.new(ssh_ip_address, username, ssh_options).download(remote_path, local_path, download_options)
end

#ssh(commands, options = {}, &blk) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/fog/compute/models/server.rb', line 87

def ssh(commands, options={}, &blk)
  require 'net/ssh'
  requires :ssh_ip_address, :username

  options = ssh_options.merge(options)

  Fog::SSH.new(ssh_ip_address, username, options).run(commands, &blk)
end

#ssh_ip_addressString

Note:

By default this returns the public_ip_address

IP Address used for ssh/scp interactions with server.

Returns:



55
56
57
58
59
# File 'lib/fog/compute/models/server.rb', line 55

def ssh_ip_address
  return public_ip_address unless @ssh_ip_address
  return @ssh_ip_address.call(self) if @ssh_ip_address.is_a?(Proc)
  @ssh_ip_address
end

#ssh_ip_address=(proc) ⇒ Object

Note:

By default scp/ssh will use the public_ip_address if this proc is not set.

Sets the proc used to determine the IP Address used for ssh/scp interactions.

Examples:

service.servers.bootstrap :name => 'bootstrap-server',
                          :flavor_id => service.flavors.first.id,
                          :image_id => service.images.find {|img| img.name =~ /Ubuntu/}.id,
                          :public_key_path => '~/.ssh/fog_rsa.pub',
                          :private_key_path => '~/.ssh/fog_rsa',
                          :ssh_ip_address => Proc.new {|server| server.private_ip_address }


48
49
50
# File 'lib/fog/compute/models/server.rb', line 48

def ssh_ip_address=(proc)
  @ssh_ip_address = proc
end

#sshable?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/fog/compute/models/server.rb', line 96

def sshable?(options={})
  ready? && !ssh_ip_address.nil? && !!Timeout::timeout(8) { ssh('pwd', options) }
rescue SystemCallError, Net::SSH::AuthenticationFailed, Net::SSH::Disconnect, Timeout::Error
  false
end