[PATCH conductor 3/5] Removed warehouse models
jprovazn at redhat.com
jprovazn at redhat.com
Wed Dec 14 14:09:22 UTC 2011
From: Jan Provaznik <jprovazn at redhat.com>
These models are implemented in aeolus-image-rubygem now.
---
src/app/models/image.rb | 36 ------
src/app/models/image_build.rb | 55 ---------
src/app/models/image_warehouse_object.rb | 96 ----------------
src/app/models/instance.rb | 4 +-
src/app/models/provider_image.rb | 39 -------
src/app/models/target_image.rb | 39 -------
src/config/application.rb | 1 -
src/lib/warehouse_client.rb | 178 ------------------------------
src/lib/warehouse_model.rb | 91 ---------------
src/spec/models/warehouse_model_spec.rb | 50 ---------
10 files changed, 2 insertions(+), 587 deletions(-)
delete mode 100644 src/app/models/image.rb
delete mode 100644 src/app/models/image_build.rb
delete mode 100644 src/app/models/image_warehouse_object.rb
delete mode 100644 src/app/models/provider_image.rb
delete mode 100644 src/app/models/target_image.rb
delete mode 100644 src/lib/warehouse_client.rb
delete mode 100644 src/lib/warehouse_model.rb
delete mode 100644 src/spec/models/warehouse_model_spec.rb
diff --git a/src/app/models/image.rb b/src/app/models/image.rb
deleted file mode 100644
index 948cd25..0000000
--- a/src/app/models/image.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-
-class Image < WarehouseModel
- @bucket_name = 'images'
-
- def initialize(attrs)
- attrs.each do |k,v|
- if k.to_sym == :latest_build
- sym = :attr_writer
- else
- sym = :attr_accessor
- end
- self.class.send(sym, k.to_sym) unless respond_to?(:"#{k}=")
- send(:"#{k}=", v)
- end
- end
-
- def image_builds
- ImageBuild.find_all_by_image_uuid(self.uuid)
- end
-end
diff --git a/src/app/models/image_build.rb b/src/app/models/image_build.rb
deleted file mode 100644
index 8f63220..0000000
--- a/src/app/models/image_build.rb
+++ /dev/null
@@ -1,55 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-class ImageBuild < WarehouseModel
- @bucket_name = 'builds'
-
- def initialize(attrs)
- attrs.each do |k,v|
- if k.to_sym == :image
- sym = :attr_writer
- else
- sym = :attr_accessor
- end
- self.class.send(sym, k.to_sym) unless respond_to?(:"#{k}=")
- send(:"#{k}=", v)
- end
- end
-
- def self.find_all_by_image_uuid(uuid)
- self.set_warehouse_and_bucket if self.bucket.nil?
- self.bucket.objects.map do |wh_object|
- if wh_object.attr('image')[1] == uuid
- ImageBuild.new(wh_object.attrs(wh_object.attr_list))
- end
- end.compact
- end
-
- def image
- Image.find(@image) if @image
- end
-
- def target_images
- TargetImage.all.select {|ti| ti.build and (ti.build.uuid == self.uuid)}
- end
-
- def provider_images
- targets = target_images
- ProviderImage.all.select do |pi|
- targets.include?(pi.target_image)
- end
- end
-end
diff --git a/src/app/models/image_warehouse_object.rb b/src/app/models/image_warehouse_object.rb
deleted file mode 100644
index e78491b..0000000
--- a/src/app/models/image_warehouse_object.rb
+++ /dev/null
@@ -1,96 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'warehouse_client'
-
-class WarehouseObjectNotFoundError < Exception;end
-
-module ImageWarehouseObject
-
- WAREHOUSE_CONFIG = YAML.load_file("#{::Rails.root.to_s}/config/image_warehouse.yml")
-
- def xml
- @xml ||= ImageDescriptorXML.new(self[:xml].to_s)
- end
-
- # this should be overriden in a model if model wants to save additional attrs
- def warehouse_attrs
- {:uuid => self.uuid, :object_type => self.class.class_name}
- end
-
- # this should be overriden in a model if model wants to save a body
- def warehouse_body
- nil
- end
-
- # TODO: it would be nice not call upload, when save was invoked by warehouse
- # sync script
- def upload
- # 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
- raise "uuid is not set" unless self.uuid
- warehouse.create_bucket(warehouse_bucket) rescue true
- # TODO: we delete existing object if it exists
- warehouse.bucket(warehouse_bucket).object(self.uuid).delete! rescue true
- warehouse.bucket(warehouse_bucket).create_object(self.uuid, warehouse_body, warehouse_attrs)
- end
-
- def delete_in_warehouse
- begin
- warehouse.bucket(warehouse_bucket).object(self.uuid).delete!
- rescue
- logger.error "failed to delete #{self.uuid} in warehouse: #{$!}"
- end
- end
-
- def warehouse
- @warehouse ||= Warehouse::Client.new(WAREHOUSE_CONFIG['baseurl'])
- end
-
- def warehouse_url
- "#{WAREHOUSE_CONFIG['baseurl']}/#{warehouse_bucket}/#{self.uuid}"
- end
-
- def safe_warehouse_sync
- warehouse_sync
- log_changes
- rescue RestClient::ResourceNotFound, WarehouseObjectNotFoundError
- logger.error "Failed to fetch #{self.class.class_name} with uuid #{self.uuid} - not found in warehouse"
- rescue => e
- logger.error "Failed to sync #{self.class.class_name} with uuid #{self.uuid}: #{e.message}"
- logger.error e.backtrace.join("\n ")
- end
-
- def log_changes
- if self.new_record?
- logger.info "#{self.class.class_name} #{self.uuid} is not in DB yet"
- elsif self.changed?
- logger.info "#{self.class.class_name} #{self.uuid} has been changed:"
- self.changed.each do |attr|
- logger.info "old #{attr}: #{self.send(attr + '_was')}"
- logger.info "new #{attr}: #{self[attr]}"
- end
- else
- logger.info "#{self.class.class_name} #{self.uuid} is without changes"
- end
- end
-
- def generate_uuid
- # TODO: generate real uuid here, e.g. with some ruby uuid generator
- self.uuid ||= UUIDTools::UUID.timestamp_create.to_s
- end
-end
diff --git a/src/app/models/instance.rb b/src/app/models/instance.rb
index ce1614b..fec47d1 100644
--- a/src/app/models/instance.rb
+++ b/src/app/models/instance.rb
@@ -177,11 +177,11 @@ class Instance < ActiveRecord::Base
def image
- Image.find(image_uuid) if image_uuid
+ Aeolus::Image::Warehouse::Image.find(image_uuid) if image_uuid
end
def image_build
- ImageBuild.find(image_build_uuid) if image_build_uuid
+ Aeolus::Image::Warehouse::ImageBuild.find(image_build_uuid) if image_build_uuid
end
def assembly_xml
diff --git a/src/app/models/provider_image.rb b/src/app/models/provider_image.rb
deleted file mode 100644
index b50a196..0000000
--- a/src/app/models/provider_image.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-class ProviderImage < WarehouseModel
- @bucket_name = 'provider_images'
-
- def initialize(attrs)
- attrs.each do |k,v|
- if [:provider, :target_image].include?(k.to_sym)
- sym = :attr_writer
- else
- sym = :attr_accessor
- end
- self.class.send(sym, k.to_sym) unless respond_to?(:"#{k}=")
- send(:"#{k}=", v)
- end
- end
-
- def target_image
- TargetImage.find(@target_image) if @target_image
- end
-
- def provider_name
- @provider
- end
-end
diff --git a/src/app/models/target_image.rb b/src/app/models/target_image.rb
deleted file mode 100644
index 195233b..0000000
--- a/src/app/models/target_image.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-class TargetImage < WarehouseModel
- @bucket_name = 'target_images'
-
- def initialize(attrs)
- attrs.each do |k,v|
- if k.to_sym == :build
- sym = :attr_writer
- else
- sym = :attr_accessor
- end
- self.class.send(sym, k.to_sym) unless respond_to?(:"#{k}=")
- send(:"#{k}=", v)
- end
- end
-
- def build
- ImageBuild.find(@build) if @build
- end
-
- def provider_images
- ProviderImage.all.select{|pi| pi.target_image and (pi.target_image.uuid == self.uuid)}
- end
-end
diff --git a/src/config/application.rb b/src/config/application.rb
index deb06c6..dfc1273 100644
--- a/src/config/application.rb
+++ b/src/config/application.rb
@@ -94,7 +94,6 @@ module Conductor
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password, :password_confirmation]
- require File.dirname(__FILE__) + '/../lib/warehouse_model'
config.after_initialize do
Haml::Template.options[:format] = :html5
diff --git a/src/lib/warehouse_client.rb b/src/lib/warehouse_client.rb
deleted file mode 100644
index ee2b421..0000000
--- a/src/lib/warehouse_client.rb
+++ /dev/null
@@ -1,178 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'rubygems'
-
-require 'rest-client'
-require 'nokogiri'
-
-#TODO: perform iwhd version-dependent URI mapping
-
-module Warehouse
-
- class BucketObject
- attr_reader :key
-
- def initialize(connection, key, bucket)
- @connection = connection
- @key = key
- @bucket = bucket
- @path = "/#{@bucket.name}/#{@key}"
- end
-
- def self.create(connection, key, bucket, body, attrs = {})
- obj = new(connection, key, bucket)
- obj.set_body(body)
- obj.set_attrs(attrs)
- obj
- end
-
- def body
- @connection.do_request @path, :plain => true
- end
-
- def set_body(body)
- @connection.do_request @path, :content => body, :method => :put
- end
-
- def attr_list
- result = @connection.do_request @path, :content => 'op=parts', :method => :post
- return result.xpath('/object/object_attr/@name').to_a.map {|item| item.value}
- end
-
- def attrs(list)
- attrs = {}
- list.each do |att|
- next if att.match('-')
- attrs[att] = (@connection.do_request("#{@path}/#{att}", :plain => true) rescue nil)
- end
- attrs
- end
-
- def attr(name)
- attrs([name]).first
- end
-
- def set_attrs(hash)
- hash.each do |name, content|
- set_attr(name, content)
- end
- end
-
- def set_attr(name, content)
- path = "#{@path}/#{name}"
- @connection.do_request path, :content => content, :method => :put
- end
-
- def delete!
- @connection.do_request @path, :method => :delete
- true
- end
-
- end
-
- class Bucket
- attr_accessor :name
-
- def initialize(name, connection)
- @name = name
- @connection = connection
- end
-
- def to_s
- "Bucket: #{@name}"
- end
-
- def object_names
- result = @connection.do_request "/#{@name}"
- result.xpath('/objects/object').map do |obj|
- obj.at_xpath('./key/text()').to_s
- end
- end
-
- def objects
- object_names.map do |name|
- object(name)
- end
- end
-
- def object(key)
- BucketObject.new @connection, key, self
- end
-
- def create_object(key, body, attrs)
- BucketObject.create(@connection, key, self, body, attrs)
- end
-
- def include?(key)
- object_names.include?(key)
- end
- end
-
- class Connection
- attr_accessor :uri
-
- def initialize(uri)
- @uri = uri
- end
-
- def do_request(path = '', opts={})
- opts[:method] ||= :get
- opts[:content] ||= ''
- opts[:plain] ||= false
- opts[:headers] ||= {}
-
- result = RestClient::Request.execute :method => opts[:method], :url => @uri + path, :payload => opts[:content], :headers => opts[:headers]
-
- return Nokogiri::XML result unless opts[:plain]
- return result
- end
-
- end
-
- class Client
-
- def initialize(uri)
- @connection = Connection.new(uri)
- end
-
- def create_bucket(bucket)
- @connection.do_request "/#{bucket}", :method => :put rescue RestClient::InternalServerError
- Bucket.new(bucket, @connection)
- end
-
- def bucket(bucket)
- Bucket.new bucket, @connection
- end
-
- def buckets
- @connection.do_request.xpath('/api/link[@rel="bucket"]').map do |obj|
- obj.at_xpath('./@href').to_s.gsub(/.*\//, '')
- end
- end
-
- def get_iwhd_version
- result = @connection.do_request.at_xpath('/api[@service="image_warehouse"]/@version')
- raise "Response does not contain <api> tag or version information" if result == nil
- return result.value
- end
-
- def query(bucket_name, query_string)
- @connection.do_request "/#{bucket_name}/_query", {:method => :post, :content => query_string}
- end
- end
-
-end
diff --git a/src/lib/warehouse_model.rb b/src/lib/warehouse_model.rb
deleted file mode 100644
index b13647c..0000000
--- a/src/lib/warehouse_model.rb
+++ /dev/null
@@ -1,91 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'warehouse_client'
-class BucketObjectNotFound < Exception;end
-class BucketNotFound < Exception;end
-
-class WarehouseModel
- WAREHOUSE_CONFIG = YAML.load_file("#{::Rails.root.to_s}/config/image_warehouse.yml")
-
- def ==(other_obj)
- # If the objects have different instance variables defined, they're definitely not ==
- return false unless instance_variables.sort == other_obj.instance_variables.sort
- # Otherwise, ensure that they're all the same
- instance_variables.each do |iv|
- return false unless other_obj.instance_variable_get(iv) == instance_variable_get(iv)
- end
- # They have the same instance variables and values, so they're equal
- true
- end
-
- class << self
- attr_accessor :warehouse, :bucket, :bucket_name
-
- def set_warehouse_and_bucket
- begin
- self.warehouse = Warehouse::Client.new(WAREHOUSE_CONFIG['baseurl'])
- self.bucket = self.warehouse.bucket(@bucket_name)
- rescue
- raise BucketNotFound
- end
- end
-
- def bucket_objects
- self.set_warehouse_and_bucket if self.bucket.nil?
-
- begin
- self.bucket.objects
- rescue RestClient::ResourceNotFound
- []
- end
- end
-
- def first
- obj = bucket_objects.first
- obj ? self.new(obj.attrs(obj.attr_list)) : nil
- end
-
- def last
- obj = bucket_objects.last
- obj ? self.new(obj.attrs(obj.attr_list)) : nil
- end
-
- def all
- bucket_objects.map do |wh_object|
- self.new(wh_object.attrs(wh_object.attr_list))
- end
- end
-
- def find(uuid)
- self.set_warehouse_and_bucket if self.bucket.nil?
- begin
- if self.bucket.include?(uuid)
- self.new(self.bucket.object(uuid).attrs(self.bucket.object(uuid).attr_list))
- else
- nil
- end
- rescue RestClient::ResourceNotFound
- nil
- end
- end
-
- def where(query_string)
- self.set_warehouse_and_bucket if self.bucket.nil?
- self.warehouse.query(@bucket_name, query_string)
- end
- end
-end
diff --git a/src/spec/models/warehouse_model_spec.rb b/src/spec/models/warehouse_model_spec.rb
deleted file mode 100644
index 10b957c..0000000
--- a/src/spec/models/warehouse_model_spec.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-#
-# Copyright 2011 Red Hat, Inc.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-require 'spec_helper'
-
-describe WarehouseModel do
-
- describe "==" do
- it "should be true for cloned object" do
- image1 = Image.new({:uuid => '123', :foo => 'bar'})
- image2 = image1.dup
- image1.should == image2
- end
-
- it "should be true for two identical lookups" do
- image1 = Image.first
- image2 = Image.first
- image1.should == image2
- end
-
- it "should be false for a changed object" do
- image1 = Image.new({:uuid => '123', :foo => 'bar'})
- image2 = image1.dup
- image2.foo = 'baz'
- image1.should_not == image2
- end
-
- it "should be false for objects with a different number of attributes" do
- image1 = Image.new({:uuid => '123', :foo => 'bar'})
- image2 = Image.new({:uuid => '123'})
- image1.should_not == image2
- image2.should_not == image1
- end
-
- end
-
-end
--
1.7.6
More information about the aeolus-devel
mailing list