From: Jan Provaznik <jprovazn(a)redhat.com>
Deployable#image_status method moved to ProviderAccount model
because it's now called from multiple places
"Imported image can't be built or pushed" message is displayed instead
of build/push button if an image is imported.
---
src/app/controllers/images_controller.rb | 45 ++++++++++++++++++++++++++++++
src/app/models/deployable.rb | 24 +---------------
src/app/models/provider_account.rb | 24 ++++++++++++++++
src/app/views/images/show.html.haml | 6 +++-
src/config/locales/en.yml | 5 +++
src/config/routes.rb | 1 +
src/spec/models/provider_account_spec.rb | 29 +++++++++++++++++++
7 files changed, 110 insertions(+), 24 deletions(-)
diff --git a/src/app/controllers/images_controller.rb
b/src/app/controllers/images_controller.rb
index 43212ef..41c48a5 100644
--- a/src/app/controllers/images_controller.rb
+++ b/src/app/controllers/images_controller.rb
@@ -71,6 +71,33 @@ class ImagesController < ApplicationController
redirect_to image_path((a)image.id)
end
+ def push_all
+ @image = Aeolus::Image::Warehouse::Image.find(params[:id])
+ @build = Aeolus::Image::Warehouse::ImageBuild.find(params[:build_id])
+ # only latest builds can be pushed
+ unless latest_build?(@build)
+ redirect_to image_path((a)image.id)
+ return
+ end
+ accounts = ProviderAccount.list_for_user(current_user, Privilege::VIEW)
+ target_images = @build.target_images
+ accounts.each do |account|
+ if account.image_status(@image) == :not_pushed
+ target = account.provider.provider_type.deltacloud_driver
+ target_image = target_images.find { |ti| ti.target == target }
+ provider_image = Aeolus::Image::Factory::ProviderImage.new(
+ :provider => account.provider.name,
+ :credentials => account.to_xml(:with_credentials => true),
+ :image_id => @image.uuid,
+ :build_id => @build.uuid,
+ :target_image_id => target_image.uuid
+ )
+ provider_image.save!
+ end
+ end
+ redirect_to image_path((a)image.id)
+ end
+
def template
image = Aeolus::Image::Warehouse::Image.find(params[:id])
template = Aeolus::Image::Warehouse::Template.find(image.template)
@@ -259,4 +286,22 @@ class ImagesController < ApplicationController
def check_permissions
require_privilege(Privilege::USE, PoolFamily)
end
+
+ def latest_build?(build)
+ unless build
+ flash[:error] = t('images.show.missing_build')
+ return false
+ end
+ begin
+ latest_build = @image.latest_pushed_or_unpushed_build.uuid
+ if latest_build != build.id
+ flash[:error] = t('images.show.only_latest_builds_can_be_pushed')
+ return false
+ end
+ rescue
+ flash[:error] = t('images.show.not_built')
+ return false
+ end
+ return true
+ end
end
diff --git a/src/app/models/deployable.rb b/src/app/models/deployable.rb
index b0e61ec..4eb5695 100644
--- a/src/app/models/deployable.rb
+++ b/src/app/models/deployable.rb
@@ -142,7 +142,7 @@ class Deployable < ActiveRecord::Base
end
def build_status(images, account)
- image_statuses = images.map { |i| image_status(i, account) }
+ image_statuses = images.map { |i| account.image_status(i) }
return :not_built if image_statuses.any? { |status| status == :not_built }
return :building if image_statuses.any? { |status| status == :building }
return :pushing if image_statuses.any? { |status| status == :pushing }
@@ -150,28 +150,6 @@ class Deployable < ActiveRecord::Base
:pushed
end
- def image_status(image, account)
- target = account.provider.provider_type.deltacloud_driver
-
- builder = Aeolus::Image::Factory::Builder.first
- return :building if builder.find_active_build_by_imageid(image.id, target)
-
- build = image.latest_pushed_or_unpushed_build
- return :not_built unless build
-
- target_image = build.target_images.find { |ti| ti.target == target }
- return :not_built unless target_image
-
- return :pushing if builder.find_active_push(target_image.id,
- account.provider.name,
-
account.credentials_hash["username"])
-
- provider_image = target_image.find_provider_image_by_provider_and_account(
- account.provider.name, account.credentials_hash["username"]).first
- return :not_pushed unless provider_image
- :pushed
- end
-
def to_polymorphic_path_param(polymorphic_path_extras)
catalog = catalogs.find(polymorphic_path_extras[:catalog_id]) if
(polymorphic_path_extras.present? &&
polymorphic_path_extras.has_key?(:catalog_id))
diff --git a/src/app/models/provider_account.rb b/src/app/models/provider_account.rb
index 110ae68..f076657 100644
--- a/src/app/models/provider_account.rb
+++ b/src/app/models/provider_account.rb
@@ -371,6 +371,30 @@ class ProviderAccount < ActiveRecord::Base
end
end
+ # TODO: it would be much better to have this method in image model,
+ # but we don't have suitable image model ATM.
+ def image_status(image)
+ target = provider.provider_type.deltacloud_driver
+
+ builder = Aeolus::Image::Factory::Builder.first
+ return :building if builder.find_active_build_by_imageid(image.id, target)
+
+ build = image.latest_pushed_or_unpushed_build
+ return :not_built unless build
+
+ target_image = build.target_images.find { |ti| ti.target == target }
+ return :not_built unless target_image
+
+ return :pushing if builder.find_active_push(target_image.id,
+ provider.name,
+ credentials_hash["username"])
+
+ provider_image = target_image.find_provider_image_by_provider_and_account(
+ provider.name, credentials_hash["username"]).first
+ return :not_pushed unless provider_image
+ :pushed
+ end
+
private
def self.apply_search_filter(search)
diff --git a/src/app/views/images/show.html.haml b/src/app/views/images/show.html.haml
index eda142d..68af9f6 100644
--- a/src/app/views/images/show.html.haml
+++ b/src/app/views/images/show.html.haml
@@ -19,8 +19,12 @@
= form_tag image_path((a)image.id), :method => :get do
= select_tag :build, options_for_build_select(@builds, @build, @latest_build)
= submit_tag t('.select_build'), :id =>
'seletect_build_button'
- - unless @image.imported?
+ - if @image.imported?
+ = t('.can_not_build_imported_image')
+ -else
= button_to t('.rebuild_all'), rebuild_all_image_path((a)image.id), :class
=> 'button pill'
+ - if @build and @build.id == @latest_build
+ = button_to t('.push_all'), push_all_image_path((a)image.id, :build_id
=> @build.id), :class => 'button pill'
.content
%ul.image_builds
- @account_groups.each do |driver, group|
diff --git a/src/config/locales/en.yml b/src/config/locales/en.yml
index 90e13ac..26bfd32 100644
--- a/src/config/locales/en.yml
+++ b/src/config/locales/en.yml
@@ -725,6 +725,11 @@ en:
provider: Provider
build_date: Build Completion Date
latest: Latest
+ only_latest_builds_can_be_pushed: "Only latest build can be pushed"
+ not_built: "The image is not built yet"
+ missing_build: "Select a build first"
+ push_all: "Push all"
+ can_not_build_imported_image: "Imported image can't be built or
pushed"
select_build: Select
template_xml: View Template XML
flash:
diff --git a/src/config/routes.rb b/src/config/routes.rb
index 9a270a8..7127bdc 100644
--- a/src/config/routes.rb
+++ b/src/config/routes.rb
@@ -227,6 +227,7 @@ Conductor::Application.routes.draw do
resources :images do
member do
post 'rebuild_all'
+ post 'push_all'
get 'template'
end
collection do
diff --git a/src/spec/models/provider_account_spec.rb
b/src/spec/models/provider_account_spec.rb
index 2fa2cf1..7c1e60d 100644
--- a/src/spec/models/provider_account_spec.rb
+++ b/src/spec/models/provider_account_spec.rb
@@ -182,4 +182,33 @@ describe ProviderAccount do
end
end
+ describe "#image_status" do
+ before(:each) do
+ provider = FactoryGirl.create(:mock_provider, :name => 'mock')
+ @account = FactoryGirl.create(:mock_provider_account, :provider => provider)
+ @image = mock(Aeolus::Image::Warehouse::Image, :latest_pushed_or_unpushed_build
=> nil, :id => 1)
+ # TODO: get rid of hardcoded image uuid here
+ @vcr_image =
Aeolus::Image::Warehouse::Image.find('53d2a281-448b-4872-b1b0-680edaad5922')
+ @factory_build = mock(Aeolus::Image::Factory::Builder,
:find_active_build_by_imageid => nil, :find_active_push => nil)
+ Aeolus::Image::Factory::Builder.stub(:first).and_return(@factory_build)
+ end
+
+ it "should return :building status" do
+ @factory_build.stub(:find_active_build_by_imageid).and_return(true)
+ @account.image_status((a)image).should == :building
+ end
+
+ it "should return :not_built status" do
+ @account.image_status((a)image).should == :not_built
+ end
+
+ it "should return :pushed status" do
+ @account.image_status((a)vcr_image).should == :pushed
+ end
+
+ it "should return :pushing status" do
+ @factory_build.stub(:find_active_push).and_return(true)
+ @account.image_status((a)vcr_image).should == :pushing
+ end
+ end
end
--
1.7.7.5