Skip to content

Commit d5bb352

Browse files
committed
crowbar: Fix Node.get_network_by_type for overridden attributes
We were looking for attributes overriding the network proposal only on the node role, but this is not consistent with what the cookbook does (it looks at attributes on the node itself), and customer using this feature are more likely to edit the node directly. So reflect that by looking at the node attributes also. This is slightly tricky, as the merge of node attributes and node role attributes occur on a chef run, and in the rails app, this can be out of sync. So we do our own merge to always see the real values.
1 parent d2458a5 commit d5bb352

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

crowbar_framework/app/models/network_service.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ def enable_interface(bc_instance, network, name)
434434

435435
def build_net_info(role, network, node)
436436
net_info = role.default_attributes["network"]["networks"][network].to_hash
437-
unless node.nil? || node.crowbar.nil?
438-
net_info.merge!(node["crowbar"]["network"][network] || {})
437+
unless node.nil?
438+
net_info.merge!(node.crowbar_network[network] || {})
439439
end
440440
net_info
441441
end

crowbar_framework/app/models/node.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,38 @@ def destroy
634634
Rails.logger.debug("Done with removal of node: #{@node.name} - #{crowbar_revision}")
635635
end
636636

637+
def crowbar_network
638+
# This is slightly annoying: we store data in the node role, but since we
639+
# allow overriding the network data on a per-node basis, users may also put
640+
# some data in the node directly.
641+
# Within a chef cookbook, it's fine because it's all merged and accessible
642+
# directly through the attribute, but we can't rely on this here as the
643+
# merge of the node attributes and the node role attributes only occurs on
644+
# a chef run. That means that if the rails app is changing the node role
645+
# attribute and then trying to read the data before a chef run, it cannot
646+
# rely solely on the the node attributes.
647+
# Therefore we manually merge here the two sets of attributes...
648+
649+
role_attrs = crowbar["crowbar"]["network"].to_hash
650+
651+
# We don't take default attributes, as we would also include the node role
652+
# attributes as they exists on the last chef run, therefore always
653+
# overriding the possibly different node role attributes that exist now. As
654+
# a result, the attribute path may not exist and we need some care when
655+
# accessing what we look for.
656+
node_normal_crowbar = @node.normal_attrs["crowbar"]
657+
node_attrs = if node_normal_crowbar.nil? || node_normal_crowbar["network"].nil?
658+
{}
659+
else
660+
@node.normal_attrs["crowbar"]["network"].to_hash
661+
end
662+
663+
role_attrs.merge(node_attrs)
664+
end
665+
637666
def networks
638667
networks = {}
639-
crowbar["crowbar"]["network"].each do |name, data|
668+
crowbar_network.each do |name, data|
640669
# note that node might not be part of network proposal yet (for instance:
641670
# if discovered, and IP got allocated by user)
642671
next if @node["network"]["networks"].nil? || !@node["network"]["networks"].key?(name)
@@ -647,11 +676,11 @@ def networks
647676

648677
def get_network_by_type(type)
649678
return nil if @role.nil?
650-
return nil unless crowbar["crowbar"]["network"].key?(type)
679+
return nil unless crowbar_network.key?(type)
651680
# note that node might not be part of network proposal yet (for instance:
652681
# if discovered, and IP got allocated by user)
653682
return nil if @node["network"]["networks"].nil? || !@node["network"]["networks"].key?(type)
654-
@node["network"]["networks"][type].to_hash.merge(crowbar["crowbar"]["network"][type].to_hash)
683+
@node["network"]["networks"][type].to_hash.merge(crowbar_network[type].to_hash)
655684
end
656685

657686
def set_network_attribute(network, attribute, value)

0 commit comments

Comments
 (0)