chef-handler-opsmatic 0.0.7 → 0.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6bd4b0a5519d114752f04bf2228ac0726d75e39
4
- data.tar.gz: beebb4d1a59d060f61a15768940248f4835a3c19
3
+ metadata.gz: 111a2d25c05ff91ddc37660487d6cf37d1dd9ad1
4
+ data.tar.gz: d3e54a7e30a4332c724176812477bbf56f9d9740
5
5
  SHA512:
6
- metadata.gz: e9047c220213d19a83f33514e379145613472020e167c4d12ea162d417ea935621ea09fbd49b17e1e6700c7bbb9536dbaa3bd09a4c26d2bf53be3d5fc95e5fd3
7
- data.tar.gz: 12725561dac8c31e4844f6b22fe76d33d8de47dc12b7cb9f5bd2a633023868b84d4eaac784aa824643025907d00bce799525ca5a944ce25c9fd243a2d50438eb
6
+ metadata.gz: 6a3fa5c15a7292e89e242d30aa5cba0acb2e3eac471620055691d8a8d9c42e7969eaa619345c318cfe9a36cf2623342c978902d80bbef06bba2aa49101305198
7
+ data.tar.gz: c5fed53a6e356fe2d1baae07455afc214b66672e3bdc13ed42f5a933b0380c66d4fbdd21b083aa9231022dd1f3e2a151c5263b17001e0461d2204d7499bf9c6f
data/README.md CHANGED
@@ -7,6 +7,12 @@ The chef-handler-opsmatic gem is a Chef report and exception handler
7
7
  The easiest way to install this handler in your Chef environment is with the `handler`
