nodeattr_utils 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c39fef993b43ef064b092d281015a4688b5cacc6c2a9b8c843ad0b4ccca54b2c
4
- data.tar.gz: 71130f30dad3a8d60535201f52126c6e87270bb78f23576da99aaf3ba17f4c14
3
+ metadata.gz: 167cc1e6ee18b4dba9038c19fe44b526834ddbe4a60f4dfc1ce97787c0181e73
4
+ data.tar.gz: 63d2c6ed0f8d386243733b951b9676785806ca6600f0e7c1b1575e97713c764c
5
5
  SHA512:
6
- metadata.gz: ba494a31a4823798431e3609552e949893e781803ce1e0f59d242ec5e37bb0bdd6d15ffc946441118f89b4212ea53578fd6022de97f0b6478ee1ffe2eda16758
7
- data.tar.gz: bf350c8ac1f1b3cba1fa93980d3b0dbf32f56196de7cfff426e992985d93afe158ffcb566e1d617eb7faa23c8b102b9dfb4e25a16a38c821618306642ae79543
6
+ metadata.gz: c0b8f32802e049dd7683337ef9b57f4c46a2880edc6ba1e3191e4026d161c280a2cb15f4dd378acf2380dc1d62a758a4e0d5e2deef2346573ab8f89449d60e3a
7
+ data.tar.gz: 43b9ede227685ede6e33c958d479515fafad34025f8e7fcf52c8e096c6a29c6821531983c0f0a0a34b000a47f5286944efda250afdf6e73872b860aed707d87f
@@ -29,5 +29,16 @@ require 'nodeattr_utils/node_parser'
29
29
  require 'nodeattr_utils/exceptions'
30
30
 
31
31
  module NodeattrUtils
32
+ module Nodes
33
+ def self.expand(string)
34
+ NodeattrUtils::NodeParser.expand(string)
35
+ end
36
+
37
+ # TODO: Actually collapse the nodes array instead of joining them as a single
38
+ # string
39
+ def self.collapse(*nodes)
40
+ nodes.flatten.join(',')
41
+ end
42
+ end
32
43
  end
