foreman_default_hostgroup 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MDQ3MDU3NTRiNDNkNmExNzcxNmJlZDBmMDM3N2NkOWJhZGQxNTExZg==
5
- data.tar.gz: !binary |-
6
- OWNkODUwM2VmYTk4YWFhYzQ2MDViZWRlZTQyZTNlMWE1ZTA5MDFlNQ==
2
+ SHA1:
3
+ metadata.gz: 5d454d40d36c903cdb01917607ff7bb5a95ec18f
4
+ data.tar.gz: a620ee8b99e5bf30147d2266378462473b326628
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NGIyZWZmN2NmNTZkMDNjZWJhMjViMTk4NWU1MWU3ODUxMzczZmVlODgzNTg1
10
- MmExYjU2MzAxMWIwNjViY2MyNGQ3MzVjMWVjNmY3YjQzOTM4MGUwOGViMzk3
11
- NjVjZDk2NDQ0MThhZmRiODc4YjQ5NzJiY2EwYWI2ZjJhZTg3Y2I=
12
- data.tar.gz: !binary |-
13
- ZTFkMzZlZTgyMjBkYTJkYmI2Nzk2OTQ4YTlkN2UxMzg4ZjdhZDk3YjZiMmJh
14
- OTQ5MTZkYjIwNjIxZGRkZDNiYWUzZjkwY2YxNWE1ZGRhYzIwZjc4ZmIxNzBk
15
- ZjBjMzE3ZDRjYzUxMzVkOGFhNGI4NDVmN2QyYjQzOTBkOGE5Y2M=
6
+ metadata.gz: 79200d8486340a22124c1f4b8f7df6645db83fc0c4933cf7f0a9b1269acc5a11648b810718c1dd8a97a9f9a4b0d0bf98fc0bfb638b56616c530cb28e0a1d150b
7
+ data.tar.gz: 6e49893a8db8af0cde5e8dc7dd242e7de90569182555cfb3232a2c2eb7b01b7bade2ec248ac932714752fb3b7f14830f4acf6ecf4a6920af68faf7cf126afac6
data/README.md CHANGED
@@ -15,32 +15,34 @@ for how to install Foreman plugins
15
15
  | <= 1.2 | 0.1.0 |
16
16
  | 1.3 | 1.0.1 |
17
17
  | 1.4 | 1.1.0 |
18
- | >= 1.5 | 2.0.1 |
18
+ | 1.5 | 2.0.1 |
19
+ | >= 1.6 | 2.1.0 |
19
20
 
20
21
  ## Usage
21
22
 
22
23
  The configuration is done inside foreman's plugin settings directory which is
23
- ```~foreman/config/settings.plugins.d/```.
24
+ `~foreman/config/settings.plugins.d/`.
24
25
 
25
- You can simply copy ```default_hostgroup.yaml.example``` and adjust it to fit
26
- your needs. The simplest example would be:
26
+ You can simply copy `default_hostgroup.yaml.example` and adjust it to fit
27
+ your needs. The format is shown in the example. The simplest form would be:
27
28
 
28
29
  ```
29
30
  ---
30
31
  :default_hostgroup:
31
- :map:
32
- "Default": ".*"
32
+ :facts_map:
33
+ "Default":
34
+ "hostname": ".*"
33
35
  ```
34
36
 
35
37
  *Important Note:* You have to restart foreman in order to apply changes in
36
- ```default_hostgroup.yaml```!
38
+ `default_hostgroup.yaml`!
37
39
 
38
- There are also two more settings under ```Settings -> DefaultHostgroup```
40
+ There are also two more settings under `Settings -> DefaultHostgroup`
39
41
 
40
42
  | Setting | Description |
41
43
  | ------- | ----------- |
42
- | ```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``` |
43
- | ```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``` |
44
+ | `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` |
45
+ | `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` |
44
46
 
45
47
  ## TODO
46
48
 
@@ -10,11 +10,11 @@ module DefaultHostgroupManagedHostPatch
10
10
 
11
11
  module ClassMethods
12
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)
13
+ @host, result = import_host_and_facts_without_match_hostgroup(hostname, facts, certname, proxy_id)
14
14
 
15
- unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:map]
15
+ unless SETTINGS[:default_hostgroup] && SETTINGS[:default_hostgroup][:facts_map]
16
16
  Rails.logger.warn "DefaultHostgroupMatch: Could not load default_hostgroup map from settings, check config."
17
- return host, result
17
+ return @host, result
18
18
  end
19
19
 
20
20
  Rails.logger.debug "DefaultHostgroupMatch: performing Hostgroup match"
@@ -22,42 +22,49 @@ module DefaultHostgroupManagedHostPatch
22
22
  if Setting[:force_hostgroup_match_only_new]
23
23
  # host.new_record? will only test for the early return in the core method, a real host
