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/compute/models/libvirt/volume.rb

180 lines
5 KiB
Ruby
Raw Normal View History

require 'fog/core/model'
require 'fog/compute/models/libvirt/util'
require 'rexml/document'
require 'erb'
require 'securerandom'
module Fog
module Compute
class Libvirt
class Volume < Fog::Model
include Fog::Compute::LibvirtUtil
identity :key
attribute :poolname
2011-08-08 17:22:55 -04:00
attribute :xml
attribute :template_options
2011-08-08 17:22:55 -04:00
2011-08-08 17:30:09 -04:00
# attribute :key
attribute :path
attribute :name
attribute :capacity
attribute :allocation
attribute :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, :poolname => "<name of pool>"
#
# @returns volume created
def initialize(attributes={} )
self.xml ||= nil unless attributes[:xml]
2011-08-08 17:22:55 -04:00
super
2011-08-08 17:22:55 -04:00
# Try to guess the default/first pool of no poolname was specificed
default_pool_name="default"
default_pool=connection.pools.all(:name => "default")
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
2011-08-08 17:30:09 -04:00
default_pool_name=first_pool.name
end
2011-08-08 17:22:55 -04:00
end
2011-08-08 17:22:55 -04:00
self.poolname ||= default_pool_name unless attributes[:poolname]
2011-08-08 17:22:55 -04:00
end
# Takes a pool and either uses :xml or :template_options->xml to create the volume
def save
2011-08-08 17:30:09 -04:00
# requires :xml
# requires :poolname
if poolname
# :disk_type => "raw",
# :disk_extension => "img",
# :disk_size => "10000",
2011-08-08 17:22:55 -04:00
# We have a template, let's generate some xml for it
if !template_options.nil?
2011-08-08 17:22:55 -04:00
template_defaults={
:type => "raw",
:extension => "img",
:size => 10,
:allocate_unit => "G",
:size_unit => "G",
:allocate => 1,
}
template_options2=template_defaults.merge(template_options)
template_options={ :name => "fog-#{SecureRandom.random_number*10E14.to_i.round}.#{template_options2[:extension]}"}.merge(template_options2)
validate_template_options(template_options)
xml=xml_from_template(template_options)
end
unless xml.nil?
volume=nil
unless poolname.nil?
pool=connection.lookup_storage_pool_by_name(poolname)
volume=pool.create_volume_xml(xml)
self.raw=volume
true
else
raise Fog::Errors::Error.new('Creating a new volume requires a pool name or uuid')
false
end
else
raise Fog::Errors::Error.new('Creating a new volume requires non empty xml')
false
end
end
end
2011-08-08 17:22:55 -04:00
2011-08-08 17:30:09 -04:00
def validate_template_options(template_options)
# Here we can validate the template_options
end
2011-08-08 17:30:09 -04:00
def xml_from_template(template_options)
2011-08-08 17:30:09 -04:00
# We only want specific variables for ERB
2011-08-08 17:22:55 -04:00
2011-08-08 17:30:09 -04:00
# we can't use an option of name type
# This clashes with ruby 1.8 Erb.type which is equivalent of .class
new_template_options={:vol_type => template_options[:type]}.merge(template_options)
2011-08-08 17:22:55 -04:00
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(xml_desc)
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
2011-08-08 17:30:09 -04:00
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
2011-08-08 17:30:09 -04:00
raw_attributes = {
:key => new_raw.key,
:path => new_raw.path,
:name => new_raw.name,
:allocation => new_raw.info.allocation,
:capacity => new_raw.info.capacity,
:type => new_raw.info.type
}
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