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, #identity, #identity=, #merge_attributes, #new_record?, #requires

Constructor Details

#initialize(attributes = {}) ⇒ Server

Returns a new instance of Server.



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

def initialize(attributes={})
  self.flavor_id ||= 1
  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



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

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

#private_key_pathObject



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

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

#public_keyObject



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

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

#public_key_pathObject



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

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

#usernameObject



114
115
116
# File 'lib/fog/compute/models/rackspace/server.rb', line 114

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



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

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

#imagesObject



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

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

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  status == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



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

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

#saveObject

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fog/compute/models/rackspace/server.rb', line 78

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

#setup(credentials = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/compute/models/rackspace/server.rb', line 92

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

#ssh(commands) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/fog/compute/models/rackspace/server.rb', line 106

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

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