1
0
Fork 0
mirror of https://github.com/nishiki/is_master.git synced 2024-10-26 17:43:17 +00:00

feat: add is_slave (unlike is_master)

This commit is contained in:
Adrien Waksberg 2018-02-24 10:00:09 +01:00
parent ef526273a4
commit cd9db909c7
6 changed files with 76 additions and 4 deletions

View file

@ -21,4 +21,5 @@ gem build is_master.gemspec
gem install "is_master-$(cat VERSION).gem"
rubocop
ruby ./test/test_cli.rb
ruby ./test/test_cli_master.rb
ruby ./test/test_cli_slave.rb

View file

@ -11,4 +11,5 @@ install:
- gem install is_master-$(cat VERSION).gem
script:
- rubocop
- ruby ./test/test_cli.rb
- ruby ./test/test_cli_master.rb
- ruby ./test/test_cli_slave.rb

37
bin/is_slave Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env ruby
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require 'socket'
if ARGV.length < 2
puts 'is_slave execute a command when it determine that server is slave'
puts "Usage: #{$PROGRAM_NAME} [VIP|FILE] COMMAND"
puts ' VIP - IP address expected to be master'
puts ' FILE - File expected to be master'
puts ' COMMAND - Command to execute if host is slave'
exit 2
end
expected = ARGV[0]
command = ARGV[1..-1].join(' ')
if !Socket.ip_address_list.map(&:ip_address).include?(expected) && !File.exist?(expected)
Kernel.exec(command)
else
puts 'OK - I am master, i going to sleep'
end

View file

@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
spec.license = 'Apache-2.0'
spec.files = %x(git ls-files -z).split("\x0")
spec.executables = ['is_master']
spec.executables = %w[is_master is_slave]
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

View file

@ -1,6 +1,6 @@
require 'test/unit'
class TestCli < Test::Unit::TestCase
class TestCliMaster < Test::Unit::TestCase
def test_missing_arg
output = %x(is_master 127.0.0.1)
assert_match('Usage: ', output)

33
test/test_cli_slave.rb Normal file
View file

@ -0,0 +1,33 @@
require 'test/unit'
class TestCliSlave < Test::Unit::TestCase
def test_missing_arg
output = %x(is_slave 127.0.0.1)
assert_match('Usage: ', output)
output = %x(is_slave)
assert_match('Usage: ', output)
end
def test_slave_mode
output = %x(is_slave 10.255.255.0 echo 'GOOD!')
assert_match('GOOD!', output)
end
def test_master_mode
output = %x(is_slave 127.0.0.1 echo 'GOOD!')
assert_match('I am master', output)
end
def test_file_slave_mode
output = %x(is_slave /tmp/test.txt echo 'GOOD!')
assert_match('GOOD!', output)
end
def test_file_master_mode
File.write('/tmp/test.txt', '')
output = %x(is_slave /tmp/test.txt echo 'GOOD!')
assert_match('I am master', output)
File.unlink('/tmp/test.txt')
end
end