From: Jan Provaznik <[email protected]>

https://bugzilla.redhat.com/show_bug.cgi?id=643030

We now check if array of targets is not empty. Also cleaned up build method.
---
 src/app/controllers/templates_controller.rb |   49 ++++++++++++++-------------
 src/app/models/image.rb                     |   30 +++++++++++++---
 src/app/views/templates/build_form.haml     |    8 ++--
 3 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/src/app/controllers/templates_controller.rb 
b/src/app/controllers/templates_controller.rb
index 0952406..693780b 100644
--- a/src/app/controllers/templates_controller.rb
+++ b/src/app/controllers/templates_controller.rb
@@ -32,7 +32,7 @@ class TemplatesController < ApplicationController
     elsif params[:edit]
       redirect_to :action => 'new', :id => get_selected_id
     elsif params[:build]
-      redirect_to :action => 'build_form', 'image[template_id]' => 
get_selected_id
+      redirect_to :action => 'build_form', 'template_id' => get_selected_id
     else
       raise "Unknown action"
     end
@@ -121,8 +121,8 @@ class TemplatesController < ApplicationController
   end
 
   def build_form
-    raise "select template to build" unless params[:image] and 
params[:image][:template_id]
-    @image = Image.new(params[:image])
+    raise "select template to build" unless id = params[:template_id]
+    @tpl = Template.find(id)
     @all_targets = Image.available_targets
   end
 
@@ -132,30 +132,31 @@ class TemplatesController < ApplicationController
       return
     end
 
-    #FIXME: The following functionality needs to come out of the controller
-    @image = Image.new(params[:image])
-    @image.template.upload_template unless @image.template.uploaded
-    # FIXME: this will need to re-render build with error messages,
-    # just fails right now if anything is wrong (like no target selected).
+    @tpl = Template.find(params[:template_id])
+    @all_targets = Image.available_targets
+
+    if params[:targets].blank?
+      flash.now[:warning] = 'You need to check at least one provider format'
+      render :action => 'build_form'
+      return
+    end
+
+    @tpl.upload_template unless @tpl.uploaded
     params[:targets].each do |target|
-      i = Image.new_if_not_exists(
-        :name => "#[email protected]}/#{target}",
-        :target => target,
-        :template_id => @image.template_id,
-        :status => Image::STATE_QUEUED
-      )
-      # FIXME: This will need to be enhanced to handle multiple
-      # providers of same type, only one is supported right now
-      if i
-        image = Image.find_by_template_id(params[:image][:template_id],
-                                :conditions => {:target => target})
-        ReplicatedImage.create!(
-          :image_id => image.id,
-          :provider_id => Provider.find_by_cloud_type(target)
-        )
+      begin
+        Image.build(@tpl, target)
+      rescue
+        flash.now[:error] ||= {}
+        flash.now[:error][:failures] ||= {}
+        flash.now[:error][:failures][target] = $!.message
       end
     end
-    redirect_to :action => 'builds'
+    if flash[:error] and not flash[:error][:failures].blank?
+      flash[:error][:summary] = 'Error while trying to build image'
+      render :action => 'build_form'
+    else
+      redirect_to :action => 'builds'
+    end
   end
 
   def builds
diff --git a/src/app/models/image.rb b/src/app/models/image.rb
index 8955426..dfe8a2a 100644
--- a/src/app/models/image.rb
+++ b/src/app/models/image.rb
@@ -49,12 +49,6 @@ class Image < ActiveRecord::Base
   ACTIVE_STATES = [ STATE_QUEUED, STATE_CREATED, STATE_BUILDING ]
   INACTIVE_STATES = [STATE_COMPLETE, STATE_FAILED, STATE_CANCELED]
 
-  def self.new_if_not_exists(data)
-    unless find_by_template_id(data[:template_id], :conditions => {:target => 
data[:target]})
-      Image.new(data).save!
-    end
-  end
-
   def self.available_targets
     return YAML.load_file("#{RAILS_ROOT}/config/image_descriptor_targets.yml")
   end
@@ -62,4 +56,28 @@ class Image < ActiveRecord::Base
   def generate_uuid
     self.uuid ||= "image-#{self.template_id}-#{Time.now.to_f.to_s}"
   end
+
+  def self.build(template, target)
+    # FIXME: This will need to be enhanced to handle multiple
+    # providers of same type, only one is supported right now
+    if img = Image.find_by_template_id(template.id, :conditions => {:target => 
target})
+      # TODO: we currently silently ignore requests for building image which is
+      # already built (or is building now)
+      return img
+    end
+
+    Image.transaction do
+      img = Image.create!(
+        :name => "#{template.xml.name}/#{target}",
+        :target => target,
+        :template_id => template.id,
+        :status => Image::STATE_QUEUED
+      )
+      ReplicatedImage.create!(
+        :image_id => img.id,
+        :provider_id => Provider.find_by_cloud_type(target)
+      )
+    end
+    return img
+  end
 end
diff --git a/src/app/views/templates/build_form.haml 
b/src/app/views/templates/build_form.haml
index 75f8a34..7084f39 100644
--- a/src/app/views/templates/build_form.haml
+++ b/src/app/views/templates/build_form.haml
@@ -1,6 +1,6 @@
 %h2 BUILD REQUEST
-- form_for @image, :url => { :action => "build" } do
-  = hidden_field :image, :template_id
+- form_tag :action => 'build' do
+  = hidden_field_tag :template_id, @tpl.id
   %h3 Deployment Definition
   %fieldset.clearfix
     = label_tag :deploy_name, 'Deployment Definition Name:', :class => 'grid_4'
@@ -25,7 +25,7 @@
   %fieldset.clearfix
     .grid_6
       %label OS:
-      = @image.template.platform
+      = @tpl.platform
     .grid_3
       Hardware Profile
     .grid_3.suffix_3
@@ -33,7 +33,7 @@
   %fieldset.clearfix
     .grid_3
       %label OS Version:
-      = @image.template.platform_version
+      = @tpl.platform_version
     .grid_3.ra
       %label Global Settings:
     .grid_3
-- 
1.7.2.3

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to