alerty 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec90bd21ce7f831f3a1a688bc29b41eaeec57261
4
+ data.tar.gz: 0aa77f32295f188671b203cbdc797b30dcb1f707
5
+ SHA512:
6
+ metadata.gz: aa300f5950d396569873bfc8773999a6e9c0ea0d8d903458927b77701c808aa9cb6c7369e91f94c397865e97a6d93fe66db0203db8f11baaf7e8ec6188c31462
7
+ data.tar.gz: b903dacd0f3df02636e17ff068f34769a65fc76e5b7570600a49896410f655d27e30cb638db7bb186bdec8917c3c1ab330dc0dcc5c59f2ade48fbbbcff305acc
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *~
2
+ .bundle
3
+ .pryrc
4
+ .rvmrc
5
+ .rbenv-version
6
+ .ruby-version
7
+ .yardoc
8
+ .DS_Store
9
+ Gemfile.lock
10
+ coverage
11
+ doc
12
+ log
13
+ tmp
14
+ vendor
15
+ *.swp
16
+ pkg/
17
+ .env
18
+ log/alerty.log
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.0.1 (2015/08/13)
2
+
3
+ first version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Naotoshi Seo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # alerty
2
+
3
+ Send an alert if a given command failed.
4
+
5
+ ## How Useful?
6
+
7
+ I use `alerty` to run commands in cron. With `alerty`, we can send an alert if a cron command fails.
8
+
9
+ ## Installation
10
+
11
+ ```
12
+ gem install alerty
13
+ ```
14
+
15
+ ## Configuration
16
+
17
+ You can write a configuration file located at `/etc/sysconfig/alerty` (You can configure this path by `ALERTY_CONFIG_FILE` environment variable, or `-c` option):
18
+
19
+ ```
20
+ log_level: 'debug'
21
+ plugins:
22
+ - type: stdout
23
+ ```
24
+
25
+ [example/alerty.yml](./example/alerty.yml)
26
+
27
+ ### CLI Example
28
+
29
+ ```
30
+ $ alerty -c example.yml -- ls -l /something_not_exist
31
+ ```
32
+
33
+ ### CLI Help
34
+
35
+ ```
36
+ $ bin/alerty -h
37
+ -c, --config CONFIG_PATH config file path (default: /etc/sysconfig/alerty)
38
+ -t, --timeout SECONDS timeout the command (default: no timeout)
39
+ -l, --lock LOCK_FILE exclusive lock file not to run the same command duplicatedly (default: no lock)
40
+ ```
41
+
42
+ ## Plugins
43
+
44
+ Following plugins are available:
45
+
46
+ * [alerty-plugin-ikachan](https://github.com/sonots/alerty-plugin-ikachan)
47
+
48
+ ## ChangeLog
49
+
50
+ See [CHANGELOG.md](CHANGELOG.md) for details.
51
+
52
+ ### ToDo
53
+
54
+ * Add tests
55
+
56
+ ### Licenses
57
+
58
+ See [LICENSE](LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8; -*-
2
+ require "bundler/gem_tasks"
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new :spec do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+ task default: :spec
10
+ rescue LoadError, NameError
11
+ # OK, they can be absent on non-development mode.
12
+ end
13
+
14
+ desc "irb console"
15
+ task :console do
16
+ require_relative "lib/alerty"
17
+ require 'irb'
18
+ require 'irb/completion'
19
+ ARGV.clear
20
+ IRB.start
21
+ end
22
+ task :c => :console
data/alerty.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = "alerty"
3
+ gem.version = '0.0.1'
4
+ gem.author = ['Naotoshi Seo']
5
+ gem.email = ['sonots@gmail.com']
6
+ gem.homepage = 'https://github.com/sonots/alerty'
7
+ gem.summary = "Send an alert if a given command failed"
8
+ gem.description = "Send an alert if a given command failed"
9
+ gem.licenses = ['MIT']
10
+
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.require_paths = ["lib"]
15
+
16
+ gem.add_runtime_dependency 'hashie'
17
+ gem.add_runtime_dependency 'oneline_log_formatter'
18
+ gem.add_runtime_dependency 'frontkick'
19
+
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'pry'
22
+ gem.add_development_dependency 'pry-nav'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'bundler'
25
+ end
data/bin/alerty ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/alerty/cli'
4
+ Alerty.new.run
data/example.yml ADDED
@@ -0,0 +1,6 @@
1
+ log_path: log/alerty.log
2
+ log_level: debug
3
+ plugins:
4
+ - type: stdout
5
+ - type: file
6
+ path: log/failure.log
data/lib/alerty/cli.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'optparse'
2
+ require_relative '../alerty'
3
+
4
+ class Alerty
5
+ def parse_options(argv = ARGV)
6
+ op = OptionParser.new
7
+ op.banner += ' -- command'
8
+
9
+ (class<<self;self;end).module_eval do
10
+ define_method(:usage) do |msg|
11
+ puts op.to_s
12
+ puts "error: #{msg}" if msg
13
+ exit 1
14
+ end
15
+ end
16
+
17
+ opts = {
18
+ config: '/etc/sysconfig/alerty',
19
+ timeout: nil,
20
+ exclusive: nil,
21
+ }
22
+
23
+ op.on('-c', '--config CONFIG_FILE', "config file path (default: #{opts[:config]})") {|v|
24
+ opts[:config] = v
25
+ }
26
+ op.on('-t', '--timeout SECONDS', "timeout the command (default: no timeout)") {|v|
27
+ opts[:timeout] = v.to_i
28
+ }
29
+ op.on('-l', '--lock LOCK_FILE', "exclusive lock file to prevent running a command duplicatedly (default: no lock)") {|v|
30
+ opts[:exclusive] = v
31
+ }
32
+
33
+ op.parse!(argv)
34
+
35
+ opts[:command] = argv.join(' ')
36
+
37
+ if opts[:command].empty?
38
+ usage "No command is given"
39
+ end
40
+
41
+ opts
42
+ end
43
+
44
+ def run
45
+ begin
46
+ opts = parse_options
47
+ rescue OptionParser::InvalidOption => e
48
+ usage e.message
49
+ end
50
+
51
+ Config.configure(config_path: opts[:config]) if opts[:config]
52
+ Config.plugins # load plugins in early stage
53
+ Command.new(command: opts[:command], opts: opts).run!
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ require 'frontkick'
2
+
3
+ class Alerty
4
+ class Command
5
+ def initialize(command: , opts: {})
6
+ @command = command
7
+ @opts = opts
8
+ end
9
+
10
+ def run!
11
+ result = Frontkick.exec("#{@command} 2>&1", @opts)
12
+ if result.success?
13
+ exit 0
14
+ else
15
+ Config.plugins.each do |plugin|
16
+ begin
17
+ plugin.alert(result.stdout)
18
+ rescue => e
19
+ Alerty.logger.warn "#{e.class} #{e.message} #{e.backtrace.join("\n")}"
20
+ end
21
+ end
22
+ exit result.exitstatus
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ require 'yaml'
2
+ require 'hashie/mash'
3
+
4
+ class Alerty
5
+ class Config
6
+ class << self
7
+ def configure(config_path: )
8
+ @config_path = config_path
9
+ end
10
+
11
+ def config_path
12
+ @config_path ||= ENV['ALERTY_CONFIG_PATH'] || '/etc/sysconfig/alerty'
13
+ end
14
+
15
+ def config
16
+ @config ||= Hashie::Mash.new(YAML.load_file(config_path))
17
+ end
18
+
19
+ def log_path
20
+ config.log_path || 'STDOUT'
21
+ end
22
+
23
+ def log_level
24
+ config.log_level || 'warn'
25
+ end
26
+
27
+ def plugins
28
+ @plugins ||= config.fetch('plugins').map do |plugin|
29
+ require "alerty/plugin/#{plugin.type}"
30
+ class_name = "Alerty::Plugin::#{StringUtil.camelize(plugin.type)}"
31
+ Object.const_get(class_name).new(plugin)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ class Alerty
2
+ class Error < StandardError
3
+ end
4
+ class ConfigError < Error
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ require 'logger'
2
+ require 'oneline_log_formatter'
3
+
4
+ class Alerty
5
+ class Logger < ::Logger
6
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
7
+ logdev = STDOUT if logdev == 'STDOUT'
8
+ super(logdev, shift_age, shift_size)
9
+ @formatter = OnelineLogFormatter.new
10
+ end
11
+
12
+ def level=(level)
13
+ level = eval("::Logger::#{level.upcase}") if level.is_a?(String)
14
+ super(level)
15
+ end
16
+
17
+ def write(msg)
18
+ @logdev.write msg
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ class Alerty
2
+ class Plugin
3
+ class File
4
+ def initialize(config)
5
+ raise ConfigError.new('file: path is not configured') unless config.path
6
+ @path = config.path
7
+ end
8
+
9
+ def alert(msg)
10
+ ::File.open(@path, 'a') do |io|
11
+ io.puts msg
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ class Alerty
2
+ class Plugin
3
+ class Stdout
4
+ def initialize(config)
5
+ end
6
+
7
+ def alert(msg)
8
+ $stdout.puts msg
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class Alerty
2
+ class StringUtil
3
+ def self.camelize(string)
4
+ string = string.sub(/^[a-z\d]*/) { $&.capitalize }
5
+ string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }
6
+ string.gsub!(/\//, '::')
7
+ string
8
+ end
9
+ end
10
+ end
data/lib/alerty.rb ADDED
@@ -0,0 +1,13 @@
1
+ require_relative 'alerty/string_util'
2
+ require_relative 'alerty/error'
3
+ require_relative 'alerty/config'
4
+ require_relative 'alerty/command'
5
+ require_relative 'alerty/logger'
6
+
7
+ class Alerty
8
+ def self.logger
9
+ @logger ||= Logger.new(Config.log_path).tap do |logger|
10
+ logger.level = Config.log_level
11
+ end
12
+ end
13
+ end
data/log/.gitkeep ADDED
File without changes
@@ -0,0 +1,10 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+ Bundler.require :test
3
+
4
+ require 'pry'
5
+ require 'alerty'
6
+
7
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
8
+
9
+ RSpec.configure do |config|
10
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alerty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Naotoshi Seo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashie
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: oneline_log_formatter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: frontkick
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-nav
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: bundler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Send an alert if a given command failed
126
+ email:
127
+ - sonots@gmail.com
128
+ executables:
129
+ - alerty
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - CHANGELOG.md
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - alerty.gemspec
140
+ - bin/alerty
141
+ - example.yml
142
+ - lib/alerty.rb
143
+ - lib/alerty/cli.rb
144
+ - lib/alerty/command.rb
145
+ - lib/alerty/config.rb
146
+ - lib/alerty/error.rb
147
+ - lib/alerty/logger.rb
148
+ - lib/alerty/plugin/file.rb
149
+ - lib/alerty/plugin/stdout.rb
150
+ - lib/alerty/string_util.rb
151
+ - log/.gitkeep
152
+ - spec/spec_helper.rb
153
+ homepage: https://github.com/sonots/alerty
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.2.2
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Send an alert if a given command failed
177
+ test_files:
178
+ - spec/spec_helper.rb