33
44
 
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ #==============================================================================
4
+ # Copyright (C) 2019-present Alces Flight Ltd.
5
+ #
6
+ # This file is part of flight-metal.
7
+ #
8
+ # This program and the accompanying materials are made available under
9
+ # the terms of the Eclipse Public License 2.0 which is available at
10
+ # <https://www.eclipse.org/legal/epl-2.0>, or alternative license
11
+ # terms made available by Alces Flight Ltd - please direct inquiries
12
+ # about licensing to licensing@alces-flight.com.
13
+ #
14
+ # This project is distributed in the hope that it will be useful, but
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
16
+ # IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
17
+ # OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A
18
+ # PARTICULAR PURPOSE. See the Eclipse Public License 2.0 for more
19
+ # details.
20
+ #
21
+ # You should have received a copy of the Eclipse Public License 2.0
22
+ # along with this project. If not, see:
23
+ #
24
+ # https://opensource.org/licenses/EPL-2.0
25
+ #
26
+ # For more information on flight-account, please visit:
27
+ # https://github.com/alces-software/flight-metal
28
+ #===============================================================================
29
+
30
+ require 'flight_config'
31
+ require 'nodeattr_utils/exceptions'
32
+
33
+ module NodeattrUtils
34
+ class Config
35
+ include FlightConfig::Updater
36
+
37
+ def self.path(_cluster)
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def __data__
42
+ super().tap { |d| d.set_if_empty(:groups, value: ['orphan']) }
43
+ end
44
+
45
+ def cluster
46
+ __inputs__.first
47
+ end
48
+
49
+ def raw_groups
50
+ __data__.fetch(:groups)
51
+ end
52
+
53
+ def raw_nodes
54
+ __data__.fetch(:nodes, default: {})
55
+ end
56
+
57
+ def nodes_list
58
+ raw_nodes.keys
59
+ end
60
+
61
+ def groups_hash
62
+ raw_groups.reject(&:nil?).map do |group|
63
+ [group, group_index(group)]
64
+ end.to_h
65
+ end
66
+
67
+ def group_index(group)
68
+ return nil if group.nil?
69
+ raw_groups.find_index(group)
70
+ end
71
+
72
+ def groups_for_node(node)
73
+ __data__.fetch(:nodes, node, default: []).dup.tap do |groups|
74
+ groups.push 'orphan' if groups.empty?
75
+ end
76
+ end
77
+
78
+ def nodes_in_group(group)
79
+ nodes_list.select { |node| groups_for_node(node).include?(group) }
80
+ end
81
+
82
+ def nodes_in_primary_group(group)
83
+ nodes_list.select { |n| groups_for_node(n).first == group }
84
+ end
85
+
86
+ def add_group(group_name)
87
+ return if raw_groups.include?(group_name)
88
+ __data__.append(group_name, to: :groups)
89
+ end
90
+
91
+ def remove_group(group_name)
92
+ error_if_removing_orphan_group(group_name)
93
+ nodes_list.select { |n| groups_for_node(n).first == group_name }
94
+ .join(',')
95
+ .tap { |node_str| remove_nodes(node_str) }
96
+ __data__.fetch(:groups).map! { |g| g == group_name ? nil : g }
97
+ end
98
+
99
+ def add_nodes(node_string, groups: [])
100
+ groups = groups.is_a?(Array) ? groups : [groups]
101
+ add_group(groups.first) unless groups.empty?
102
+ NodeattrUtils::Nodes.expand(node_string).each do |node|
103
+ __data__.set(:nodes, node, value: groups)
104
+ end
105
+ end
106
+
107
+ def remove_nodes(node_string)
108
+ NodeattrUtils::Nodes.expand(node_string).map do |node|
109
+ __data__.delete(:nodes, node)
110
+ end
111
+ end
112
+
113
+ def orphans
114
+ nodes_in_group('orphan')
115
+ end
116
+
117
+ private
118
+
119
+ def error_if_removing_orphan_group(group)
120
+ return unless group == 'orphan'
121
+ raise RemovingOrphanGroupError, <<~ERROR.chomp
122
+ Can not remove the orphan group
123
+ ERROR
124
+ end
125
+ end
126
+ end
127
+
@@ -30,5 +30,7 @@ module NodeattrUtils
30
30
 
31
31
  class NodeSyntaxError < NodeattrUtilsError
32
32
  end
33
+
34
+ class RemovingOrphanGroupError < NodeattrUtilsError; end
33
35
  end
34
36
 
@@ -25,5 +25,5 @@
25
25
  # https://github.com/openflighthpc/nodeattr_utils
26
26
  #==============================================================================m
27
27
  module NodeattrUtils
28
- VERSION = "1.0.0"
28
+ VERSION = "1.1.0"
29
29
  end
@@ -46,6 +46,8 @@ Gem::Specification.new do |spec|
46
46
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
47
47
  spec.require_paths = ["lib"]
48
48
 
49
+ spec.add_runtime_dependency 'flight_config', '~>0.2'
50
+
49
51
  spec.add_development_dependency "bundler", "~> 1.16"
50
52
  spec.add_development_dependency "rake", "~> 10.0"
51
53
  spec.add_development_dependency "rspec", "~> 3.0"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nodeattr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alces Flight Ltd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-04 00:00:00.000000000 Z
11
+ date: 2019-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flight_config
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +111,7 @@ files:
97
111
  - README.md
98
112
  - Rakefile
99
113
  - lib/nodeattr_utils.rb
114
+ - lib/nodeattr_utils/config.rb
100
115
  - lib/nodeattr_utils/exceptions.rb
101
116
  - lib/nodeattr_utils/node_parser.rb
102
117
  - lib/nodeattr_utils/version.rb
@@ -120,8 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
135
  - !ruby/object:Gem::Version
121
136
  version: '0'
122
137
  requirements: []
123
- rubyforge_project:
124
- rubygems_version: 2.7.6
138
+ rubygems_version: 3.0.3
125
139
  signing_key:
126
140
  specification_version: 4
127
141
  summary: Ruby implementation of nodeattr behaviour