From cd9db909c7db031cd257bce25a081606551af184 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sat, 24 Feb 2018 10:00:09 +0100 Subject: [PATCH] feat: add is_slave (unlike is_master) --- .docker-test | 3 +- .travis.yml | 3 +- bin/is_slave | 37 ++++++++++++++++++++++++ is_master.gemspec | 2 +- test/{test_cli.rb => test_cli_master.rb} | 2 +- test/test_cli_slave.rb | 33 +++++++++++++++++++++ 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100755 bin/is_slave rename test/{test_cli.rb => test_cli_master.rb} (94%) create mode 100644 test/test_cli_slave.rb diff --git a/.docker-test b/.docker-test index bcec51c..2c33944 100644 --- a/.docker-test +++ b/.docker-test @@ -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 diff --git a/.travis.yml b/.travis.yml index 1e8773f..61cea59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/bin/is_slave b/bin/is_slave new file mode 100755 index 0000000..cbb8713 --- /dev/null +++ b/bin/is_slave @@ -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 diff --git a/is_master.gemspec b/is_master.gemspec index 73dcb53..24bd4a9 100644 --- a/is_master.gemspec +++ b/is_master.gemspec @@ -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'] diff --git a/test/test_cli.rb b/test/test_cli_master.rb similarity index 94% rename from test/test_cli.rb rename to test/test_cli_master.rb index 7a55938..1b98e47 100644 --- a/test/test_cli.rb +++ b/test/test_cli_master.rb @@ -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) diff --git a/test/test_cli_slave.rb b/test/test_cli_slave.rb new file mode 100644 index 0000000..f36b127 --- /dev/null +++ b/test/test_cli_slave.rb @@ -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