papertrail 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'papertrail/cli_add_group'
3
+
4
+ begin
5
+ Papertrail::CliAddGroup.new.run
6
+ rescue Interrupt
7
+ exit(0)
8
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'papertrail/cli_add_system'
3
+
4
+ begin
5
+ Papertrail::CliAddSystem.new.run
6
+ rescue Interrupt
7
+ exit(0)
8
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'papertrail/cli_join_group'
3
+
4
+ begin
5
+ Papertrail::CliJoinGroup.new.run
6
+ rescue Interrupt
7
+ exit(0)
8
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'papertrail/cli_remove_system'
3
+
4
+ begin
5
+ Papertrail::CliRemoveSystem.new.run
6
+ rescue Interrupt
7
+ exit(0)
8
+ end
data/lib/papertrail.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Papertrail
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.3"
3
3
  end
4
4
 
5
5
  require 'papertrail/search_client'
@@ -2,9 +2,12 @@ require 'optparse'
2
2
  require 'yaml'
3
3
 
4
4
  require 'papertrail/connection'
5
+ require 'papertrail/cli_helpers'
5
6
 
6
7
  module Papertrail
7
8
  class Cli
9
+ include Papertrail::CliHelpers
10
+
8
11
  def run
9
12
  # Let it slide if we have invalid JSON
10
13
  if JSON.respond_to?(:default_options)
@@ -99,28 +102,6 @@ module Papertrail
99
102
  end
100
103
  end
101
104
 
102
- def find_configfile
103
- if File.exists?(path = File.expand_path('.papertrail.yml'))
104
- return path
105
- end
106
- if File.exists?(path = File.expand_path('~/.papertrail.yml'))
107
- return path
108
- end
109
- end
110
-
111
- def load_configfile(file_path)
112
- symbolize_keys(YAML.load_file(file_path))
113
- end
114
-
115
- def symbolize_keys(hash)
116
- new_hash = {}
117
- hash.each do |(key, value)|
118
- new_hash[(key.to_sym rescue key) || key] = value
119
- end
120
-
121
- new_hash
122
- end
123
-
124
105
  def usage
125
106
  <<-EOF
126
107
 
@@ -140,4 +121,4 @@ module Papertrail
140
121
  EOF
141
122
  end
142
123
  end
