Skip to content

Commit

Permalink
Merge branch 'master' into autodetect_config_file
Browse files Browse the repository at this point in the history
  • Loading branch information
nisanthchunduru committed Oct 11, 2015
2 parents b936f1b + ba52943 commit 3acdea1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
70 changes: 70 additions & 0 deletions contrib/completion/invoker-completion.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# BASH completion function for Invoker

# source it from bashrc
# dependencies:
# 1) netcat
# 2) find

check_open_port()
{
local port=$1
if [[ $(which nc) ]]; then
local open=$(nc -z -w2 localhost $port > /dev/null; echo $?)
if [[ "$open" == "1" ]]; then
COMPREPLY=( $(compgen -W "${port}" -- ${cur}) )
else
check_open_port $(($port+1))
fi
fi
}
_invoker()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="add add_http help list reload remove setup"
opts="$opts start stop tail uninstall version"

case "${prev}" in
add | add_http | list | reload | remove | setup | stop | tail \
| uninstall | version)
COMPREPLY=()
;;
-d | --daemon | --no-daemon)
local extra_opts=("--port")
COMPREPLY=( $(compgen -W "${extra_opts}" -- ${cur}) )
;;
--port)
# auto-suggest port
check_open_port 9000
;;
help)
# Show opts again, but only once; don't infinitely recurse
local prev2="${COMP_WORDS[COMP_CWORD-2]}"
if [ "$prev2" == "help" ]; then
COMPREPLY=()
else
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
fi
;;
start)
local filename=$(find . -type f -name "*.ini")
if [[ $filename ]]; then
COMPREPLY=( $(compgen -W "${filename}" -- ${cur}) )
else
COMPREPLY=()
fi
;;
*.ini)
local start_opts="-d --daemon --no-daemon --port"
COMPREPLY=( $(compgen -W "${start_opts}" -- ${cur}) )
;;
invoker)
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
;;
esac

return 0
}
complete -F _invoker invoker
8 changes: 7 additions & 1 deletion lib/invoker/parsers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ class Config
attr_accessor :processes, :power_config
attr_reader :filename

# initialize takes a port form cli and decrements it by 1 and sets the
# instance variable @port. This port value is used as the environment
# variable $PORT mentioned inside invoker.ini. When method pick_port gets
# fired it increments the value of port by 1, subsequently when pick_port
# again gets fired, for another command, it will again increment port
# value by 1, that way generating different ports for different commands.
def initialize(filename, port)
@filename = filename || autodetect_config_file

print_message_and_abort if invalid_config_file?

@port = port
@port = port - 1
@processes = load_config
if Invoker.can_run_balancer?
@power_config = Invoker::Power::Config.load_config()
Expand Down
3 changes: 3 additions & 0 deletions lib/invoker/power/setup/distro/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def self.distro_installer
when "Debian"
require "invoker/power/setup/distro/debian"
Debian.new
when "LinuxMint"
require "invoker/power/setup/distro/mint"
Mint.new
else
raise "Your selected distro is not supported by Invoker"
end
Expand Down
10 changes: 10 additions & 0 deletions lib/invoker/power/setup/distro/mint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "invoker/power/setup/distro/ubuntu"

module Invoker
module Power
module Distro
class Mint < Ubuntu
end
end
end
end
7 changes: 3 additions & 4 deletions lib/invoker/power/setup/distro/ubuntu.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require "invoker/power/setup/distro/debian"

module Invoker
module Power
module Distro
class Ubuntu < Base
def install_required_software
system("apt-get --assume-yes install dnsmasq rinetd")
end
class Ubuntu < Debian
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/invoker/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@
config = Invoker::Parsers::Config.new(file.path, 9000)
command1 = config.processes.first

expect(command1.port).to eq(9001)
expect(command1.cmd).to match(/9001/)
expect(command1.port).to eq(9000)
expect(command1.cmd).to match(/9000/)

command2 = config.processes[1]

expect(command2.port).to eq(9002)
expect(command2.cmd).to match(/9002/)
expect(command2.port).to eq(9001)
expect(command2.cmd).to match(/9001/)

command2 = config.processes[2]

Expand Down Expand Up @@ -118,8 +118,8 @@
config = Invoker::Parsers::Config.new(file.path, 9000)
command1 = config.processes.first

expect(command1.port).to eq(9001)
expect(command1.cmd).to match(/9001/)
expect(command1.port).to eq(9000)
expect(command1.cmd).to match(/9000/)

command2 = config.processes[1]

Expand Down Expand Up @@ -162,7 +162,7 @@
config = Invoker::Parsers::Config.new("/tmp/Procfile", 9000)
command1 = config.processes.first

expect(command1.port).to eq(9001)
expect(command1.port).to eq(9000)
expect(command1.cmd).to match(/bundle exec rails/)
ensure
File.delete("/tmp/Procfile")
Expand All @@ -183,7 +183,7 @@

expect(dns_cache.dns_data).to_not be_empty
expect(dns_cache.dns_data['web']).to_not be_empty
expect(dns_cache.dns_data['web']['port']).to eql 9001
expect(dns_cache.dns_data['web']['port']).to eql 9000
ensure
File.delete("/tmp/Procfile")
end
Expand Down

0 comments on commit 3acdea1

Please sign in to comment.