Class: Fog::Vcloud::Compute::Server

Inherits:
Model show all
Defined in:
lib/fog/vcloud/models/compute/server.rb

Instance Attribute Summary

Attributes inherited from Model

#loaded

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Model

#load_unless_loaded!

Methods inherited from Model

#initialize, #inspect, #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 Method Details

#add_disk(size) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/fog/vcloud/models/compute/server.rb', line 127

def add_disk(size)
  if @disk_change == :deleted
    raise RuntimeError, "Can't add a disk w/o saving changes or reloading"
  else
    load_unless_loaded!
    @disk_change = :added

    @add_disk = {
      :'rasd:HostResource' => {:vcloud_capacity => size},
      :'rasd:AddressOnParent' =>  (disk_mess.map { |dm| dm[:'rasd:AddressOnParent'] }.sort.last.to_i + 1).to_s,
      :'rasd:ResourceType' => '17'
    }
  end
  true
end

#cpusObject



96
97
98
99
100
101
# File 'lib/fog/vcloud/models/compute/server.rb', line 96

def cpus
  if cpu_mess
    { :count => cpu_mess[:"rasd:VirtualQuantity"].to_i,
      :units => cpu_mess[:"rasd:AllocationUnits"] }
  end
end

#cpus=(qty) ⇒ Object



103
104
105
106
# File 'lib/fog/vcloud/models/compute/server.rb', line 103

def cpus=(qty)
  @changed = true
  cpu_mess[:"rasd:VirtualQuantity"] = qty.to_s
end

#delete_disk(number) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fog/vcloud/models/compute/server.rb', line 143

def delete_disk(number)
  if @disk_change == :added
    raise RuntimeError, "Can't delete a disk w/o saving changes or reloading"
  else
    load_unless_loaded!
    unless number == 0
      @disk_change = :deleted
      @remove_disk = number
    end
  end
  true
end

#description=(description) ⇒ Object



156
157
158
159
# File 'lib/fog/vcloud/models/compute/server.rb', line 156

def description=(description)
  @description_changed = true unless attributes[:description] == description || attributes[:description] == nil
  attributes[:description] = description
end

#destroyObject Also known as: delete



207
208
209
210
211
212
213
214
215
216
# File 'lib/fog/vcloud/models/compute/server.rb', line 207

def destroy
  if on?
    undeploy
    wait_for { off? }
  end
  wait_for { off? } # be sure..
  wait_for { ready? } # be doubly sure..
  sleep 2 # API lies. need to give it some time to be happy.
  connection.delete_vapp(href).body[:status] == "running"
end

#disksObject



121
122
123
124
125
# File 'lib/fog/vcloud/models/compute/server.rb', line 121

def disks
  disk_mess.map do |dm|
    { :number => dm[:"rasd:AddressOnParent"], :size => dm[:"rasd:VirtualQuantity"].to_i, :resource => dm[:"rasd:HostResource"] }
  end
end

#friendly_statusObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/vcloud/models/compute/server.rb', line 28

def friendly_status
  load_unless_loaded!
  case status
  when '0'
    'creating'
  when '8'
    'off'
  when '4'
    'on'
  else
    'unkown'
  end
end

#graceful_restartObject



79
80
81
82
83
84
# File 'lib/fog/vcloud/models/compute/server.rb', line 79

def graceful_restart
  requires :href
  shutdown
  wait_for { off? }
  power_on
end

#ip_addressObject



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

def ip_address
  load_unless_loaded!
  vm[0][:NetworkConnectionSection][:NetworkConnection][:IpAddress]
end

#memoryObject



108
109
110
111
112
113
# File 'lib/fog/vcloud/models/compute/server.rb', line 108

def memory
  if memory_mess
    { :amount => memory_mess[:"rasd:VirtualQuantity"].to_i,
      :units => memory_mess[:"rasd:AllocationUnits"] }
  end
end

#memory=(amount) ⇒ Object



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

def memory=(amount)
  @changed = true
  @update_memory_value = amount
  amount
end

#name=(name) ⇒ Object



91
92
93
94
# File 'lib/fog/vcloud/models/compute/server.rb', line 91

def name=(new_name)
  attributes[:name] = new_name
  @changed = true
end

#off?Boolean

Returns:

  • (Boolean)


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

def off?
  reload_status # always ensure we have the correct status
  status == '8'
end

#on?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/fog/vcloud/models/compute/server.rb', line 48

def on?
  reload_status # always ensure we have the correct status
  status == '4'
end

#power_offObject



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

def power_off
  power_operation( :power_off => :powerOff )
end

#power_onObject



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

def power_on
  power_operation( :power_on => :powerOn )
end

#power_resetObject



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

def power_reset
  power_operation( :power_reset => :reset )
end

#ready?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/fog/vcloud/models/compute/server.rb', line 42

def ready?
  reload_status # always ensure we have the correct status
  running_tasks = tasks && tasks.flatten.any? {|ti| ti.kind_of?(Hash) && ti[:status] == 'running' }
  status != '0' && !running_tasks # 0 is provisioning, and no running tasks
end

#reloadObject



166
167
168
169
# File 'lib/fog/vcloud/models/compute/server.rb', line 166

def reload
  reset_tracking
  super
end

#saveObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/fog/vcloud/models/compute/server.rb', line 171

def save
  if new_record?
    #Lame ...
    raise RuntimeError, "Should not be here"
  else
    if on?
      if @changed
        raise RuntimeError, "Can't save cpu, name or memory changes while the VM is on."
      end
    end
    if @update_memory_value
      memory_mess[:"rasd:VirtualQuantity"] = @update_memory_value.to_s
      connection.configure_vm_memory(memory_mess)
    end
    if @disk_change == :deleted
      data = disk_mess.delete_if do |vh|
        vh[:'rasd:ResourceType'] == '17' &&
          vh[:'rasd:AddressOnParent'].to_s == @remove_disk.to_s
      end
      connection.configure_vm_disks(vm_href, data)
    end
    if @disk_change == :added
      data = disk_mess
      data << @add_disk
      connection.configure_vm_disks(vm_href, data)
    end
    if @name_changed || @description_changed
      edit_uri = links.select {|i| i[:rel] == 'edit'}
      edit_uri = edit_uri.kind_of?(Array) ? edit_uri.flatten[0][:href] : edit_uri[:href]
      connection.configure_vm_name_description(edit_uri, self.name, self.description)
    end
  end
  reset_tracking
  true
end

#shutdownObject



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

def shutdown
  power_operation( :power_shutdown => :shutdown )
end

#undeployObject

This is the real power-off operation



75
76
77
# File 'lib/fog/vcloud/models/compute/server.rb', line 75

def undeploy
  connection.undeploy href
end

#vmObject



86
87
88
89
# File 'lib/fog/vcloud/models/compute/server.rb', line 86

def vm
  load_unless_loaded!
  self.vm_data
end

#vm_hrefObject



219
220
221
222
223
224
225
# File 'lib/fog/vcloud/models/compute/server.rb', line 219

def vm_href
  load_unless_loaded!
  #require 'pp'
  #pp vm_data
  #vm_data[0][:Link].select {|v| v[:rel] == 'edit'}[0][:href]
  vm_data.kind_of?(Array)? vm_data[0][:href] : vm_data[:href]
end