foreman_default_hostgroup 3.0.0 → 4.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 +7 -4
- data/app/models/setting/default_hostgroup.rb +8 -8
- data/lib/default_hostgroup.rake +21 -8
- data/lib/default_hostgroup_base_host_patch.rb +83 -0
- data/lib/foreman_default_hostgroup/engine.rb +19 -14
- data/lib/foreman_default_hostgroup/version.rb +1 -1
- data/test/unit/default_hostgroup_test.rb +184 -120
- metadata +4 -4
- data/lib/default_hostgroup_managed_host_patch.rb +0 -74
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8405ed690d11f8d107656ee4a9237c14b0a41b99
|
4
|
+
data.tar.gz: 45e335b0f194f8016c020b533e0fee740b4869a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a06c0e8a304b5775e594261fc4fde1b906312f00a057bfbd415a3796e36c37915e8c1c4442ee9e16adc6546063ad35dcab980adf3095088403b00a6c4c832d1
|
7
|
+
data.tar.gz: a68ce3071ce99f86a14a6f87571de073932822d2ee7b94fba7d3acf7b2f6a11618491a22385b8af00027b05aeb85bf08b45bdc35cf404e17ee51eb97ef529924
|
data/README.md
CHANGED
@@ -5,8 +5,7 @@ a Hostgroup set.
|
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
8
|
-
See [
|
9
|
-
for how to install Foreman plugins
|
8
|
+
See Foreman's [plugin installation documentation](https://theforeman.org/plugins/#2.Installation).
|
10
9
|
|
11
10
|
## Compatibility
|
12
11
|
|
@@ -16,12 +15,13 @@ for how to install Foreman plugins
|
|
16
15
|
| 1.3 | 1.0.1 |
|
17
16
|
| 1.4 | 1.1.0 |
|
18
17
|
| 1.5 | 2.0.1 |
|
19
|
-
|
|
18
|
+
| 1.6 - 1.11 | 3.0.0 |
|
19
|
+
| >= 1.12 | 4.0.0 |
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
The configuration is done inside foreman's plugin settings directory which is
|
24
|
-
|
24
|
+
`/etc/foreman/plugins/`.
|
25
25
|
|
26
26
|
You can simply copy `default_hostgroup.yaml.example` and adjust it to fit
|
27
27
|
your needs. The format is shown in the example. The simplest form would be:
|
@@ -33,6 +33,9 @@ your needs. The format is shown in the example. The simplest form would be:
|
|
33
33
|
"Default":
|
34
34
|
"hostname": ".*"
|
35
35
|
```
|
36
|
+
If you are ugrading from plugin version 2.0.1 or older the format of this
|
37
|
+
file changes and you will need modify `default_hostgroup.yaml.example` to
|
38
|
+
follow the format above.
|
36
39
|
|
37
40
|
*Important Note:* You have to restart foreman in order to apply changes in
|
38
41
|
`default_hostgroup.yaml`!
|
@@ -3,17 +3,17 @@ class Setting::DefaultHostgroup < ::Setting
|
|
3
3
|
|
4
4
|
def self.load_defaults
|
5
5
|
# Check the table exists
|
6
|
+
return unless ActiveRecord::Base.connection.table_exists?('settings')
|
6
7
|
return unless super
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
Setting.transaction do
|
10
|
+
[
|
11
|
+
set('force_hostgroup_match', 'Apply hostgroup matching even if a host already has one.', false),
|
12
|
+
set('force_hostgroup_match_only_new', 'Apply hostgroup matching only on new hosts', true),
|
13
|
+
set('force_host_environment', "Apply hostgroup's environment to host even if a host already has a different one", true)
|
14
|
+
].compact.each { |s| create s.update(category: 'Setting::DefaultHostgroup') }
|
15
|
+
end
|
14
16
|
|
15
17
|
true
|
16
|
-
|
17
18
|
end
|
18
|
-
|
19
19
|
end
|
data/lib/default_hostgroup.rake
CHANGED
@@ -1,21 +1,34 @@
|
|
1
|
+
# Tests
|
1
2
|
namespace :test do
|
2
3
|
desc "Test DefaultHostgroup plugin"
|
3
|
-
Rake::TestTask.new(:
|
4
|
+
Rake::TestTask.new(:foreman_default_hostgroup) do |t|
|
4
5
|
test_dir = File.join(File.dirname(__FILE__), '..', 'test')
|
5
6
|
t.libs << ["test",test_dir]
|
6
7
|
t.pattern = "#{test_dir}/**/*_test.rb"
|
7
8
|
t.verbose = true
|
8
9
|
end
|
9
|
-
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
namespace :foreman_default_hostgroup do
|
13
|
+
task :rubocop do
|
14
|
+
begin
|
15
|
+
require 'rubocop/rake_task'
|
16
|
+
RuboCop::RakeTask.new(:rubocop_foreman_default_hostgroup) do |task|
|
17
|
+
task.patterns = ["#{ForemanDefaultHostgroup::Engine.root}/app/**/*.rb",
|
18
|
+
"#{ForemanDefaultHostgroup::Engine.root}/lib/**/*.rb",
|
19
|
+
"#{ForemanDefaultHostgroup::Engine.root}/test/**/*.rb"]
|
20
|
+
end
|
21
|
+
rescue
|
22
|
+
puts 'Rubocop not loaded.'
|
23
|
+
end
|
24
|
+
|
25
|
+
Rake::Task['rubocop_foreman_default_hostgroup'].invoke
|
26
|
+
end
|
14
27
|
end
|
15
28
|
|
29
|
+
Rake::Task[:test].enhance ['test:foreman_default_hostgroup']
|
30
|
+
|
16
31
|
load 'tasks/jenkins.rake'
|
17
|
-
if Rake::Task.task_defined?(:'jenkins:
|
18
|
-
Rake::Task[
|
19
|
-
Rake::Task['test:default_hostgroup'].invoke
|
20
|
-
end
|
32
|
+
if Rake::Task.task_defined?(:'jenkins:unit')
|
33
|
+
Rake::Task['jenkins:unit'].enhance ['test:foreman_default_hostgroup', 'foreman_default_hostgroup:rubocop']
|
21
34
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module DefaultHostgroupBaseHostPatch
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
alias_method_chain :import_facts, :match_hostgroup
|
6
|
+
end
|
7
|
+
|
8
|
+
def import_facts_with_match_hostgroup(facts)
|
9
|
+
# Load the facts anyway, hook onto the end of it
|
10
|
+
result = import_facts_without_match_hostgroup(facts)
|
11
|
+
|
12
|
+
return result unless settings_exist?
|
13
|
+
|
14
|
+
Rails.logger.debug 'DefaultHostgroupMatch: performing Hostgroup match'
|
15
|
+
|
16
|
+
return result unless host_new_or_forced?
|
17
|
+
return result unless host_has_no_hostgroup_or_forced?
|
18
|
+
|
19
|
+
facts_map = SETTINGS[:default_hostgroup][:facts_map]
|
20
|
+
new_hostgroup = find_match(facts_map)
|
21
|
+
|
22
|
+
return result unless new_hostgroup
|
23
|
+
|
24
|
+
self.hostgroup = new_hostgroup
|
25
|
+
if Setting[:force_host_environment] == true
|
26
|
+
self.environment = new_hostgroup.environment
|
27
|
+
end
|
28
|
+
save(validate: false)
|
29
|
+
Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_match(facts_map)
|
35
|
+
facts_map.each do |group_name, facts|
|
36
|
+
hg = Hostgroup.find_by_title(group_name)
|
37
|
+
return hg if hg.present? && group_matches?(facts)
|
38
|
+
end
|
39
|
+
Rails.logger.info 'No match ...'
|
40
|
+
false
|
41
|
+
end
|
42
|
+
|
43
|
+
def group_matches?(facts)
|
44
|
+
facts.each do |fact_name, fact_regex|
|
45
|
+
fact_regex.gsub!(%r{(\A/|/\z)}, '')
|
46
|
+
host_fact_value = facts_hash[fact_name]
|
47
|
+
Rails.logger.info "Fact = #{fact_name}"
|
48
|
+
Rails.logger.info "Regex = #{fact_regex}"
|
49
|
+
return true if Regexp.new(fact_regex).match(host_fact_value)
|
50
|
+
end
|
51
|
+
false
|
52
|
+
end
|
53
|
+
|
54
|
+
def settings_exist?
|
55
|
+
unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
|
56
|
+
Rails.logger.warn 'DefaultHostgroupMatch: Could not load :default_hostgroup map from Settings.'
|
57
|
+
return false
|
58
|
+
end
|
59
|
+
true
|
60
|
+
end
|
61
|
+
|
62
|
+
def host_new_or_forced?
|
63
|
+
if Setting[:force_hostgroup_match_only_new]
|
64
|
+
# hosts have already been saved during import_host, so test the creation age instead
|
65
|
+
new_host = ((Time.current - created_at) < 300)
|
66
|
+
unless new_host && hostgroup.nil? && reports.empty?
|
67
|
+
Rails.logger.debug 'DefaultHostgroupMatch: skipping, host exists'
|
68
|
+
return false
|
69
|
+
end
|
70
|
+
end
|
71
|
+
true
|
72
|
+
end
|
73
|
+
|
74
|
+
def host_has_no_hostgroup_or_forced?
|
75
|
+
unless Setting[:force_hostgroup_match]
|
76
|
+
if hostgroup.present?
|
77
|
+
Rails.logger.debug 'DefaultHostgroupMatch: skipping, host has hostgroup'
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
true
|
82
|
+
end
|
83
|
+
end
|
@@ -1,28 +1,33 @@
|
|
1
|
-
require '
|
1
|
+
require 'default_hostgroup_base_host_patch'
|
2
2
|
|
3
3
|
module ForemanDefaultHostgroup
|
4
|
-
#Inherit from the Rails module of the parent app (Foreman), not
|
5
|
-
#Thus, inherits from ::Rails::Engine and not from
|
4
|
+
# Inherit from the Rails module of the parent app (Foreman), not
|
5
|
+
# the plugin. Thus, inherits from ::Rails::Engine and not from
|
6
|
+
# Rails::Engine
|
6
7
|
class Engine < ::Rails::Engine
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
require_dependency File.expand_path("../../../app/models/setting/default_hostgroup.rb", __FILE__) if (Setting.table_exists? rescue(false))
|
8
|
+
initializer 'foreman_default_hostgroup.load_default_settings',
|
9
|
+
before: :load_config_initializers do
|
10
|
+
require_dependency File.expand_path(
|
11
|
+
'../../../app/models/setting/default_hostgroup.rb', __FILE__)
|
12
12
|
end
|
13
13
|
|
14
|
-
initializer 'foreman_default_hostgroup.register_plugin',
|
15
|
-
|
16
|
-
|
14
|
+
initializer 'foreman_default_hostgroup.register_plugin',
|
15
|
+
before: :finisher_hook do
|
16
|
+
Foreman::Plugin.register :foreman_plugin_template do
|
17
|
+
requires_foreman '>= 1.12'
|
18
|
+
end
|
17
19
|
end
|
18
20
|
|
19
21
|
config.to_prepare do
|
20
|
-
|
22
|
+
begin
|
23
|
+
::Host::Base.send(:include, DefaultHostgroupBaseHostPatch)
|
24
|
+
rescue => e
|
25
|
+
Rails.logger.warn "ForemanDefaultHostgroup: skipping engine hook (#{e})"
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
rake_tasks do
|
24
|
-
load
|
30
|
+
load 'default_hostgroup.rake'
|
25
31
|
end
|
26
|
-
|
27
32
|
end
|
28
33
|
end
|
@@ -1,155 +1,219 @@
|
|
1
1
|
require 'test_plugin_helper'
|
2
2
|
|
3
|
+
# Tests for the plugin
|
3
4
|
class DefaultHostgroupTest < ActiveSupport::TestCase
|
4
5
|
setup do
|
5
6
|
disable_orchestration
|
6
|
-
User.current = User.find_by_login
|
7
|
+
User.current = User.find_by_login 'admin'
|
8
|
+
setup_hostgroup_matchers
|
9
|
+
setup_host_and_facts
|
7
10
|
end
|
8
11
|
|
9
|
-
def
|
10
|
-
return JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + relative_path)))
|
11
|
-
end
|
12
|
-
|
13
|
-
def setup_hostgroup_match
|
12
|
+
def setup_hostgroup_matchers
|
14
13
|
# The settings.yml fixture in Core wipes out the Setting table,
|
15
14
|
# so we use FactoryGirl to re-create it
|
16
15
|
FactoryGirl.create(:setting,
|
17
|
-
:
|
18
|
-
:
|
16
|
+
name: 'force_hostgroup_match',
|
17
|
+
category: 'Setting::DefaultHostgroup')
|
19
18
|
FactoryGirl.create(:setting,
|
20
|
-
:
|
21
|
-
:
|
19
|
+
name: 'force_hostgroup_match_only_new',
|
20
|
+
category: 'Setting::DefaultHostgroup')
|
21
|
+
FactoryGirl.create(:setting,
|
22
|
+
name: 'force_host_environment',
|
23
|
+
category: 'Setting::DefaultHostgroup')
|
24
|
+
# Set the defaults
|
22
25
|
Setting[:force_hostgroup_match] = false
|
23
26
|
Setting[:force_hostgroup_match_only_new] = true
|
24
|
-
|
27
|
+
Setting[:force_host_environment] = true
|
28
|
+
|
29
|
+
# Mimic plugin config fron config file
|
30
|
+
FactoryGirl.create(:hostgroup, :with_environment, name: 'Test Default')
|
31
|
+
SETTINGS[:default_hostgroup] = {}
|
32
|
+
SETTINGS[:default_hostgroup][:facts_map] = {
|
33
|
+
'Test Default' => { 'hostname' => '.*' }
|
34
|
+
}
|
25
35
|
end
|
26
36
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
raw = parse_json_fixture('/facts.json')
|
33
|
-
|
34
|
-
assert Host.import_host_and_facts(raw['name'], raw['facts'])
|
35
|
-
assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
|
37
|
+
def setup_host_and_facts
|
38
|
+
raw = JSON.parse(File.read(File.expand_path(File.dirname(__FILE__) + '/facts.json')))
|
39
|
+
@name = raw['name']
|
40
|
+
@host = Host.import_host(raw['name'], 'puppet')
|
41
|
+
@facts = raw['facts']
|
36
42
|
end
|
37
43
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
raw = parse_json_fixture('/facts.json')
|
44
|
+
context 'import_facts_with_match_hostgroup' do
|
45
|
+
test 'matched host is saved with new hostgroup' do
|
46
|
+
assert @host.import_facts(@facts)
|
47
|
+
assert_equal Hostgroup.find_by_name('Test Default'), Host.find_by_name(@name).hostgroup
|
48
|
+
end
|
44
49
|
|
45
|
-
|
46
|
-
|
47
|
-
|
50
|
+
test 'matched host not updated if host already has a hostgroup' do
|
51
|
+
hostgroup = FactoryGirl.create(:hostgroup)
|
52
|
+
@host.hostgroup = hostgroup
|
53
|
+
@host.save(validate: false)
|
48
54
|
|
49
|
-
|
50
|
-
|
51
|
-
|
55
|
+
assert @host.import_facts(@facts)
|
56
|
+
assert_equal hostgroup, Host.find_by_name(@name).hostgroup
|
57
|
+
end
|
52
58
|
|
53
|
-
hostgroup
|
54
|
-
|
59
|
+
test 'hostgroup is not updated if host is not new' do
|
60
|
+
@host.created_at = Time.current - 1000
|
61
|
+
@host.save(validate: false)
|
55
62
|
|
56
|
-
|
57
|
-
|
63
|
+
assert @host.import_facts(@facts)
|
64
|
+
refute Host.find_by_name(@name).hostgroup
|
65
|
+
end
|
58
66
|
end
|
59
67
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
68
|
+
context 'force host environment setting' do
|
69
|
+
test 'environment is updated if enabled' do
|
70
|
+
h = FactoryGirl.create(:host, :with_environment, created_at: Time.current)
|
71
|
+
h.import_facts(@facts)
|
72
|
+
assert_equal Hostgroup.find_by_name('Test Default').environment, h.environment
|
73
|
+
end
|
74
|
+
|
75
|
+
test 'environment not updated if disabled' do
|
76
|
+
Setting[:force_host_environment] = false
|
77
|
+
h = FactoryGirl.create(:host, :with_environment, created_at: Time.current)
|
78
|
+
h.import_facts(@facts)
|
79
|
+
refute_equal Hostgroup.find_by_name('Test Default').environment, h.environment
|
80
|
+
end
|
69
81
|
end
|
70
82
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
83
|
+
context 'find_match' do
|
84
|
+
# takes a config map, returns a group or false
|
85
|
+
test 'match a single hostgroup' do
|
86
|
+
facts_map = SETTINGS[:default_hostgroup][:facts_map]
|
87
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
88
|
+
assert_equal Hostgroup.find_by_name('Test Default'), @host.find_match(facts_map)
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'returns false for no match' do
|
92
|
+
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
93
|
+
'Test Default' => { 'hostname' => 'nosuchhost' }
|
94
|
+
}
|
95
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
96
|
+
refute @host.find_match(facts_map)
|
97
|
+
end
|
98
|
+
|
99
|
+
test 'matches first available hostgroup' do
|
100
|
+
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
101
|
+
'Test Default' => { 'hostname' => '.*' },
|
102
|
+
'Some Other Group' => { 'hostname' => '/\.lan$/' }
|
103
|
+
}
|
104
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
105
|
+
assert_equal Hostgroup.find_by_name('Test Default'), @host.find_match(facts_map)
|
106
|
+
end
|
107
|
+
|
108
|
+
test 'nonexistant groups are ignored' do
|
109
|
+
facts_map = SETTINGS[:default_hostgroup][:facts_map] = {
|
110
|
+
'Some Other Group' => { 'hostname' => '.*' },
|
111
|
+
'Test Default' => { 'hostname' => '/\.lan$/' }
|
112
|
+
}
|
113
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
114
|
+
assert_equal Hostgroup.find_by_name('Test Default'), @host.find_match(facts_map)
|
115
|
+
end
|
80
116
|
end
|
81
117
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
118
|
+
context 'group_matches?' do
|
119
|
+
# passing a hash of (group_name, regex) pairs
|
120
|
+
test 'full regex matches' do
|
121
|
+
regex = { 'hostname' => '^sinn1636.lan$' }
|
122
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
123
|
+
assert @host.group_matches?(regex)
|
124
|
+
end
|
125
|
+
|
126
|
+
test 'partial regex matches' do
|
127
|
+
regex = { 'hostname' => '.lan$' }
|
128
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
129
|
+
assert @host.group_matches?(regex)
|
130
|
+
end
|
131
|
+
|
132
|
+
test 'regex slashes are stripped' do
|
133
|
+
regex = { 'hostname' => '/\.lan$/' }
|
134
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
135
|
+
assert @host.group_matches?(regex)
|
136
|
+
end
|
137
|
+
|
138
|
+
test 'invalid keys are ignored' do
|
139
|
+
regex = { 'nosuchfact' => '.*' }
|
140
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
141
|
+
refute @host.group_matches?(regex)
|
142
|
+
end
|
143
|
+
|
144
|
+
test 'unmatched values are ignored' do
|
145
|
+
regex = { 'hostname' => 'nosuchname' }
|
146
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
147
|
+
refute @host.group_matches?(regex)
|
148
|
+
end
|
149
|
+
|
150
|
+
test 'multiple entries with invalid keys / values match' do
|
151
|
+
regex = {
|
152
|
+
'nosuchfact' => '.*',
|
153
|
+
'osfamily' => 'nosuchos',
|
154
|
+
'hostname' => '.lan$'
|
155
|
+
}
|
156
|
+
assert @host.import_facts_without_match_hostgroup(@facts)
|
157
|
+
assert @host.group_matches?(regex)
|
158
|
+
end
|
91
159
|
end
|
92
160
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
161
|
+
context 'settings_exist?' do
|
162
|
+
test 'true when Settings exist' do
|
163
|
+
h = FactoryGirl.create(:host)
|
164
|
+
assert h.settings_exist?
|
165
|
+
end
|
166
|
+
|
167
|
+
test 'false when Settings are missing' do
|
168
|
+
SETTINGS[:default_hostgroup] = {}
|
169
|
+
h = FactoryGirl.create(:host)
|
170
|
+
refute h.settings_exist?
|
171
|
+
end
|
102
172
|
end
|
103
173
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
174
|
+
context 'host_new_or_forced?' do
|
175
|
+
test 'true when host is new' do
|
176
|
+
h = FactoryGirl.create(:host, created_at: Time.current)
|
177
|
+
assert h.host_new_or_forced?
|
178
|
+
end
|
179
|
+
|
180
|
+
test 'false when host has existed for > 300s' do
|
181
|
+
h = FactoryGirl.create(:host, created_at: Time.current - 1000)
|
182
|
+
refute h.host_new_or_forced?
|
183
|
+
end
|
184
|
+
|
185
|
+
test 'false when host has a hostgroup' do
|
186
|
+
h = FactoryGirl.create(:host, :with_hostgroup, created_at: Time.current)
|
187
|
+
refute h.host_new_or_forced?
|
188
|
+
end
|
189
|
+
|
190
|
+
test 'false when host has reports' do
|
191
|
+
h = FactoryGirl.create(:host, :with_reports, created_at: Time.current)
|
192
|
+
refute h.host_new_or_forced?
|
193
|
+
end
|
194
|
+
|
195
|
+
test 'true when setting is forced' do
|
196
|
+
Setting[:force_hostgroup_match_only_new] = false
|
197
|
+
h = FactoryGirl.create(:host, :with_hostgroup, created_at: Time.current)
|
198
|
+
assert h.host_new_or_forced?
|
199
|
+
end
|
118
200
|
end
|
119
201
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
host
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
202
|
+
context 'host_has_no_hostgroup_or_forced?' do
|
203
|
+
test 'true if host has no hostgroup' do
|
204
|
+
h = FactoryGirl.create(:host)
|
205
|
+
assert h.host_has_no_hostgroup_or_forced?
|
206
|
+
end
|
207
|
+
|
208
|
+
test 'false if host has hostgroup' do
|
209
|
+
h = FactoryGirl.create(:host, :with_hostgroup)
|
210
|
+
refute h.host_has_no_hostgroup_or_forced?
|
211
|
+
end
|
212
|
+
|
213
|
+
test 'true if host has hostgroup and setting forced' do
|
214
|
+
Setting[:force_hostgroup_match] = true
|
215
|
+
h = FactoryGirl.create(:host, :with_hostgroup)
|
216
|
+
assert h.host_has_no_hostgroup_or_forced?
|
217
|
+
end
|
136
218
|
end
|
137
|
-
|
138
|
-
test "hostgroup is not updated if host is not new" do
|
139
|
-
setup_hostgroup_match
|
140
|
-
Setting[:force_hostgroup_match] = true
|
141
|
-
SETTINGS[:default_hostgroup][:facts_map] = { "Test Default" => {'hostname' => '.*'} }
|
142
|
-
|
143
|
-
hostgroup = Hostgroup.create(:name => "Test Group")
|
144
|
-
Hostgroup.create(:name => "Test Default")
|
145
|
-
raw = parse_json_fixture('/facts.json')
|
146
|
-
|
147
|
-
host, result = Host.import_host_and_facts_without_match_hostgroup(raw['name'], raw['facts'])
|
148
|
-
host.hostgroup = hostgroup
|
149
|
-
host.save(:validate => false)
|
150
|
-
|
151
|
-
assert Host.import_host_and_facts(raw['name'], raw['facts'])
|
152
|
-
assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
|
153
|
-
end
|
154
|
-
|
155
219
|
end
|
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: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Sutcliffe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-07-06 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
|
@@ -24,7 +24,7 @@ files:
|
|
24
24
|
- app/models/setting/default_hostgroup.rb
|
25
25
|
- default_hostgroup.yaml.example
|
26
26
|
- lib/default_hostgroup.rake
|
27
|
-
- lib/
|
27
|
+
- lib/default_hostgroup_base_host_patch.rb
|
28
28
|
- lib/foreman_default_hostgroup.rb
|
29
29
|
- lib/foreman_default_hostgroup/engine.rb
|
30
30
|
- lib/foreman_default_hostgroup/version.rb
|
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
53
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.2.
|
54
|
+
rubygems_version: 2.2.5
|
55
55
|
signing_key:
|
56
56
|
specification_version: 4
|
57
57
|
summary: Default Hostgroup Plugin for Foreman
|
@@ -1,74 +0,0 @@
|
|
1
|
-
module DefaultHostgroupManagedHostPatch
|
2
|
-
def self.included(base)
|
3
|
-
base.extend ClassMethods
|
4
|
-
base.class_eval do
|
5
|
-
class << self
|
6
|
-
alias_method_chain :import_host_and_facts, :match_hostgroup
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module ClassMethods
|
12
|
-
def import_host_and_facts_with_match_hostgroup hostname, facts, certname = nil, proxy_id = nil
|
13
|
-
@host, result = import_host_and_facts_without_match_hostgroup(hostname, facts, certname, proxy_id)
|
14
|
-
|
15
|
-
unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
|
16
|
-
Rails.logger.warn "DefaultHostgroupMatch: Could not load default_hostgroup map from settings, check config."
|
17
|
-
return @host, result
|
18
|
-
end
|
19
|
-
|
20
|
-
Rails.logger.debug "DefaultHostgroupMatch: performing Hostgroup match"
|
21
|
-
|
22
|
-
if Setting[:force_hostgroup_match_only_new]
|
23
|
-
# host.new_record? will only test for the early return in the core method, a real host
|
24
|
-
# will have already been saved at least once.
|
25
|
-
unless @host.present? && !@host.new_record? && @host.hostgroup.nil? && @host.reports.empty?
|
26
|
-
|
27
|
-
Rails.logger.debug "DefaultHostgroupMatch: skipping, host exists"
|
28
|
-
return @host, result
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
unless Setting[:force_hostgroup_match]
|
33
|
-
if @host.hostgroup.present?
|
34
|
-
Rails.logger.debug "DefaultHostgroupMatch: skipping, host has hostgroup"
|
35
|
-
return @host, result
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
facts_map = SETTINGS[:default_hostgroup][:facts_map]
|
40
|
-
new_hostgroup = find_match(facts_map)
|
41
|
-
|
42
|
-
return @host, result unless new_hostgroup
|
43
|
-
|
44
|
-
@host.hostgroup = new_hostgroup
|
45
|
-
@host.save(:validate => false)
|
46
|
-
Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"
|
47
|
-
|
48
|
-
return @host, result
|
49
|
-
end
|
50
|
-
|
51
|
-
def group_matches?(fact)
|
52
|
-
fact.each do |fact_name, fact_regex|
|
53
|
-
fact_regex.gsub!(/(\A\/|\/\z)/, '')
|
54
|
-
host_fact_value = @host.facts_hash[fact_name]
|
55
|
-
Rails.logger.info "Fact = #{fact_name}"
|
56
|
-
Rails.logger.info "Regex = #{fact_regex}"
|
57
|
-
return true if Regexp.new(fact_regex).match(host_fact_value)
|
58
|
-
end
|
59
|
-
return false
|
60
|
-
end
|
61
|
-
|
62
|
-
def find_match(facts_map)
|
63
|
-
facts_map.each do |group_name, facts|
|
64
|
-
return Hostgroup.find_by_title(group_name) if group_matches?(facts) and valid_hostgroup?(group_name)
|
65
|
-
end
|
66
|
-
Rails.logger.info "No match ..."
|
67
|
-
return false
|
68
|
-
end
|
69
|
-
|
70
|
-
def valid_hostgroup?(hostgroup)
|
71
|
-
Hostgroup.find_by_title(hostgroup) ? true : false
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|