2011-06-28 09:47:16 -04:00
|
|
|
require 'fog/core/model'
|
2011-08-03 06:48:44 -04:00
|
|
|
require 'fog/compute/models/libvirt/util'
|
|
|
|
require 'rexml/document'
|
|
|
|
require 'erb'
|
|
|
|
require 'securerandom'
|
2011-06-28 09:47:16 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Libvirt
|
|
|
|
|
|
|
|
class Volume < Fog::Model
|
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
include Fog::Compute::LibvirtUtil
|
|
|
|
|
|
|
|
identity :key
|
2011-08-01 09:18:00 -04:00
|
|
|
|
|
|
|
attribute :poolname
|
2011-08-03 06:48:44 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
attribute :xml
|
2011-08-03 06:48:44 -04:00
|
|
|
attribute :template_options
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
# attribute :key
|
|
|
|
attribute :path
|
|
|
|
attribute :name
|
|
|
|
attribute :capacity
|
|
|
|
attribute :allocation
|
|
|
|
attribute :type
|
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
# Can be created by passing in :xml => "<xml to create volume>"
|
2011-08-03 06:48:44 -04:00
|
|
|
# A volume always belongs to a pool, :poolname => "<name of pool>"
|
2011-08-01 09:18:00 -04:00
|
|
|
#
|
|
|
|
# @returns volume created
|
|
|
|
def initialize(attributes={} )
|
|
|
|
self.xml ||= nil unless attributes[:xml]
|
2011-08-03 06:48:44 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
super
|
2011-08-03 06:48:44 -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')
|
|
|
|
else
|
|
|
|
default_pool_name=first_pool.name
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
self.poolname ||= default_pool_name unless attributes[:poolname]
|
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
end
|
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
# Takes a pool and either uses :xml or :template_options->xml to create the volume
|
2011-08-01 09:18:00 -04:00
|
|
|
def save
|
2011-08-03 06:48:44 -04:00
|
|
|
# requires :xml
|
|
|
|
# requires :poolname
|
|
|
|
|
|
|
|
if poolname
|
|
|
|
# :disk_type => "raw",
|
|
|
|
# :disk_extension => "img",
|
|
|
|
# :disk_size => "10000",
|
|
|
|
# We have a template, let's generate some xml for it
|
|
|
|
if !template_options.nil?
|
|
|
|
|
|
|
|
template_defaults={
|
|
|
|
:type => "raw",
|
|
|
|
:extension => "img",
|
|
|
|
:name => "fog-#{SecureRandom.random_number*10E14.to_i.round}",
|
|
|
|
:size => 10,
|
|
|
|
:allocate_unit => "G",
|
|
|
|
:size_unit => "G",
|
|
|
|
:allocate => 1,
|
|
|
|
}
|
|
|
|
template_options2=template_defaults.merge(template_options)
|
|
|
|
template_options={ :path => "#{template_options2[:name]}.#{template_options2[:extension]}"}.merge(template_options2)
|
|
|
|
|
|
|
|
validate_template_options(template_options)
|
|
|
|
|
|
|
|
xml=xml_from_template(template_options)
|
|
|
|
# require 'pp'
|
|
|
|
# pp xml
|
|
|
|
# exit
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
unless xml.nil?
|
|
|
|
volume=nil
|
|
|
|
unless poolname.nil?
|
|
|
|
pool=connection.lookup_storage_pool_by_name(poolname)
|
2011-08-01 09:33:25 -04:00
|
|
|
volume=pool.create_volume_xml(xml)
|
2011-08-03 06:48:44 -04:00
|
|
|
self.raw=volume
|
|
|
|
true
|
|
|
|
else
|
|
|
|
raise Fog::Errors::Error.new('Creating a new volume requires a pool name or uuid')
|
|
|
|
false
|
2011-08-01 09:33:25 -04:00
|
|
|
end
|
2011-08-01 09:18:00 -04:00
|
|
|
else
|
2011-08-03 06:48:44 -04:00
|
|
|
raise Fog::Errors::Error.new('Creating a new volume requires non empty xml')
|
2011-08-01 09:18:00 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
|
|
|
|
def validate_template_options(template_options)
|
|
|
|
# Here we can validate the template_options
|
|
|
|
end
|
2011-08-01 09:18:00 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
def xml_from_template(template_options)
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
# 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
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
# Destroy a volume
|
|
|
|
def destroy
|
|
|
|
requires :raw
|
|
|
|
raw.delete
|
|
|
|
true
|
|
|
|
end
|
2011-06-28 09:47:16 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
# Wipes a volume , zeroes disk
|
|
|
|
def wipe
|
|
|
|
requires :raw
|
|
|
|
raw.wipe
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
|
|
|
requires :raw
|
|
|
|
raw.key
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
requires :raw
|
|
|
|
raw.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
requires :raw
|
|
|
|
raw.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def xml_desc
|
|
|
|
requires :raw
|
|
|
|
raw.xml_desc
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def allocation
|
|
|
|
requires :raw
|
|
|
|
raw.info.allocation
|
|
|
|
end
|
|
|
|
|
|
|
|
def capacity
|
|
|
|
requires :raw
|
|
|
|
@capacity=raw.info.capacity
|
|
|
|
end
|
|
|
|
|
|
|
|
def type
|
|
|
|
requires :raw
|
|
|
|
@type=raw.info.type
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
def raw
|
|
|
|
@raw
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw=(new_raw)
|
|
|
|
@raw = new_raw
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_attributes(raw_attributes)
|
|
|
|
end
|
2011-06-28 09:47:16 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
2011-06-28 09:47:16 -04:00
|
|
|
end
|
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|