Skip to content

Commit

Permalink
Merge pull request #4865 from adamruzicka/dynflow-system-update
Browse files Browse the repository at this point in the history
Fixes #6184 - Dynflowizes system update
  • Loading branch information
jlsherrill committed Jan 13, 2015
2 parents 83c7758 + 729f9f4 commit 106bb48
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 118 deletions.
3 changes: 1 addition & 2 deletions app/controllers/katello/api/v2/systems_controller.rb
Expand Up @@ -143,8 +143,7 @@ def create
param :host_collection_ids, Array, :desc => N_("Specify the host collections as an array")
def update
system_params = system_params(params)
@system.update_attributes!(system_params)
@system.refresh_subscriptions if system_params[:autoheal]
sync_task(::Actions::Katello::System::Update, @system, system_params)
respond_for_update
end

Expand Down
31 changes: 31 additions & 0 deletions app/lib/actions/candlepin/consumer/refresh_subscriptions.rb
@@ -0,0 +1,31 @@
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
module Candlepin
module Consumer
class RefreshSubscrpitions < Candlepin::Abstract
input_format do
param :uuid, String
end

def plan(system, sys_params)
plan_self(:uuid => system.uuid) if sys_params[:autoheal]
end

def run
::Katello::Resources::Candlepin::Consumer.refresh_entitlements(input[:uuid])
end
end
end
end
end
45 changes: 45 additions & 0 deletions app/lib/actions/candlepin/consumer/update.rb
@@ -0,0 +1,45 @@
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
module Candlepin
module Consumer
class Update < Candlepin::Abstract
def plan(system)
plan_self(:uuid => system.uuid,
:facts => system.facts,
:guestIds => system.guestIds,
:installedProducts => system.installedProducts,
:autoheal => system.autoheal,
:releaseVer => system.releaseVer,
:serviceLevel => system.serviceLevel,
:cp_environment_id => system.cp_environment_id,
:capabilities => system.capabilities,
:lastCheckin => system.lastCheckin)
end

def run
::Katello::Resources::Candlepin::Consumer.update(input[:uuid],
input[:facts],
input[:guestIds],
input[:installedProducts],
input[:autoheal],
input[:releaseVer],
input[:serviceLevel],
input[:cp_environment_id],
input[:capabilities],
input[:lastCheckin])
end
end
end
end
end
47 changes: 47 additions & 0 deletions app/lib/actions/katello/foreman/host_update.rb
@@ -0,0 +1,47 @@
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
module Katello
module Foreman
class HostUpdate < Actions::EntryAction
input_format do
param :system_id, Integer
param :environment_id, Integer
end

def plan(system)
if system.foreman_host &&
system.foreman_host.environment.lifecycle_environment &&
system.foreman_host.environment.content_view

if puppet_env = system.content_view.puppet_env(system.environment).try(:puppet_environment)
if puppet_env.id != system.foreman_host.environment_id
plan_self(:system_id => system.id, :environment_id => puppet_env.id)
end
else
fail Errors::NotFound,
_("Couldn't find puppet environment associated with lifecycle environment '%{env}' and content view '%{view}'") %
{ :env => system.environment.name, :view => system.content_view.name }
end
end
end

def run
system = ::Katello::System.find(input[:system_id])
system.foreman_host.environment_id = input[:environment_id]
system.foreman_host.save!
end
end
end
end
end
32 changes: 32 additions & 0 deletions app/lib/actions/katello/system/update.rb
@@ -0,0 +1,32 @@
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
module Katello
module System
class Update < Actions::EntryAction
middleware.use ::Actions::Middleware::RemoteAction

def plan(system, sys_params)
system.disable_auto_reindex!
action_subject system
system.update_attributes!(sys_params)
plan_action(::Actions::Katello::Foreman::HostUpdate, system)
plan_action(::Actions::Pulp::Consumer::Update, system) if ::Katello.config.use_pulp
plan_action(::Actions::Candlepin::Consumer::Update, system) if ::Katello.config.use_cp
plan_action(ElasticSearch::Reindex, system) if ::Katello.config.use_elasticsearch
plan_action(::Actions::Candlepin::Consumer::RefreshSubscrpitions, system, sys_params) if ::Katello.config.use_cp
end
end
end
end
end
32 changes: 32 additions & 0 deletions app/lib/actions/pulp/consumer/update.rb
@@ -0,0 +1,32 @@
#
# Copyright 2014 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public
# License as published by the Free Software Foundation; either version
# 2 of the License (GPLv2) or (at your option) any later version.
# There is NO WARRANTY for this software, express or implied,
# including the implied warranties of MERCHANTABILITY,
# NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
# have received a copy of GPLv2 along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

