Class: WeBee::Datacenter

Inherits:
Object
  • Object
show all
Includes:
SAXMachine, RestResource
Defined in:
lib/webee.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RestResource

included

Methods included from SAXMachine

#old_parse, #parse

Class Method Details

.create(attributes) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/webee.rb', line 204

def self.create(attributes)
  if attributes[:remote_services].nil?
    attributes[:remote_services] = WeBee::RemoteService.create_for_host(Api.host)
  end
  xm = Builder::XmlMarkup.new
  xm.datacenter {
    xm.name attributes[:name]
    xm.location(attributes[:location] ||  'California, USA')
    xm.cpuSoft(attributes[:cpu_soft] || "0")
    xm.cpuHard(attributes[:cpu_hard] || "0")
    xm.vlanSoft(attributes[:vlan_soft] || "0")
    xm.vlanHard(attributes[:vlan_hard] || "0")
    xm.ramSoft(attributes[:ram_soft] || "0")
    xm.ramHard(attributes[:ram_hard] || "0")
    xm.repositorySoft(attributes[:repository_soft] || "0")  
    xm.repositoryHard(attributes[:repository_hard] || "0") 
    xm.publicIpsSoft(attributes[:public_ip_soft] || "0" ) 
    xm.publicIpsHard(attributes[:public_ip_hard] || "0" ) 
    xm.hdSoft(attributes[:hd_soft] || "0")
    xm.hdHard(attributes[:hd_hard] || "0")
    xm.storageSoft(attributes[:storage_soft] || "0")
    xm.storageHard(attributes[:storage_hard] || "0")
    xm.remoteServices {
      attributes[:remote_services].each do |rs|
        xm.remoteService {
          xm.uri  rs.uri
          xm.type rs.rs_type
        }
      end
    }
  }
  res = RestClient.post(Api.url + '/admin/datacenters', xm.target!, :content_type => :xml)
  Datacenter.parse(res)
end

.find_by_name(name, options = {}) ⇒ Object

Return all the Datacenters matching name



242
243
244
# File 'lib/webee.rb', line 242

def self.find_by_name(name, options = {})
  Datacenter.all.find_all { |dc| dc.name =~ /#{name}/ }
end

Instance Method Details

#add_rack(params) ⇒ Object



258
259
260
# File 'lib/webee.rb', line 258

def add_rack(params)
  Rack.create datacenter_id, params
end

#discover_machine(params) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/webee.rb', line 262

def discover_machine(params)
  p = {}
  p[:ip] = params[:ip]
  p[:hypervisortype] = params[:hypervisortype] || 'kvm'
  p[:user] = params[:user] || 'user'
  p[:password] = params[:secret] || 'secret'
  p[:port] = params[:port] || '8889'
  p[:virtual_switch] = params[:virtual_switch]
  res = RestClient.get Api.url + "/admin/datacenters/#{datacenter_id}/action/discover", :params => p, :content_type => :xml
  machine = Machine.parse res
end

#find_vms_by_name(regexp) ⇒ Object

Find a WeBee::VirtualMachine by name in this Datacenter.

returns a WeBee::VirtualMachine



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/webee.rb', line 280

def find_vms_by_name(regexp)
  matches = []
  self.racks.each do |rack|
    rack.machines.each do |m|
      m.virtual_machines.each do |vm|
        matches << vm if vm.name =~ /#{regexp}/i
      end
    end
  end
  matches
end

#racksObject



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/webee.rb', line 246

def racks
  items = []
  doc = Nokogiri.parse(RestClient.get(Api.url + "/admin/datacenters/#{@datacenter_id}/racks", :accept => :xml))
  doc.search 
  doc.search('//rack').each do |node|
    rack = Rack.parse(node.to_s)
    rack.datacenter_id = @datacenter_id
    items << rack
  end
  items 
end

#statsObject

Return Datacenter Statistics RAM in MB HD in GB



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
# File 'lib/webee.rb', line 172

def stats
  s = {
    :free_hd => 0, 
    :real_hd => 0,
    :used_hd => 0, 
    :machines => 0,
    :free_ram => 0,
    :real_ram => 0,
    :used_ram => 0,
    :real_cpus => 0,
    :virtual_machines => 0,
  }
  Datacenter.all.each do |dc|
    dc.racks.each do |rack|
      rack.machines.each do |m|
        s[:machines] += 1
        s[:used_ram] += m.ram_used.to_i
        s[:real_ram] += m.real_ram.to_i
        s[:real_cpus] += m.real_cpu.to_i
        s[:used_hd] += m.hd_used.to_i.bytes.to.gigabytes.to_f.round
        s[:real_hd] += m.real_hd.to_i.bytes.to.gigabytes.to_f.round
        m.virtual_machines.each do |vm|
          s[:virtual_machines] += 1
        end
      end
    end
  end
  s[:free_ram] = s[:real_ram] - s[:used_ram]
  s[:free_hd] = s[:real_hd] - s[:used_hd]
  return s
end