Parent

Included Modules

Files

Class/Module Index [+]

Quicksearch

Fog::Compute::Libvirt::Volume

Public Class Methods

new(attributes={} ) click to toggle source

Can be created by passing in :xml => "<xml to create volume>" A volume always belongs to a pool, :pool_name => "<name of pool>"

@returns volume created

# File lib/fog/libvirt/models/compute/volume.rb, line 32
def initialize(attributes={} )
  self.xml  ||= nil unless attributes[:xml]
  self.key  = nil
  self.format_type ||= "raw" unless attributes[:format_type]
  extension = self.format_type=="raw" ? "img" : self.format_type
  self.name ||= "fog-#{SecureRandom.random_number*10E14.to_i.round}.#{extension}" unless attributes[:name]
  self.capacity ||= "10G" unless attributes[:capacity]
  self.allocation ||= "1G" unless attributes[:allocation]
  super

  #We need a connection to calculate the poolname
  #This is why we do this after super
  self.pool_name  ||= default_pool_name unless attributes[:pool_name]
end

Public Instance Methods

clone(name) click to toggle source

Clones this volume to the name provided

# File lib/fog/libvirt/models/compute/volume.rb, line 131
def clone(name)
  pool=@raw.pool
  xml = REXML::Document.new(self.xml)
  xml.root.elements['/volume/name'].text=name
  xml.root.elements['/volume/key'].text=name
  xml.delete_element('/volume/target/path')
  pool.create_volume_xml_from(xml.to_s,@raw)
  return connection.volumes.all(:name => name).first
end
default_pool_name() click to toggle source

Try to guess the default/first pool of no pool_name was specificed

# File lib/fog/libvirt/models/compute/volume.rb, line 48
def default_pool_name
  default_name="default"
  default_pool=@connection.pools.all(:name => default_name)

  if default_pool.nil?
    first_pool=@connection.pools.first
    if first_pool.nil?
      raise Fog::Errors::Error.new('We could not find a pool called "default" and there was no other pool defined')
    else
      default_name=first_pool.name
    end
  end
  return default_name
end
destroy() click to toggle source

Destroy a volume

# File lib/fog/libvirt/models/compute/volume.rb, line 117
def destroy
  requires :raw
  raw.delete
  true
end
save() click to toggle source

Takes a pool and either :xml or other settings

# File lib/fog/libvirt/models/compute/volume.rb, line 64
def save
  requires :pool_name

  raise Fog::Errors::Error.new('Resaving an existing volume may create a duplicate') if key

  xml=xml_from_template if xml.nil?

  begin
    volume=nil
    pool=connection.raw.lookup_storage_pool_by_name(pool_name)
    volume=pool.create_volume_xml(xml)
    self.raw=volume
    true
  rescue
    raise Fog::Errors::Error.new("Error creating volume: #{$!}")
    false
  end

end
split_size_unit(text) click to toggle source
# File lib/fog/libvirt/models/compute/volume.rb, line 84
def split_size_unit(text)
  matcher=text.match(/(\d+)(.+)/)
  size=matcher[1]
  unit=matcher[2]
  return size , unit
end
wipe() click to toggle source

Wipes a volume , zeroes disk

# File lib/fog/libvirt/models/compute/volume.rb, line 124
def wipe
  requires :raw
  raw.wipe
  true
end
xml_from_template() click to toggle source

Create a valid xml for the volume based on the template

# File lib/fog/libvirt/models/compute/volume.rb, line 92
def xml_from_template

  allocation_size,allocation_unit=split_size_unit(self.allocation)
  capacity_size,capacity_unit=split_size_unit(self.capacity)

  template_options={
    :name => self.name,
    :format_type => self.format_type,
    :allocation_size => allocation_size,
    :allocation_unit => allocation_unit,
    :capacity_size => capacity_size,
    :capacity_unit => capacity_unit
  }

  # We only want specific variables for ERB
  vars = ErbBinding.new(template_options)
  template_path=File.join(File.dirname(__FILE__),"templates","volume.xml.erb")
  template=File.open(template_path).readlines.join
  erb = ERB.new(template)
  vars_binding = vars.send(:get_binding)
  result=erb.result(vars_binding)
  return result
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.