24
24
  # will have already been saved at least once.
25
- unless host.present? && !host.new_record? && host.hostgroup.nil? && host.reports.empty?
25
+ unless @host.present? && !@host.new_record? && @host.hostgroup.nil? && @host.reports.empty?
26
26
 
27
27
  Rails.logger.debug "DefaultHostgroupMatch: skipping, host exists"
28
- return host, result
28
+ return @host, result
29
29
  end
30
30
  end
31
31
 
32
32
  unless Setting[:force_hostgroup_match]
33
- if host.hostgroup.present?
33
+ if @host.hostgroup.present?
34
34
  Rails.logger.debug "DefaultHostgroupMatch: skipping, host has hostgroup"
35
- return host, result
35
+ return @host, result
36
36
  end
37
37
  end
38
38
 
39
- map = SETTINGS[:default_hostgroup][:map]
40
- new_hostgroup = nil
39
+ facts_map = SETTINGS[:default_hostgroup][:facts_map]
40
+ new_hostgroup = find_match(facts_map)
41
41
 
42
- map.each do |hostgroup, regex|
43
- unless valid_hostgroup?(hostgroup)
44
- Rails.logger.error "DefaultHostgroupMatch: #{hostgroup} is not a valid hostgroup, skipping."
45
- next
46
- end
47
- regex.gsub!(/(\A\/|\/\z)/, '')
48
- if Regexp.new(regex).match(hostname)
49
- new_hostgroup = Hostgroup.find_by_title(hostgroup)
50
- break
51
- end
52
- end
42
+ return @host, result unless new_hostgroup
53
43
 
54
- return host, result unless new_hostgroup
55
-
56
- host.hostgroup = new_hostgroup
57
- host.save(:validate => false)
44
+ @host.hostgroup = new_hostgroup
45
+ @host.save(:validate => false)
58
46
  Rails.logger.info "DefaultHostgroupMatch: #{hostname} added to #{new_hostgroup}"
59
47
 
60
- return host, result
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
61
68
  end
62
69
 
63
70
  def valid_hostgroup?(hostgroup)
@@ -1,3 +1,3 @@
1
1
  module ForemanDefaultHostgroup
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -26,7 +26,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
26
26
 
27
27
  test "full matching regex not enclosed in /" do
28
28
  setup_hostgroup_match
29
- SETTINGS[:default_hostgroup][:map] = { "Test Full" => '^sinn1636.lan$' }
29
+ SETTINGS[:default_hostgroup][:facts_map] = { "Test Full" => {'hostname' =>'^sinn1636.lan$'} }
30
30
 
31
31
  hostgroup = Hostgroup.create(:name => "Test Full")
32
32
  raw = parse_json_fixture('/facts.json')
@@ -37,7 +37,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
37
37
 
38
38
  test "partial matching regex enclosed in /" do
39
39
  setup_hostgroup_match
40
- SETTINGS[:default_hostgroup][:map] = { "Test Partial" => '/\.lan$/' }
40
+ SETTINGS[:default_hostgroup][:facts_map] = { "Test Partial" => {'hostname' => '/\.lan$/' }}
41
41
 
42
42
  hostgroup = Hostgroup.create(:name => "Test Partial")
43
43
  raw = parse_json_fixture('/facts.json')
@@ -46,9 +46,9 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
46
46
  assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
47
47
  end
48
48
 
49
- test "invalid hostgroup does nothing" do
49
+ test "invalid hostgroup name is ignored" do
50
50
  setup_hostgroup_match
51
- SETTINGS[:default_hostgroup][:map] = { "Nonexistent Group" => '.*', "Existent Group" => '/\.lan$/' }
51
+ SETTINGS[:default_hostgroup][:facts_map] = { "Nonexistent Group" => {'hostname' => '.*'}, "Existent Group" => {'hostname' => '/\.lan$/'} }
52
52
 
53
53
  hostgroup = Hostgroup.create(:name => "Existent Group")
54
54
  raw = parse_json_fixture('/facts.json')
@@ -57,9 +57,42 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
57
57
  assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
58
58
  end
59
59
 
