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

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

Instance Attribute Summary collapse

Attributes inherited from Model

#connection

Instance Method Summary collapse

Methods inherited from Model

#collection, #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/rackspace/models/compute/server.rb', line 24

def initialize(attributes={})
  @flavor_id ||= 1
  super
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#private_keyObject



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

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

#private_key_pathObject



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

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

#public_keyObject



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

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

#public_key_pathObject



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

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

#usernameObject



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

def username
  @username ||= 'root'
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



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

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

#imagesObject



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

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

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  @status == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



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

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

#saveObject

Raises:



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

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :flavor_id, :image_id, :name
  options = {
    'metadata'    => @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



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

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



104
105
106
107
108
# File 'lib/fog/rackspace/models/compute/server.rb', line 104

def ssh(commands)
  requires :addresses, :identity, :private_key, :username
  @ssh ||= Fog::SSH.new(addresses['public'].first, username, :key_data => [private_key])
  @ssh.run(commands)
end