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/pool.rb

201 lines
4.4 KiB
Ruby
Raw Normal View History

require 'fog/core/model'
module Fog
module Compute
class Libvirt
class Pool < Fog::Model
identity :uuid
2011-08-08 17:22:55 -04:00
# These attributes are only used for creation
attribute :xml
attribute :create_persistent
attribute :create_auto_build
2011-08-08 17:22:55 -04:00
# Can be created by passing in XML
def initialize(attributes={} )
self.xml ||= nil unless attributes[:xml]
self.create_persistent ||= true unless attributes[:create_persistent]
self.create_auto_build ||= true unless attributes[:create_auto_build]
super
end
2011-08-08 17:22:55 -04:00
def save
requires :xml
unless xml.nil?
pool=nil
if self.create_persistent
pool=connection.raw.define_storage_pool_xml(xml)
else
pool=connection.raw.create_storage_pool_xml(xml)
2011-08-08 17:22:55 -04:00
end
self.raw=pool
true
else
2011-08-08 17:22:55 -04:00
raise Fog::Errors::Error.new('Creating a new pool requires proper xml')
false
end
end
2011-08-08 17:22:55 -04:00
# Start the pool = make it active
# Performs a libvirt create (= start)
def start
requires :raw
2011-08-08 17:22:55 -04:00
@raw.create
end
2011-08-08 17:22:55 -04:00
# Stop the pool = make it non-active
# Performs a libvirt destroy (= stop)
def stop
requires :raw
2011-08-08 17:22:55 -04:00
@raw.destroy
end
2011-08-08 17:22:55 -04:00
# Shuts down the pool
def shutdown
requires :raw
2011-08-08 17:22:55 -04:00
@raw.destroy
true
end
2011-08-08 17:22:55 -04:00
# Build this storage pool
def build
requires :raw
2011-08-08 17:22:55 -04:00
@raw.build unless @raw.nil?
end
2011-08-08 17:22:55 -04:00
# Destroys the storage pool
def destroy( destroy_options={})
requires :raw
2011-08-08 17:22:55 -04:00
# Shutdown pool if active
@raw.destroy if @raw.active?
2011-08-08 17:22:55 -04:00
# Delete corresponding data in this pool
# @raw.delete
# If this is a persistent domain we need to undefine it
@raw.undefine if @raw.persistent?
2011-08-08 17:22:55 -04:00
end
2011-08-08 17:22:55 -04:00
# Set autostart value of the storage pool (true|false)
def auto_start=(flag)
requires :raw
2011-08-08 17:22:55 -04:00
@raw.auto_start(flag)
end
2011-08-08 17:22:55 -04:00
# Is the pool active or not?
def active?
requires :raw
@raw.active?
end
2011-08-08 17:22:55 -04:00
# Will the pool autostart or not?
def auto_start?
requires :raw
@raw.autostart?
end
2011-08-08 17:22:55 -04:00
# Is the pool persistent or not?
def persistent?
requires :raw
@raw.persistent?
end
2011-08-08 17:22:55 -04:00
# Returns the xml description of the current pool
def xml_desc
requires :raw
2011-08-08 17:22:55 -04:00
@raw.xml_desc unless @raw.nil?
end
2011-08-08 17:22:55 -04:00
# Retrieves the name of the pool
def name
requires :raw
@raw.name
end
# Retrieves the uuid of the pool
def uuid
requires :raw
@raw.uuid
end
2011-08-08 17:22:55 -04:00
# Retrieves the allocated disk space of the pool
def allocation
requires :raw
@raw.info.allocation
end
2011-08-08 17:22:55 -04:00
# Retrieves the capacity of disk space of the pool
def capacity
requires :raw
@raw.info.capacity
end
2011-08-08 17:22:55 -04:00
# Retrieves the number of volumes available in this pool
def num_of_volumes
requires :raw
@raw.num_of_volumes
end
2011-08-08 17:22:55 -04:00
def state
requires :raw
2011-08-08 17:18:03 -04:00
#INACTIVE = INT2NUM(VIR_STORAGE_POOL_INACTIVE) virStoragePoolState
#BUILDING = INT2NUM(VIR_STORAGE_POOL_BUILDING)
#RUNNING = INT2NUM(VIR_STORAGE_POOL_RUNNING)
#DEGRADED = INT2NUM(VIR_STORAGE_POOL_DEGRADED)
#INACCESSIBLE = INT2NUM(VIR_STORAGE_POOL_INACCESSIBLE)
states=[:inactive, :building,:running,:degrated,:inaccessible]
return states[@raw.info.state]
2011-08-08 17:22:55 -04:00
end
2011-08-08 17:22:55 -04:00
# Retrieves the volumes of this pool
def volumes
2011-08-08 17:22:55 -04:00
volumes=Array.new
@raw.list_volumes.each do |volume|
fog_volume=connection.volumes.all(:name => volume).first
volumes << fog_volume
end
return volumes
end
2011-08-08 17:22:55 -04:00
private
def raw
@raw
end
def raw=(new_raw)
@raw = new_raw
2011-08-08 17:22:55 -04:00
raw_attributes = {
:uuid => new_raw.uuid,
2011-08-08 17:22:55 -04:00
}
2011-08-08 17:22:55 -04:00
merge_attributes(raw_attributes)
end
2011-08-08 17:22:55 -04:00
end
end
end
end