Skip to content

Commit

Permalink
boot2docker ❤️ Pow
Browse files Browse the repository at this point in the history
  • Loading branch information
DVG committed Apr 24, 2015
1 parent cbfd7a7 commit b15dfaa
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
81 changes: 81 additions & 0 deletions bin/boot2docker-depowify
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

set -u

POW_HOST_ROOT=${POW_HOST_ROOT:-$HOME/Library/Application Support/Pow/Hosts}

for_each() {
while read line; do
[[ -n "$line" ]] || continue
"$@" $line
done
}

rm_proxy_conf() {
local port=$1
local hostname=$2
local conf="$POW_HOST_ROOT/$hostname"
if [[ -e "$conf" && $(cat "$conf") = $port ]]; then
rm "$conf"
echo "Removed $conf"
fi
}

stop_port_forwarding() {
local port=$1
pgrep boot2docker | while read pid; do
local forwarder="boot2docker ssh -N -L $port:0.0.0.0:$port"
if [[ $(ps -o command= $pid) = "$forwarder" ]]; then
# Kill ssh spawned by boot2docker ssh.
# Doing so will terminate boot2docker ssh itself.
kill_with_ppid $pid
echo "Stopped forwarding port $port"
fi
done
}

kill_with_ppid() {
local ppid=$1
ps -o 'ppid=,pid=' | for_each kill_with_ppid_ $ppid
}

kill_with_ppid_() {
local expected_ppid=$1
local ppid=$2
local pid=$3
if [[ $ppid = $expected_ppid ]]; then
kill $pid
echo "Killed ssh with pid $pid"
fi
}

teardown_domain() {
local port=$1
local name=$2
local hostname=$(sed 's/_/-/g' <<< $name)
rm_proxy_conf $port $hostname
stop_port_forwarding $port
echo "======> http://$hostname.dev has gone"
}

to_port_and_name_list() {
awk 'NR>1 { print $(NF-1) " " $NF }' | grep -e '->' | sed 's/^.*://; s/->.* / /'
}

filter_by_name() {
local target_names=$*
if [[ -z "$target_names" ]]; then
cat
return
fi
while read line; do
local name=$(cut -d' ' -f2 <<< $line)
if grep -q " $name " <<< " $target_names "; then
echo $line
fi
done
}

target_names=$*
list=$(docker ps --no-trunc=true | to_port_and_name_list | filter_by_name "$target_names")
for_each teardown_domain <<< "$list"
66 changes: 66 additions & 0 deletions bin/boot2docker-powify
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

set -u

POW_HOST_ROOT=${POW_HOST_ROOT:-$HOME/Library/Application Support/Pow/Hosts}

for_each() {
while read line; do
[[ -n "$line" ]] || continue
"$@" $line
done
}

make_proxy_conf() {
local port=$1
local hostname=$2
local conf="$POW_HOST_ROOT/$hostname"
echo $port > "$conf"
echo "Created $conf"
}

start_port_forwarding() {
local port=$1
local forwarder="boot2docker ssh -N -L $port:0.0.0.0:$port"
local forwarder_exists=
pgrep boot2docker | while read pid; do
if [[ $(ps -o command= $pid) = "$forwarder" ]]; then
echo "Port $port is already forwarded"
return 99
fi
done
[[ $? = 99 ]] && return
$forwarder &
echo "Started forwarding port $port"
}

setup_domain() {
local port=$1
local name=$2
local hostname=$(sed 's/_/-/g' <<< $name)
make_proxy_conf $port $hostname
start_port_forwarding $port
echo "======> http://$hostname.dev is live"
}

to_port_and_name_list() {
awk 'NR>1 { print $(NF-1) " " $NF }' | grep -e '->' | sed 's/^.*://; s/->.* / /'
}

filter_by_name() {
local target_names=$*
if [[ -z "$target_names" ]]; then
cat
return
fi
while read line; do
local name=$(cut -d' ' -f2 <<< $line)
if grep -q " $name " <<< " $target_names "; then
echo $line
fi
done
}

target_names=$*
list=$(docker ps --no-trunc=true | to_port_and_name_list | filter_by_name "$target_names")
for_each setup_domain <<< "$list"

0 comments on commit b15dfaa

Please sign in to comment.