alerty 0.3.0 → 0.4.0

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: 3ae60fa6de1c6a296b2aaefc0429cf84d716a9f8
4
- data.tar.gz: b499e6edb6b41470af44ffad38c07f21ea5daff4
3
+ metadata.gz: ca4e9b30326bd03ff18ef0c79614f9134716221f
4
+ data.tar.gz: d4cbe7d427ebc8ab5a967d7dd2a3226c4b98f4d8
5
5
  SHA512:
6
- metadata.gz: f88a28b14ce75957b38708936969d8e77fa1f2a8b2789ddbb838fb23ca1af0a9efb3e0cba9b06b92a107f783908727389822c8988f036591f26dee7e03935ddf
7
- data.tar.gz: 3bd9c34db9e0140f0e096e284dea90dc23fdee11d3a43772bd0f57c363c2a4e47034d0ffeec197bd75437f97932f8bfee3bc6e0aa9584503ff01359e76140eba
6
+ metadata.gz: e8a5cb471fcd6d320d29412474bfc860e49877bbda411c7ae71f6f3c8342d8edb196da7517697bd8822edd9bfb952a7dec85bc9439433aa0148c4c3ae5569571
7
+ data.tar.gz: e8f6947ff4dad1cb48abaef96f00f11c7706d8422091ee3444d469e89fe69dbb3270b74a9dbf42942f5016eb4cc34bc73ec5eb50b2b35704a667bf090846c231
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.*
4
+ - 2.2.*
5
+ - 2.3.*
6
+ - 2.4.*
7
+ before_install:
8
+ - gem update bundler
@@ -1,3 +1,9 @@
1
+ # 0.4.0 (2017/09/29)
2
+
3
+ Enhancements:
4
+
5
+ * [experimental] send alert from stdin
6
+
1
7
  # 0.3.0 (2017/03/20)
2
8
 
3
9
  Enhancements:
data/README.md CHANGED
@@ -58,6 +58,16 @@ Usage: alerty [options] -- command
58
58
  --dotenv Load environment variables from .env file with dotenv
