Class: Fog::Compute::Libvirt::Volume
- Includes:
- Fog::Compute::LibvirtUtil
- Defined in:
- lib/fog/libvirt/models/compute/volume.rb
Instance Attribute Summary
Attributes inherited from Model
Instance Method Summary collapse
-
#clone(name) ⇒ Object
Clones this volume to the name provided.
-
#default_pool_name ⇒ Object
Try to guess the default/first pool of no pool_name was specificed.
-
#destroy ⇒ Object
Destroy a volume.
-
#initialize(attributes = {}) ⇒ Volume
constructor
Can be created by passing in :xml => “<xml to create volume>” A volume always belongs to a pool, :pool_name => “<name of pool>”.
-
#save ⇒ Object
Takes a pool and either :xml or other settings.
- #split_size_unit(text) ⇒ Object
-
#wipe ⇒ Object
Wipes a volume , zeroes disk.
-
#xml_from_template ⇒ Object
Create a valid xml for the volume based on the template.
Methods included from Fog::Compute::LibvirtUtil
Methods inherited from Model
#inspect, #reload, #to_json, #wait_for
Methods included from Attributes::ClassMethods
#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes
Methods included from Attributes::InstanceMethods
#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one
Constructor Details
#initialize(attributes = {}) ⇒ Volume
Can be created by passing in :xml => “<xml to create volume>” A volume always belongs to a pool, :pool_name => “<name of pool>”
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |
Instance Method Details
#clone(name) ⇒ Object
Clones this volume to the name provided
131 132 133 134 135 136 137 138 139 |
# 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 ⇒ Object
Try to guess the default/first pool of no pool_name was specificed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# 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 ⇒ Object
Destroy a volume
117 118 119 120 121 |
# File 'lib/fog/libvirt/models/compute/volume.rb', line 117 def destroy requires :raw raw.delete true end |
#save ⇒ Object
Takes a pool and either :xml or other settings
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
84 85 86 87 88 89 |
# 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 ⇒ Object
Wipes a volume , zeroes disk
124 125 126 127 128 |
# File 'lib/fog/libvirt/models/compute/volume.rb', line 124 def wipe requires :raw raw.wipe true end |
#xml_from_template ⇒ Object
Create a valid xml for the volume based on the template
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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) ={ :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_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 |