143
- end
124
+ end
@@ -0,0 +1,83 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ require 'papertrail/cli_helpers'
5
+ require 'papertrail/connection'
6
+
7
+ module Papertrail
8
+ class CliAddGroup
9
+ include Papertrail::CliHelpers
10
+
11
+ def run
12
+ # Let it slide if we have invalid JSON
13
+ if JSON.respond_to?(:default_options)
14
+ JSON.default_options[:check_utf8] = false
15
+ end
16
+
17
+ options = {
18
+ :configfile => nil,
19
+ }
20
+
21
+ if configfile = find_configfile
22
+ configfile_options = load_configfile(configfile)
23
+ options.merge!(configfile_options)
24
+ end
25
+
26
+ OptionParser.new do |opts|
27
+ opts.banner = "papertrail-add-group"
28
+
29
+ opts.on("-h", "--help", "Show usage") do |v|
30
+ puts opts
31
+ exit
32
+ end
33
+ opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
34
+ options[:configfile] = File.expand_path(v)
35
+ end
36
+ opts.on("-g", "--group SYSTEM", "Name of group to add") do |v|
37
+ options[:group] = v
38
+ end
39
+ opts.on("-w", "--system-wildcard WILDCARD", "Wildcard for system match") do |v|
40
+ options[:wildcard] = v
41
+ end
42
+
43
+ opts.separator usage
44
+ end.parse!
45
+
46
+ if options[:configfile]
47
+ configfile_options = load_configfile(options[:configfile])
48
+ options.merge!(configfile_options)
49
+ end
50
+
51
+ raise OptionParser::MissingArgument, 'group' if options[:group].nil?
52
+
53
+ connection = Papertrail::Connection.new(options)
54
+
55
+ # Bail if group already exists
56
+ if connection.show_group(options[:group])
57
+ exit 0
58
+ end
59
+
60
+ if connection.create_group(options[:group], options[:wildcard])
61
+ exit 0
62
+ end
63
+
64
+ exit 1
65
+ rescue OptionParser::ParseError => e
66
+ puts "Error: #{e}"
67
+ puts usage
68
+ exit 1
69
+ end
70
+
71
+ def usage
72
+ <<-EOF
73
+
74
+ Usage:
75
+ papertrail-add-group [-g group] [-w system-wildcard] [-c papertrail.yml]
76
+
77
+ Example:
78
+ papertrail-add-group -g mygroup -w mygroup-systems*
79
+
80
+ EOF
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,84 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ require 'papertrail/cli_helpers'
5
+ require 'papertrail/connection'
6
+
7
+ module Papertrail
8
+ class CliAddSystem
9
+ include Papertrail::CliHelpers
10
+
11
+ def run
12
+ # Let it slide if we have invalid JSON
13
+ if JSON.respond_to?(:default_options)
14
+ JSON.default_options[:check_utf8] = false
15
+ end
16
+
17
+ options = {
18
+ :configfile => nil,
19
+ }
20
+
21
+ if configfile = find_configfile
22
+ configfile_options = load_configfile(configfile)
23
+ options.merge!(configfile_options)
24
+ end
25
+
26
+ OptionParser.new do |opts|
27
+ opts.banner = "papertrail-add-system"
28
+
29
+ opts.on("-h", "--help", "Show usage") do |v|
30
+ puts opts
31
+ exit
32
+ end
33
+ opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
34
+ options[:configfile] = File.expand_path(v)
35
+ end
36
+ opts.on("-s", "--system SYSTEM", "Name of system to add") do |v|
37
+ options[:system] = v
38
+ end
39
+ opts.on("-i", "--ip-address IP_ADDRESS", "IP address of system") do |v|
40
+ options[:ip] = v
41
+ end
42
+
43
+ opts.separator usage
44
+ end.parse!
45
+
46
+ if options[:configfile]
47
+ configfile_options = load_configfile(options[:configfile])
48
+ options.merge!(configfile_options)
49
+ end
50
+
51
+ raise OptionParser::MissingArgument, 'system' if options[:system].nil?
52
+ raise OptionParser::MissingArgument, 'ip' if options[:ip].nil?
53
+
54
+ connection = Papertrail::Connection.new(options)
55
+
56
+ # Bail if system already exists
57
+ if connection.show_source(options[:system])
58
+ exit 0
59
+ end
60
+
61
+ if connection.register_source(options[:system], options[:ip])
62
+ exit 0
63
+ end
64
+
65
+ exit 1
66
+ rescue OptionParser::ParseError => e
67
+ puts "Error: #{e}"
68
+ puts usage
69
+ exit 1
70
+ end
71
+
72
+ def usage
73
+ <<-EOF
74
+
75
+ Usage:
76
+ papertrail-add-system [-s system] [-i ip-address] [-c papertrail.yml]
77
+
78
+ Example:
79
+ papertrail-add-system -s mysystemname -i 1.2.3.4
80
+
81
+ EOF
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,25 @@
1
+ module Papertrail
2
+ module CliHelpers
3
+ def find_configfile
4
+ if File.exists?(path = File.expand_path('.papertrail.yml'))
5
+ return path
6
+ end
7
+ if File.exists?(path = File.expand_path('~/.papertrail.yml'))
8
+ return path
9
+ end
10
+ end
11
+
12
+ def load_configfile(file_path)
13
+ symbolize_keys(YAML.load_file(file_path))
14
+ end
15
+
16
+ def symbolize_keys(hash)
17
+ new_hash = {}
18
+ hash.each do |(key, value)|
19
+ new_hash[(key.to_sym rescue key) || key] = value
20
+ end
21
+
22
+ new_hash
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,79 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ require 'papertrail/cli_helpers'
5
+ require 'papertrail/connection'
6
+
7
+ module Papertrail
8
+ class CliJoinGroup
9
+ include Papertrail::CliHelpers
10
+
11
+ def run
12
+ # Let it slide if we have invalid JSON
13
+ if JSON.respond_to?(:default_options)
14
+ JSON.default_options[:check_utf8] = false
15
+ end
16
+
17
+ options = {
18
+ :configfile => nil,
19
+ }
20
+
21
+ if configfile = find_configfile
22
+ configfile_options = load_configfile(configfile)
23
+ options.merge!(configfile_options)
24
+ end
25
+
26
+ OptionParser.new do |opts|
27
+ opts.banner = "papertrail-join-group"
28
+
29
+ opts.on("-h", "--help", "Show usage") do |v|
30
+ puts opts
31
+ exit
32
+ end
33
+ opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
34
+ options[:configfile] = File.expand_path(v)
35
+ end
36
+ opts.on("-s", "--system SYSTEM", "Name of system to add to group") do |v|
37
+ options[:system] = v
38
+ end
39
+ opts.on("-g", "--group GROUP", "Name of group to join") do |v|
40
+ options[:group] = v
41
+ end
42
+
43
+ opts.separator usage
44
+ end.parse!
45
+
46
+ if options[:configfile]
47
+ configfile_options = load_configfile(options[:configfile])
48
+ options.merge!(configfile_options)
49
+ end
50
+
51
+ raise OptionParser::MissingArgument, 'system' if options[:system].nil?
52
+ raise OptionParser::MissingArgument, 'group' if options[:group].nil?
53
+
54
+ connection = Papertrail::Connection.new(options)
55
+
56
+ if connection.join_group(options[:system], options[:group])
57
+ exit 0
58
+ end
59
+
60
+ exit 1
61
+ rescue OptionParser::ParseError => e
62
+ puts "Error: #{e}"
63
+ puts usage
64
+ exit 1
65
+ end
66
+
67
+ def usage
68
+ <<-EOF
69
+
70
+ Usage:
71
+ papertrail-join-group [-s system] [-g group] [-c papertrail.yml]
72
+
73
+ Example:
74
+ papertrail-join-group -s mymachine -g mygroup
75
+
76
+ EOF
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,75 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ require 'papertrail/cli_helpers'
5
+ require 'papertrail/connection'
6
+
7
+ module Papertrail
8
+ class CliRemoveSystem
9
+ include Papertrail::CliHelpers
10
+
11
+ def run
12
+ # Let it slide if we have invalid JSON
13
+ if JSON.respond_to?(:default_options)
14
+ JSON.default_options[:check_utf8] = false
15
+ end
16
+
17
+ options = {
18
+ :configfile => nil,
19
+ }
20
+
21
+ if configfile = find_configfile
22
+ configfile_options = load_configfile(configfile)
23
+ options.merge!(configfile_options)
24
+ end
25
+
26
+ OptionParser.new do |opts|
27
+ opts.banner = "papertrail-remove-system"
28
+
29
+ opts.on("-h", "--help", "Show usage") do |v|
30
+ puts opts
31
+ exit
32
+ end
33
+ opts.on("-c", "--configfile PATH", "Path to config (~/.papertrail.yml)") do |v|
34
+ options[:configfile] = File.expand_path(v)
35
+ end
36
+ opts.on("-s", "--system SYSTEM", "Name of system to remove") do |v|
37
+ options[:system] = v
38
+ end
39
+
40
+ opts.separator usage
41
+ end.parse!
42
+
43
+ if options[:configfile]
44
+ configfile_options = load_configfile(options[:configfile])
45
+ options.merge!(configfile_options)
46
+ end
47
+
48
+ raise OptionParser::MissingArgument, 'system' if options[:system].nil?
49
+
50
+ connection = Papertrail::Connection.new(options)
51
+
52
+ if connection.unregister_source(options[:system])
53
+ exit 0
54
+ end
55
+
56
+ exit 1
57
+ rescue OptionParser::ParseError => e
58
+ puts "Error: #{e}"
59
+ puts usage
60
+ exit 1
61
+ end
62
+
63
+ def usage
64
+ <<-EOF
65
+
66
+ Usage:
67
+ papertrail-remove-system [-s system] [-c papertrail.yml]
68
+
69
+ Example:
70
+ papertrail-remove-system -s mysystemname
71
+
72
+ EOF
73
+ end
74
+ end
75
+ end
@@ -1,3 +1,4 @@
1
+ require 'addressable/uri'
1
2
  require 'faraday'
