lib/dcmgr/models/volume.rb in wakame-vdc-agents-10.12.0 vs lib/dcmgr/models/volume.rb in wakame-vdc-agents-11.06.0
- old
+ new
@@ -30,25 +30,37 @@
Fixnum :boot_dev, :null=>false, :default=>0
String :snapshot_id
String :host_device_name
String :guest_device_name
String :export_path, :null=>false
+# String :intermediate_path, :null=>false
Text :transport_information
Time :deleted_at
Time :attached_at
Time :detached_at
+
index :storage_pool_id
index :instance_id
index :snapshot_id
+ index :deleted_at
end
with_timestamps
many_to_one :storage_pool
many_to_one :instance
plugin ArchiveChangedColumn, :histories
+ subset(:lives, {:deleted_at => nil})
+
+ RECENT_TERMED_PERIOD=(60 * 15)
+ # lists the volumes are available and deleted within
+ # RECENT_TERMED_PERIOD sec.
+ def_dataset_method(:alives_and_recent_termed) {
+ filter("deleted_at IS NULL OR deleted_at >= ?", (Time.now.utc - RECENT_TERMED_PERIOD))
+ }
+
# serialization plugin must be defined at the bottom of all class
# method calls.
# Possible column data:
# iscsi:
# {:iqn=>'iqn.1986-03.com.sun:02:a1024afa-775b-65cf-b5b0-aa17f3476bfc', :lun=>0}
@@ -56,26 +68,27 @@
class DiskError < RuntimeError; end
class RequestError < RuntimeError; end
def before_create
- # check the volume size
sp = self.storage_pool
- volume_size = Volume.dataset.where(:storage_pool_id=> self.storage_pool_id).get{sum(:size)}
- total_size = sp.offerring_disk_space - volume_size.to_i
+ volume_size = sp.volumes_dataset.lives.sum(:size).to_i
+ # check if the sum of available volume and new volume is under
+ # the limit of offering capacity.
+ total_size = sp.offering_disk_space - volume_size.to_i
if self.size > total_size
raise DiskError, "out of disk space"
end
+ # TODO: Here may not be the right place for capacity validation.
+ per_account_totoal = self.class.filter(:account_id=>self.account_id).lives.sum(:size).to_i
+ if self.account.quota.volume_total_size < per_account_totoal + self.size.to_i
+ raise DiskError, "Out of account quota: #{self.account.quota.volume_total_size}, #{self.size.to_i}, #{per_account_totoal}"
+ end
super
end
- def before_save
- self.updated_at = Time.now
- super
- end
-
def self.get_list(account_id, *args)
data = args.first
vl = self.dataset.where(:account_id=>account_id)
vl = vl.limit(data[:limit], data[:start]) if data[:start] && data[:limit]
if data[:target] && data[:sort]
@@ -132,10 +145,15 @@
:created_at => self.created_at,
:attached_at => self.attached_at,
:state => self.state,
:instance_id => (self.instance && self.instance.canonical_uuid),
:deleted_at => self.deleted_at,
+ :detached_at => self.detached_at,
}
+ end
+
+ def ready_to_take_snapshot?
+ %w(available attached).member?(self.state)
end
def create_snapshot(account_id)
vs = VolumeSnapshot.create(:account_id=>account_id,
:storage_pool_id=>self.storage_pool_id,