foreman_default_hostgroup 7.0.0 → 7.1.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/db/migrate/20221103122801_fix_default_hostgroup_settings_category_to_dsl.rb +1 -1
- data/lib/default_hostgroup_base_host_patch.rb +15 -17
- data/lib/foreman_default_hostgroup/engine.rb +6 -10
- data/lib/foreman_default_hostgroup/version.rb +1 -1
- data/test/unit/default_hostgroup_test.rb +37 -40
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08730dfb1749cc946d09ae2617082c8b5b0d3d9adbe2d1d4596139b9f02324b7'
|
4
|
+
data.tar.gz: 721b72f3b18205aefca9b3197762512a45c5ad00168da3abde9a0409222ed8f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bffa62fa45943f54e3903c58f76b33c51b28baf7875528ee2606118510f658803857d8d92888857356181c6d827a6df4e6384cb4b57bc0d9f8f8dd9b2934de29
|
7
|
+
data.tar.gz: e7e0d7c88f204afa63e06ef66b0add324809f3520b2d014cbd78830ae355ca1039bf06c5da4d48a645237ee6cfc9f28f5daf9873e23c6b3add3e0de918d48be7
|
@@ -3,7 +3,7 @@
|
|
3
3
|
class FixDefaultHostgroupSettingsCategoryToDsl < ActiveRecord::Migration[6.0]
|
4
4
|
def up
|
5
5
|
# rubocop:disable Rails/SkipsModelValidations
|
6
|
-
Setting.where(category: 'Setting::DefaultHostgroup').update_all(category: 'Setting')
|
6
|
+
Setting.where(category: 'Setting::DefaultHostgroup').update_all(category: 'Setting') if column_exists?(:settings, :category)
|
7
7
|
# rubocop:enable Rails/SkipsModelValidations
|
8
8
|
end
|
9
9
|
end
|
@@ -24,7 +24,7 @@ module DefaultHostgroupBaseHostPatch
|
|
24
24
|
# Check settings are created
|
25
25
|
return result unless settings_exist?
|
26
26
|
|
27
|
-
Rails.logger.debug
|
27
|
+
Rails.logger.debug 'DefaultHostgroupMatch: performing Hostgroup match'
|
28
28
|
|
29
29
|
return result unless host_new_or_forced?
|
30
30
|
return result unless host_has_no_hostgroup_or_forced?
|
@@ -34,10 +34,10 @@ module DefaultHostgroupBaseHostPatch
|
|
34
34
|
|
35
35
|
return result unless new_hostgroup
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
Rails.logger.info "DefaultHostgroupMatch: #{facts[
|
37
|
+
host.hostgroup = new_hostgroup
|
38
|
+
host.environment = new_hostgroup.environment if (Setting[:force_host_environment] == true) && (facts[:_type] == :puppet)
|
39
|
+
host.save(validate: false)
|
40
|
+
Rails.logger.info "DefaultHostgroupMatch: #{facts['hostname']} added to #{new_hostgroup}"
|
41
41
|
|
42
42
|
result
|
43
43
|
end
|
@@ -52,14 +52,14 @@ module DefaultHostgroupBaseHostPatch
|
|
52
52
|
hg = Hostgroup.find_by(title: group_name)
|
53
53
|
return hg if hg.present? && group_matches?(facts)
|
54
54
|
end
|
55
|
-
Rails.logger.info
|
55
|
+
Rails.logger.info 'No match ...'
|
56
56
|
false
|
57
57
|
end
|
58
58
|
|
59
59
|
def group_matches?(facts)
|
60
60
|
facts.each do |fact_name, fact_regex|
|
61
|
-
fact_regex.gsub!(%r{(\A/|/\z)},
|
62
|
-
host_fact_value =
|
61
|
+
fact_regex.gsub!(%r{(\A/|/\z)}, '')
|
62
|
+
host_fact_value = host.facts[fact_name]
|
63
63
|
Rails.logger.info "Fact = #{fact_name}"
|
64
64
|
Rails.logger.info "Regex = #{fact_regex}"
|
65
65
|
return true if Regexp.new(fact_regex).match?(host_fact_value)
|
@@ -69,7 +69,7 @@ module DefaultHostgroupBaseHostPatch
|
|
69
69
|
|
70
70
|
def settings_exist?
|
71
71
|
unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
|
72
|
-
Rails.logger.warn
|
72
|
+
Rails.logger.warn 'DefaultHostgroupMatch: Could not load :default_hostgroup map from Settings.'
|
73
73
|
return false
|
74
74
|
end
|
75
75
|
true
|
@@ -78,9 +78,9 @@ module DefaultHostgroupBaseHostPatch
|
|
78
78
|
def host_new_or_forced?
|
79
79
|
if Setting[:force_hostgroup_match_only_new]
|
80
80
|
# hosts have already been saved during import_host, so test the creation age instead
|
81
|
-
new_host = ((Time.current -
|
82
|
-
unless new_host &&
|
83
|
-
Rails.logger.debug
|
81
|
+
new_host = ((Time.current - host.created_at) < 300)
|
82
|
+
unless new_host && host.hostgroup.nil? && host.reports.empty?
|
83
|
+
Rails.logger.debug 'DefaultHostgroupMatch: skipping, host exists'
|
84
84
|
return false
|
85
85
|
end
|
86
86
|
end
|
@@ -88,11 +88,9 @@ module DefaultHostgroupBaseHostPatch
|
|
88
88
|
end
|
89
89
|
|
90
90
|
def host_has_no_hostgroup_or_forced?
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
return false
|
95
|
-
end
|
91
|
+
if !Setting[:force_hostgroup_match] && host.hostgroup.present?
|
92
|
+
Rails.logger.debug 'DefaultHostgroupMatch: skipping, host has hostgroup'
|
93
|
+
return false
|
96
94
|
end
|
97
95
|
true
|
98
96
|
end
|
@@ -1,8 +1,8 @@
|
|
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
|
|
@@ -13,10 +13,10 @@ module ForemanDefaultHostgroup
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
initializer
|
16
|
+
initializer 'foreman_default_hostgroup.register_plugin',
|
17
17
|
before: :finisher_hook do
|
18
18
|
Foreman::Plugin.register :foreman_default_hostgroup do
|
19
|
-
requires_foreman
|
19
|
+
requires_foreman '>= 3.0'
|
20
20
|
|
21
21
|
settings do
|
22
22
|
category(:default_hostgroup, N_('Default Hostgroup')) do
|
@@ -38,12 +38,8 @@ module ForemanDefaultHostgroup
|
|
38
38
|
end
|
39
39
|
|
40
40
|
config.to_prepare do
|
41
|
-
|
42
|
-
|
43
|
-
::HostFactImporter.prepend DefaultHostgroupBaseHostPatch::ManagedOverrides
|
44
|
-
rescue StandardError => e
|
45
|
-
Rails.logger.warn "ForemanDefaultHostgroup: skipping engine hook (#{e})"
|
46
|
-
end
|
41
|
+
::HostFactImporter.include DefaultHostgroupBaseHostPatch
|
42
|
+
::HostFactImporter.prepend DefaultHostgroupBaseHostPatch::ManagedOverrides
|
47
43
|
end
|
48
44
|
end
|
49
45
|
end
|
@@ -17,14 +17,11 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
17
17
|
# The settings.yml fixture in Core wipes out the Setting table,
|
18
18
|
# so we use FactoryBot to re-create it
|
19
19
|
FactoryBot.create(:setting,
|
20
|
-
name: 'force_hostgroup_match'
|
21
|
-
category: 'Setting')
|
20
|
+
name: 'force_hostgroup_match')
|
22
21
|
FactoryBot.create(:setting,
|
23
|
-
name: 'force_hostgroup_match_only_new'
|
24
|
-
category: 'Setting')
|
22
|
+
name: 'force_hostgroup_match_only_new')
|
25
23
|
FactoryBot.create(:setting,
|
26
|
-
name: 'force_host_environment'
|
27
|
-
category: 'Setting')
|
24
|
+
name: 'force_host_environment')
|
28
25
|
# Set the defaults
|
29
26
|
Setting[:force_hostgroup_match] = false
|
30
27
|
Setting[:force_hostgroup_match_only_new] = true
|
@@ -39,7 +36,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
39
36
|
end
|
40
37
|
|
41
38
|
def setup_host_and_facts
|
42
|
-
raw = JSON.parse(File.read(File.
|
39
|
+
raw = JSON.parse(File.read(File.join(__dir__, 'facts.json')))
|
43
40
|
@name = raw['name']
|
44
41
|
@host = Host.import_host(raw['name'], 'puppet')
|
45
42
|
@facts = raw['facts']
|
@@ -47,7 +44,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
47
44
|
|
48
45
|
context 'import_facts_with_match_hostgroup' do
|
49
46
|
test 'matched host is saved with new hostgroup' do
|
50
|
-
assert @host.import_facts(@facts)
|
47
|
+
assert HostFactImporter.new(@host).import_facts(@facts)
|
51
48
|
assert_equal Hostgroup.find_by(name: 'Test Default'), Host.find_by(name: @name).hostgroup
|
52
49
|
end
|
53
50
|
|
@@ -56,7 +53,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
56
53
|
@host.hostgroup = hostgroup
|
57
54
|
@host.save(validate: false)
|
58
55
|
|
59
|
-
assert @host.import_facts(@facts)
|
56
|
+
assert HostFactImporter.new(@host).import_facts(@facts)
|
60
57
|
assert_equal hostgroup, Host.find_by(name: @name).hostgroup
|
61
58
|
end
|
62
59
|
|
@@ -64,7 +61,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
64
61
|
@host.created_at = Time.current - 1000
|
65
62
|
@host.save(validate: false)
|
66
63
|
|
67
|
-
assert @host.import_facts(@facts)
|
64
|
+
assert HostFactImporter.new(@host).import_facts(@facts)
|
68
65
|
assert_not Host.find_by(name: @name).hostgroup
|
69
66
|
end
|
70
67
|
end
|
@@ -73,16 +70,16 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
73
70
|
# takes a config map, returns a group or false
|
74
71
|
test 'match a single hostgroup' do
|
75
72
|
facts_map = SETTINGS[:default_hostgroup][:facts_map]
|
76
|
-
assert @host.import_facts(@facts, nil, true)
|
77
|
-
assert_equal Hostgroup.find_by(name: 'Test Default'), @host.find_match(facts_map)
|
73
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
74
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), HostFactImporter.new(@host).find_match(facts_map)
|
78
75
|
end
|
79
76
|
|
80
77
|
test 'returns false for no match' do
|
81
78
|
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
82
79
|
'Test Default' => { 'hostname' => 'nosuchhost' }
|
83
80
|
}
|
84
|
-
assert @host.import_facts(@facts, nil, true)
|
85
|
-
assert_not @host.find_match(facts_map)
|
81
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
82
|
+
assert_not HostFactImporter.new(@host).find_match(facts_map)
|
86
83
|
end
|
87
84
|
|
88
85
|
test 'matches first available hostgroup' do
|
@@ -90,8 +87,8 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
90
87
|
'Test Default' => { 'hostname' => '.*' },
|
91
88
|
'Some Other Group' => { 'hostname' => '/\.lan$/' }
|
92
89
|
}
|
93
|
-
assert @host.import_facts(@facts, nil, true)
|
94
|
-
assert_equal Hostgroup.find_by(name: 'Test Default'), @host.find_match(facts_map)
|
90
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
91
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), HostFactImporter.new(@host).find_match(facts_map)
|
95
92
|
end
|
96
93
|
|
97
94
|
test 'nonexistant groups are ignored' do
|
@@ -99,8 +96,8 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
99
96
|
'Some Other Group' => { 'hostname' => '.*' },
|
100
97
|
'Test Default' => { 'hostname' => '/\.lan$/' }
|
101
98
|
}
|
102
|
-
assert @host.import_facts(@facts, nil, true)
|
103
|
-
assert_equal Hostgroup.find_by(name: 'Test Default'), @host.find_match(facts_map)
|
99
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
100
|
+
assert_equal Hostgroup.find_by(name: 'Test Default'), HostFactImporter.new(@host).find_match(facts_map)
|
104
101
|
end
|
105
102
|
end
|
106
103
|
|
@@ -108,32 +105,32 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
108
105
|
# passing a hash of (group_name, regex) pairs
|
109
106
|
test 'full regex matches' do
|
110
107
|
regex = { 'hostname' => '^sinn1636.lan$' }
|
111
|
-
assert @host.import_facts(@facts, nil, true)
|
112
|
-
assert @host.group_matches?(regex)
|
108
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
109
|
+
assert HostFactImporter.new(@host).group_matches?(regex)
|
113
110
|
end
|
114
111
|
|
115
112
|
test 'partial regex matches' do
|
116
113
|
regex = { 'hostname' => '.lan$' }
|
117
|
-
assert @host.import_facts(@facts, nil, true)
|
118
|
-
assert @host.group_matches?(regex)
|
114
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
115
|
+
assert HostFactImporter.new(@host).group_matches?(regex)
|
119
116
|
end
|
120
117
|
|
121
118
|
test 'regex slashes are stripped' do
|
122
119
|
regex = { 'hostname' => '/\.lan$/' }
|
123
|
-
assert @host.import_facts(@facts, nil, true)
|
124
|
-
assert @host.group_matches?(regex)
|
120
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
121
|
+
assert HostFactImporter.new(@host).group_matches?(regex)
|
125
122
|
end
|
126
123
|
|
127
124
|
test 'invalid keys are ignored' do
|
128
125
|
regex = { 'nosuchfact' => '.*' }
|
129
|
-
assert @host.import_facts(@facts, nil, true)
|
130
|
-
assert_not @host.group_matches?(regex)
|
126
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
127
|
+
assert_not HostFactImporter.new(@host).group_matches?(regex)
|
131
128
|
end
|
132
129
|
|
133
130
|
test 'unmatched values are ignored' do
|
134
131
|
regex = { 'hostname' => 'nosuchname' }
|
135
|
-
assert @host.import_facts(@facts, nil, true)
|
136
|
-
assert_not @host.group_matches?(regex)
|
132
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
133
|
+
assert_not HostFactImporter.new(@host).group_matches?(regex)
|
137
134
|
end
|
138
135
|
|
139
136
|
test 'multiple entries with invalid keys / values match' do
|
@@ -142,67 +139,67 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
|
|
142
139
|
'osfamily' => 'nosuchos',
|
143
140
|
'hostname' => '.lan$'
|
144
141
|
}
|
145
|
-
assert @host.import_facts(@facts, nil, true)
|
146
|
-
assert @host.group_matches?(regex)
|
142
|
+
assert HostFactImporter.new(@host).import_facts(@facts, nil, true)
|
143
|
+
assert HostFactImporter.new(@host).group_matches?(regex)
|
147
144
|
end
|
148
145
|
end
|
149
146
|
|
150
147
|
context 'settings_exist?' do
|
151
148
|
test 'true when Settings exist' do
|
152
149
|
h = FactoryBot.create(:host)
|
153
|
-
assert h.settings_exist?
|
150
|
+
assert HostFactImporter.new(h).settings_exist?
|
154
151
|
end
|
155
152
|
|
156
153
|
test 'false when Settings are missing' do
|
157
154
|
SETTINGS[:default_hostgroup] = {}
|
158
155
|
h = FactoryBot.create(:host)
|
159
|
-
assert_not h.settings_exist?
|
156
|
+
assert_not HostFactImporter.new(h).settings_exist?
|
160
157
|
end
|
161
158
|
end
|
162
159
|
|
163
160
|
context 'host_new_or_forced?' do
|
164
161
|
test 'true when host is new' do
|
165
162
|
h = FactoryBot.create(:host, created_at: Time.current)
|
166
|
-
assert h.host_new_or_forced?
|
163
|
+
assert HostFactImporter.new(h).host_new_or_forced?
|
167
164
|
end
|
168
165
|
|
169
166
|
test 'false when host has existed for > 300s' do
|
170
167
|
h = FactoryBot.create(:host, created_at: Time.current - 1000)
|
171
|
-
assert_not h.host_new_or_forced?
|
168
|
+
assert_not HostFactImporter.new(h).host_new_or_forced?
|
172
169
|
end
|
173
170
|
|
174
171
|
test 'false when host has a hostgroup' do
|
175
172
|
h = FactoryBot.create(:host, :with_hostgroup, created_at: Time.current)
|
176
|
-
assert_not h.host_new_or_forced?
|
173
|
+
assert_not HostFactImporter.new(h).host_new_or_forced?
|
177
174
|
end
|
178
175
|
|
179
176
|
test 'false when host has reports' do
|
180
177
|
h = FactoryBot.create(:host, :with_reports, created_at: Time.current)
|
181
|
-
assert_not h.host_new_or_forced?
|
178
|
+
assert_not HostFactImporter.new(h).host_new_or_forced?
|
182
179
|
end
|
183
180
|
|
184
181
|
test 'true when setting is forced' do
|
185
182
|
Setting[:force_hostgroup_match_only_new] = false
|
186
183
|
h = FactoryBot.create(:host, :with_hostgroup, created_at: Time.current)
|
187
|
-
assert h.host_new_or_forced?
|
184
|
+
assert HostFactImporter.new(h).host_new_or_forced?
|
188
185
|
end
|
189
186
|
end
|
190
187
|
|
191
188
|
context 'host_has_no_hostgroup_or_forced?' do
|
192
189
|
test 'true if host has no hostgroup' do
|
193
190
|
h = FactoryBot.create(:host)
|
194
|
-
assert h.host_has_no_hostgroup_or_forced?
|
191
|
+
assert HostFactImporter.new(h).host_has_no_hostgroup_or_forced?
|
195
192
|
end
|
196
193
|
|
197
194
|
test 'false if host has hostgroup' do
|
198
195
|
h = FactoryBot.create(:host, :with_hostgroup)
|
199
|
-
assert_not h.host_has_no_hostgroup_or_forced?
|
196
|
+
assert_not HostFactImporter.new(h).host_has_no_hostgroup_or_forced?
|
200
197
|
end
|
201
198
|
|
202
199
|
test 'true if host has hostgroup and setting forced' do
|
203
200
|
Setting[:force_hostgroup_match] = true
|
204
201
|
h = FactoryBot.create(:host, :with_hostgroup)
|
205
|
-
assert h.host_has_no_hostgroup_or_forced?
|
202
|
+
assert HostFactImporter.new(h).host_has_no_hostgroup_or_forced?
|
206
203
|
end
|
207
204
|
end
|
208
205
|
end
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: foreman_default_hostgroup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.
|
4
|
+
version: 7.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Sutcliffe
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-04-03 00:00:00.000000000 Z
|
12
11
|
dependencies: []
|
13
12
|
description: Adds the option to specify a default hostgroup for new hosts created
|
14
13
|
from facts/reports
|
@@ -35,7 +34,6 @@ homepage: https://github.com/theforeman/foreman_default_hostgroup
|
|
35
34
|
licenses:
|
36
35
|
- GPL-3.0
|
37
36
|
metadata: {}
|
38
|
-
post_install_message:
|
39
37
|
rdoc_options: []
|
40
38
|
require_paths:
|
41
39
|
- lib
|
@@ -43,15 +41,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
41
|
requirements:
|
44
42
|
- - ">="
|
45
43
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
44
|
+
version: '2.7'
|
45
|
+
- - "<"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4'
|
47
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
49
|
requirements:
|
49
50
|
- - ">="
|
50
51
|
- !ruby/object:Gem::Version
|
51
52
|
version: '0'
|
52
53
|
requirements: []
|
53
|
-
rubygems_version: 3.
|
54
|
-
signing_key:
|
54
|
+
rubygems_version: 3.6.2
|
55
55
|
specification_version: 4
|
56
56
|
summary: Default Hostgroup Plugin for Foreman
|
57
57
|
test_files:
|