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-08 17:22:55 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
attribute :xml
|
2011-08-03 06:48:44 -04:00
|
|
|
attribute :template_options
|
2011-08-08 17:22:55 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
# attribute :key
|
2011-08-03 06:48:44 -04:00
|
|
|
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>"
|
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-08 17:22:55 -04:00
|
|
|
|
2011-08-01 09:18:00 -04:00
|
|
|
super
|
2011-08-08 17:22:55 -04:00
|
|
|
|
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')
|
2011-08-08 17:22:55 -04:00
|
|
|
else
|
2011-08-08 17:30:09 -04:00
|
|
|
default_pool_name=first_pool.name
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
2011-08-08 17:22:55 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
2011-08-08 17:22:55 -04:00
|
|
|
|
2011-08-03 06:48:44 -04:00
|
|
|
self.poolname ||= default_pool_name unless attributes[:poolname]
|
2011-08-08 17:22:55 -04:00
|
|
|
|
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-08 17:30:09 -04:00
|
|
|
# requires :xml
|
|
|
|
# requires :poolname
|
2011-08-03 06:48:44 -04:00
|
|
|
|
|
|
|
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
|
2011-08-03 06:48:44 -04:00
|
|
|
if !template_options.nil?
|
|
|
|
|
2011-08-08 17:22:55 -04:00
|
|
|
template_defaults={
|
2011-08-03 06:48:44 -04:00
|
|
|
:type => "raw",
|
|
|
|
:extension => "img",
|
|
|
|
:size => 10,
|
|
|
|
:allocate_unit => "G",
|
|
|
|
:size_unit => "G",
|
|
|
|
:allocate => 1,
|
|
|
|
}
|
|
|
|
template_options2=template_defaults.merge(template_options)
|
2011-08-03 08:28:40 -04:00
|
|
|
template_options={ :name => "fog-#{SecureRandom.random_number*10E14.to_i.round}.#{template_options2[:extension]}"}.merge(template_options2)
|
2011-08-03 06:48:44 -04:00
|
|
|
|
|
|
|
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)
|
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-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-01 09:18:00 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
def xml_from_template(template_options)
|
2011-06-28 09:47:16 -04:00
|
|
|
|
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-06-28 09:47:16 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
# Destroy a volume
|
|
|
|
def destroy
|
|
|
|
requires :raw
|
|
|
|
raw.delete
|
|
|
|
true
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
# Wipes a volume , zeroes disk
|
|
|
|
def wipe
|
|
|
|
requires :raw
|
|
|
|
raw.wipe
|
|
|
|
true
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
|
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-03 06:48:44 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
def xml_desc
|
|
|
|
requires :raw
|
|
|
|
raw.xml_desc
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
private
|
|
|
|
def raw
|
|
|
|
@raw
|
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
def raw=(new_raw)
|
|
|
|
@raw = new_raw
|
2011-08-03 06:48:44 -04:00
|
|
|
|
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-06-28 09:47:16 -04:00
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
merge_attributes(raw_attributes)
|
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
|
|
|
|
2011-08-08 17:30:09 -04:00
|
|
|
end
|
2011-08-03 06:48:44 -04:00
|
|
|
end
|
2011-08-08 17:30:09 -04:00
|
|
|
|
|
|
|
end
|