serf-handler 1.0.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 +7 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/serf-handler +5 -0
- data/bin/setup +8 -0
- data/lib/serf/handler.rb +101 -0
- data/lib/serf/handler/cli.rb +23 -0
- data/lib/serf/handler/event.rb +15 -0
- data/lib/serf/handler/events/describe-handler.rb +15 -0
- data/lib/serf/handler/events/df.rb +8 -0
- data/lib/serf/handler/events/git-index-deploy.rb +46 -0
- data/lib/serf/handler/events/git-index-list.rb +11 -0
- data/lib/serf/handler/events/list-handlers.rb +13 -0
- data/lib/serf/handler/events/load_average.rb +9 -0
- data/lib/serf/handler/events/mpstat.rb +9 -0
- data/lib/serf/handler/events/ping.rb +9 -0
- data/lib/serf/handler/task.rb +38 -0
- data/lib/serf/handler/tasklist.rb +8 -0
- data/lib/serf/handler/version.rb +5 -0
- data/serf-handler.gemspec +34 -0
- metadata +118 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 89e48bd05d6d46a7feeade8446bc03f7a7f2a7f456bca0496f05376bb34beefc
|
|
4
|
+
data.tar.gz: 8f86ce486ec9db1c382bccac2a6fc78656fa9a7d77e9ce7e3ab081aa0fc5219b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: '095ade60b063e1d407de476c05004957ed7a7387184ca970cba1398d24fef5af58b43ddbc8bb08dec48e81d9f8d0055cdf07b98e7beb9803203f535a1d15b07a'
|
|
7
|
+
data.tar.gz: 49164a7711e770f76335ea4941d4adf2d6a221b8343e81eeb75af4f75492e168dbf50b72fa9a92686b273ad8c9c374963b0a576f42aa0281f76fb793db08b2a2
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Kirk Haines
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Kirk Haines
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Serf::Handler
|
|
2
|
+
|
|
3
|
+
Serf (https://www.serf.io/) is a decentralized tool, by HashiCorp, for managing
|
|
4
|
+
cluster membership, failure detection, and Orchestration. One of Serf's key
|
|
5
|
+
features is the ability to issue queries and trigger events on other agents
|
|
6
|
+
in the cluster. Serf can run arbitrary handlers in order to handle these
|
|
7
|
+
queries and events.
|
|
8
|
+
|
|
9
|
+
This package encapsulates a small, simple library to facilitate writing flexible
|
|
10
|
+
Serf handlers with Ruby. Also included in the package is a set of prebuilt
|
|
11
|
+
handlers which can be used immediately in your own Serf cluster.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require 'serf/handler'
|
|
17
|
+
|
|
18
|
+
Serf::Handler.run
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
No additional code is necessary to implement a Serf handler. This basic
|
|
22
|
+
implementation is available in `bin/serf-handler.rb`.
|
|
23
|
+
|
|
24
|
+
Specific query/event handlers are implemented as Ruby classes, by `include`ing
|
|
25
|
+
`Serf::Handler` into your class. A very basic DSL is provided to aid
|
|
26
|
+
implementation.
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require 'serf/handler'
|
|
30
|
+
include Serf::Handler
|
|
31
|
+
|
|
32
|
+
describe "Provide a list of all available handlers."
|
|
33
|
+
|
|
34
|
+
on :query, 'list-handlers', 0 do |event|
|
|
35
|
+
Serf::Handler::Tasks.collect do |task|
|
|
36
|
+
"#{task.type}: #{task.name}\n"
|
|
37
|
+
end.join
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
* `describe`: Attach a human readable description to whatever event is defined next.
|
|
42
|
+
* `on`: Takes two, or, optionally, three arguments (type, name, order) to describe the task, and a block that implements the task.
|
|
43
|
+
|
|
44
|
+
When a query or an event is received by the handler, it will run any tasks with
|
|
45
|
+
a matching type that do not have a name, in an order described by sorting the
|
|
46
|
+
tasks numerically on their order value. The default value for order, if not
|
|
47
|
+
given, is 0. It will then run any named task or tasks with a matching name.
|
|
48
|
+
|
|
49
|
+
## Development
|
|
50
|
+
|
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then,
|
|
52
|
+
run `rake test` to run the tests. You can also run `bin/console` for an
|
|
53
|
+
interactive prompt that will allow you to experiment.
|
|
54
|
+
|
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
56
|
+
To release a new version, update the version number in `version.rb`, and then
|
|
57
|
+
run `bundle exec rake release`, which will create a git tag for the version,
|
|
58
|
+
push git commits and tags, and push the `.gem` file to
|
|
59
|
+
[rubygems.org](https://rubygems.org).
|
|
60
|
+
|
|
61
|
+
## Contributing
|
|
62
|
+
|
|
63
|
+
Bug reports and pull requests are welcome on GitHub at
|
|
64
|
+
https://github.com/wyhaines/serf-handler.
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
The gem is available as open source under the terms of the
|
|
69
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "serf/handler"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/serf-handler
ADDED
data/bin/setup
ADDED
data/lib/serf/handler.rb
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
require "serf/handler/version"
|
|
2
|
+
require "serf/handler/cli"
|
|
3
|
+
require "serf/handler/tasklist"
|
|
4
|
+
require "serf/handler/event"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module Serf
|
|
8
|
+
module Handler
|
|
9
|
+
|
|
10
|
+
Tasks = TaskList.new
|
|
11
|
+
|
|
12
|
+
def describe(*strings)
|
|
13
|
+
description = ''
|
|
14
|
+
strings.each do |line|
|
|
15
|
+
if description[-1] =~ /[^\s]/ && line[0] =~ /[^\s]/
|
|
16
|
+
description << " #{line}"
|
|
17
|
+
else
|
|
18
|
+
description << line
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
@_handler_description = description
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def on(*args, &block)
|
|
26
|
+
if Hash === args.first
|
|
27
|
+
type = args[:type]
|
|
28
|
+
name = args[:name]
|
|
29
|
+
order = args[:order]
|
|
30
|
+
else
|
|
31
|
+
type, name, order = *args
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
type = :event if type == :user
|
|
35
|
+
type ||= :query
|
|
36
|
+
name ||= nil
|
|
37
|
+
order ||= 0
|
|
38
|
+
|
|
39
|
+
desc = @_handler_description
|
|
40
|
+
@_handler_description = ''
|
|
41
|
+
Tasks << Task.new(type, name, order, desc, &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
class << self
|
|
45
|
+
|
|
46
|
+
def run
|
|
47
|
+
load_tasks
|
|
48
|
+
result = run_tasks
|
|
49
|
+
STDOUT.write ( result.length > 1 ? result.to_json : result.first )
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def event
|
|
53
|
+
unless @event
|
|
54
|
+
config = Serf::Handler::Cli.new
|
|
55
|
+
@event = Serf::Handler::Event.new( config )
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
@event
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def load_tasks
|
|
62
|
+
sources = []
|
|
63
|
+
sources << ENV['SERF_HANDLER_CONFIG'] if ENV['SERF_HANDLER_CONFIG']
|
|
64
|
+
sources += find_serf_handler_directories
|
|
65
|
+
source = sources.collect {|s| File.join(s, 'config.rb')}.
|
|
66
|
+
select {|s| FileTest.exist?(s)}.first
|
|
67
|
+
require File.expand_path(source) if source
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def find_serf_handler_directories
|
|
71
|
+
sources = []
|
|
72
|
+
sources << File.join(Dir.pwd,'.serf-handler')
|
|
73
|
+
sources << File.join(Dir.home, '.serf-handler') if Dir.home rescue nil
|
|
74
|
+
sources << '/etc/serf/handlers/.serf-handler'
|
|
75
|
+
sources.select {|s| FileTest.exist?(s)}
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def run_tasks
|
|
79
|
+
result = []
|
|
80
|
+
Tasks.sort.each do |task|
|
|
81
|
+
next if task.type && task.type != event.type
|
|
82
|
+
begin
|
|
83
|
+
if task.type == :event && ( task.name.to_s.empty? || task.name.to_s == event.name.to_s )
|
|
84
|
+
task.call event
|
|
85
|
+
elsif task.type == :query && ( task.name.to_s.empty? || task.name.to_s == event.name.to_s )
|
|
86
|
+
result << task.call( event )
|
|
87
|
+
end
|
|
88
|
+
rescue Exception => e
|
|
89
|
+
result << "ERROR: #{e}"
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
result
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
require 'serf/handler/events/list-handlers'
|
|
101
|
+
require 'serf/handler/events/describe-handler'
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'optparse'
|
|
2
|
+
|
|
3
|
+
module Serf
|
|
4
|
+
module Handler
|
|
5
|
+
class Cli < Hash
|
|
6
|
+
def initialize
|
|
7
|
+
OptionParser.new do |opts|
|
|
8
|
+
opts.banner = "Usage: handler [options]\n\nWhen given CLI options, payload items that are normally passed to the handler via Serf can be specified manually."
|
|
9
|
+
opts.separator ''
|
|
10
|
+
opts.on('-t','--type [TYPE]',[:query, :event],'The type of request to trigger -- a query or an event. Defaults to an event.') do |type|
|
|
11
|
+
self[:type] = type
|
|
12
|
+
end
|
|
13
|
+
opts.on('-n','--name [NAME]',String,'The name of the query or event to trigger.') do |name|
|
|
14
|
+
self[:name] = name
|
|
15
|
+
end
|
|
16
|
+
opts.on('-p','--payload [PAYLOAD]',String,'The payload to deliver to the event handler that processes the request.') do |payload|
|
|
17
|
+
self[:payload] = payload.to_s
|
|
18
|
+
end
|
|
19
|
+
end.parse!
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Serf
|
|
2
|
+
module Handler
|
|
3
|
+
class Event
|
|
4
|
+
attr_accessor :type, :name, :payload
|
|
5
|
+
|
|
6
|
+
def initialize(cli = {})
|
|
7
|
+
@type = cli[:type] || ENV['SERF_EVENT']&.to_sym || :query
|
|
8
|
+
@type = :event if @type == :user
|
|
9
|
+
@name = cli[:name] || ( @type == :query ? ENV['SERF_QUERY_NAME'] : ENV['SERF_USER_EVENT'] )
|
|
10
|
+
@payload = cli[:payload] || STDIN.read
|
|
11
|
+
@payload.strip!
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Takes the name of a handler and returns its description. If there are",
|
|
5
|
+
"multiple handlers with the same name, all matching results will be",
|
|
6
|
+
"returned."
|
|
7
|
+
|
|
8
|
+
on :query, 'describe-handler' do |event|
|
|
9
|
+
name = event.payload.strip
|
|
10
|
+
Serf::Handler::Tasks.select do |task|
|
|
11
|
+
name == task.name
|
|
12
|
+
end.collect do |task|
|
|
13
|
+
task.description
|
|
14
|
+
end.join("\n---\n")
|
|
15
|
+
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Return a comma separated table of filesystem information."
|
|
5
|
+
|
|
6
|
+
on :query, 'df' do |event|
|
|
7
|
+
`/bin/df -k`.split(/\n/).collect {|r| r.split.join(',').sub(/Mounted,on/,"Mounted on")}.join("\n")
|
|
8
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Expects a hash code in the payload which will be queried using",
|
|
5
|
+
"git-index. A 'git fetch --all && git pull' will be executed on all",
|
|
6
|
+
"matching repositories. Deployment hooks are available in order to",
|
|
7
|
+
"execute arbitrary code during the deploy process. If any of the",
|
|
8
|
+
"following files are found in the REPO root directory, they will be",
|
|
9
|
+
"executed in the order described by their name.\n",
|
|
10
|
+
".serf-before-deploy\n",
|
|
11
|
+
".serf-after-deploy\n",
|
|
12
|
+
".serf-on-deploy-failure\n",
|
|
13
|
+
".serf-on-deploy-success\n"
|
|
14
|
+
|
|
15
|
+
on :event, 'git-index-deploy' do |event|
|
|
16
|
+
user = `whoami` # Serf's executable environments are stripped of even basic information like HOME
|
|
17
|
+
dir = `eval echo "~#{user}"`.strip
|
|
18
|
+
`git-index -d #{dir}/.git-index.db -q #{event.payload}`.split(/\n/).each do |match|
|
|
19
|
+
hash,data = match.split(/:\s+/,2)
|
|
20
|
+
path,url = data.split(/\|/,2)
|
|
21
|
+
|
|
22
|
+
ENV['SERF_DEPLOY_PAYLOAD'] = event.payload
|
|
23
|
+
ENV['SERF_DEPLOY_HASH'] = hash
|
|
24
|
+
ENV['SERF_DEPLOY_PATH'] = path
|
|
25
|
+
ENV['SERF_DEPLOY_URL'] = url
|
|
26
|
+
|
|
27
|
+
success = Dir.chdir(path)
|
|
28
|
+
break unless success
|
|
29
|
+
|
|
30
|
+
if FileTest.exist?(File.join(path, ".serf-before-deploy"))
|
|
31
|
+
system(File.join(path, ".serf-before-deploy"))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
success = system("git fetch --all && git pull")
|
|
35
|
+
|
|
36
|
+
if success && FileTest.exist?(File.join(path, ".serf-on-deploy-success"))
|
|
37
|
+
system(File.join(path, ".serf-on-deploy-success"))
|
|
38
|
+
elsif !success && FileTest.exist?(File.join(path, ".serf-on-deploy-failure"))
|
|
39
|
+
system(File.join(path, ".serf-on-deploy-failure"))
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
if FileTest.exist?(File.join(path, ".serf-after-deploy"))
|
|
43
|
+
system(File.join(path, ".serf-after-deploy"))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Returns a list of all repositories that git-index knows of, if",
|
|
5
|
+
"git-index is available to execute."
|
|
6
|
+
|
|
7
|
+
on :query, 'git-index-list' do |event|
|
|
8
|
+
user = `whoami` # Serf's executable environments are stripped of even basic information like HOME
|
|
9
|
+
dir = `eval echo "~#{user}"`.strip
|
|
10
|
+
`git-index -d #{dir}/.git-index.db --list`
|
|
11
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Provide a list of all available handlers."
|
|
5
|
+
|
|
6
|
+
on :query, 'list-handlers' do |event|
|
|
7
|
+
r = ''
|
|
8
|
+
Serf::Handler::Tasks.each do |task|
|
|
9
|
+
r << "#{task.type}: #{task.name}\n"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
r
|
|
13
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Return the 1-minute, 5-minute, and 15-minute load averages as a",
|
|
5
|
+
"comma separated list of values."
|
|
6
|
+
|
|
7
|
+
on :query, 'load-average' do |event|
|
|
8
|
+
`/usr/bin/uptime`.gsub(/^.*load\s+averages:\s+/,'').split.join(',').strip
|
|
9
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Return a comma separated table of CPU performance information",
|
|
5
|
+
"collected from mpstat."
|
|
6
|
+
|
|
7
|
+
on :query, 'mpstat' do |event|
|
|
8
|
+
`/usr/bin/mpstat`.split(/\n/)[2..-1].collect {|r| r.split.join(',').sub(/,/,' ')}.join("\n")
|
|
9
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require 'serf/handler' unless Object.const_defined?(:Serf) && Serf.const_defined?(:Handler)
|
|
2
|
+
include Serf::Handler
|
|
3
|
+
|
|
4
|
+
describe "Return a hearbeat from the system with the amount of time that it",
|
|
5
|
+
"has been up."
|
|
6
|
+
|
|
7
|
+
on :query, 'ping' do |event|
|
|
8
|
+
`/usr/bin/uptime`.sub(/^.*(up[^,]+).*\n?$/,'\1')
|
|
9
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module Serf
|
|
2
|
+
module Handler
|
|
3
|
+
class Task
|
|
4
|
+
include Comparable
|
|
5
|
+
|
|
6
|
+
attr_accessor :order, :type, :name, :description, :task
|
|
7
|
+
|
|
8
|
+
def initialize(type = :query, name = nil, order = 0, description = '', &task)
|
|
9
|
+
@order = order
|
|
10
|
+
@type = type
|
|
11
|
+
@name = name
|
|
12
|
+
@description = description
|
|
13
|
+
@task = task
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def <=>(another_task)
|
|
17
|
+
if @order < another_task.order
|
|
18
|
+
-1
|
|
19
|
+
elsif @order > another_task.order
|
|
20
|
+
1
|
|
21
|
+
else
|
|
22
|
+
if @task.to_s < another_task.task.to_s
|
|
23
|
+
-1
|
|
24
|
+
elsif @task.to_s > another_task.task.to_s
|
|
25
|
+
1
|
|
26
|
+
else
|
|
27
|
+
0
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def call(*args)
|
|
33
|
+
@task.call(*args) if @task
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "serf/handler/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "serf-handler"
|
|
8
|
+
spec.version = Serf::Handler::VERSION
|
|
9
|
+
spec.authors = ["Kirk Haines"]
|
|
10
|
+
spec.email = ["wyhaines@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{This is a framework for a modular handler for serf.}
|
|
13
|
+
spec.description = <<~EDESC
|
|
14
|
+
This is a framework for creating handlers for serf, by Hashicorp. The handlers
|
|
15
|
+
are modular, loading themselves according to a configuration file that
|
|
16
|
+
indicates what commands are supported, and where to load the code for those
|
|
17
|
+
commands. The framework will also support executing the handler on the
|
|
18
|
+
command line, with the serf payload provided on the command line. This is
|
|
19
|
+
useful for testing.
|
|
20
|
+
EDESC
|
|
21
|
+
spec.homepage = ""
|
|
22
|
+
spec.license = "MIT"
|
|
23
|
+
|
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
|
26
|
+
end
|
|
27
|
+
spec.bindir = "bin"
|
|
28
|
+
spec.executables = spec.files.grep(%r{^#{spec.bindir}/}) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
|
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.15"
|
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
34
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: serf-handler
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kirk Haines
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.15'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.15'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: minitest
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '5.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '5.0'
|
|
55
|
+
description: |
|
|
56
|
+
This is a framework for creating handlers for serf, by Hashicorp. The handlers
|
|
57
|
+
are modular, loading themselves according to a configuration file that
|
|
58
|
+
indicates what commands are supported, and where to load the code for those
|
|
59
|
+
commands. The framework will also support executing the handler on the
|
|
60
|
+
command line, with the serf payload provided on the command line. This is
|
|
61
|
+
useful for testing.
|
|
62
|
+
email:
|
|
63
|
+
- wyhaines@gmail.com
|
|
64
|
+
executables:
|
|
65
|
+
- console
|
|
66
|
+
- serf-handler
|
|
67
|
+
- setup
|
|
68
|
+
extensions: []
|
|
69
|
+
extra_rdoc_files: []
|
|
70
|
+
files:
|
|
71
|
+
- Gemfile
|
|
72
|
+
- LICENSE
|
|
73
|
+
- LICENSE.txt
|
|
74
|
+
- README.md
|
|
75
|
+
- Rakefile
|
|
76
|
+
- bin/console
|
|
77
|
+
- bin/serf-handler
|
|
78
|
+
- bin/setup
|
|
79
|
+
- lib/serf/handler.rb
|
|
80
|
+
- lib/serf/handler/cli.rb
|
|
81
|
+
- lib/serf/handler/event.rb
|
|
82
|
+
- lib/serf/handler/events/describe-handler.rb
|
|
83
|
+
- lib/serf/handler/events/df.rb
|
|
84
|
+
- lib/serf/handler/events/git-index-deploy.rb
|
|
85
|
+
- lib/serf/handler/events/git-index-list.rb
|
|
86
|
+
- lib/serf/handler/events/list-handlers.rb
|
|
87
|
+
- lib/serf/handler/events/load_average.rb
|
|
88
|
+
- lib/serf/handler/events/mpstat.rb
|
|
89
|
+
- lib/serf/handler/events/ping.rb
|
|
90
|
+
- lib/serf/handler/task.rb
|
|
91
|
+
- lib/serf/handler/tasklist.rb
|
|
92
|
+
- lib/serf/handler/version.rb
|
|
93
|
+
- serf-handler.gemspec
|
|
94
|
+
homepage: ''
|
|
95
|
+
licenses:
|
|
96
|
+
- MIT
|
|
97
|
+
metadata: {}
|
|
98
|
+
post_install_message:
|
|
99
|
+
rdoc_options: []
|
|
100
|
+
require_paths:
|
|
101
|
+
- lib
|
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - ">="
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: '0'
|
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
|
+
requirements:
|
|
109
|
+
- - ">="
|
|
110
|
+
- !ruby/object:Gem::Version
|
|
111
|
+
version: '0'
|
|
112
|
+
requirements: []
|
|
113
|
+
rubyforge_project:
|
|
114
|
+
rubygems_version: 2.7.3
|
|
115
|
+
signing_key:
|
|
116
|
+
specification_version: 4
|
|
117
|
+
summary: This is a framework for a modular handler for serf.
|
|
118
|
+
test_files: []
|