From: Jan Provaznik <[email protected]>
---
src/app/models/image.rb | 4 +-
src/app/models/template.rb | 30 ++++++++++++++------
src/db/migrate/20100830150014_create_templates.rb | 4 +++
3 files changed, 27 insertions(+), 11 deletions(-)
diff --git a/src/app/models/image.rb b/src/app/models/image.rb
index d4f4fa4..75cd9a6 100644
--- a/src/app/models/image.rb
+++ b/src/app/models/image.rb
@@ -27,7 +27,7 @@ class Image < ActiveRecord::Base
cattr_reader :per_page
@@per_page = 15
- belongs_to :template
+ belongs_to :template, :counter_cache => true
has_many :replicated_images, :dependent => :destroy
has_many :providers, :through => :replicated_images
@@ -45,7 +45,7 @@ class Image < ActiveRecord::Base
STATE_COMPLETE = 'complete'
STATE_CANCELED = 'canceled'
- ACTIVE_STATES = [ STATE_WAITING, STATE_BUILDING ]
+ ACTIVE_STATES = [ STATE_QUEUED, STATE_WAITING, STATE_BUILDING ]
def self.new_if_not_exists(data)
unless find_by_template_id(data[:template_id], :conditions => {:target =>
data[:target]})
diff --git a/src/app/models/template.rb b/src/app/models/template.rb
index 239de4d..f4f77fe 100644
--- a/src/app/models/template.rb
+++ b/src/app/models/template.rb
@@ -4,6 +4,7 @@ require 'typhoeus'
class Template < ActiveRecord::Base
has_many :images, :dependent => :destroy
before_validation :update_attrs
+ before_save :discard_upload
WAREHOUSE_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/image_warehouse.yml")
@@ -12,14 +13,18 @@ class Template < ActiveRecord::Base
# uncomment this after reworking view (currently is used wizard,
# so there can be situation when save is called and name and platform can be
# unset)
- #validates_presence_of :name
- #validates_presence_of :platform
+ validates_presence_of :name
+ validates_presence_of :platform
+ validates_presence_of :platform_version
+ validates_presence_of :architecture
def update_xml_attributes!(opts = {})
doc = xml
doc.name = opts[:name] if opts[:name]
doc.platform = opts[:platform] if opts[:platform]
doc.description = opts[:description] if opts[:description]
+ doc.platform_version = opts[:platform_version] if opts[:platform_version]
+ doc.architecture = opts[:architecture] if opts[:architecture]
doc.services = (opts[:services] || []) if opts[:services] or
opts[:set_services]
doc.packages = (opts[:packages] || []) if opts[:packages] or
opts[:set_packages]
save_xml!
@@ -28,7 +33,8 @@ class Template < ActiveRecord::Base
def save_xml!
self[:xml] = xml.to_xml
@xml = nil
- save!
+ update_attrs
+ save_without_validation!
end
def xml
@@ -38,12 +44,7 @@ class Template < ActiveRecord::Base
def upload_template
self.uri = File.join(WAREHOUSE_CONFIG['baseurl'], "template_#{id}")
response = Typhoeus::Request.put(self.uri, :body => xml.to_xml, :timeout
=> 30000)
- if response.code == 200
- save!
- else
- raise "failed to upload template (return code #{response.code}):
#{response.body}"
- end
- return true
+ raise "failed to upload template" unless response.code == 200
end
def update_attrs
@@ -52,6 +53,8 @@ class Template < ActiveRecord::Base
self.name = xml.name
self.summary = xml.description
self.platform = xml.platform
+ self.platform_version = xml.platform_version
+ self.architecture = xml.architecture
end
def providers
@@ -61,4 +64,13 @@ class Template < ActiveRecord::Base
:conditions => {:images => {:template_id => self.id}}
).map {|p| p.provider}
end
+
+ def self.find_or_create(id)
+ id ? Template.find(id) : Template.new
+ end
+
+ def discard_upload
+ uploaded = false
+ return true
+ end
end
diff --git a/src/db/migrate/20100830150014_create_templates.rb
b/src/db/migrate/20100830150014_create_templates.rb
index 00b1966..4e6f89d 100644
--- a/src/db/migrate/20100830150014_create_templates.rb
+++ b/src/db/migrate/20100830150014_create_templates.rb
@@ -6,8 +6,12 @@ class CreateTemplates < ActiveRecord::Migration
t.string :uri
t.string :name
t.string :platform
+ t.string :platform_version
+ t.string :architecture
t.text :summary
t.boolean :complete, :default => false
+ t.boolean :uploaded, :default => false
+ t.integer :images_count
t.timestamps
end
end
--
1.7.2.3
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel