1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/libvirt/models/compute/volume.rb

177 lines
5 KiB
Ruby
Raw Normal View History

require 'fog/core/model'
require 'fog/libvirt/models/compute/util'
require 'rexml/document'
require 'erb'
require 'securerandom'
module Fog
module Compute
class Libvirt
class Volume < Fog::Model
include Fog::Compute::LibvirtUtil
identity :id , :aliases => 'key'
attribute :pool_name
2011-08-08 17:22:55 -04:00
attribute :xml
2011-08-08 17:22:55 -04:00
attribute :key
attribute :path
attribute :name
attribute :capacity
attribute :allocation
attribute :format_type
2011-08-08 17:22:55 -04:00
# 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
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
2011-08-08 17:22:55 -04:00
#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
# Try to guess the default/first pool of no pool_name was specificed
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')
2011-08-08 17:22:55 -04:00
else
default_name=first_pool.name
end
end
return default_name
end
# Takes a pool and either :xml or other settings
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.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
2011-08-08 17:22:55 -04:00
def split_size_unit(text)
matcher=text.match(/(\d+)(.+)/)
size=matcher[1]
unit=matcher[2]
return size , unit
2011-08-08 17:30:09 -04:00
end
# Create a valid xml for the volume based on the template
def xml_from_template
allocation_size,allocation_unit=split_size_unit(self.allocation)
capacity_size,capacity_unit=split_size_unit(self.capacity)
2011-08-08 17:22:55 -04:00
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
}
2011-08-08 17:22:55 -04:00
# We only want specific variables for ERB
2011-08-08 17:30:09 -04:00
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
2011-08-08 17:30:09 -04:00
# Destroy a volume
def destroy
requires :raw
raw.delete
true
end
2011-08-08 17:30:09 -04:00
# Wipes a volume , zeroes disk
def wipe
requires :raw
raw.wipe
true
end
2011-08-08 17:30:09 -04:00
# Clones this volume to the name provided
def clone(name)
pool=@raw.pool
xml = REXML::Document.new(self.xml)
2011-08-08 17:30:09 -04:00
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
#def xml_desc
#requires :raw
#raw.xml_desc
#end
2011-08-08 17:30:09 -04:00
private
def raw
@raw
end
2011-08-08 17:30:09 -04:00
def raw=(new_raw)
@raw = new_raw
xml = REXML::Document.new(new_raw.xml_desc)
format_type=xml.root.elements['/volume/target/format'].attributes['type']
2011-08-08 17:30:09 -04:00
raw_attributes = {
:key => new_raw.key,
:id => new_raw.key,
2011-08-08 17:30:09 -04:00
:path => new_raw.path,
:name => new_raw.name,
:format_type => format_type,
2011-08-08 17:30:09 -04:00
:allocation => new_raw.info.allocation,
:capacity => new_raw.info.capacity,
:xml => new_raw.xml_desc
2011-08-08 17:30:09 -04:00
}
2011-08-08 17:30:09 -04:00
merge_attributes(raw_attributes)
end
end
2011-08-08 17:30:09 -04:00
end
end
2011-08-08 17:30:09 -04:00
end