Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crowbar_framework/app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#

require "chef/mixin/deep_merge"
require "set"
require "timeout"
require "open3"

Expand All @@ -37,6 +38,7 @@ def initialize(node)
end
# deep clone of @role.default_attributes, used when saving node
@attrs_last_saved = @role.default_attributes.deep_dup
@attributes_changed = Set.new
@node = node
end

Expand Down Expand Up @@ -67,7 +69,10 @@ def availability_zone
def availability_zone=(value)
@node["crowbar_wall"] ||= {}
@node["crowbar_wall"]["openstack"] ||= {}
@node["crowbar_wall"]["openstack"]["availability_zone"] = value
if @node["crowbar_wall"]["openstack"]["availability_zone"] != value
@node["crowbar_wall"]["openstack"]["availability_zone"] = value
@attributes_changed.add("availability_zone")
end
end

def intended_role
Expand Down Expand Up @@ -257,6 +262,7 @@ def validate_public_name(value, unique_check = true)
def update_public_name(value)
unless value.nil?
crowbar["crowbar"]["public_name"] = value
@attributes_changed.add("public_name")
end
end

Expand Down Expand Up @@ -619,6 +625,20 @@ def _remove_elements_from_node(old, new, from_node)
@attrs_last_saved = @role.default_attributes.deep_dup

Rails.logger.debug("Done saving node: #{@node.name} - #{crowbar_revision}")

unless @attributes_changed.empty?
Rails.logger.debug("Notifying about saved node: #{@node.name}")

details = {
node: @node.name,
attributes: @attributes_changed
}
Crowbar::EventDispatcher.trigger_hooks(:node_changed, details)

@attributes_changed = Set.new

Rails.logger.debug("Done notifying about saved node: #{@node.name}")
end
end

def destroy
Expand Down
7 changes: 7 additions & 0 deletions crowbar_framework/app/models/service_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,13 @@ def apply_role(role, inst, in_queue, bootstrap = false)
proposal.save unless roles_to_remove.empty?

update_proposal_status(inst, "success", "")

details = {
barclamp: @bc_name,
instance: inst
}
Crowbar::EventDispatcher.trigger_hooks(:proposal_applied, details)

[200, {}]
rescue StandardError => e
@logger.fatal("apply_role: Uncaught exception #{e.message} #{e.backtrace.join("\n")}")
Expand Down
48 changes: 48 additions & 0 deletions crowbar_framework/lib/crowbar/event_dispatcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# Copyright 2017, SUSE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module Crowbar
class EventDispatcher
class << self
def trigger_hooks(event, details)
BarclampCatalog.barclamps.keys.each do |barclamp|
begin
cls = ServiceObject.get_service(barclamp)
rescue NameError
# catalog may contain barclamps which don't have services
next
end

next unless cls.method_defined?(:event_hook)

service = cls.new(Rails.logger)

proposals = Proposal.where(barclamp: barclamp)
proposals.each do |proposal|
role = proposal.role
next if role.nil?
begin
service.event_hook(role, event, details)
rescue StandardError => e
raise "Error while executing event hook of #{barclamp} for #{event}: #{e.message}"
end
end
end
end
handle_asynchronously :trigger_hooks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a blank line before this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, maybe not. It's kind of like a decorator.

end
end
end