Skip to content

Commit e6cb56b

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 e6cb56b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-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: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,33 @@ 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+
role_attrs = crowbar["crowbar"]["network"].to_hash
649+
# We don't take default attributes, as we would also include the node role
650+
# attributes as they exists on the last chef run, therefore always
651+
# overriding the possibly different node role attributes that exist now. As
652+
# a result, the attribute path may not exist and we need some care when
653+
# accessing what we look for.
654+
node_normal_crowbar = @node.normal_attrs["crowbar"]
655+
node_attrs = unless node_normal_crowbar.nil? || node_normal_crowbar["network"].nil?
656+
@node.normal_attrs["crowbar"]["network"].to_hash
657+
end
658+
role_attrs.merge(node_attrs)
659+
end
660+
637661
def networks
638662
networks = {}
639-
crowbar["crowbar"]["network"].each do |name, data|
663+
crowbar_network.each do |name, data|
640664
# note that node might not be part of network proposal yet (for instance:
641665
# if discovered, and IP got allocated by user)
642666
next if @node["network"]["networks"].nil? || !@node["network"]["networks"].key?(name)
@@ -647,11 +671,11 @@ def networks
647671

648672
def get_network_by_type(type)
649673
return nil if @role.nil?
650-
return nil unless crowbar["crowbar"]["network"].key?(type)
674+
return nil unless crowbar_network.key?(type)
651675
# note that node might not be part of network proposal yet (for instance:
652676
# if discovered, and IP got allocated by user)
653677
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)
678+
@node["network"]["networks"][type].to_hash.merge(crowbar_network[type].to_hash)
655679
end
656680

657681
def set_network_attribute(network, attribute, value)

0 commit comments

Comments
 (0)