59
59
  ```
60
60
 
61
+ ### Experimental: Send alert from STDIN
62
+
63
+ This interface allows us to send notification even if a command does not fail.
64
+
65
+ CLI Example:
66
+
67
+ ```
68
+ $ echo 'this is a test' | alerty -c example.yml
69
+ ```
70
+
61
71
  ## Plugins
62
72
 
63
73
  Following plugins are available:
@@ -13,4 +13,23 @@ class Alerty
13
13
  logger.level = Config.log_level
14
14
  end
15
15
  end
16
+
17
+ # @param [Hash] record
18
+ # @option record [String] :hostname
19
+ # @option record [String] :command
20
+ # @option record [Integer] :exitstatus
21
+ # @option record [String] :output
22
+ # @option record [Float] :started_at unix timestamp
23
+ # @option record [Float] :duration
24
+ # @option record [Integer] :retries number of being retried
25
+ def self.send(record)
26
+ PluginFactory.plugins.each do |plugin|
27
+ begin
28
+ plugin.alert(record)
29
+ rescue => e
30
+ puts "#{e.class} #{e.message} #{e.backtrace.join("\n")}" if Config.debug?
31
+ Alerty.logger.warn "#{e.class} #{e.message} #{e.backtrace.join("\n")}"
32
+ end
33
+ end
34
+ end
16
35
  end
@@ -1,4 +1,5 @@
1
1
  require 'optparse'
2
+ require 'socket'
2
3
  require_relative '../alerty'
3
4
 
4
5
  class Alerty
@@ -55,11 +56,7 @@ class Alerty
55
56
  }
56
57
 
57
58
  op.parse!(argv)
58
- opts[:command] = argv.join(' ')
59
-
60
- if opts[:command].empty?
61
- raise OptionParser::InvalidOption.new("No command is given")
62
- end
59
+ opts[:command] = argv.join(' ') || ''
63
60
 
64
61
  opts
65
62
  end
@@ -73,8 +70,23 @@ class Alerty
73
70
 
74
71
  Config.configure(opts)
75
72
  PluginFactory.plugins # load plugins in early stage
76
- command = Command.new(command: opts[:command])
77
- command.run!
73
+
74
+ if !opts[:command].empty?
75
+ command = Command.new(command: opts[:command])
76
+ record = command.run
77
+ unless record[:exitstatus] == 0
78
+ Alerty.send(record)
79
+ exit record[:exitstatus]
80
+ end
81
+ else
82
+ begin
83
+ stdin = $stdin.read_nonblock(100 * 1024 * 1024)
84
+ record = {hostname: Socket.gethostname, output: stdin}
85
+ Alerty.send(record)
86
+ rescue IO::EAGAINWaitReadable => e
87
+ usage 'command argument or STDIN is required'
88
+ end
89
+ end
78
90
  end
79
91
  end
80
92
  end
@@ -10,7 +10,7 @@ class Alerty
10
10
  @hostname = Socket.gethostname
11
11
  end
12
12
 
13
- def run!
13
+ def run
14
14
  record = {}
15
15
  with_retry do |retries|
16
16
  started_at = Time.now
@@ -50,17 +50,7 @@ class Alerty
50
50
  Alerty.logger.info { "result: #{record.to_json}" }
51
51
  record
52
52
  end
53
- unless record[:exitstatus] == 0
54
- PluginFactory.plugins.each do |plugin|
55
- begin
56
- plugin.alert(record)
57
- rescue => e
58
- puts "#{e.class} #{e.message} #{e.backtrace.join("\n")}" if Config.debug?
59
- Alerty.logger.warn "#{e.class} #{e.message} #{e.backtrace.join("\n")}"
60
- end
61
- end
62
- exit record[:exitstatus]
63
- end
53
+ record
64
54
  end
65
55
 
66
56
  private
@@ -1,3 +1,3 @@
1
1
  class Alerty
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -7,10 +7,6 @@ describe Alerty::CLI do
7
7
  OUTPUT_FILE = File.join(File.dirname(__FILE__), 'cli_spec.out')
8
8
 
9
9
  describe '#parse_options' do
10
- it 'incorrect' do
11
- expect { Alerty::CLI.new.parse_options([]) }.to raise_error(OptionParser::InvalidOption)
12
- end
13
-
14
10
  it 'command' do
15
11
  expect(Alerty::CLI.new.parse_options(['--', 'ls', '-l'])[:command]).to eql('ls -l')
16
12
  end
@@ -21,28 +17,48 @@ describe Alerty::CLI do
21
17
  end
22
18
 
23
19
  describe '#run' do
24
- context 'with success' do
25
- before :all do
26
- FileUtils.rm(OUTPUT_FILE, force: true)
27
- system("#{File.join(BIN_DIR, 'alerty')} -c #{CONFIG_PATH} -- echo foo")
28
- sleep 0.1
20
+ context 'with command' do
21
+ context 'with success' do
22
+ before :all do
23
+ FileUtils.rm(OUTPUT_FILE, force: true)
24
+ system("#{File.join(BIN_DIR, 'alerty')} -c #{CONFIG_PATH} -- echo foo")
25
+ sleep 0.1
26
+ end
27
+
28
+ it 'should not output' do
29
+ expect(File.size?(OUTPUT_FILE)).to be_falsey
30
+ end
31
+
32
+ it { expect($?.exitstatus).to eq(0) }
29
33
  end
30
34
 
31
- it 'should not output' do
32
- expect(File.size?(OUTPUT_FILE)).to be_falsey
35
+ context 'with failure' do
36
+ before :all do
37
+ FileUtils.rm(OUTPUT_FILE, force: true)
38
+ system("#{File.join(BIN_DIR, 'alerty')} -c #{CONFIG_PATH} -- [ a = b ]")
39
+ sleep 0.1
40
+ end
41
+
42
+ it 'should output' do
43
+ expect(File.size?(OUTPUT_FILE)).to be_truthy
44
+ end
45
+
46
+ it { expect($?.exitstatus).to eq(1) }
33
47
  end
34
48
  end
35
49
 
36
- context 'with failure' do
50
+ context 'with stdin' do
37
51
  before :all do
38
52
  FileUtils.rm(OUTPUT_FILE, force: true)
39
- system("#{File.join(BIN_DIR, 'alerty')} -c #{CONFIG_PATH} -- [ a = b ]")
53
+ open("| #{File.join(BIN_DIR, 'alerty')} -c #{CONFIG_PATH}", 'w') {|f| f.puts 'test' }
40
54
  sleep 0.1
41
55
  end
42
56
 
43
57
  it 'should output' do
44
58
  expect(File.size?(OUTPUT_FILE)).to be_truthy
45
59
  end
60
+
61
+ it { expect($?.exitstatus).to eq(0) }
46
62
  end
47
63
  end
48
64
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Alerty::Command do
4
- describe 'run!' do
4
+ describe 'run' do
5
5
  context 'Frontkick.exec' do
6
6
  before do
7
7
  Alerty::Config.configure(
@@ -21,7 +21,7 @@ describe Alerty::Command do
21
21
  exclusive: '/tmp/lock',
22
22
  popen2e: true,
23
23
  }).and_return(Frontkick::Result.new(exit_code: 0))
24
- expect { command.run! }.not_to raise_error
24
+ command.run
25
25
  end
26
26
  end
27
27
 
@@ -48,8 +48,8 @@ describe Alerty::Command do
48
48
  exclusive: nil,
49
49
  popen2e: true,
50
50
  }).and_return(Frontkick::Result.new(stdout: 'foo', exit_code: 1))
51
- stdout = capture_stdout { expect { command.run! }.to raise_error(SystemExit) }
52
- expect(JSON.parse(stdout)["output"]).to eql("foo")
51
+ record = command.run
52
+ expect(record[:output]).to eql("foo")
53
53
  end
54
54
  end
55
55
 
@@ -76,8 +76,8 @@ describe Alerty::Command do
76
76
  exclusive: '/tmp/lock',
77
77
  popen2e: true,
78
78
  }).and_raise(Frontkick::Timeout.new(111, 'sleep 1', true))
79
- stdout = capture_stdout { expect { command.run! }.to raise_error(SystemExit) }
80
- expect(JSON.parse(stdout)["output"]).to include("timeout")
79
+ record = command.run
80
+ expect(record[:output]).to include("timeout")
81
81
  end
82
82
  end
83
83
 
@@ -104,8 +104,8 @@ describe Alerty::Command do
104
104
  exclusive: '/tmp/lock',
105
105
  popen2e: true,
106
106
  }).and_raise(Frontkick::Locked)
107
- stdout = capture_stdout { expect { command.run! }.to raise_error(SystemExit) }
108
- expect(JSON.parse(stdout)["output"]).to include("lock")
107
+ record = command.run
108
+ expect(record[:output]).to include("lock")
109
109
  end
110
110
  end
111
111
 
@@ -131,8 +131,8 @@ describe Alerty::Command do
131
131
  exclusive: nil,
132
132
  popen2e: true,
133
133
  }).and_return(Frontkick::Result.new(stdout: 'foo', exit_code: 1))
134
- stdout = capture_stdout { expect { command.run! }.to raise_error(SystemExit) }
135
- expect(JSON.parse(stdout)["retries"]).to eql(1)
134
+ record = command.run
135
+ expect(record[:retries]).to eql(1)
136
136
  end
137
137
  end
138
138
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alerty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-20 00:00:00.000000000 Z
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -145,6 +145,7 @@ extensions: []
145
145
  extra_rdoc_files: []
146
146
  files:
147
147
  - ".gitignore"
148
+ - ".travis.yml"
148
149
  - CHANGELOG.md
149
150
  - Gemfile
150
151
  - LICENSE
@@ -191,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
192
  version: '0'
192
193
  requirements: []
193
194
  rubyforge_project:
194
- rubygems_version: 2.5.1
195
+ rubygems_version: 2.6.13
195
196
  signing_key:
196
197
  specification_version: 4
197
198
  summary: Send an alert if a given command failed