These patches handle tasks 2139[1] and 2140[2].
This adds another dependency to Conductor but it's a light one. It is not yet packaged for Fedora, but both of its dependencies are.
The gem (Archivist[3]) transparently moves (whitelisted) models to a separate table instead of deleting them.
This is useful for our event logging, because we can easily log and handle the deletion events. Additionally, this should give us things like checking permissions on deleted objects for free.
Right now, the only model that is handled this way is Instance. Others may easilly follow as needed.
Thomas
[1]: https://www.aeolusproject.org/redmine/issues/2139 [2]: https://www.aeolusproject.org/redmine/issues/2140 [3]: https://github.com/tpickett66/archivist
From: Tomas Sedovic tsedovic@redhat.com
Using the Archivist gem, instances that are deleted will be moved to the `archived_instances` table instead.
They can be accessed using familiar methods on the Instance::Archive class:
Instance::Archive.find 10 Instance::Archive.find_by_name 'myapp/frontend'
Methods `delete!` and `destroy!` bypass archiving and will permanently delete the model. --- src/Gemfile | 1 + src/app/models/instance.rb | 1 + .../20110822110535_create_archived_instances.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 0 deletions(-) create mode 100644 src/db/migrate/20110822110535_create_archived_instances.rb
diff --git a/src/Gemfile b/src/Gemfile index ce11e9d..eeb11d0 100644 --- a/src/Gemfile +++ b/src/Gemfile @@ -21,6 +21,7 @@ gem 'pg' gem 'thin' gem 'json' gem 'railties' +gem 'archivist' group :development, :test do #gem 'rspec-rails' #gem 'factory_girl_rails' diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb index aee4750..9c06a30 100644 --- a/src/app/models/instance.rb +++ b/src/app/models/instance.rb @@ -59,6 +59,7 @@ require 'util/assembly_xml' require 'util/condormatic' class Instance < ActiveRecord::Base + has_archive include PermissionedObject
cattr_reader :per_page diff --git a/src/db/migrate/20110822110535_create_archived_instances.rb b/src/db/migrate/20110822110535_create_archived_instances.rb new file mode 100644 index 0000000..731710a --- /dev/null +++ b/src/db/migrate/20110822110535_create_archived_instances.rb @@ -0,0 +1,8 @@ +class CreateArchivedInstances < ActiveRecord::Migration + def self.up + Archivist.update Instance + end + + def self.down + end +end
From: Tomas Sedovic tsedovic@redhat.com
https://www.aeolusproject.org/redmine/issues/2140
This makes sure events survive even when the instance is deleted and the deletion event is logged as well. --- src/app/models/event.rb | 10 ++++++++++ src/app/models/instance.rb | 2 +- src/app/models/instance_observer.rb | 6 ++++++ src/spec/models/instance_observer_spec.rb | 7 +++++++ 4 files changed, 24 insertions(+), 1 deletions(-)
diff --git a/src/app/models/event.rb b/src/app/models/event.rb index 1e17f35..c897a33 100644 --- a/src/app/models/event.rb +++ b/src/app/models/event.rb @@ -39,4 +39,14 @@ class Event < ActiveRecord::Base
validates_presence_of :source_id validates_presence_of :source_type + + def source + begin + model = Kernel.const_get(source_type) + model.find source_id + rescue ActiveRecord::RecordNotFound + model = model.const_get('Archive') + model.find source_id + end + end end diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb index 9c06a30..e80e224 100644 --- a/src/app/models/instance.rb +++ b/src/app/models/instance.rb @@ -78,7 +78,7 @@ class Instance < ActiveRecord::Base has_many :permissions, :as => :permission_object, :dependent => :destroy, :include => [:role], :order => "permissions.id ASC" - has_many :events, :as => :source, :dependent => :destroy + has_many :events, :as => :source after_create "assign_owner_roles(owner)"
validates_presence_of :pool_id diff --git a/src/app/models/instance_observer.rb b/src/app/models/instance_observer.rb index a7b53ed..983a04d 100644 --- a/src/app/models/instance_observer.rb +++ b/src/app/models/instance_observer.rb @@ -88,6 +88,12 @@ class InstanceObserver < ActiveRecord::Observer end end
+ def after_destroy(instance) + event = Event.new(:source => instance, :event_time => DateTime.now, + :summary => "deleted") + event.save! + end + end
InstanceObserver.instance diff --git a/src/spec/models/instance_observer_spec.rb b/src/spec/models/instance_observer_spec.rb index c4d0a42..9df12fc 100644 --- a/src/spec/models/instance_observer_spec.rb +++ b/src/spec/models/instance_observer_spec.rb @@ -189,6 +189,13 @@ describe InstanceObserver do @instance = Instance.find(@instance.id) @instance.events.should have(3).items @instance.events[2].summary.should match /state.*stopped/ + + @instance.destroy + + Event.all.should have(4).items + Event.last.summary.should match /deleted/ + Event.last.source.id.should == @instance.id + Event.last.source.name.should == @instance.name end
it "should destroy the instance key when the instance is stopped" do
On Mon, Aug 22, 2011 at 05:30:14PM +0200, tsedovic@redhat.com wrote:
These patches handle tasks 2139[1] and 2140[2].
This adds another dependency to Conductor but it's a light one. It is not yet packaged for Fedora, but both of its dependencies are.
The gem (Archivist[3]) transparently moves (whitelisted) models to a separate table instead of deleting them.
ACK, works as described and all tests pass.
I have not pushed this yet, as I think we want for https://www.aeolusproject.org/redmine/issues/2187 (the packaging task) to be done at the same time, and for the resulting package to be required in the Conductor RPM spec.
-- Matt
aeolus-devel@lists.fedorahosted.org