module Actions
module Pulp
module Consumer
class Update < Pulp::Abstract
input_format do
param :uuid, String
param :display_name, String
end

def plan(system)
plan_self(:uuid => system.uuid, :display_name => system.name)
end

def run
::Katello.pulp_server.extensions.consumer.update(input[:uuid], :display_name => input[:name])
end
end
end
end
end
42 changes: 7 additions & 35 deletions app/models/katello/glue/candlepin/consumer.rb
Expand Up @@ -24,20 +24,18 @@ def self.included(base) # rubocop:disable MethodLength
base.send :extend, ClassMethods

base.class_eval do
before_save :save_candlepin_orchestration

as_json_hook :consumer_as_json

attr_accessible :cp_type, :owner, :serviceLevel, :installedProducts, :facts, :guestIds, :releaseVer, :autoheal

lazy_accessor :href, :facts, :cp_type, :href, :idCert, :owner, :lastCheckin, :created, :guestIds,
:installedProducts, :autoheal, :releaseVer, :serviceLevel, :capabilities, :entitlementStatus,
:initializer => (lambda do |_s|
if uuid
consumer_json = Resources::Candlepin::Consumer.get(uuid)
convert_from_cp_fields(consumer_json)
end
end)
:installedProducts, :autoheal, :releaseVer, :serviceLevel, :capabilities, :entitlementStatus,
:initializer => (lambda do |_s|
if uuid
consumer_json = Resources::Candlepin::Consumer.get(uuid)
convert_from_cp_fields(consumer_json)
end
end)
lazy_accessor :entitlements, :initializer => lambda { |_s| Resources::Candlepin::Consumer.entitlements(uuid) }
lazy_accessor :pools, :initializer => lambda { |_s| entitlements.collect { |ent| Resources::Candlepin::Pool.find ent["pool"]["id"] } }
lazy_accessor :available_pools, :initializer => lambda { |_s| Resources::Candlepin::Consumer.available_pools(uuid, false) }
Expand Down Expand Up @@ -105,15 +103,6 @@ def load_from_cp(consumer_json)
end
end

def update_candlepin_consumer
Rails.logger.debug "Updating consumer in candlepin: #{name}"
Resources::Candlepin::Consumer.update(self.uuid, @facts, @guestIds, @installedProducts, @autoheal,
@releaseVer, self.serviceLevel, self.cp_environment_id, @capabilities, @lastCheckin)
rescue => e
Rails.logger.error "Failed to update candlepin consumer #{name}: #{e}, #{e.backtrace.join("\n")}"
raise e
end

def checkin(checkin_time)
Rails.logger.debug "Updating consumer check-in time: #{name}"
Resources::Candlepin::Consumer.checkin(self.uuid, checkin_time)
Expand All @@ -122,14 +111,6 @@ def checkin(checkin_time)
raise e
end

def refresh_subscriptions
Rails.logger.debug "Refreshing consumer subscriptions in candlepin: #{name}"
Resources::Candlepin::Consumer.refresh_entitlements(self.uuid)
rescue => e
Rails.logger.error "Failed to refresh consumer subscriptions in candlepin for #{name}: #{e}, #{e.backtrace.join("\n")}"
raise e
end

def regenerate_identity_certificates
Rails.logger.debug "Regenerating consumer identity certificates: #{name}"
Resources::Candlepin::Consumer.regenerate_identity_certificates(self.uuid)
Expand Down Expand Up @@ -216,15 +197,6 @@ def reject_db_columns(cp_json)
cp_json.reject { |k, _v| self.class.column_defaults.keys.member?(k.to_s) }
end

def save_candlepin_orchestration
case orchestration_for
when :hypervisor
# it's already saved = do nothing
when :update
pre_queue.create(:name => "update candlepin consumer: #{self.name}", :priority => 3, :action => [self, :update_candlepin_consumer])
end
end

def cp_environment_id
if self.content_view
self.content_view.cp_environment_id(self.environment)
Expand Down
10 changes: 0 additions & 10 deletions app/models/katello/glue/pulp/consumer.rb
Expand Up @@ -17,8 +17,6 @@ def self.included(base)
base.send :include, LazyAccessor

base.class_eval do
before_save :save_pulp_orchestration

