Class: Fog::Rackspace::Servers::Server

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/rackspace/models/servers/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.



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

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

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#private_key_pathObject

Returns the value of attribute private_key_path.



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

def private_key_path
  @private_key_path
end

#public_key_pathObject

Returns the value of attribute public_key_path.



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

def public_key_path
  @public_key_path
end

Instance Method Details

#destroyObject



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

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

#flavorObject



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

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

#imageObject



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

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

#imagesObject



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

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

#ready?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/fog/rackspace/models/servers/server.rb', line 49

def ready?
  @status == 'ACTIVE'
end

#reboot(type = 'SOFT') ⇒ Object



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

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

#saveObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/rackspace/models/servers/server.rb', line 67

def save
  requires :flavor_id, :image_id
  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

#setupObject



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

def setup
  requires :addresses, :identity, :password, :public_key_path
  Fog::SSH.new(@addresses['public'].first, 'root', :password => password).run([
    %{mkdir .ssh},
    %{echo "#{File.read(File.expand_path(public_key_path))}" >> ~/.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



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

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