60
+ test "first matching hostgroup is used" do
61
+ setup_hostgroup_match
62
+ SETTINGS[:default_hostgroup][:facts_map] = { "First Group" => {'hostname' => '.*'}, "Second Group" => {'hostname' => '.*'} }
63
+
64
+ hostgroup = Hostgroup.create(:name => "First Group")
65
+ raw = parse_json_fixture('/facts.json')
66
+
67
+ assert Host.import_host_and_facts(raw['name'], raw['facts'])
68
+ assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
69
+ end
70
+
71
+ test "invalid keys ignored" do
72
+ setup_hostgroup_match
73
+ SETTINGS[:default_hostgroup][:facts_map] = { "First Group" => {'nosuchfact' => '.*', 'hostname' => '.*'} }
74
+
75
+ hostgroup = Hostgroup.create(:name => "First Group")
76
+ raw = parse_json_fixture('/facts.json')
77
+
78
+ assert Host.import_host_and_facts(raw['name'], raw['facts'])
79
+ assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
80
+ end
81
+
82
+ test "unmatched values ignored" do
83
+ setup_hostgroup_match
84
+ SETTINGS[:default_hostgroup][:facts_map] = { "First Group" => {'hostname' => 'nosuchname', 'osfamily' => '.*'} }
85
+
86
+ hostgroup = Hostgroup.create(:name => "First Group")
87
+ raw = parse_json_fixture('/facts.json')
88
+
89
+ assert Host.import_host_and_facts(raw['name'], raw['facts'])
90
+ assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
91
+ end
92
+
60
93
  test "default hostgroup" do
61
94
  setup_hostgroup_match
62
- SETTINGS[:default_hostgroup][:map] = { "Test Default" => '.*' }
95
+ SETTINGS[:default_hostgroup][:facts_map] = { "Test Default" => {'hostname' =>'.*'} }
63
96
 
64
97
  hostgroup = Hostgroup.create(:name => "Test Default")
65
98
  raw = parse_json_fixture('/facts.json')
@@ -70,7 +103,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
70
103
 
71
104
  test "host already has a hostgroup" do
72
105
  setup_hostgroup_match
73
- SETTINGS[:default_hostgroup][:map] = { "Test Default" => '.*' }
106
+ SETTINGS[:default_hostgroup][:facts_map] = { "Test Default" => {'hostname' => '.*'} }
74
107
 
75
108
  hostgroup = Hostgroup.create(:name => "Test Group")
76
109
  Hostgroup.create(:name => "Test Default")
@@ -88,7 +121,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
88
121
  setup_hostgroup_match
89
122
  Setting[:force_hostgroup_match] = true
90
123
  Setting[:force_hostgroup_match_only_new] = false
91
- SETTINGS[:default_hostgroup][:map] = { "Test Default" => '.*' }
124
+ SETTINGS[:default_hostgroup][:facts_map] = { "Test Default" => {'hostname' => '.*'} }
92
125
 
93
126
  hostgroup = Hostgroup.create(:name => "Test Group")
94
127
  default = Hostgroup.create(:name => "Test Default")
@@ -105,7 +138,7 @@ class DefaultHostgroupTest < ActiveSupport::TestCase
105
138
  test "hostgroup is not updated if host is not new" do
106
139
  setup_hostgroup_match
107
140
  Setting[:force_hostgroup_match] = true
108
- SETTINGS[:default_hostgroup][:map] = { "Test Default" => '.*' }
141
+ SETTINGS[:default_hostgroup][:facts_map] = { "Test Default" => {'hostname' => '.*'} }
109
142
 
110
143
  hostgroup = Hostgroup.create(:name => "Test Group")
111
144
  Hostgroup.create(:name => "Test Default")
data/test/unit/facts.json CHANGED
@@ -26,7 +26,7 @@
26
26
  "serialnumber": "abcdefg",
27
27
  "processor7": "Intel(R) Xeon(R) CPU E5620 @ 2.40GHz",
28
28
  "macaddress_vnet4": "FE:54:00:62:53:67",
29
- "hostname": "h02",
29
+ "hostname": "sinn1636.lan",
30
30
  "osfamily": "RedHat",
31
31
  "gateway_if": "br180",
32
32
  "lsbrelease": ":core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch",
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: 2.0.1
4
+ version: 2.1.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: 2014-04-29 00:00:00.000000000 Z
11
+ date: 2014-12-15 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
@@ -22,7 +22,6 @@ files:
22
22
  - LICENSE
23
23
  - README.md
24
24
  - app/models/setting/default_hostgroup.rb
25
- - config/environment.rb
26
25
  - lib/default_hostgroup.rake
27
26
  - lib/default_hostgroup_managed_host_patch.rb
28
27
  - lib/foreman_default_hostgroup.rb
@@ -41,21 +40,21 @@ require_paths:
41
40
  - lib
42
41
  required_ruby_version: !ruby/object:Gem::Requirement
43
42
  requirements:
44
- - - ! '>='
43
+ - - ">="
45
44
  - !ruby/object:Gem::Version
46
45
  version: '0'
47
46
  required_rubygems_version: !ruby/object:Gem::Requirement
48
47
  requirements:
49
- - - ! '>='
48
+ - - ">="
50
49
  - !ruby/object:Gem::Version
51
50
  version: '0'
52
51
  requirements: []
53
52
  rubyforge_project:
54
- rubygems_version: 2.2.1
53
+ rubygems_version: 2.2.2
55
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/facts.json
61
59
  - test/unit/default_hostgroup_test.rb
60
+ - test/unit/facts.json
File without changes