lazy_accessor :pulp_facts, :initializer => lambda { |_s| Katello.pulp_server.extensions.consumer.retrieve(uuid) }
lazy_accessor :package_profile, :initializer => lambda { |_s| fetch_package_profile }
lazy_accessor :simple_packages, :initializer => (lambda do |_s|
Expand Down Expand Up @@ -209,14 +207,6 @@ def deactivate_pulp_node
Katello.pulp_server.extensions.consumer.deactivate_node(self.uuid)
end

def save_pulp_orchestration
return true if self.is_a? Hypervisor
case orchestration_for
when :update
pre_queue.create(:name => "update pulp consumer: #{self.name}", :priority => 3, :action => [self, :update_pulp_consumer])
end
end

def katello_agent_installed?
return false if self.is_a? Hypervisor
simple_packages.any? { |package| package.name == "katello-agent" }
Expand Down
22 changes: 0 additions & 22 deletions app/models/katello/system.rb
Expand Up @@ -75,8 +75,6 @@ class System < Katello::Model
before_create :fill_defaults
after_create :init_default_custom_info

before_update :update_foreman_host, :if => proc { |r| r.environment_id_changed? || r.content_view_id_changed? }

scope :in_environment, lambda { |env| where('environment_id = ?', env) unless env.nil? }
scope :completer_scope, lambda { |options| readable(options[:organization_id]) }
scope :by_uuids, lambda { |uuids| where(:uuid => uuids) }
Expand Down Expand Up @@ -356,26 +354,6 @@ def set_default_content_view
self.content_view = self.environment.try(:default_content_view) unless self.content_view
end

def update_foreman_host
# If the lifecycle environment and/or content view are being changed for the content host
# (system), then we may also need to update the associated foreman host's puppet environment
if self.foreman_host &&
self.foreman_host.environment.lifecycle_environment &&
self.foreman_host.environment.content_view

if puppet_env = self.content_view.puppet_env(self.environment).try(:puppet_environment)
if puppet_env.id != self.foreman_host.environment_id
self.foreman_host.environment_id = puppet_env.id
self.foreman_host.save!
end
else
fail Errors::NotFound,
_("Couldn't find puppet environment associated with lifecycle environment '%{env}' and content view '%{view}'") %
{ :env => self.environment.name, :view => self.content_view.name }
end
end
end

# rubocop:disable SymbolName
def collect_installed_product_names
self.installedProducts ? self.installedProducts.map { |p| p[:productName] } : []
Expand Down
14 changes: 0 additions & 14 deletions spec/models/system_spec.rb
Expand Up @@ -149,20 +149,6 @@ module Katello
@system.save!
end

it "should give facts to Resources::Candlepin::Consumer" do
@system.facts = facts
@system.installedProducts = nil # simulate it's not loaded in memory
Resources::Candlepin::Consumer.expects(:update).once.with(uuid, facts, nil, nil, nil, nil, nil, anything, nil, nil).returns(true)
@system.save!
end

it "should give installeProducts to Resources::Candlepin::Consumer" do
@system.installedProducts = installed_products
@system.facts = nil # simulate it's not loaded in memory
Resources::Candlepin::Consumer.expects(:update).once.with(uuid, nil, nil, installed_products, nil, nil, nil, anything, nil, nil).returns(true)
@system.save!
end

it "should fail if the content view is not in the enviornment" do
content_view = FactoryGirl.build_stubbed(:katello_content_view)
@system.stubs(:content_view_id).returns(content_view.id)
Expand Down
25 changes: 25 additions & 0 deletions test/actions/katello/system_test.rb
Expand Up @@ -61,6 +61,31 @@ class CreateTest < TestBase
end
end

class UpdateTest < TestBase
let(:action_class) { ::Actions::Katello::System::Update }
let(:input) { { :name => 'newname' } }

let(:system) do
env = build(:katello_k_t_environment,
:library,
organization: build(:katello_organization, :acme_corporation))
build(:katello_system, :alabama, environment: env)
end

it 'plans' do
stub_remote_user
system.expects(:disable_auto_reindex!)
action.expects(:action_subject).with(system)
system.expects(:update_attributes!).with(input)

plan_action(action, system, input)
assert_action_planed(action, ::Actions::Katello::Foreman::HostUpdate)
assert_action_planed(action, ::Actions::Pulp::Consumer::Update)
assert_action_planed(action, ::Actions::Candlepin::Consumer::Update)
assert_action_planed(action, ::Actions::ElasticSearch::Reindex)
end
end

class DestroyTest < TestBase
let(:action_class) { ::Actions::Katello::System::Destroy }

Expand Down

0 comments on commit 106bb48

Please sign in to comment.