1
0
Fork 0
mirror of https://github.com/nishiki/is_master.git synced 2025-02-21 13:50:05 +00:00

feat: add exepected file option

This commit is contained in:
Adrien Waksberg 2017-12-15 13:40:12 +01:00
parent fb275120e4
commit ef526273a4
5 changed files with 40 additions and 13 deletions

10
CHANGELOG.md Normal file
View file

@ -0,0 +1,10 @@
# CHANGELOG
## 1.1.0 (2017-12-15)
* add expected file option
* add changelog
* fix copyright in LICENSE file
## 1.0.0 (2017-11-09)
* first version

View file

@ -1,9 +1,9 @@
# is_master
[![Version](https://img.shields.io/badge/latest_version-1.0.0-green.svg)](https://github.com/nishiki/is_master/releases)
[![Version](https://img.shields.io/badge/latest_version-1.1.0-green.svg)](https://github.com/nishiki/is_master/releases)
[![Build Status](https://travis-ci.org/nishiki/is_master.svg?branch=master)](https://travis-ci.org/nishiki/is_master)
[![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/nishiki/is_master/blob/master/LICENSE)
is_master execute a command when it determine that server is master
is_master execute a command when on the machine there is the expected vip or file.
## Install
@ -19,6 +19,10 @@ It's simple:
```
is_master 10.0.254.2 ls -l
```
or
```
is_master /path/to/file ls -l
```
## Development
### Test on local machine with docker

View file

@ -1 +1 @@
1.0.0
1.1.0

View file

@ -20,16 +20,17 @@ require 'socket'
if ARGV.length < 2
puts 'is_master execute a command when it determine that server is master'
puts "Usage: #{$PROGRAM_NAME} VIP COMMAND"
puts ' VIP - IP address expected to be master'
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 master'
exit 2
end
vip = ARGV[0]
command = ARGV[1..-1].join(' ')
expected = ARGV[0]
command = ARGV[1..-1].join(' ')
if Socket.ip_address_list.map(&:ip_address).include?(vip)
if Socket.ip_address_list.map(&:ip_address).include?(expected) || File.exist?(expected)
Kernel.exec(command)
else
puts 'OK - I am slave, i going to sleep'

View file

@ -1,6 +1,14 @@
require 'test/unit'
class TestCli < Test::Unit::TestCase
def test_missing_arg
output = %x(is_master 127.0.0.1)
assert_match('Usage: ', output)
output = %x(is_master)
assert_match('Usage: ', output)
end
def test_slave_mode
output = %x(is_master 10.255.255.0 echo 'GOOD!')
assert_match('I am slave', output)
@ -11,11 +19,15 @@ class TestCli < Test::Unit::TestCase
assert_match('GOOD!', output)
end
def test_missing_arg
output = %x(is_master 127.0.0.1)
assert_match('Usage: ', output)
def test_file_slave_mode
output = %x(is_master /tmp/test.txt echo 'GOOD!')
assert_match('I am slave', output)
end
output = %x(is_master)
assert_match('Usage: ', output)
def test_file_master_mode
File.write('/tmp/test.txt', '')
output = %x(is_master /tmp/test.txt echo 'GOOD!')
assert_match('GOOD!', output)
File.unlink('/tmp/test.txt')
end
end