foreman_default_hostgroup 5.0.0 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -12
- data/app/models/setting/default_hostgroup.rb +2 -0
- data/lib/default_hostgroup_base_host_patch.rb +22 -19
- data/lib/foreman_default_hostgroup/engine.rb +10 -10
- data/lib/foreman_default_hostgroup/version.rb +1 -1
- data/lib/tasks/foreman_default_hostgroup.rake +2 -2
- data/test/unit/default_hostgroup_test.rb +23 -23
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbf9d232426c55fc207fed30c696a03c1268f02df5d481daf7bbaec627867a8b
|
4
|
+
data.tar.gz: b4e30338ca588f25c2ef0f74d383aa432357c5f94f9fae2603445107c55ae382
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 965c2c192b549c92c474af2d6074a837e8f2ef9fe1241394f083ac519730aaab34732e3ccc99897c8521e97195dc81ab6cc5517f96f5aaa5b4dff4b326ce3209
|
7
|
+
data.tar.gz: 59434c38a0e0a3da2d63160ccebf1d06b08b6b3907df95455ba8229821d1302c7cad7471adae8e44e72c88c98a8265ab4efc88e27f599493b3ae761b40fbdd93
|
data/README.md
CHANGED
@@ -10,15 +10,16 @@ See Foreman's [plugin installation documentation](https://theforeman.org/plugins
|
|
10
10
|
## Compatibility
|
11
11
|
|
12
12
|
| Foreman Version | Plugin Version |
|
13
|
-
| --------------- |
|
14
|
-
| <= 1.2 |
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
| >= 1.12 |
|
20
|
-
| >= 1.16 |
|
21
|
-
| >= 1.16 |
|
13
|
+
| --------------- | -------------: |
|
14
|
+
| <= 1.2 | 0.1.0 |
|
15
|
+
| 1.3 | 1.0.1 |
|
16
|
+
| 1.4 | 1.1.0 |
|
17
|
+
| 1.5 | 2.0.1 |
|
18
|
+
| 1.6 - 1.11 | 3.0.0 |
|
19
|
+
| >= 1.12 | 4.0.0 |
|
20
|
+
| >= 1.16 | 4.0.1 |
|
21
|
+
| >= 1.16 | 5.0.0 |
|
22
|
+
| >= 2.2.0 | 6.0.0 |
|
22
23
|
|
23
24
|
## Usage
|
24
25
|
|
@@ -35,6 +36,12 @@ your needs. The format is shown in the example. The simplest form would be:
|
|
35
36
|
"Default":
|
36
37
|
"hostname": ".*"
|
37
38
|
```
|
39
|
+
|
40
|
+
`Default` is the host group name (more precisely title) that will be assigned if all its the rules matches.
|
41
|
+
Under the host group name, there's a list of rules. If all of them (in this example just one) is matching,
|
42
|
+
the host is assigned to the `Default` host group. The `hostname` is the name of the fact while the value `.*`
|
43
|
+
is used as a regular expression. This rule means host with any `hostname` is added to the `Default` host group.
|
44
|
+
|
38
45
|
If you are ugrading from plugin version 2.0.1 or older the format of this
|
39
46
|
file changes and you will need modify `default_hostgroup.yaml.example` to
|
40
47
|
follow the format above.
|
@@ -44,9 +51,9 @@ follow the format above.
|
|
44
51
|
|
45
52
|
There are also two more settings under `Settings -> DefaultHostgroup`
|
46
53
|
|
47
|
-
| Setting
|
48
|
-
|
|
49
|
-
| `force_hostgroup_match`
|
54
|
+
| Setting | Description |
|
55
|
+
| -------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
56
|
+
| `force_hostgroup_match` | Setting this to `true` will perform matching even on hosts that already have a hostgroup set. Enabling this needs `force_hostgroup_match_only_new` to be `false`. Default: `false` |
|
50
57
|
| `force_hostgroup_match_only_new` | Setting this to `true` will only perform matching when a host uploads its facts for the first time, i.e. after provisioning or when adding an existing puppetmaster and thus its nodes into foreman. Default: `true` |
|
51
58
|
|
52
59
|
## TODO
|
@@ -2,13 +2,18 @@ module DefaultHostgroupBaseHostPatch
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
module ManagedOverrides
|
5
|
-
|
5
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
6
|
+
def import_facts(facts, source_proxy = nil, without_alias = false)
|
7
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
6
8
|
super(facts, source_proxy)
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
12
|
module Overrides
|
13
|
+
# rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
|
11
14
|
def import_facts(facts, source_proxy = nil, without_alias = false)
|
15
|
+
# rubocop:enable Metrics/AbcSize,Metrics/CyclomaticComplexity
|
16
|
+
|
12
17
|
# Load the facts anyway, hook onto the end of it
|
13
18
|
result = super(facts, source_proxy)
|
14
19
|
|
@@ -19,7 +24,7 @@ module DefaultHostgroupBaseHostPatch
|
|
19
24
|
# Check settings are created
|
20
25
|
return result unless settings_exist?
|
21
26
|
|
22
|
-
Rails.logger.debug
|
27
|
+
Rails.logger.debug "DefaultHostgroupMatch: performing Hostgroup match"
|
23
28
|
|
24
29
|
return result unless host_new_or_forced?
|
25
30
|
return result unless host_has_no_hostgroup_or_forced?
|
@@ -29,12 +34,10 @@ module DefaultHostgroupBaseHostPatch
|
|
29
34
|
|
30
35
|
return result unless new_hostgroup
|
31
36
|
|
32
|
-
self.hostgroup = new_hostgroup
|
33
|
-
if Setting[:force_host_environment] == true
|
34
|
-
|
35
|
-
|
36
|
-
save(validate: false)
|
37
|
-
Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"
|
37
|
+
self.host.hostgroup = new_hostgroup
|
38
|
+
self.host.environment = new_hostgroup.environment if Setting[:force_host_environment] == true
|
39
|
+
self.host.save(validate: false)
|
40
|
+
Rails.logger.info "DefaultHostgroupMatch: #{facts["hostname"]} added to #{new_hostgroup}"
|
38
41
|
|
39
42
|
result
|
40
43
|
end
|
@@ -46,27 +49,27 @@ module DefaultHostgroupBaseHostPatch
|
|
46
49
|
|
47
50
|
def find_match(facts_map)
|
48
51
|
facts_map.each do |group_name, facts|
|
49
|
-
hg = Hostgroup.
|
52
|
+
hg = Hostgroup.find_by(title: group_name)
|
50
53
|
return hg if hg.present? && group_matches?(facts)
|
51
54
|
end
|
52
|
-
Rails.logger.info
|
55
|
+
Rails.logger.info "No match ..."
|
53
56
|
false
|
54
57
|
end
|
55
58
|
|
56
59
|
def group_matches?(facts)
|
57
60
|
facts.each do |fact_name, fact_regex|
|
58
|
-
fact_regex.gsub!(%r{(\A/|/\z)},
|
59
|
-
host_fact_value =
|
61
|
+
fact_regex.gsub!(%r{(\A/|/\z)}, "")
|
62
|
+
host_fact_value = self.host.facts[fact_name]
|
60
63
|
Rails.logger.info "Fact = #{fact_name}"
|
61
64
|
Rails.logger.info "Regex = #{fact_regex}"
|
62
|
-
return true if Regexp.new(fact_regex).match(host_fact_value)
|
65
|
+
return true if Regexp.new(fact_regex).match?(host_fact_value)
|
63
66
|
end
|
64
67
|
false
|
65
68
|
end
|
66
69
|
|
67
70
|
def settings_exist?
|
68
71
|
unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
|
69
|
-
Rails.logger.warn
|
72
|
+
Rails.logger.warn "DefaultHostgroupMatch: Could not load :default_hostgroup map from Settings."
|
70
73
|
return false
|
71
74
|
end
|
72
75
|
true
|
@@ -75,9 +78,9 @@ module DefaultHostgroupBaseHostPatch
|
|
75
78
|
def host_new_or_forced?
|
76
79
|
if Setting[:force_hostgroup_match_only_new]
|
77
80
|
# hosts have already been saved during import_host, so test the creation age instead
|
78
|
-
new_host = ((Time.current - created_at) < 300)
|
79
|
-
unless new_host && hostgroup.nil? && reports.empty?
|
80
|
-
Rails.logger.debug
|
81
|
+
new_host = ((Time.current - self.host.created_at) < 300)
|
82
|
+
unless new_host && self.host.hostgroup.nil? && self.host.reports.empty?
|
83
|
+
Rails.logger.debug "DefaultHostgroupMatch: skipping, host exists"
|
81
84
|
return false
|
82
85
|
end
|
83
86
|
end
|
@@ -86,8 +89,8 @@ module DefaultHostgroupBaseHostPatch
|
|
86
89
|
|
87
90
|
def host_has_no_hostgroup_or_forced?
|
88
91
|
unless Setting[:force_hostgroup_match]
|
89
|
-
if hostgroup.present?
|
90
|
-
Rails.logger.debug
|
92
|
+
if self.host.hostgroup.present?
|
93
|
+
Rails.logger.debug "DefaultHostgroupMatch: skipping, host has hostgroup"
|
91
94
|
return false
|
92
95
|
end
|
93
96
|
end
|
@@ -1,32 +1,32 @@
|
|
1
|
-
require
|
1
|
+
require "default_hostgroup_base_host_patch"
|
2
2
|
|
3
3
|
module ForemanDefaultHostgroup
|
4
4
|
class Engine < ::Rails::Engine
|
5
|
-
engine_name
|
5
|
+
engine_name "foreman_default_hostgroup"
|
6
6
|
|
7
7
|
config.autoload_paths += Dir["#{config.root}/app/models"]
|
8
8
|
|
9
|
-
initializer
|
9
|
+
initializer "foreman_default_hostgroup.load_default_settings",
|
10
10
|
before: :load_config_initializers do
|
11
11
|
require_dependency File.expand_path(
|
12
|
-
|
12
|
+
"../../app/models/setting/default_hostgroup.rb", __dir__
|
13
|
+
)
|
13
14
|
end
|
14
15
|
|
15
|
-
initializer
|
16
|
+
initializer "foreman_default_hostgroup.register_plugin",
|
16
17
|
before: :finisher_hook do
|
17
18
|
Foreman::Plugin.register :foreman_default_hostgroup do
|
18
|
-
requires_foreman
|
19
|
+
requires_foreman ">= 2.2"
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
23
|
config.to_prepare do
|
23
24
|
begin
|
24
|
-
::
|
25
|
-
::
|
26
|
-
rescue => e
|
25
|
+
::HostFactImporter.include DefaultHostgroupBaseHostPatch
|
26
|
+
::HostFactImporter.prepend DefaultHostgroupBaseHostPatch::ManagedOverrides
|
27
|
+
rescue StandardError => e
|
27
28
|
Rails.logger.warn "ForemanDefaultHostgroup: skipping engine hook (#{e})"
|
28
29
|
end
|
29
30
|
end
|
30
|
-
|
31
31
|
end
|
32
32
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rake/testtask'
|
2
2
|
# Tests
|
3
3
|
namespace :test do
|
4
|
-
desc
|
4
|
+
desc 'Test DefaultHostgroup plugin'
|
5
5
|
Rake::TestTask.new(:foreman_default_hostgroup) do |t|
|
6
6
|
test_dir = File.join(File.dirname(__FILE__), '../..', 'test')
|
7
7
|
t.libs << ['test', test_dir]
|
@@ -20,7 +20,7 @@ namespace :foreman_default_hostgroup do
|
|
20
20
|
"#{ForemanDefaultHostgroup::Engine.root}/lib/**/*.rb",
|
21
21
|
"#{ForemanDefaultHostgroup::Engine.root}/test/**/*.rb"]
|
22
22
|
end
|
23
|
-
rescue
|
23
|
+
rescue StandardError
|
24
24
|
puts 'Rubocop not loaded.'
|
25
25
|
end
|
26
26
|
|
@@ -40,15 +40,15 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
40
40
|
|
41
41
|
def setup_host_and_facts
|
42
42
|
raw = JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + '/facts.json')))
|
43
|
-
@name
|
44
|
-
@host
|
43
|
+
@name = raw['name']
|
44
|
+
@host = Host.import_host(raw['name'], 'puppet')
|
45
45
|
@facts = raw['facts']
|
46
46
|
end
|
47
47
|
|
48
48
|
context 'import_facts_with_match_hostgroup' do
|
49
49
|
test 'matched host is saved with new hostgroup' do
|
50
50
|
assert @host.import_facts(@facts)
|
51
|
-
assert_equal Hostgroup.
|
51
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), Host.find_by(name: @name).hostgroup
|
52
52
|
end
|
53
53
|
|
54
54
|
test 'matched host not updated if host already has a hostgroup' do
|
@@ -57,7 +57,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
57
57
|
@host.save(validate: false)
|
58
58
|
|
59
59
|
assert @host.import_facts(@facts)
|
60
|
-
assert_equal hostgroup, Host.
|
60
|
+
assert_equal hostgroup, Host.find_by(name: @name).hostgroup
|
61
61
|
end
|
62
62
|
|
63
63
|
test 'hostgroup is not updated if host is not new' do
|
@@ -65,7 +65,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
65
65
|
@host.save(validate: false)
|
66
66
|
|
67
67
|
assert @host.import_facts(@facts)
|
68
|
-
|
68
|
+
assert_not Host.find_by(name: @name).hostgroup
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -73,14 +73,14 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
73
73
|
test 'environment is updated if enabled' do
|
74
74
|
h = FactoryBot.create(:host, :with_environment, created_at: Time.current)
|
75
75
|
h.import_facts(@facts)
|
76
|
-
assert_equal Hostgroup.
|
76
|
+
assert_equal Hostgroup.find_by(name: 'Test Default').environment, h.environment
|
77
77
|
end
|
78
78
|
|
79
79
|
test 'environment not updated if disabled' do
|
80
80
|
Setting[:force_host_environment] = false
|
81
81
|
h = FactoryBot.create(:host, :with_environment, created_at: Time.current)
|
82
82
|
h.import_facts(@facts)
|
83
|
-
|
83
|
+
assert_not_equal Hostgroup.find_by(name: 'Test Default').environment, h.environment
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -89,33 +89,33 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
89
89
|
test 'match a single hostgroup' do
|
90
90
|
facts_map = SETTINGS[:default_hostgroup][:facts_map]
|
91
91
|
assert @host.import_facts(@facts, nil, true)
|
92
|
-
assert_equal Hostgroup.
|
92
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), @host.find_match(facts_map)
|
93
93
|
end
|
94
94
|
|
95
95
|
test 'returns false for no match' do
|
96
96
|
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
97
|
-
'Test Default'
|
97
|
+
'Test Default' => { 'hostname' => 'nosuchhost' }
|
98
98
|
}
|
99
99
|
assert @host.import_facts(@facts, nil, true)
|
100
|
-
|
100
|
+
assert_not @host.find_match(facts_map)
|
101
101
|
end
|
102
102
|
|
103
103
|
test 'matches first available hostgroup' do
|
104
104
|
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
105
|
-
'Test Default'
|
105
|
+
'Test Default' => { 'hostname' => '.*' },
|
106
106
|
'Some Other Group' => { 'hostname' => '/\.lan$/' }
|
107
107
|
}
|
108
108
|
assert @host.import_facts(@facts, nil, true)
|
109
|
-
assert_equal Hostgroup.
|
109
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), @host.find_match(facts_map)
|
110
110
|
end
|
111
111
|
|
112
112
|
test 'nonexistant groups are ignored' do
|
113
113
|
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
114
114
|
'Some Other Group' => { 'hostname' => '.*' },
|
115
|
-
'Test Default'
|
115
|
+
'Test Default' => { 'hostname' => '/\.lan$/' }
|
116
116
|
}
|
117
117
|
assert @host.import_facts(@facts, nil, true)
|
118
|
-
assert_equal Hostgroup.
|
118
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), @host.find_match(facts_map)
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
@@ -142,20 +142,20 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
142
142
|
test 'invalid keys are ignored' do
|
143
143
|
regex = { 'nosuchfact' => '.*' }
|
144
144
|
assert @host.import_facts(@facts, nil, true)
|
145
|
-
|
145
|
+
assert_not @host.group_matches?(regex)
|
146
146
|
end
|
147
147
|
|
148
148
|
test 'unmatched values are ignored' do
|
149
149
|
regex = { 'hostname' => 'nosuchname' }
|
150
150
|
assert @host.import_facts(@facts, nil, true)
|
151
|
-
|
151
|
+
assert_not @host.group_matches?(regex)
|
152
152
|
end
|
153
153
|
|
154
154
|
test 'multiple entries with invalid keys / values match' do
|
155
155
|
regex = {
|
156
156
|
'nosuchfact' => '.*',
|
157
|
-
'osfamily'
|
158
|
-
'hostname'
|
157
|
+
'osfamily' => 'nosuchos',
|
158
|
+
'hostname' => '.lan$'
|
159
159
|
}
|
160
160
|
assert @host.import_facts(@facts, nil, true)
|
161
161
|
assert @host.group_matches?(regex)
|
@@ -171,7 +171,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
171
171
|
test 'false when Settings are missing' do
|
172
172
|
SETTINGS[:default_hostgroup] = {}
|
173
173
|
h = FactoryBot.create(:host)
|
174
|
-
|
174
|
+
assert_not h.settings_exist?
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
@@ -183,17 +183,17 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
183
183
|
|
184
184
|
test 'false when host has existed for > 300s' do
|
185
185
|
h = FactoryBot.create(:host, created_at: Time.current - 1000)
|
186
|
-
|
186
|
+
assert_not h.host_new_or_forced?
|
187
187
|
end
|
188
188
|
|
189
189
|
test 'false when host has a hostgroup' do
|
190
190
|
h = FactoryBot.create(:host, :with_hostgroup, created_at: Time.current)
|
191
|
-
|
191
|
+
assert_not h.host_new_or_forced?
|
192
192
|
end
|
193
193
|
|
194
194
|
test 'false when host has reports' do
|
195
195
|
h = FactoryBot.create(:host, :with_reports, created_at: Time.current)
|
196
|
-
|
196
|
+
assert_not h.host_new_or_forced?
|
197
197
|
end
|
198
198
|
|
199
199
|
test 'true when setting is forced' do
|
@@ -211,7 +211,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
211
211
|
|
212
212
|
test 'false if host has hostgroup' do
|
213
213
|
h = FactoryBot.create(:host, :with_hostgroup)
|
214
|
-
|
214
|
+
assert_not h.host_has_no_hostgroup_or_forced?
|
215
215
|
end
|
216
216
|
|
217
217
|
test 'true if host has hostgroup and setting forced' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreman_default_hostgroup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 6.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Sutcliffe
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Adds the option to specify a default hostgroup for new hosts created
|
14
14
|
from facts/reports
|
@@ -31,11 +31,11 @@ files:
|
|
31
31
|
- test/test_plugin_helper.rb
|
32
32
|
- test/unit/default_hostgroup_test.rb
|
33
33
|
- test/unit/facts.json
|
34
|
-
homepage:
|
34
|
+
homepage: https://github.com/theforeman/foreman_default_hostgroup
|
35
35
|
licenses:
|
36
|
-
- GPL-3
|
36
|
+
- GPL-3.0
|
37
37
|
metadata: {}
|
38
|
-
post_install_message:
|
38
|
+
post_install_message:
|
39
39
|
rdoc_options: []
|
40
40
|
require_paths:
|
41
41
|
- lib
|
@@ -50,12 +50,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
|
-
|
54
|
-
|
55
|
-
signing_key:
|
53
|
+
rubygems_version: 3.0.3
|
54
|
+
signing_key:
|
56
55
|
specification_version: 4
|
57
56
|
summary: Default Hostgroup Plugin for Foreman
|
58
57
|
test_files:
|
59
58
|
- test/test_plugin_helper.rb
|
60
|
-
- test/unit/default_hostgroup_test.rb
|
61
59
|
- test/unit/facts.json
|
60
|
+
- test/unit/default_hostgroup_test.rb
|