From: Jan Provaznik jprovazn@redhat.com
--- src/app/models/image_warehouse_object.rb | 38 +++++++++++++++++++++++++---- src/app/models/template.rb | 13 ++++++++- src/config/image_warehouse.yml | 2 +- src/spec/factories/template.rb | 6 ++++ src/spec/models/image_spec.rb | 5 ---- 5 files changed, 50 insertions(+), 14 deletions(-)
diff --git a/src/app/models/image_warehouse_object.rb b/src/app/models/image_warehouse_object.rb index df01ea3..7717ccd 100644 --- a/src/app/models/image_warehouse_object.rb +++ b/src/app/models/image_warehouse_object.rb @@ -16,6 +16,9 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. A copy of the GNU General Public License is # also available at http://www.gnu.org/copyleft/gpl.html. + +require 'warehouse_client' + module ImageWarehouseObject
WAREHOUSE_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/image_warehouse.yml") @@ -24,13 +27,36 @@ module ImageWarehouseObject @xml ||= ImageDescriptorXML.new(self[:xml].to_s) end
+ # this should be overriden in a model if we want to save additional attrs + # for the model + def warehouse_attrs + {:uuid => self.uuid, :object_type => self.class.class_name} + end + + # this should be overriden in a model if we want to save body + def warehouse_body + nil + end + + # TODO: it would be nice not call upload, when save was invoked by warehouse + # sync script def upload - self.uri = File.join(WAREHOUSE_CONFIG['baseurl'], "#{uuid}") - response = Typhoeus::Request.put(self.uri, :body => xml.to_xml, :timeout => 30000) - if response.code == 200 - update_attribute(:uploaded, true) - else - raise "failed to upload template (code #{response.code}): #{response.body}" + whouse = Warehouse::Client.new(WAREHOUSE_CONFIG['baseurl']) + # TODO: for now there is no way how it check if bucket exists in warehouse + # so we try to create bucket everytime, if bucket exists, warehouse returns + # 500 Internal server error + whouse.create_bucket(warehouse_bucket) rescue true + # TODO: we delete existing object if it exists + whouse.bucket(warehouse_bucket).object(self.uuid).delete! rescue true + whouse.bucket(warehouse_bucket).create_object(self.uuid, warehouse_body, warehouse_attrs) + end + + def delete_in_warehouse + whouse = Warehouse::Client.new(WAREHOUSE_CONFIG['baseurl']) + begin + whouse.bucket(warehouse_bucket).object(self.uuid).delete! + rescue + logger.error "failed to delete #{self.uuid} in warehouse: #{$!}" end end
diff --git a/src/app/models/template.rb b/src/app/models/template.rb index 7ef0449..b84409c 100644 --- a/src/app/models/template.rb +++ b/src/app/models/template.rb @@ -26,6 +26,7 @@ require 'typhoeus' class Template < ActiveRecord::Base include PermissionedObject include ImageWarehouseObject + searchable do text :name, :as => :code_substring text :platform, :as => :code_substring @@ -38,8 +39,8 @@ class Template < ActiveRecord::Base has_many :instances has_and_belongs_to_many :assemblies before_validation :generate_uuid - before_save :update_xml - before_destroy :no_instances? + before_save [:update_xml, :upload] + before_destroy [:no_instances?, :delete_in_warehouse]
has_many :permissions, :as => :permission_object, :dependent => :destroy, :include => [:role], @@ -137,4 +138,12 @@ class Template < ActiveRecord::Base xml.clear_groups groups.to_a.each {|group| xml.add_group(group)} end + + def warehouse_body + self.xml.to_xml + end + + def warehouse_bucket + 'templates' + end end diff --git a/src/config/image_warehouse.yml b/src/config/image_warehouse.yml index cc7545c..ede2baa 100644 --- a/src/config/image_warehouse.yml +++ b/src/config/image_warehouse.yml @@ -1,3 +1,3 @@ -baseurl: http://localhost:9090/templates/ +baseurl: http://localhost:9090 # to set up the bucket, run: # curl -X PUT http://localhost:9090/templates diff --git a/src/spec/factories/template.rb b/src/spec/factories/template.rb index 0305147..1bb6fa6 100644 --- a/src/spec/factories/template.rb +++ b/src/spec/factories/template.rb @@ -1,4 +1,10 @@ Factory.define :template do |i| i.sequence(:name) { |n| "template#{n}" } i.platform 'fedora' + i.after_build do |tpl| + if tpl.respond_to?(:stub!) + tpl.stub!(:upload).and_return(true) + tpl.stub!(:delete_in_warehouse).and_return(true) + end + end end diff --git a/src/spec/models/image_spec.rb b/src/spec/models/image_spec.rb index a5c3534..d364919 100644 --- a/src/spec/models/image_spec.rb +++ b/src/spec/models/image_spec.rb @@ -59,11 +59,6 @@ describe Image do account.stub!(:connect).and_return(client) account.stub!(:valid_credentials?).and_return(true) account.save! - Template.stub!(:new).and_return do |*args| - tpl = Template.proxied_by_rspec__new(*args) - tpl.should_receive(:upload).and_return(true) - tpl - end
lambda do lambda do