Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DOCKER_ARGS and various other shell-quoting bugs #41

Merged
merged 2 commits into from
Jun 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@
# For more details, please visit http://fnichol.github.io/dvm
#

def shq(s) # sh(1)-style quoting
sprintf("'%s'", s.gsub(/'/, "'\\\\''"))
end

ip = ENV.fetch("DOCKER_IP", "192.168.42.43")
port = ENV.fetch("DOCKER_PORT", "4243")
memory = ENV.fetch("DOCKER_MEMORY", "512")
cpus = ENV.fetch("DOCKER_CPUS", "1")
cidr = ENV.fetch("DOCKER0_CIDR", "")
args = ENV.fetch("DOCKER_ARGS", "")

unless args.empty?
args = "EXTRA_ARGS=#{args}"
end

docker0_bridge_setup = ""
bridge_utils_url = "ftp://ftp.nl.netbsd.org/vol/2/metalab/distributions/tinycorelinux/4.x/x86/tcz/bridge-utils.tcz"
unless cidr.empty?
if args.empty?
args = "EXTRA_ARGS='--bip=#{cidr}'"
else
args += " --bip=#{cidr}"
end
args += " --bip=#{cidr}"

as_docker_usr = 'su - docker -c'
dl_dir = '/home/docker'
filename = 'bridge-utils.tcz'
dl_br_utils = "wget -P #{dl_dir} -O #{filename} #{bridge_utils_url}"
dl_br_utils = "wget -P #{dl_dir} -O #{filename} #{shq bridge_utils_url}"
install_br_utils = "tce-load -i #{dl_dir}/#{filename}"
brctl = '/usr/local/sbin/brctl'
ifcfg = '/sbin/ifconfig'
Expand All @@ -37,10 +33,10 @@ unless cidr.empty?

docker0_bridge_setup = <<-BRIDGE_SETUP
sudo $INITD stop
echo '#{as_docker_usr} "#{dl_br_utils}"'
#{as_docker_usr} "#{dl_br_utils}"
echo '#{as_docker_usr} "#{install_br_utils}"'
#{as_docker_usr} "#{install_br_utils}"
echo #{shq "#{as_docker_usr} #{shq dl_br_utils}"}
#{as_docker_usr} #{shq dl_br_utils}
echo #{shq "#{as_docker_usr} #{shq install_br_utils}"}
#{as_docker_usr} #{shq install_br_utils}
sudo #{take_docker0_down}
sudo #{delete_docker0}
BRIDGE_SETUP
Expand Down Expand Up @@ -93,7 +89,7 @@ module VagrantPlugins
pid = "/var/run/udhcpc.eth#{n[:interface]}.pid"
broadcast = (IPAddr.new(n[:ip]) | (~ IPAddr.new(n[:netmask]))).to_s
comm.sudo("#{ifc} down")
comm.sudo("if [ -f '#{pid}' ]; then kill `cat #{pid}` && rm -f '#{pid}'; fi")
comm.sudo("if [ -f #{pid} ]; then kill `cat #{pid}` && rm -f #{pid}; fi")
comm.sudo("#{ifc} #{n[:ip]} netmask #{n[:netmask]} broadcast #{broadcast}")
comm.sudo("#{ifc} up")
end
Expand Down Expand Up @@ -126,17 +122,19 @@ Vagrant.configure("2") do |config|
end
end

args = "export EXTRA_ARGS=#{shq args.strip}" unless args.empty?

config.vm.provision :shell, :inline => <<-PREPARE
INITD=/usr/local/etc/init.d/docker
#{docker0_bridge_setup}
if [ '#{port}' -ne '4243' ]; then
if [ #{port} -ne '4243' ]; then
echo "---> Configuring docker to listen on port '#{port}' and restarting"
sudo sed -i -e 's|\\(DOCKER_HOST="-H tcp://0.0.0.0:\\)4243|\\1#{port}|' $INITD
sudo $INITD restart
fi
if [ -n '#{args}' ]; then
echo "---> Configuring docker with args '#{args}' and restarting"
echo '#{args}' > /var/lib/boot2docker/profile
if [ -n #{shq args} ]; then
echo '---> Configuring docker with args "'#{shq args}'" and restarting'
echo #{shq args} > /var/lib/boot2docker/profile
sudo $INITD restart
fi
if ! grep -q '8\.8\.8\.8' /etc/resolv.conf >/dev/null; then
Expand Down
42 changes: 21 additions & 21 deletions bin/dvm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e

usage() {
printf "
Usage: $(basename $0) [-v|-h] command [<args>]
Usage: $(basename "$0") [-v|-h] command [<args>]

Options

Expand Down Expand Up @@ -39,29 +39,29 @@ fail() {
}

resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
"$(type -p greadlink readlink | head -1)" "$1"
}

abs_dirname() {
local cwd="$(pwd)"
local path="$1"
local cwd=$(pwd)
local path=$1

while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
local name=${path##*/}
path=$(resolve_link "$name" || true)
done

pwd
cd "$cwd"
}

project_path() {
local parent_path="$(dirname $(abs_dirname $0))"
local home_dvm_path="$HOME/.dvm"
local parent_path=$(dirname "$(abs_dirname "$0")")
local home_dvm_path=$HOME/.dvm

if [ -f "$VAGRANTFILE" ] ; then
echo "$(abs_dirname $VAGRANTFILE)"
echo "$(abs_dirname "$VAGRANTFILE")"
elif [ -f "$parent_path/Vagrantfile" ] ; then
echo "$parent_path"
elif [ -f "$home_dvm_path/Vagrantfile" ] ; then
Expand All @@ -73,19 +73,19 @@ project_path() {

load_conf() {
if [ -f "$project_path/dvm.conf" ] ; then
source $project_path/dvm.conf
source "$project_path/dvm.conf"
fi
}

exec_vagrant() {
load_conf

if [ -f "$VAGRANTFILE" ] ; then
export VAGRANT_VAGRANTFILE="$(basename $VAGRANTFILE)"
export VAGRANT_VAGRANTFILE=$(basename "$VAGRANTFILE")
fi

cd $project_path
exec vagrant $*
exec vagrant "$@"
}

check() {
Expand Down Expand Up @@ -128,15 +128,15 @@ case "$1" in
--version|-v) echo "$(basename $0): $DVM_VERSION";;
--help|-h|help) usage;;
check|c*) check;;
destroy|d*) shift; exec_vagrant destroy $*;;
destroy|d*) shift; exec_vagrant destroy "$@";;
env|e*) setup_env;;
halt|h*|stop|sto*) shift; exec_vagrant halt $*;;
reload|rel*) shift; exec_vagrant reload --provision $*;;
resume|res*) shift; exec_vagrant resume $*;;
ssh|ss*) shift; exec_vagrant ssh $*;;
status|stat*) shift; exec_vagrant status $*;;
suspend|su*|pause|p) shift; exec_vagrant suspend $*;;
up|u*|start|star*) shift; exec_vagrant up --provision $*;;
vagrant|v*) shift; exec_vagrant $*;;
halt|h*|stop|sto*) shift; exec_vagrant halt "$@";;
reload|rel*) shift; exec_vagrant reload --provision "$@";;
resume|res*) shift; exec_vagrant resume "$@";;
ssh|ss*) shift; exec_vagrant ssh "$@";;
status|stat*) shift; exec_vagrant status "$@";;
suspend|su*|pause|p) shift; exec_vagrant suspend "$@";;
up|u*|start|star*) shift; exec_vagrant up --provision "$@";;
vagrant|v*) shift; exec_vagrant "$@";;
*) usage; exit 1;;
esac