-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |