This also contains sample settings for warehouse and
mock target, so you can do build for mock driver. We were
not yet creating a ReplicatedImage, which is needed to complete
any build.
---
src/app/controllers/templates_controller.rb | 15 +++++++++-
src/app/models/image.rb | 3 +-
src/app/models/template.rb | 2 +-
src/config/image_descriptor_targets.yml | 4 ++
src/config/image_warehouse.yml | 4 ++-
src/runner/image_builder_service.rb | 33 ++++++++++++++------
src/spec/controllers/templates_controller_spec.rb | 22 ++++++++++++++
7 files changed, 69 insertions(+), 14 deletions(-)
diff --git a/src/app/controllers/templates_controller.rb
b/src/app/controllers/templates_controller.rb
index 78113bf..cde4493 100644
--- a/src/app/controllers/templates_controller.rb
+++ b/src/app/controllers/templates_controller.rb
@@ -87,15 +87,28 @@ 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).
params[:targets].each do |target|
- Image.new_if_not_exists(
+ 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)
+ )
+ end
end
redirect_to :action => 'builds'
end
diff --git a/src/app/models/image.rb b/src/app/models/image.rb
index 75cd9a6..b1fa6d7 100644
--- a/src/app/models/image.rb
+++ b/src/app/models/image.rb
@@ -44,8 +44,9 @@ class Image < ActiveRecord::Base
STATE_BUILDING = 'building'
STATE_COMPLETE = 'complete'
STATE_CANCELED = 'canceled'
+ STATE_FAILED = 'failed'
- ACTIVE_STATES = [ STATE_QUEUED, STATE_WAITING, STATE_BUILDING ]
+ ACTIVE_STATES = [ STATE_QUEUED, STATE_WAITING, STATE_BUILDING, STATE_FAILED ]
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 4c1bcca..4af8a02 100644
--- a/src/app/models/template.rb
+++ b/src/app/models/template.rb
@@ -41,7 +41,7 @@ class Template < ActiveRecord::Base
end
def upload_template
- self.uri = File.join(WAREHOUSE_CONFIG['baseurl'], "template_#{id}")
+ 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)
diff --git a/src/config/image_descriptor_targets.yml
b/src/config/image_descriptor_targets.yml
index e859560..e711534 100644
--- a/src/config/image_descriptor_targets.yml
+++ b/src/config/image_descriptor_targets.yml
@@ -12,3 +12,7 @@ ec2:
#vmware_esx:
# name: VMWare ESX
+
+# For development, just uncomment this target to do fake image builds
+#mock:
+# name: Mock
\ No newline at end of file
diff --git a/src/config/image_warehouse.yml b/src/config/image_warehouse.yml
index ccc3571..cc7545c 100644
--- a/src/config/image_warehouse.yml
+++ b/src/config/image_warehouse.yml
@@ -1 +1,3 @@
-baseurl: http://localhost:9090/my_bucket/
+baseurl: http://localhost:9090/templates/
+# to set up the bucket, run:
+# curl -X PUT http://localhost:9090/templates
diff --git a/src/runner/image_builder_service.rb
b/src/runner/image_builder_service.rb
index f6a361b..e2be26c 100644
--- a/src/runner/image_builder_service.rb
+++ b/src/runner/image_builder_service.rb
@@ -36,6 +36,7 @@ class Logger
end
end
+#TODO: Make this whole thing less fragile
class ImageBuilderService
def initialize()
@console ||= ImageBuilderConsole.new
@@ -61,18 +62,23 @@ class ImageBuilderService
}
end
- def build(descriptor_target)
+ def build(image)
#targets.each do |t|
puts "========================================"
- puts "target: " + descriptor_target.name + ", status: " +
descriptor_target.status
+ puts "target: " + image.target + ", status: " + image.status
puts "========================================"
- ab = @console.build_image(descriptor_target.template.xml.to_xml,
descriptor_target.name)
+ # FIXME: this should be contained elsewhere (probably Image model) so we
+ # can keep logic out of here. Also, this currently only handles one
+ # account, we will need to be able to specify at some point.
+ creds =
image.replicated_images.first.provider.cloud_accounts.first.build_credentials
+ #TODO: switch this back to uri once ActiveBuild retrieves it properly
+ ab = @console.build_image(image.template.xml.to_xml, image.target,
image.uuid, creds)
if ab
- update_build_list(ab, descriptor_target)
- descriptor_target.build_id = ab.object_id.to_s
- descriptor_target.save!
+ update_build_list(ab, image)
+ image.build_id = ab.object_id.to_s
+ image.save!
puts "========================================"
- puts "Build id saved as: " + descriptor_target.build_id
+ puts "Build id saved as: " + image.build_id
puts "========================================"
end
#end
@@ -112,13 +118,20 @@ class ImageBuilderService
puts "========================================"
puts "Getting ar object to update using " + obj[:build].target.inspect + "
and " + obj[:ar_id].inspect + " ..."
puts "========================================"
- idt = Image.find(:first, :conditions => { :name => obj[:build].target.to_s,
+ image = Image.find(:first, :conditions => { :target =>
obj[:build].target.to_s,
:template_id =>
obj[:ar_id].to_i })
puts "========================================"
puts "Updating with status: " + new_status
puts "========================================"
- idt.status = new_status
- idt.save!
+ image.status = new_status
+ if new_status == 'complete'
+ ri = image.replicated_images.first
+ ri.provider_image_key = obj[:build].finished_image
+ ri.uploaded =true
+ ri.registered=true
+ ri.save!
+ end
+ image.save!
puts "========================================"
puts "database updated!"
puts "========================================"
diff --git a/src/spec/controllers/templates_controller_spec.rb
b/src/spec/controllers/templates_controller_spec.rb
index 50ef5cc..af837d6 100644
--- a/src/spec/controllers/templates_controller_spec.rb
+++ b/src/spec/controllers/templates_controller_spec.rb
@@ -23,4 +23,26 @@ describe TemplatesController do
get :new
response.should_not render_template("new")
end
+
+ context "when a user has permission to build templates" do
+ #FIXME: The following functionality needs to come out of the controller
+
+ before(:each) do
+ UserSession.create(@admin)
+ @template = Factory.create(:template)
+ end
+
+ it "should create a new Image" do
+ lambda do
+ post :build, :image => {:template_id => @template.id}, :targets =>
["mock"]
+ end.should change(Image, :count).by(1)
+ end
+
+ it "should create a new ReplicatedImage" do
+ mock = Factory.create(:mock_provider)
+ lambda do
+ post :build, :image => {:template_id => @template.id}, :targets =>
["mock"]
+ end.should change(ReplicatedImage, :count).by(1)
+ end
+ end
end
--
1.7.2.3
_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel