Class: Fog::Compute::Ecloud::Server

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

Instance Attribute Summary

Attributes inherited from Ecloud::Model

#loaded

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods inherited from Ecloud::Model

#load_unless_loaded!, #reload

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 Method Details

#add_disk(size) ⇒ Object



173
174
175
176
177
178
# File 'lib/fog/ecloud/models/compute/server.rb', line 173

def add_disk(size)
  index = disks.map { |d| d[:Index].to_i }.sort[-1] + 1
  vm_disks = disks << {:Index => index, :Size=>{:Unit => "GB", :Value => size}}
  data = connection.virtual_machine_edit_hardware_configuration(href + "/hardwareConfiguration", _configuration_data(:disks => vm_disks)).body
  task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0]
end

#add_ip(options) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fog/ecloud/models/compute/server.rb', line 193

def add_ip(options)
  begin
    slice_ips = ips
  rescue
    slice_ips = []
  end
  begin
    slice_networks = networks
  rescue
    slice_networks = []
  end
  slice_networks = slice_networks.map { |n| {:href => n.href, :name => n.name.split(' ')[0], :type => 'application/vnd.tmrk.cloud.network'} }.push({:href => options[:href], :name => options[:network_name], :type => 'application/vnd.tmrk.cloud.network'}).uniq
  slice_ips = slice_ips.map { |i| {:name => i.name, :network_name => i.other_links[:Link][:name]} }.push({:name => options[:ip], :network_name => options[:network_name]})
  slice_ips.each do |ip|
    slice_networks.each do |network|
      if network[:name] == ip[:network_name]
        network[:ips] ||= []
        network[:ips].push(ip[:name])
      end
    end
  end
  data = connection.virtual_machine_add_ip(href + "/assignedIps", slice_networks).body
  task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0]
end

#add_nic(network) ⇒ Object



186
187
188
189
190
191
# File 'lib/fog/ecloud/models/compute/server.rb', line 186

def add_nic(network)
  unit_number = nics.map { |n| n[:UnitNumber].to_i }.sort[-1] + 1
  vm_nics = nics << {:UnitNumber => unit_number, :Network => {:href => network.href, :name => network.name, :type => "application/vnd.tmrk.cloud.network"}}
  data = connection.virtual_machine_edit_hardware_configuration(href + "/hardwareConfiguration", _configuration_data(:nics => vm_nics)).body
  task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0]
end

#compute_pool_idObject



245
246
247
# File 'lib/fog/ecloud/models/compute/server.rb', line 245

def compute_pool_id
  other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.computePool" }[:href].scan(/\d+/)[0]
end

#configurationObject



34
35
36
# File 'lib/fog/ecloud/models/compute/server.rb', line 34

def configuration
  @configuration ||= Fog::Compute::Ecloud::ServerConfigurationOptions.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/configurationOptions")[0]
end

#copy(options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fog/ecloud/models/compute/server.rb', line 102

def copy(options = {})
  options = {:type => :copy}.merge(options)
  options[:source] ||= href
  if options[:type] == :copy
    options[:cpus] ||= 1
    options[:memory] ||= 512
    options[:customization] ||= :linux
    options[:tags] ||= []
    options[:powered_on] ||= false
    if options[:ips]
      options[:ips] = options[:ips].is_a?(String) ? [options[:ips]] : options[:ips]
    else
      options[:network_uri] = options[:network_uri].is_a?(String) ? [options[:network_uri]] : options[:network_uri]
      options[:network_uri].each do |uri|
        index = options[:network_uri].index(uri)
        ip = Fog::Compute::Ecloud::IpAddresses.new(:connection => connection, :href => uri).detect { |i| i.host == nil }.name
        options[:ips] ||= []
        options[:ips][index] = ip
      end
    end
    data = connection.virtual_machine_copy("/cloudapi/ecloud/virtualMachines/computePools/#{compute_pool_id}/action/copyVirtualMachine", options).body
  elsif options[:type] == :identical
    data = connection.virtual_machine_copy_identical("/cloudapi/ecloud/virtualMachines/computePools/#{compute_pool_id}/action/copyIdenticalVirtualMachine", options).body
  end
  vm = collection.from_data(data)
  vm
end

#create_rnat(options) ⇒ Object



160
161
162
163
164
165
# File 'lib/fog/ecloud/models/compute/server.rb', line 160

def create_rnat(options)
  options[:host_ip_href] ||= ips.first.href
  options[:uri] = "/cloudapi/ecloud/rnats/environments/#{environment_id}/action/createAssociation"
  data = connection.rnat_associations_create_device(options).body
  rnat = Fog::Compute::Ecloud::Associations.new(:connection => connection, :href => data[:href])[0]
end

#deleteObject



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

def delete
  data = connection.virtual_machine_delete(href).body
  task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0]
end

#disksObject



167
168
169
170
171
# File 'lib/fog/ecloud/models/compute/server.rb', line 167

def disks
  c = hardware_configuration.reload.storage[:Disk]
  c = c.is_a?(Hash) ? [c] : c
  @disks = c
end

#edit(options = {}) ⇒ Object



153
154
155
156
157
158
# File 'lib/fog/ecloud/models/compute/server.rb', line 153