2
3
  require 'openssl'
3
4
  require 'faraday_middleware'
@@ -11,7 +12,7 @@ module Papertrail
11
12
 
12
13
  attr_reader :connection
13
14
 
14
- def_delegators :@connection, :get, :put, :post
15
+ def_delegators :@connection, :get, :put, :post, :delete
15
16
 
16
17
  def initialize(options)
17
18
  ssl_options = {
@@ -26,7 +27,8 @@ module Papertrail
26
27
  ssl_options[:ca_file] = '/etc/ssl/certs/ca-certificates.crt'
27
28
  end
28
29
 
29
- @connection = Faraday::Connection.new(:url => 'https://papertrailapp.com', :ssl => ssl_options) do |builder|
30
+ @connection = Faraday::Connection.new(:url => 'https://papertrailapp.com/api/v1', :ssl => ssl_options) do |builder|
31
+ builder.use Faraday::Request::UrlEncoded
30
32
  builder.adapter Faraday.default_adapter
31
33
  builder.use Faraday::Response::RaiseError
32
34
  builder.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
@@ -40,14 +42,13 @@ module Papertrail
40
42
  end
41
43
 
42
44
  def find_id_for_source(name)
43
- response = @connection.get('/api/v1/systems.json')
45
+ response = @connection.get('systems.json')
44
46
 
45
47
  find_id_for_item(response.body, name)
46
48
  end
47
49
 
48
50
  def find_id_for_group(name)
49
- response = @connection.get('/api/v1/groups.json')
50
-
51
+ response = @connection.get('groups.json')
51
52
  find_id_for_item(response.body, name)
52
53
  end
53
54
 
@@ -59,10 +60,49 @@ module Papertrail
59
60
  items.each do |item|
60
61
  return item['id'] if item['name'] =~ /#{Regexp.escape(name_wanted)}/i
61
62
  end
63
+ return nil
64
+ end
65
+
66
+ def create_group(name, system_wildcard = nil)
67
+ group = { :group => { :name => name } }
68
+ if system_wildcard
69
+ group[:group][:system_wildcard] = system_wildcard
70
+ end
71
+ @connection.post("groups.json", group)
72
+ end
73
+
74
+ def show_group(name)
75
+ if id = find_id_for_group(name)
76
+ @connection.get("groups/#{id}.json").body
77
+ end
78
+ end
79
+
80
+ def show_source(name)
81
+ if id = find_id_for_source(name)
82
+ @connection.get("systems/#{id}.json").body
83
+ end
84
+ end
85
+
86
+ def join_group(source_name, group_name)
87
+ source_id = find_id_for_source(source_name)
88
+ group_id = find_id_for_group(group_name)
89
+ if source_id && group_id
90
+ @connection.post("systems/#{source_id}/join.json", :group_id => group_id)
91
+ end
92
+ end
93
+
94
+ def register_source(name, ip_address)
95
+ @connection.post("systems.json", :system => { :name => name, :ip_address => ip_address })
96
+ end
97
+
98
+ def unregister_source(name)
99
+ if source_id = find_id_for_source(name)
100
+ @connection.delete("systems/#{source_id}.json")
101
+ end
62
102
  end
63
103
 
64
104
  def query(query = nil, options = {})
65
105
  Papertrail::SearchQuery.new(self, query, options)
66
106
  end
67
107
  end
68
- end
108
+ end
data/papertrail.gemspec CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'papertrail'
16
- s.version = '0.9.2'
17
- s.date = '2012-05-02'
16
+ s.version = '0.9.3'
17
+ s.date = '2012-07-12'
18
18
  s.rubyforge_project = 'papertrail'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
38
38
  # s.extensions = %w[ext/extconf.rb]
39
39
 
40
40
  ## If your gem includes any executables, list them here.
41
- s.executables = ["papertrail"]
41
+ s.executables = ["papertrail", "papertrail-add-system", "papertrail-remove-system", "papertrail-add-group", "papertrail-join-group"]
42
42
  s.default_executable = 'papertrail'
43
43
 
44
44
  ## Specify any RDoc options here. You'll want to add your README and
@@ -49,6 +49,7 @@ Gem::Specification.new do |s|
49
49
 
50
50
  ## List your runtime dependencies here. Runtime dependencies are those
51
51
  ## that are needed for an end user to actually USE your code.
52
+ s.add_dependency('addressable')
52
53
  s.add_dependency('yajl-ruby')
53
54
  s.add_dependency('faraday', [ '>= 0.6', '< 0.9' ])
54
55
  s.add_dependency('faraday_middleware', [ '~> 0.8.4' ])
@@ -67,9 +68,18 @@ Gem::Specification.new do |s|
67
68
  README.md
68
69
  Rakefile
69
70
  bin/papertrail
71
+ bin/papertrail-add-group
72
+ bin/papertrail-add-system
73
+ bin/papertrail-join-group
74
+ bin/papertrail-remove-system
70
75
  examples/papertrail.yml.example
71
76
  lib/papertrail.rb
72
77
  lib/papertrail/cli.rb
78
+ lib/papertrail/cli_add_group.rb
79
+ lib/papertrail/cli_add_system.rb
80
+ lib/papertrail/cli_helpers.rb
81
+ lib/papertrail/cli_join_group.rb
82
+ lib/papertrail/cli_remove_system.rb
73
83
  lib/papertrail/connection.rb
74
84
  lib/papertrail/event.rb
75
85
  lib/papertrail/search_query.rb
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 2
9
- version: 0.9.2
8
+ - 3
9
+ version: 0.9.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Papertrail
@@ -14,10 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-05-02 00:00:00 -07:00
17
+ date: 2012-07-12 00:00:00 -07:00
18
18
  default_executable: papertrail
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
+ type: :runtime
21
22
  version_requirements: &id001 !ruby/object:Gem::Requirement
22
23
  requirements:
23
24
  - - ">="
@@ -25,12 +26,24 @@ dependencies:
25
26
  segments:
26
27
  - 0
27
28
  version: "0"
29
+ name: addressable
28
30
  requirement: *id001
29
- name: yajl-ruby
30
31
  prerelease: false
31
- type: :runtime
32
32
  - !ruby/object:Gem::Dependency
33
+ type: :runtime
33
34
  version_requirements: &id002 !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ segments:
39
+ - 0
40
+ version: "0"
41
+ name: yajl-ruby
42
+ requirement: *id002
43
+ prerelease: false
44
+ - !ruby/object:Gem::Dependency
45
+ type: :runtime
46
+ version_requirements: &id003 !ruby/object:Gem::Requirement
34
47
  requirements:
35
48
  - - ">="
36
49
  - !ruby/object:Gem::Version
@@ -44,12 +57,12 @@ dependencies:
44
57
  - 0
45
58
  - 9
46
59
  version: "0.9"
47
- requirement: *id002
48
60
  name: faraday
61
+ requirement: *id003
49
62
  prerelease: false
50
- type: :runtime
51
63
  - !ruby/object:Gem::Dependency
52
- version_requirements: &id003 !ruby/object:Gem::Requirement
64
+ type: :runtime
65
+ version_requirements: &id004 !ruby/object:Gem::Requirement
53
66
  requirements:
54
67
  - - ~>
55
68
  - !ruby/object:Gem::Version
@@ -58,14 +71,17 @@ dependencies:
58
71
  - 8
59
72
  - 4
60
73
  version: 0.8.4
61
- requirement: *id003
62
74
  name: faraday_middleware
75
+ requirement: *id004
63
76
  prerelease: false
64
- type: :runtime
65
77
  description: Command-line client for Papertrail hosted log management service. Tails and searches app server logs and system syslog. Supports Boolean search and works with grep and pipe output (Unix).
66
78
  email: troy@sevenscale.com
67
79
  executables:
68
80
  - papertrail
81
+ - papertrail-add-system
82
+ - papertrail-remove-system
83
+ - papertrail-add-group
84
+ - papertrail-join-group
69
85
  extensions: []
70
86
 
71
87
  extra_rdoc_files: []
@@ -76,9 +92,18 @@ files:
76
92
  - README.md
77
93
  - Rakefile
78
94
  - bin/papertrail
95
+ - bin/papertrail-add-group
96
+ - bin/papertrail-add-system
97
+ - bin/papertrail-join-group
98
+ - bin/papertrail-remove-system
79
99
  - examples/papertrail.yml.example
80
100
  - lib/papertrail.rb
81
101
  - lib/papertrail/cli.rb
102
+ - lib/papertrail/cli_add_group.rb
103
+ - lib/papertrail/cli_add_system.rb
104
+ - lib/papertrail/cli_helpers.rb
105
+ - lib/papertrail/cli_join_group.rb
106
+ - lib/papertrail/cli_remove_system.rb
82
107
  - lib/papertrail/connection.rb
83
108
  - lib/papertrail/event.rb
84
109
  - lib/papertrail/search_query.rb