Class: NodeattrUtils::Config

Inherits:
Object
  • Object
show all
Includes:
FlightConfig::Updater
Defined in:
lib/nodeattr_utils/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.path(_cluster) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/nodeattr_utils/config.rb', line 37

def self.path(_cluster)
  raise NotImplementedError
end

Instance Method Details

#__data__Object



41
42
43
# File 'lib/nodeattr_utils/config.rb', line 41

def __data__
  super().tap { |d| d.set_if_empty(:groups, value: ['orphan']) }
end

#add_group(group_name) ⇒ Object



86
87
88
89
# File 'lib/nodeattr_utils/config.rb', line 86

def add_group(group_name)
  return if raw_groups.include?(group_name)
  __data__.append(group_name, to: :groups)
end

#add_nodes(node_string, groups: []) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/nodeattr_utils/config.rb', line 99

def add_nodes(node_string, groups: [])
  groups = groups.is_a?(Array) ? groups : [groups]
  add_group(groups.first) unless groups.empty?
  NodeattrUtils::Nodes.expand(node_string).each do |node|
    __data__.set(:nodes, node, value: groups)
  end
end

#clusterObject



45
46
47
# File 'lib/nodeattr_utils/config.rb', line 45

def cluster
  __inputs__.first
end

#group_index(group) ⇒ Object



67
68
69
70
# File 'lib/nodeattr_utils/config.rb', line 67

def group_index(group)
  return nil if group.nil?
  raw_groups.find_index(group)
end

#groups_for_node(node) ⇒ Object



72
73
74
75
76
# File 'lib/nodeattr_utils/config.rb', line 72

def groups_for_node(node)
  __data__.fetch(:nodes, node, default: []).dup.tap do |groups|
    groups.push 'orphan' if groups.empty?
  end
end

#groups_hashObject



61
62
63
64
65
# File 'lib/nodeattr_utils/config.rb', line 61

def groups_hash
  raw_groups.reject(&:nil?).map do |group|
    [group, group_index(group)]
  end.to_h
end

#nodes_in_group(group) ⇒ Object



78
79
80
# File 'lib/nodeattr_utils/config.rb', line 78

def nodes_in_group(group)
  nodes_list.select { |node| groups_for_node(node).include?(group) }
end

#nodes_in_primary_group(group) ⇒ Object



82
83
84
# File 'lib/nodeattr_utils/config.rb', line 82

def nodes_in_primary_group(group)
  nodes_list.select { |n| groups_for_node(n).first == group }
end

#nodes_listObject



57
58
59
# File 'lib/nodeattr_utils/config.rb', line 57

def nodes_list
  raw_nodes.keys
end

#orphansObject



113
114
115
# File 'lib/nodeattr_utils/config.rb', line 113

def orphans
  nodes_in_group('orphan')
end

#raw_groupsObject



49
50
51
# File 'lib/nodeattr_utils/config.rb', line 49

def raw_groups
  __data__.fetch(:groups)
end

#raw_nodesObject



53
54
55
# File 'lib/nodeattr_utils/config.rb', line 53

def raw_nodes
  __data__.fetch(:nodes, default: {})
end

#remove_group(group_name) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/nodeattr_utils/config.rb', line 91

def remove_group(group_name)
  error_if_removing_orphan_group(group_name)
  nodes_list.select { |n| groups_for_node(n).first == group_name }
            .join(',')
            .tap { |node_str| remove_nodes(node_str) }
  __data__.fetch(:groups).map! { |g| g == group_name ? nil : g }
end

#remove_nodes(node_string) ⇒ Object



107
108
109
110
111
# File 'lib/nodeattr_utils/config.rb', line 107

def remove_nodes(node_string)
  NodeattrUtils::Nodes.expand(node_string).map do |node|
    __data__.delete(:nodes, node)
  end
end