def edit(options = {})
  data = connection.virtual_machine_edit(href, options).body
  if data[:type] == "application/vnd.tmrk.cloud.task"
    task = Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => data[:href])[0]
  end
end

#environment_idObject



249
250
251
# File 'lib/fog/ecloud/models/compute/server.rb', line 249

def environment_id
  other_links[:Link].detect { |l| l[:type] == "application/vnd.tmrk.cloud.environment" }[:href].scan(/\d+/)[0]
end

#hardware_configurationObject



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

def hardware_configuration
  @hardware_configuration ||= Fog::Compute::Ecloud::HardwareConfigurations.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/hardwareConfiguration")[0]
end

#idObject



253
254
255
# File 'lib/fog/ecloud/models/compute/server.rb', line 253

def id
  href.scan(/\d+/)[0]
end

#ipsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fog/ecloud/models/compute/server.rb', line 39

def ips
  network_hash = ip_addresses[:AssignedIpAddresses][:Networks] || []
  network_hash[:Network] = network_hash[:Network].is_a?(Hash) ? [network_hash[:Network]] : network_hash[:Network]
  network_hash[:Network].each do |network|
    network[:IpAddresses][:IpAddress] = network[:IpAddresses][:IpAddress].is_a?(String) ? [network[:IpAddresses][:IpAddress]] : network[:IpAddresses][:IpAddress]
  end
  @ips = nil
  networks = Fog::Compute::Ecloud::Networks.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/assignedIps")
  networks.each do |network|
    if networks.index(network) == 0
      if @ips.nil?
        @ips = network.ips.select do |ip|
          network_hash[:Network].any? do |network|
            network[:IpAddresses][:IpAddress].include?(ip.name)
          end
        end
      else
        network.ips.each do |ip|
          network_hash[:Network].any? do |network|
            network[:IpAddresses][:IpAddress].each do |i|
              @ips << ip if i == ip.name
            end
          end
        end
      end
    else
      network.ips.each do |ip|
        network_hash[:Network].each do |network|
          network[:IpAddresses][:IpAddress].each do |i|
            @ips << ip if i == ip.name
          end
        end
      end
    end
  end
  @ips
end

#networksObject



77
78
79
# File 'lib/fog/ecloud/models/compute/server.rb', line 77

def networks
  @networks ||= Fog::Compute::Ecloud::Networks.new(:connection => connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/assignedIps")
end

#nicsObject



180
181
182
183
184
# File 'lib/fog/ecloud/models/compute/server.rb', line 180

def nics
  c = hardware_configuration.network_cards[:Nic]
  c = c.is_a?(Hash) ? [c] : c
  @nics = c
end

#off?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/fog/ecloud/models/compute/server.rb', line 241

def off?
  powered_on == false
end

#on?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/fog/ecloud/models/compute/server.rb', line 237

def on?
  powered_on == true
end

#power_offObject



85
86
87
# File 'lib/fog/ecloud/models/compute/server.rb', line 85

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

#power_onObject



81
82
83
# File 'lib/fog/ecloud/models/compute/server.rb', line 81

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

#power_resetObject



93
94
95
# File 'lib/fog/ecloud/models/compute/server.rb', line 93

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

#processesObject



26
27
28
# File 'lib/fog/ecloud/models/compute/server.rb', line 26

def processes
  @processes ||= Fog::Compute::Ecloud::GuestProcesses.new(:connection, connection, :href => "/cloudapi/ecloud/virtualMachines/#{id}/guest/processes")
end

#ready?Boolean

Returns:

  • (Boolean)


228
229
230
231
232
233
234
235
# File 'lib/fog/ecloud/models/compute/server.rb', line 228

def ready?
  load_unless_loaded!
  unless status =~ /NotDeployed|Orphaned|TaskInProgress|CopyInProgress/
    true
  else
    false
  end
end

#rnatsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/fog/ecloud/models/compute/server.rb', line 130

def rnats
  rnats = Fog::Compute::Ecloud::Rnats.new(:connection => connection, :href => "/cloudapi/ecloud/rnats/environments/#{environment_id}")
  associations = nil
  rnats.each do |rnat|
    if rnats.index(rnat) == 0
      associations = rnat.associations.select do |association|
        ips.any? do |ip|
          association.name == ip.name
        end
      end
    else
      rnat.associations.select do |association|
        ips.each do |ip|
          if ip.name == association.name
            associations << association
          end
        end
      end
    end
  end
  associations
end

#shutdownObject



89
90
91
# File 'lib/fog/ecloud/models/compute/server.rb', line 89

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

#storage_sizeObject



223
224
225
226
# File 'lib/fog/ecloud/models/compute/server.rb', line 223

def storage_size
  vm_disks = disks
  disks.map! { |d| d[:Size][:Value].to_i }.inject(0){|sum,item| sum + item} * 1024 * 1024        
end

#tasksObject



22
23
24
# File 'lib/fog/ecloud/models/compute/server.rb', line 22

def tasks
  @tasks ||= Fog::Compute::Ecloud::Tasks.new(:connection => connection, :href => "/cloudapi/ecloud/tasks/virtualMachines/#{id}")
end

#upload_file(options) ⇒ Object



218
219
220
221
# File 'lib/fog/ecloud/models/compute/server.rb', line 218

def upload_file(options)
  connection.virtual_machine_upload_file(href + "/guest/action/files", options)
  true
end