dogknife 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Gemfile +14 -0
  2. data/README.md +4 -0
  3. data/Rakefile +46 -0
  4. data/VERSION +1 -0
  5. data/bin/dogknife +87 -0
  6. metadata +121 -0
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Add dependencies required to use your gem here.
4
+ gem "chef", ">= 0.9.18"
5
+ gem "dogapi", ">= 1.3.3"
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "shoulda", ">= 0"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", "~> 1.6.4"
13
+ gem "rdoc", ">= 0"
14
+ end
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ dogknife
2
+ ========
3
+
4
+ A sweet integration between knife and datadog
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "dogknife"
18
+ gem.homepage = "http://www.datadoghq.com/"
19
+ gem.license = "Apache License 2.0"
20
+ gem.summary = %Q{Knife }
21
+ gem.description = %Q{This wrapper exports knife actions to Datadog.}
22
+ gem.email = "packages@datadoghq.com"
23
+ gem.authors = ["Alexis Le-Quoc"]
24
+ gem.bindir = "bin"
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ RDoc::Task.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "dogknife #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/dogknife ADDED
@@ -0,0 +1,87 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Author:: Alexis Le-Quoc (<alq@datadoghq.com>)
4
+ # Copyright:: Copyright (c) 2012 Datadog, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+ require 'rubygems'
20
+ require 'chef/config'
21
+ require 'chef/knife'
22
+ require 'chef/log'
23
+ require 'chef/application/knife'
24
+ require 'dogapi'
25
+
26
+ # Save Chef::Knife.run to wrap it
27
+ # There may be a much more natural way to monkey-patch methods in ruby
28
+ $real_run = Chef::Knife.method(:run)
29
+
30
+ class Chef
31
+ class Knife
32
+ def self.run(args, options={})
33
+ # Capture logging
34
+ lrd, lwr = IO.pipe
35
+ dd_logger = Logger.new(lwr)
36
+ Chef::Log.loggers << dd_logger
37
+
38
+ # Measure duration
39
+ start = Time.now.to_i
40
+ # Call run
41
+ $real_run.call(args, options)
42
+
43
+ duration = Time.now.to_i - start
44
+
45
+ # Don't capture help messages skip help
46
+ unless args.include?("help") && args.include?("--help")
47
+
48
+ # Look up some properties
49
+ who = %x[whoami].strip
50
+ if Chef::Config.has_key?("datadog_user")
51
+ who = Chef::Config.datadog_user
52
+ end
53
+
54
+ dog = nil
55
+ if Chef::Config.has_key?("datadog_api_key")
56
+ dog = Dogapi::Client.new(Chef::Config.datadog_api_key)
57
+ elsif ENV.has_key?("DATADOG_KEY")
58
+ dog = Dogapi::Client.new(ENV['DATADOG_KEY'])
59
+ else
60
+ self.msg("Missing datadog_api_key in knife configuration")
61
+ end
62
+
63
+ # Emit event to Datadog
64
+ unless dog.nil?
65
+ begin
66
+ lwr.close_write
67
+ logged = lrd.read
68
+ body = "Nothing logged"
69
+ body = "Knife logged\n@@@#{logged}@@@" if logged.length > 0
70
+ dog.emit_event(Dogapi::Event.new(body,
71
+ :msg_title => "#{who} ran: knife #{args.join(' ')} in #{duration} second(s)",
72
+ :event_type => "config_management.command",
73
+ :aggregation_key => "#{who}-knife",
74
+ :alert_type => "info",
75
+ :source_type_name => "chef",
76
+ :tags => ["knife"]
77
+ ))
78
+ rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
79
+ ui.error("Could not send data to Datadog")
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ Chef::Application::Knife.new.run
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dogknife
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexis Le-Quoc
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: &70140927832600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.9.18
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70140927832600
25
+ - !ruby/object:Gem::Dependency
26
+ name: dogapi
27
+ requirement: &70140927831760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70140927831760
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &70140927831100 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70140927831100
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &70140927830100 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70140927830100
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &70140927829300 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.6.4
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70140927829300
69
+ - !ruby/object:Gem::Dependency
70
+ name: rdoc
71
+ requirement: &70140927828720 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70140927828720
80
+ description: This wrapper exports knife actions to Datadog.
81
+ email: packages@datadoghq.com
82
+ executables:
83
+ - dogknife
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - README.md
87
+ files:
88
+ - Gemfile
89
+ - README.md
90
+ - Rakefile
91
+ - VERSION
92
+ - bin/dogknife
93
+ homepage: http://www.datadoghq.com/
94
+ licenses:
95
+ - Apache License 2.0
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ segments:
107
+ - 0
108
+ hash: 1989731841401929675
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.10
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Knife
121
+ test_files: []