Created
April 1, 2016 15:44
-
-
Save dosaboy/b32cbdd08a8e4a238c64ade1efba4c22 to your computer and use it in GitHub Desktop.
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
#!/bin/bash -eux | |
PREFIX=10.6.0.0 | |
ROUTER= | |
NUMNETS=1 # number if networks/interfaces to add per unit | |
while (($#)); do | |
case "$1" in | |
-s|--service) | |
SERVICE="$2" | |
shift | |
;; | |
-n|--numnets) | |
NUMNETS=$2 | |
shift | |
;; | |
--prefix16) | |
PREFIX=$2 | |
shift | |
;; | |
-r|--router) | |
ROUTER=$2 | |
shift | |
;; | |
*) | |
echo "USAGE: -s <service> -n <numnets> --prefix16 a.b.c.d" | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
# No nulls | |
[ -n "$SERVICE" ] && [ -n "$NUMNETS" ] && [ -n "$PREFIX" ] | |
addrs () | |
{ | |
set -eu | |
cat <<- EOF| python - | |
import json, subprocess | |
data = subprocess.check_output(['juju', 'status', '--format=json']) | |
j = json.loads(data) | |
services = j['services'] | |
for unit in services["$1"]['units'].itervalues(): | |
print unit['public-address'] | |
EOF | |
} | |
host_to_machineid () | |
{ | |
set -eu | |
local service=$1 | |
local host=$2 | |
cat <<- EOF| python - | |
import json, subprocess | |
data = subprocess.check_output(['juju', 'status', '--format=json']) | |
j = json.loads(data) | |
services = j['services'] | |
for unit in services["$1"]['units'].itervalues(): | |
if unit['public-address'] == "$2": | |
print unit['machine'] | |
break | |
EOF | |
} | |
attach_port () | |
{ | |
set -eu | |
local netname=$1 | |
local host=$2 | |
local index=$3 | |
local target=$4 | |
local gateway=$5 | |
server=$(nova list| awk "\$4==\"juju-hopem-machine-$(host_to_machineid $SERVICE $host)\" {print \$2}") | |
netid=$(neutron port-create $1| awk "\$2==\"id\" {print \$4}") | |
nova interface-attach --port-id $netid $server | |
cat <<- EOF| ssh -i ~/.juju/ssh/juju_id_rsa $host "cat -| sudo tee /etc/network/interfaces.d/eth${index}.cfg" | |
auto eth${index} | |
iface eth${index} inet dhcp | |
post-up route add -net $target gw $gateway dev eth${index} | |
EOF | |
ssh -i ~/.juju/ssh/juju_id_rsa $host "sudo ifup eth${index}" | |
} | |
source ~/novarc | |
index=1 | |
hosts=( `addrs $SERVICE` ) | |
numhosts=${#hosts[@]} | |
for ((i=1;i<=$((NUMNETS * $numhosts));i++)); do | |
nid=$(neutron net-list| awk "\$4==\"__net${i}__\" {print \$2}") | |
if [ -z "$nid" ]; then | |
nid=$(neutron net-create __net${i}__| awk "\$2==\"id\" {print \$4}") | |
fi | |
neutron net-show $nid | |
snid=$(neutron subnet-list| awk "\$4==\"__net${i}__subnet\" {print \$2}") | |
dquad=`echo $PREFIX| sed -E "s/(.+\.)[0-9]*(\..*)/\1$i\2/"` | |
if [ -z "$snid" ]; then | |
snid=$(neutron subnet-create $nid --name __net${i}__subnet $dquad/24| \ | |
awk "\$2==\"id\" {print \$4}") | |
echo "Created subnet $snid" | |
fi | |
neutron subnet-show $snid | |
if [ -n $ROUTER ]; then | |
if ! `neutron router-port-list $ROUTER| grep -q $snid`; then | |
neutron router-interface-add $ROUTER $snid | |
fi | |
fi | |
host=${hosts[$((i % numhosts))]} | |
gateway=`echo $dquad| sed -E "s/(.+\.[0-9]*)\..+/\1.1/"` | |
target=`echo $dquad| sed -E "s/(.+\.[0-9]+\.)[0-9]+\..*/\10.0\/16/"` # FIXME: support whatever --prefix16 is | |
attach_port __net${i}__ $host $index $target $gateway | |
if ! ((i % $numhosts)); then | |
((index++)) | |
fi | |
done | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment