chef_handler_foreman 0.0.8 → 0.0.9

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
  SHA1:
3
- metadata.gz: fcb9f434ee4a3880fec8c391c148cb91f540d142
4
- data.tar.gz: c9855ab7ed58b2ab39b41f0bac246bbb8c7915ad
3
+ metadata.gz: 3a662768086d4d4ce4be456b5062a0c104446499
4
+ data.tar.gz: eb8cd1d3ef5c3fd26d731f82d3ee50ad3199c9e7
5
5
  SHA512:
6
- metadata.gz: 6e10748f0e44c3a44be8bbd3146a1d5488b1d4213014479725154425e3593f40a9b90a377fadd2777808d89d30e61f9fdbb3f933333e4a57a4a00232c4cc996e
7
- data.tar.gz: 01811fde3fecd877bd719dd9bc70310e2e98023e3e4d6ac8826e2bfee4efcced03d1ad759db586a04f22b1cb7156a87fe5822c244471ea8e0d1b5b8e76d77758
6
+ metadata.gz: fbd9c7fb1931bc7cfd9c5f7b6cd4edf17b278c471e8067248482e39b14c4b9fa045d519a037db4060caa61c1b1abdc878c902bc49fc8e2283e562f3712b0bb02
7
+ data.tar.gz: b3af6bc4a822064115011e9b0d8987dac2ef2402a064d33dd0b7de10681d09a064a5f4670a2fa390cccdb43473679a13297a65313e3bc59d4e5ccfac34e861ae
data/README.md CHANGED
@@ -22,6 +22,13 @@ require 'chef_handler_foreman'
22
22
  foreman_server_options :url => 'http://your.server/foreman'
23
23
  # add following line if you want to upload node attributes (facts in Foreman language)
24
24
  foreman_facts_upload true
25
+ ## Facts whitelist / blacklisting
26
+ # add following line if you want to upload only specific node attributes - only top-level attributes
27
+ foreman_facts_whitelist ['lsb','network','cpu']
28
+ # add following line if you want to avoid uploading specific node attributes - any part from the key will do
29
+ foreman_facts_blacklist ['kernel','counters','interfaces::sit0']
30
+ # enable caching of attributes - (full) upload will be performed only if attributes changed
31
+ foreman_facts_cache_file '/var/cache/chef_foreman_cache.md5'
25
32
  # add following line if you want to upload reports
26
33
  foreman_reports_upload true
27
34
  # add following line to manage reports verbosity. Allowed values are debug, notice and error
@@ -41,3 +48,16 @@ chef. The configuration line will look like this:
41
48
  ```ruby
42
49
  foreman_reports_upload true, 2
43
50
  ```
51
+
52
+ ### Caching of facts
53
+
54
+ Note that some attributes, such as network counters or used memory, change on every chef-client run.
55
+ For caching to work, you would need to blacklist such attributes, otherwise facts will be uploaded
56
+ on every run.
57
+
58
+ ## Facts whitelisting / blacklisting
59
+
60
+ Cherry picking which facts to upload, coupled with caching, allows to scale the solution to many
61
+ thousands of nodes. Note, however, that some attributes are expected by Foreman to exist, and thus
62
+ should not be blacklisted. The whitelist and blacklist examples above include a minimal set of
63
+ attributes known to work in a large scale production environment.
@@ -12,15 +12,21 @@
12
12
  #along with this program. If not, see <http://www.gnu.org/licenses/>
13
13
 
14
14
  require 'chef/handler'
15
+ require 'digest/md5'
15
16
 
16
17
  module ChefHandlerForeman
17
18
  class ForemanFacts < Chef::Handler
18
19
  attr_accessor :uploader
20
+ attr_accessor :blacklist
21
+ attr_accessor :whitelist
22
+ attr_accessor :cache_file
23
+ attr_accessor :cache_expired
19
24
 
20
25
  def report
21
26
  send_attributes(prepare_facts)
22
27
  end
23
28
 
29
+
24
30
  private
25
31
 
26
32
  def prepare_facts
@@ -55,7 +61,10 @@ module ChefHandlerForeman
55
61
  def plain_attributes
56
62
  # chef 10 attributes can be access by to_hash directly, chef 11 uses attributes method
57
63
  attributes = node.respond_to?(:attributes) ? node.attributes : node.to_hash
58
- plainify(attributes.to_hash).flatten.inject(&:merge)
64
+ attributes = attributes.select { |key, value| @whitelist.include?(key) } if @whitelist
65
+ attrs = plainify(attributes.to_hash).flatten.inject(&:merge)
66
+ verify_checksum(attrs) if @cache_file
67
+ attrs
59
68
  end
60
69
 
61
70
  def plainify(hash, prefix = nil)
@@ -67,8 +76,11 @@ module ChefHandlerForeman
67
76
  result.push plainify(array_to_hash(value), get_key(key, prefix))
68
77
  else
69
78
  new = {}
70
- new[get_key(key, prefix)] = value
71
- result.push new
79
+ full_key = get_key(key, prefix)
80
+ if @blacklist.nil? || !@blacklist.any? { |black_key| full_key.include?(black_key) }
81
+ new[full_key] = value
82
+ result.push new
83
+ end
72
84
  end
73
85
  end
74
86
  result
@@ -85,11 +97,33 @@ module ChefHandlerForeman
85
97
  end
86
98
 
87
99
  def send_attributes(attributes)
88
- if uploader
89
- uploader.foreman_request('/api/hosts/facts', attributes, node.name)
100
+ if @cache_file and !@cache_expired
101
+ Chef::Log.info "No attributes have changed - not uploading to foreman"
90
102
  else
91
- Chef::Log.error "No uploader registered for foreman facts, skipping facts upload"
103
+ if uploader
104
+ Chef::Log.info 'Sending attributes to foreman'
105
+ Chef::Log.debug attributes.inspect
106
+ uploader.foreman_request('/api/hosts/facts', attributes, node.name)
107
+ else
108
+ Chef::Log.error "No uploader registered for foreman facts, skipping facts upload"
109
+ end
110
+ end
111
+ end
112
+
113
+ def verify_checksum(attributes)
114
+ @cache_expired = true
115
+ attrs_checksum = Digest::MD5.hexdigest(attributes.to_s)
116
+ if File.exist?(@cache_file)
117
+ contents = File.read(@cache_file)
118
+ if attrs_checksum == contents
119
+ @cache_expired = false
120
+ end
92
121
  end
122
+ File.open(@cache_file, 'w') { |f| f.write(attrs_checksum) }
123
+ rescue => e
124
+ @cache_expired = true
125
+ Chef::Log.info "unable to verify cache checksum - #{e.message}, facts will be sent"
126
+ Chef::Log.debug e.backtrace.join("\n")
93
127
  end
94
128
  end
95
129
  end
@@ -52,6 +52,24 @@ module ChefHandlerForeman
52
52
  end
53
53
  end
54
54
 
55
+ def foreman_facts_blacklist(blacklist)
56
+ if @foreman_facts_handler
57
+ @foreman_facts_handler.blacklist = blacklist
58
+ end
59
+ end
60
+
61
+ def foreman_facts_whitelist(whitelist)
62
+ if @foreman_facts_handler
63
+ @foreman_facts_handler.whitelist = whitelist
64
+ end
65
+ end
66
+
67
+ def foreman_facts_cache_file(cache_file)
68
+ if @foreman_facts_handler
69
+ @foreman_facts_handler.cache_file = cache_file
70
+ end
71
+ end
72
+
55
73
  # level can be string error notice debug
56
74
  def reports_log_level(level)
57
75
  raise ArgumentError, 'unknown level: ' + level.to_s unless %w(error notice debug).include?(level)
@@ -1,3 +1,3 @@
1
1
  module ChefHandlerForeman
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef_handler_foreman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marek Hulan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-19 00:00:00.000000000 Z
11
+ date: 2015-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -45,16 +45,16 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - lib/chef_handler_foreman/foreman_resource_reporter.rb
49
- - lib/chef_handler_foreman/foreman_reporting.rb
50
- - lib/chef_handler_foreman/foreman_hooks.rb
51
- - lib/chef_handler_foreman/foreman_facts.rb
52
- - lib/chef_handler_foreman/foreman_uploader.rb
53
- - lib/chef_handler_foreman/version.rb
54
- - lib/chef_handler_foreman.rb
55
48
  - LICENSE.txt
56
49
  - README.md
57
50
  - Rakefile
51
+ - lib/chef_handler_foreman.rb
52
+ - lib/chef_handler_foreman/foreman_facts.rb
53
+ - lib/chef_handler_foreman/foreman_hooks.rb
54
+ - lib/chef_handler_foreman/foreman_reporting.rb
55
+ - lib/chef_handler_foreman/foreman_resource_reporter.rb
56
+ - lib/chef_handler_foreman/foreman_uploader.rb
57
+ - lib/chef_handler_foreman/version.rb
58
58
  homepage: https://github.com/theforeman/chef-handler-foreman
59
59
  licenses:
60
60
  - MIT
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  version: '0'
76
76
  requirements: []
77
77
  rubyforge_project:
78
- rubygems_version: 2.0.3
78
+ rubygems_version: 2.4.6
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: This gem adds chef handlers so your chef-client can upload attributes (facts)