serf_handler 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/example/custom_query_handler.rb +3 -2
- data/lib/serf_handler.rb +21 -4
- data/serf_handler.gemspec +1 -1
- data/spec/serf_handler_spec.rb +18 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27535af1c02822c2c7522fd82505733ae6ff791f
|
4
|
+
data.tar.gz: 19564f301e7b6a89c50e48844ebf26ab11db387b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a65cf643dcf1ba871cfabebfecb284fb546345d7413d5333ea942920eac2e6db4a4eb3cef0360ffd2e4ffc0e6cc05616a11313df2c8367e67b95817964bd103
|
7
|
+
data.tar.gz: ee808177d262827a4a2d50315578bd5c99718c21cb0f66761551717b51e5e385591902b67b521e22823f7cf9c3fb25c1d7d9d282a6d5ea519c5a56c3bc7be55b
|
@@ -5,7 +5,7 @@
|
|
5
5
|
# Control services
|
6
6
|
# serf query -tag role=web status iis
|
7
7
|
# Control system shutdown or reboot
|
8
|
-
# serf
|
8
|
+
# serf query -node your_host shutdown
|
9
9
|
|
10
10
|
require 'yaml'
|
11
11
|
require 'serf_handler'
|
@@ -38,7 +38,7 @@ class CustomQueryHandler < SerfHandler
|
|
38
38
|
|
39
39
|
def execute_command(command)
|
40
40
|
result = `#{command}`
|
41
|
-
|
41
|
+
info "execute #{command} => result: #{$CHILD_STATUS}"
|
42
42
|
result_response $CHILD_STATUS unless command.downcase.include?('status')
|
43
43
|
status_response result if command.downcase.include?('status')
|
44
44
|
end
|
@@ -48,6 +48,7 @@ class CustomQueryHandler < SerfHandler
|
|
48
48
|
response('success')
|
49
49
|
else
|
50
50
|
response('fail')
|
51
|
+
error 'Failed to execute the command.'
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
data/lib/serf_handler.rb
CHANGED
@@ -9,6 +9,7 @@ class SerfHandler
|
|
9
9
|
|
10
10
|
def initialize(log_file = nil)
|
11
11
|
@logger = create_logger(log_file)
|
12
|
+
set_logging if @logger.is_a? Logger
|
12
13
|
@logger.level = Logger::INFO
|
13
14
|
@name = ENV['SERF_SELF_NAME']
|
14
15
|
@role = ENV['SERF_TAG_ROLE'] || ENV['SERF_SELF_ROLE']
|
@@ -19,6 +20,13 @@ class SerfHandler
|
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
23
|
+
def set_logging
|
24
|
+
@logger.formatter = proc do |sv, datetime, _pn, msg|
|
25
|
+
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}][#{sv}] #{msg}\n"
|
26
|
+
end
|
27
|
+
@logger.level = Logger::INFO
|
28
|
+
end
|
29
|
+
|
22
30
|
def create_logger(log_file)
|
23
31
|
if log_file
|
24
32
|
log_file.is_a?(String) ? Logger.new(log_file) : log_file
|
@@ -27,14 +35,23 @@ class SerfHandler
|
|
27
35
|
end
|
28
36
|
end
|
29
37
|
|
30
|
-
def
|
38
|
+
def info(msg)
|
31
39
|
@logger.info(msg)
|
32
40
|
end
|
41
|
+
alias_method :log, :info
|
42
|
+
|
43
|
+
def warn(msg)
|
44
|
+
@logger.warn(msg)
|
45
|
+
end
|
46
|
+
|
47
|
+
def error(msg)
|
48
|
+
@logger.error(msg)
|
49
|
+
end
|
33
50
|
|
34
51
|
def response(msg)
|
35
52
|
if msg.bytesize > 1024
|
36
53
|
message = 'message exceeds limit of 1024 bytes.'
|
37
|
-
|
54
|
+
warn message
|
38
55
|
puts message
|
39
56
|
else
|
40
57
|
puts msg
|
@@ -68,10 +85,10 @@ class SerfHandlerProxy < SerfHandler
|
|
68
85
|
begin
|
69
86
|
the_handler.send @event
|
70
87
|
rescue NoMethodError
|
71
|
-
|
88
|
+
warn "#{@event} event not implemented by class"
|
72
89
|
end
|
73
90
|
else
|
74
|
-
|
91
|
+
info 'no handler for role'
|
75
92
|
end
|
76
93
|
end
|
77
94
|
end
|
data/serf_handler.gemspec
CHANGED
data/spec/serf_handler_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
require 'logger'
|
3
3
|
|
4
4
|
describe 'SerfHandler' do
|
5
|
-
context 'Check initialize process' do
|
5
|
+
context 'Check initialize process & error method' do
|
6
6
|
before do
|
7
7
|
ENV['SERF_SELF_NAME'] = 'local'
|
8
8
|
ENV['SERF_SELF_ROLE'] = 'web'
|
@@ -23,6 +23,22 @@ describe 'SerfHandler' do
|
|
23
23
|
handler = SerfHandler.new
|
24
24
|
expect(handler.event).to eq('member_join')
|
25
25
|
end
|
26
|
+
|
27
|
+
it 'write error' do
|
28
|
+
logger = double('Logger')
|
29
|
+
expect(logger).to receive(:level=).with(Logger::INFO)
|
30
|
+
expect(logger).to receive(:error).with('Error occurred').and_return(true)
|
31
|
+
handler = SerfHandler.new(logger)
|
32
|
+
expect(handler.error('Error occurred')).to be true
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'alias_method' do
|
36
|
+
logger = double('Logger')
|
37
|
+
expect(logger).to receive(:level=).with(Logger::INFO)
|
38
|
+
expect(logger).to receive(:info).with('alias method test').and_return(true)
|
39
|
+
handler = SerfHandler.new(logger)
|
40
|
+
expect(handler.log('alias method test')).to be true
|
41
|
+
end
|
26
42
|
end
|
27
43
|
|
28
44
|
context 'Check user event processing' do
|
@@ -73,7 +89,7 @@ describe 'SerfHandlerProxy' do
|
|
73
89
|
it 'has no method implemented' do
|
74
90
|
logger = double('Logger')
|
75
91
|
expect(logger).to receive(:level=).with(Logger::INFO)
|
76
|
-
expect(logger).to receive(:
|
92
|
+
expect(logger).to receive(:warn).with('member_join event not implemented by class').and_return(true)
|
77
93
|
handler = SerfHandlerProxy.new(logger)
|
78
94
|
handler.register('default', SerfHandler.new)
|
79
95
|
expect(handler.run).to be true
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serf_handler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- KONNO Katsuyuki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logger
|