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, #connection

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 Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #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

#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



32
33
34
35
36
37
38
39
# File 'lib/fog/compute/models/server.rb', line 32

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

  scp_options = {:port => ssh_port}
  scp_options[:key_data] = [private_key] if private_key
  Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end

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



43
44
45
46
47
48
49
50
# File 'lib/fog/compute/models/server.rb', line 43

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

  scp_options = {:port => ssh_port}
  scp_options[:key_data] = [private_key] if private_key
  Fog::SCP.new(public_ip_address, username, scp_options).download(remote_path, local_path, download_options)
end

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



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

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

  options[:key_data] = [private_key] if private_key
  options[:port] ||= ssh_port
  Fog::SSH.new(public_ip_address, username, options).run(commands, &blk)
end

#ssh_portObject



61
62
63
# File 'lib/fog/compute/models/server.rb', line 61

def ssh_port
  22
end

#sshable?Boolean

Returns:

  • (Boolean)


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

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