Class: Fog::Rackspace::Compute::Server

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

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#inspect, #reload, #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

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



24
25
26
27
28
# File 'lib/fog/compute/models/rackspace/server.rb', line 24

def initialize(attributes={})
  self.flavor_id  ||= 1  # 256 server
  self.image_id   ||= 49 # Ubuntu 10.04 LTS 64bit
  super
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



21
22
23
# File 'lib/fog/compute/models/rackspace/server.rb', line 21

def password
  @password
end

#private_keyObject



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

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

#private_key_pathObject



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

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

#public_keyObject



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

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

#public_key_pathObject



68
69
70
71
# File 'lib/fog/compute/models/rackspace/server.rb', line 68

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

#usernameObject



131
132
133
# File 'lib/fog/compute/models/rackspace/server.rb', line 131

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



30
31
32
33
34
# File 'lib/fog/compute/models/rackspace/server.rb', line 30

def destroy
  requires :id
  connection.delete_server(id)
  true
end

#flavorObject



36
37
38
39
# File 'lib/fog/compute/models/rackspace/server.rb', line 36

def flavor
  requires :flavor_id
  connection.flavors.get(flavor_id)
end

#imageObject



41
42
43
44
# File 'lib/fog/compute/models/rackspace/server.rb', line 41

def image
  requires :image_id
  connection.images.get(image_id)
end

#imagesObject



46
47
48
49
# File 'lib/fog/compute/models/rackspace/server.rb', line 46

def images
  requires :id
  connection.images(:server => self)
end

#private_ip_addressObject



51
52
53
# File 'lib/fog/compute/models/rackspace/server.rb', line 51

def private_ip_address
  nil
end

#public_ip_addressObject



64
65
66
# File 'lib/fog/compute/models/rackspace/server.rb', line 64

def public_ip_address
  addresses['public'].first
end

#ready?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/fog/compute/models/rackspace/server.rb', line 77

def ready?
  self.state == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



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

def reboot(type = 'SOFT')
  requires :id
  connection.reboot_server(id, type)
  true
end

#saveObject

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fog/compute/models/rackspace/server.rb', line 87

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_id, :image_id
  options = {
    'metadata'    => ,
    'name'        => name,
    'personality' => personality
  }
  options = options.reject {|key, value| value.nil?}
  data = connection.create_server(flavor_id, image_id, options)
  merge_attributes(data.body['server'])
  true
end

#scp(local_path, remote_path) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/fog/compute/models/rackspace/server.rb', line 123

def scp(local_path, remote_path)
  requires :public_ip_address, :username

  options = {}
  options[:key_data] = [private_key] if private_key
  Fog::SCP.new(public_ip_address, username, options).upload(local_path, remote_path)
end

#setup(credentials = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fog/compute/models/rackspace/server.rb', line 101

def setup(credentials = {})
  requires :public_ip_address, :identity, :public_key, :username
  Fog::SSH.new(public_ip_address, username, credentials).run([
    %{mkdir .ssh},
    %{echo "#{public_key}" >> ~/.ssh/authorized_keys},
    %{passwd -l #{username}},
    %{echo "#{attributes.to_json}" >> ~/attributes.json},
    %{echo "#{.to_json}" >> ~/metadata.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#ssh(commands) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/fog/compute/models/rackspace/server.rb', line 115

def ssh(commands)
  requires :public_ip_address, :identity, :username

  options = {}
  options[:key_data] = [private_key] if private_key
  Fog::SSH.new(public_ip_address, username, options).run(commands)
end