From: Martyn Taylor <mtaylor(a)redhat.com>
---
lib/aeolus_cli.rb | 7 +-
lib/aeolus_image/model/base.rb | 31 +++++
lib/aeolus_image/model/build.rb | 20 +++
lib/aeolus_image/model/image.rb | 77 +------------
lib/aeolus_image/model/image_build.rb | 68 -----------
lib/aeolus_image/model/provider_image.rb | 32 +-----
lib/aeolus_image/model/target_image.rb | 42 +------
lib/aeolus_image/model/template.rb | 29 -----
lib/aeolus_image/model/warehouse_client.rb | 179 ----------------------------
lib/aeolus_image/model/warehouse_model.rb | 162 -------------------------
spec/spec_helper.rb | 5 +-
11 files changed, 65 insertions(+), 587 deletions(-)
create mode 100644 lib/aeolus_image/model/base.rb
create mode 100644 lib/aeolus_image/model/build.rb
delete mode 100644 lib/aeolus_image/model/image_build.rb
delete mode 100644 lib/aeolus_image/model/template.rb
delete mode 100644 lib/aeolus_image/model/warehouse_client.rb
delete mode 100644 lib/aeolus_image/model/warehouse_model.rb
diff --git a/lib/aeolus_cli.rb b/lib/aeolus_cli.rb
index f17ed66..fc71073 100644
--- a/lib/aeolus_cli.rb
+++ b/lib/aeolus_cli.rb
@@ -21,10 +21,9 @@ require File.join(File.dirname(__FILE__),
'aeolus_image/command', 'push_command'
require File.join(File.dirname(__FILE__), 'aeolus_image/command',
'import_command')
require File.join(File.dirname(__FILE__), 'aeolus_image/command',
'delete_command')
require File.join(File.dirname(__FILE__), 'aeolus_image/command',
'config_parser')
-require File.join(File.dirname(__FILE__), 'aeolus_image/model',
'warehouse_client') # Not sure we need this
-require File.join(File.dirname(__FILE__), 'aeolus_image/model',
'warehouse_model') # We may be able to factor this out?
+
+require File.join(File.dirname(__FILE__), 'aeolus_image/model', 'base')
require File.join(File.dirname(__FILE__), 'aeolus_image/model', 'image')
-require File.join(File.dirname(__FILE__), 'aeolus_image/model',
'image_build')
+require File.join(File.dirname(__FILE__), 'aeolus_image/model', 'build')
require File.join(File.dirname(__FILE__), 'aeolus_image/model',
'provider_image')
require File.join(File.dirname(__FILE__), 'aeolus_image/model',
'target_image')
-require File.join(File.dirname(__FILE__), 'aeolus_image/model',
'template')
diff --git a/lib/aeolus_image/model/base.rb b/lib/aeolus_image/model/base.rb
new file mode 100644
index 0000000..692a87a
--- /dev/null
+++ b/lib/aeolus_image/model/base.rb
@@ -0,0 +1,31 @@
+# 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 'active_resource'
+module Aeolus
+ module CLI
+ class Base < ActiveResource::Base
+ self.timeout = 600
+ class << self
+ def instantiate_collection(collection, prefix_options = {})
+ unless collection.kind_of? Array
+ [instantiate_record(collection, prefix_options)]
+ else
+ collection.collect! { |record| instantiate_record(record, prefix_options) }
+ end
+ end
+ end
+ end
+ end
+end
\ No newline at end of file
diff --git a/lib/aeolus_image/model/build.rb b/lib/aeolus_image/model/build.rb
new file mode 100644
index 0000000..624f18d
--- /dev/null
+++ b/lib/aeolus_image/model/build.rb
@@ -0,0 +1,20 @@
+# 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.
+
+module Aeolus
+ module CLI
+ class Build < Base
+ end
+ end
+end
diff --git a/lib/aeolus_image/model/image.rb b/lib/aeolus_image/model/image.rb
index 73b004e..249009f 100644
--- a/lib/aeolus_image/model/image.rb
+++ b/lib/aeolus_image/model/image.rb
@@ -13,79 +13,8 @@
# limitations under the License.
module Aeolus
- module Image
- class Image < WarehouseModel
- OS = Struct.new(:name, :version, :arch)
-
- @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
- @xml_body = Nokogiri::XML @body
- end
-
- def template_xml
- unless @template_xml
- begin
- @template_xml = Nokogiri::XML
image_builds.first.target_images.first.target_template.body
- rescue
- @template_xml = Nokogiri::XML "<template></template>"
- end
- end
- @template_xml
- end
-
- def latest_pushed_build
- ImageBuild.find(@latest_build) if @latest_build
- end
-
- def image_builds
- ImageBuild.find_all_by_image_uuid(@uuid)
- end
-
- #TODO: We should get the image fields from the object body once we have it
defined.
- def name
- unless @name
- @name = @xml_body.xpath("/image/name").text
- if @name.empty?
- @name = template_xml.xpath("/template/name").text
- end
- end
- @name
- end
-
- def os
- unless @os
- @os = OS.new(template_xml.xpath("/template/os/name").text,
template_xml.xpath("/template/os/version").text,
template_xml.xpath("/template/os/arch").text)
- end
- @os
- end
-
- def description
- unless @description
- @description = template_xml.xpath("/template/description").text
- end
- @description
- end
-
- # Delete this image and all child objects
- def delete!
- begin
- image_builds.each do |build|
- build.delete!
- end
- rescue NoMethodError
- end
- Image.delete(@uuid)
- end
+ module CLI
+ class Image < Base
end
end
-end
+end
\ No newline at end of file
diff --git a/lib/aeolus_image/model/image_build.rb
b/lib/aeolus_image/model/image_build.rb
deleted file mode 100644
index b751117..0000000
--- a/lib/aeolus_image/model/image_build.rb
+++ /dev/null
@@ -1,68 +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.
-
-module Aeolus
- module Image
- 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') == 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
-
- # Deletes this image and all child objects
- def delete!
- begin
- target_images.each do |ti|
- ti.delete!
- end
- rescue NoMethodError
- end
- ImageBuild.delete(@uuid)
- end
- end
- end
-end
diff --git a/lib/aeolus_image/model/provider_image.rb
b/lib/aeolus_image/model/provider_image.rb
index a5f319d..c3d3a52 100644
--- a/lib/aeolus_image/model/provider_image.rb
+++ b/lib/aeolus_image/model/provider_image.rb
@@ -13,34 +13,8 @@
# limitations under the License.
module Aeolus
- module Image
- 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
-
- # Deletes this provider image
- def delete!
- ProviderImage.delete(@uuid)
- end
+ module CLI
+ class ProviderImage < Base
end
end
-end
+end
\ No newline at end of file
diff --git a/lib/aeolus_image/model/target_image.rb
b/lib/aeolus_image/model/target_image.rb
index c029430..9972372 100644
--- a/lib/aeolus_image/model/target_image.rb
+++ b/lib/aeolus_image/model/target_image.rb
@@ -13,44 +13,8 @@
# limitations under the License.
module Aeolus
- module Image
- 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
-
- def target_template
- Template.find(@template) if @template
- end
-
- # Deletes this targetimage and all child objects
- def delete!
- begin
- provider_images.each do |pi|
- pi.delete!
- end
- rescue NoMethodError
- end
- TargetImage.delete(@uuid)
- end
+ module CLI
+ class TargetImage < Base
end
end
-end
+end
\ No newline at end of file
diff --git a/lib/aeolus_image/model/template.rb b/lib/aeolus_image/model/template.rb
deleted file mode 100644
index e1ac3cb..0000000
--- a/lib/aeolus_image/model/template.rb
+++ /dev/null
@@ -1,29 +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.
-
-module Aeolus
- module Image
- class Template < WarehouseModel
- @bucket_name = 'templates'
-
- def initialize(attrs)
- attrs.each do |k,v|
- sym = :attr_accessor
- self.class.send(sym, k.to_sym) unless respond_to?(:"#{k}=")
- send(:"#{k}=", v)
- end
- end
- end
- end
-end
diff --git a/lib/aeolus_image/model/warehouse_client.rb
b/lib/aeolus_image/model/warehouse_client.rb
deleted file mode 100644
index 7210dd3..0000000
--- a/lib/aeolus_image/model/warehouse_client.rb
+++ /dev/null
@@ -1,179 +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 Aeolus
- module Image
- module Warehouse
-
- class BucketObject
- attr_reader :key
-
- def initialize(connection, key, bucket)
- @connection = connection
- @key = key
- @bucket = bucket
- @path = "/#{@bucket.name}/#{(a)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/(a)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])[name]
- 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[(a)rel="bucket"]').map
do |obj|
- obj.at_xpath('./(a)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
- end
-end
diff --git a/lib/aeolus_image/model/warehouse_model.rb
b/lib/aeolus_image/model/warehouse_model.rb
deleted file mode 100644
index 2586f6d..0000000
--- a/lib/aeolus_image/model/warehouse_model.rb
+++ /dev/null
@@ -1,162 +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.
-
-module Aeolus
- module Image
- # require 'warehouse_client'
- include Warehouse
- class BucketObjectNotFound < Exception;end
- class BucketNotFound < Exception;end
-
- class WarehouseModel
-
- 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
- @@config ||= load_config
- self.warehouse = Warehouse::Client.new(@@config[:iwhd][:url])
- 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
-
- def delete(uuid)
- self.set_warehouse_and_bucket if self.bucket.nil?
- begin
- if self.bucket.include?(uuid)
- self.bucket.object(uuid).delete!
- else
- false
- end
- rescue RestClient::ResourceNotFound
- false
- end
- end
-
- protected
- # Copy over entirely too much code to load the config file
- def load_config
- # TODO - Is this always the case? We should probably have /etc/aeolus-cli or
something too?
- # Or allow Rails to override this
- @config_location ||= "~/.aeolus-cli"
- begin
- file_str = read_file(@config_location)
- if is_file?(@config_location) &&
!file_str.include?(":url")
- lines = File.readlines(File.expand_path((a)config_location)).map do |line|
- "#" + line
- end
- File.open(File.expand_path(@config_location), 'w') do |file|
- file.puts lines
- end
- write_file
- end
- write_file unless is_file?(@config_location)
- YAML::load(File.open(File.expand_path(@config_location)))
- rescue Errno::ENOENT
- #TODO: Create a custom exception to wrap CLI Exceptions
- raise "Unable to locate or write configuration file: \"" +
@config_location + "\""
- end
- end
-
- def write_file
- example = File.read(File.expand_path(File.dirname(__FILE__) +
"/../../examples/aeolus-cli"))
- File.open(File.expand_path(@config_location), 'a+') do |f|
- f.write(example)
- end
- end
-
- def read_file(path)
- begin
- full_path = File.expand_path(path)
- if is_file?(path)
- File.read(full_path)
- else
- return nil
- end
- rescue
- nil
- end
- end
-
- def is_file?(path)
- full_path = File.expand_path(path)
- if File.exist?(full_path) && !File.directory?(full_path)
- return true
- end
- false
- end
-
- end
-
- end
- end
-end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 69b1139..53f43b4 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -16,10 +16,9 @@ $: << File.expand_path(File.join(File.dirname(__FILE__),
"../lib/aeolus_image/co
$: << File.expand_path(File.join(File.dirname(__FILE__),
"../lib/aeolus_image/model/"))
require 'rubygems'
-require 'warehouse_client'
-require 'warehouse_model'
+require 'base'
require 'image'
-require 'image_build'
+require 'build'
require 'target_image'
require 'provider_image'
require 'config_parser'
--
1.7.6.2