8
8
  recipe in the [opsmatic-cookbook](https://github.com/opsmatic/opsmatic-cookbook) cookbook.
9
9
 
10
+ ## Changelog
11
+
12
+ #### 0.0.9 (2014-08-19)
13
+ * adds support for generating a list of chef managed files for the Opsmatic agent to watch for changes
14
+ * adds sending chef-handler-opsmatic version in the user-agent string
15
+
10
16
  ## Contributing
11
17
 
12
18
  1. Fork it ( https://github.com/[my-github-username]/chef-handler-opsmatic/fork )
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'chef/handler/version'
4
+ require 'chef/handler/opsmatic'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "chef-handler-opsmatic"
@@ -7,10 +7,15 @@ require 'json'
7
7
  class Chef
8
8
  class Handler
9
9
  class Opsmatic < ::Chef::Handler
10
+ VERSION = "0.0.9"
11
+
10
12
  def initialize(config = {})
11
13
  @config = config
14
+ @config[:agent_dir] ||= "/var/db/opsmatic-agent"
15
+ @watch_files = {}
12
16
  end
13
17
 
18
+ # prepares a report of the current chef run
14
19
  def report
15
20
  if @config[:integration_token].nil? || @config[:integration_token].empty?
16
21
  Chef::Log.warn("Opsmatic integraton integration_token missing, report handler disabled")
@@ -60,9 +65,48 @@ class Chef
60
65
  opsmatic_event[:data][:exception] = clean_exception
61
66
  end
62
67
 
68
+ # analyze and collect any potentially monitorable resources
69
+ collect_resources run_status.all_resources
70
+
71
+ # submit our event
63
72
  submit opsmatic_event
64
73
  end
65
74
 
75
+ # collects up details on file resources managed by chef on the host and writes
76
+ # the list to a directory for the opsmatic-agent to consume to hint at interesting
77
+ # files the agent can watch
78
+ def collect_resources(all_resources)
79
+ return unless File.directory?(@config[:agent_dir])
80
+
81
+ all_resources.each do |resource|
82
+ case resource
83
+ when Chef::Resource::CookbookFile
84
+ @watch_files[resource.path] = true
85
+ when Chef::Resource::Template
86
+ @watch_files[resource.path] = true
87
+ when Chef::Resource::RemoteFile
88
+ @watch_files[resource.path] = true
89
+ end
90
+ end
91
+
92
+ begin
93
+ data_dir = "#{@config[:agent_dir]}/external.d"
94
+ if not File.directory?(data_dir)
95
+ Dir.mkdir(data_dir)
96
+ end
97
+ File.open("#{data_dir}/chef_resources.json", "w") do |f|
98
+ watchlist = []
99
+ @watch_files.keys.each do |k|
100
+ watchlist << { "path" => k }
101
+ end
102
+ f.write({ "files" => watchlist }.to_json)
103
+ end
104
+ rescue Exception => msg
105
+ Chef::Log.warn("Unable to save opsmatic agent file watch list: #{msg}")
106
+ end
107
+ end
108
+
109
+ # submit report to the opsmatic collector
66
110
  def submit(event)
67
111
  Chef::Log.info("Posting chef run report to Opsmatic")
68
112
 
@@ -80,6 +124,7 @@ class Chef
80
124
 
81
125
  request = Net::HTTP::Post.new(url.request_uri)
82
126
  request["Content-Type"] = "application/json"
127
+ request["User-Agent"] = "Opsmatic Chef Handler #{Chef::Handler::Opsmatic::VERSION}"
83
128
  request.body = event.to_json
84
129
 
85
130
  begin
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
-
3
2
  require 'pp'
4
3
 
5
4
  describe "Chef::Handler::Opsmatic" do
@@ -7,20 +6,36 @@ describe "Chef::Handler::Opsmatic" do
7
6
  COLLECTOR_URL = "http://api.opsmatic.com/webhooks/events/chef"
8
7
  INTEGRATION_TOKEN = "xxxxx-yyyyy"
9
8
  HOSTNAME = "foo.example.com"
9
+ AGENT_DIR = "/var/tmp/opsmatic"
10
+ WATCHLIST_PATH = "external.d/chef_resources.json"
10
11
 
11
12
  @handler = Chef::Handler::Opsmatic.new(
12
13
  :integration_token => INTEGRATION_TOKEN,
13
- :collector_url => COLLECTOR_URL
14
+ :collector_url => COLLECTOR_URL,
15
+ :agent_dir => "/var/tmp/opsmatic"
14
16
  )
15
17
 
16
18
  @node = Chef::Node.build(HOSTNAME)
17
19
  @node.attributes.default[:fqdn] = HOSTNAME
20
+
21
+ unless File.directory?(AGENT_DIR)
22
+ Dir.mkdir(AGENT_DIR)
23
+ end
18
24
  end
19
25
 
20
26
  before (:each) do
21
27
  @events = Chef::EventDispatch::Dispatcher.new
22
28
  @run_context = Chef::RunContext.new(@node, {}, @events)
23
29
  @run_status = Chef::RunStatus.new(@node, @events)
30
+
31
+ # mock some resources
32
+ template = Chef::Resource::Template.new "/etc/sudoers.d/deploy_user"
33
+ cookbook = Chef::Resource::CookbookFile.new "/etc/nginx/conf.d/ssl.conf"
34
+ remote_file = Chef::Resource::RemoteFile.new "/var/db/translations.db"
35
+
36
+ @all_resources = [ template, cookbook, remote_file ]
37
+ @run_context.resource_collection.all_resources.replace(@all_resources)
38
+
24
39
  @run_status.start_clock
25
40
  @run_status.stop_clock
26
41
  @run_status.run_context = @run_context
@@ -37,4 +52,37 @@ describe "Chef::Handler::Opsmatic" do
37
52
  @handler.run_report_unsafe(@run_status)
38
53
  expect a_request(:post, COLLECTOR_URL)
39
54
  end
55
+
56
+ it "should write a list of resources for the opsmatic agent to watch" do
57
+ @handler.run_report_unsafe(@run_status)
58
+ expect a_request(:post, COLLECTOR_URL)
59
+ expect(File.exists?("#{AGENT_DIR}/#{WATCHLIST_PATH}")).to be true
60
+ end
61
+
62
+ it "should write a list of resources for the opsmatic agent to watch" do
63
+ @handler.run_report_unsafe(@run_status)
64
+ expect a_request(:post, COLLECTOR_URL)
65
+ expect(File.exists?("#{AGENT_DIR}/#{WATCHLIST_PATH}")).to be true
66
+ end
67
+
68
+ it "should be valid json containing all the resources" do
69
+ @handler.run_report_unsafe(@run_status)
70
+ expect a_request(:post, COLLECTOR_URL)
71
+
72
+ watched_files = {}
73
+ result = JSON.parse(File.read("#{AGENT_DIR}/#{WATCHLIST_PATH}"))
74
+ result["files"].each do |file|
75
+ watched_files[file["path"]] = true
76
+ end
77
+
78
+ @all_resources.each do |resource|
79
+ watched_files.delete(resource.path)
80
+ end
81
+
82
+ expect(watched_files.length).to equal(0)
83
+ end
84
+
85
+ after(:all) do
86
+ FileUtils.rmtree(AGENT_DIR)
87
+ end
40
88
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require 'chef/handler/opsmatic'
2
2
  require 'webmock/rspec'
3
+ require 'chef'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-handler-opsmatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcus Barczak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-07 00:00:00.000000000 Z
11
+ date: 2014-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,7 +94,6 @@ files:
94
94
  - Rakefile
95
95
  - chef-handler-opsmatic.gemspec
96
96
  - lib/chef/handler/opsmatic.rb
97
- - lib/chef/handler/version.rb
98
97
  - spec/opsmatic_spec.rb
99
98
  - spec/spec_helper.rb
100
99
  homepage: https://github.com/opsmatic/chef-handler-opsmatic
@@ -1,7 +0,0 @@
1
- module Chef
2
- module Handler
3
- module Opsmatic
4
- VERSION = "0.0.7"
5
- end
6
- end
7
- end