Class: Fog::Compute::RackspaceV2::Server

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

Constant Summary collapse

ACTIVE =

States

'ACTIVE'
BUILD =
'BUILD'
DELETED =
'DELETED'
ERROR =
'ERROR'
HARD_REBOOT =
'HARD_REBOOT'
MIGRATING =
'MIGRATING'
PASSWORD =
'PASSWORD'
REBOOT =
'REBOOT'
REBUILD =
'REBUILD'
RESCUE =
'RESCUE'
RESIZE =
'RESIZE'
REVERT_RESIZE =
'REVERT_RESIZE'
SUSPENDED =
'SUSPENDED'
UNKNOWN =
'UNKNOWN'
VERIFY_RESIZE =
'VERIFY_RESIZE'

Instance Attribute Summary collapse

Attributes inherited from Server

#private_key, #private_key_path, #public_key, #public_key_path, #username

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #ssh_port, #sshable?

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

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

Instance Method Details

#attachmentsObject



92
93
94
95
96
97
98
99
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 92

def attachments
  @attachments ||= begin
    Fog::Compute::RackspaceV2::Attachments.new({
      :connection => connection,
      :server => self
    })
  end
end

#change_admin_password(password) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 146

def change_admin_password(password)
  requires :identity
  connection.change_server_password(identity, password)
  self.state = PASSWORD
  @password = password
  true
end

#confirm_resizeObject



134
135
136
137
138
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 134

def confirm_resize
  requires :identity
  connection.confirm_resize_server(identity)
  true
end

#createObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 56

def create
  requires :name, :image_id, :flavor_id

  options = {}
  options[:disk_config] = disk_config unless disk_config.nil?
  options[:metadata] =  unless .nil?
  options[:personality] = personality unless personality.nil?

  data = connection.create_server(name, image_id, flavor_id, 1, 1, options)
  merge_attributes(data.body['server'])
  true
end

#destroyObject



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

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

#flavorObject



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

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

#imageObject



87
88
89
90
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 87

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

#private_ip_addressObject



101
102
103
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 101

def private_ip_address
  addresses['private'].select{|a| a["version"] == 4}[0]["addr"]
end

#public_ip_addressObject



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

def public_ip_address
  ipv4_address
end

#ready?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 109

def ready?
  state == ACTIVE
end

#reboot(type = 'SOFT') ⇒ Object



113
114
115
116
117
118
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 113

def reboot(type = 'SOFT')
  requires :identity
  connection.reboot_server(identity, type)
  self.state = type == 'SOFT' ? REBOOT : HARD_REBOOT
  true
end

#rebuild(image_id) ⇒ Object



127
128
129
130
131
132
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 127

def rebuild(image_id)
  requires :identity
  connection.rebuild_server(identity, image_id)
  self.state = REBUILD
  true
end

#resize(flavor_id) ⇒ Object



120
121
122
123
124
125
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 120

def resize(flavor_id)
  requires :identity
  connection.resize_server(identity, flavor_id)
  self.state = RESIZE
  true
end

#revert_resizeObject



140
141
142
143
144
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 140

def revert_resize
  requires :identity
  connection.revert_resize_server(identity)
  true
end

#saveObject



47
48
49
50
51
52
53
54
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 47

def save
  if identity
    update
  else
    create
  end
  true
end

#setup(credentials = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fog/rackspace/models/compute_v2/server.rb', line 154

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 "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json},
    %{echo "#{Fog::JSON.encode()}" >> ~/metadata.json}
  ])
rescue Errno::ECONNREFUSED
  sleep(1)
  retry
end

#updateObject



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

def update
  requires :identity, :name
  data = connection.update_server(identity, name)
  merge_attributes(data.body['server'])
  true
end