Skip to content

Commit

Permalink
Ensure that we only consider ACTIVE ports.
Browse files Browse the repository at this point in the history
Also be more clever in selecting the right port when a VM has multiple IPs:
* Prefer the one in our own subnet.
* Otherwise use last in list from own VPC.
* If not in own VPC, use first with EIP.
  • Loading branch information
Kurt Garloff committed Nov 9, 2018
1 parent 78f06f8 commit 9ecbc9e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
55 changes: 47 additions & 8 deletions scp_otc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ RED="\e[0;31m"

is_uuid() { echo "$1" | grep '^[0-9a-f]\{8\}\-[0-9a-f]\{4\}\-[0-9a-f]\{4\}\-[0-9a-f]\{4\}\-[0-9a-f]\{12\}$' >/dev/null 2>&1; }
is_ip() { echo "$1" | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$' >/dev/null 2>&1; }

getVPC()
{
# By convention, VMs are normally tagged with the VPC in OTC
Expand All @@ -35,6 +36,36 @@ getVPC()
return 1
}

ipval()
{
declare -i val=0
OLDIFS="$IFS"
IFS="."
for oct in $1; do
val=$(((val<<8)+$oct))
done
IFS="$OLDIFS"
echo "$val"
}

# $1: net/pref
# $2-: IPs to test
# echos matching IP (if any)
ipmatch()
{
PREF=${1#*/}
MASK=$((0xffffffff^(0xffffffff>>$PREF)))
NET=$(ipval ${1%%/*})
NET=$((NET&MASK))
shift
#echo "$PREF $MASK $NET"
for ip in "$@"; do
#echo "Debug: ipval $ip & $MASK == $NET: $(ipval $ip): $(($(ipval $ip)&$MASK))" 1>&2
if test $(($(ipval $ip)&$MASK)) == $NET; then echo "$ip"; return 0; fi
done
return 1
}

getvm()
{
VM=$1
Expand All @@ -45,7 +76,7 @@ getvm()
fi

VMINFO=$(otc.sh vm show $VM) || { echo "No such VM \"$VM\"" 1>&2; exit 2; }
IP=$(echo "$VMINFO" | jq '.interfaceAttachments[].fixed_ips[].ip_address' | tr -d '"' | head -n1)
IPS=$(echo "$VMINFO" | jq '.interfaceAttachments[] | select(.port_state == "ACTIVE") | .fixed_ips[].ip_address' | tr -d '"')
NAME=$(echo "$VMINFO" | jq '.server.name' | tr -d '"')
FLAVOR=$(echo "$VMINFO" | jq '.server.flavor.id' | tr -d '"')
IMGID=$(echo "$VMINFO" | jq '.server.image.id' | tr -d '"')
Expand All @@ -60,17 +91,25 @@ getvm()
OSVER=$(echo "$IMGINFO" | jq '.__os_version' | tr -d '"')
fi
if [[ "$OSVER" = "Ubuntu"* ]] && [ "$USER" == "linux" ]; then USER=ubuntu; fi
echo -e "${YELLOW}#VM Info: $VM $NAME $FLAVOR $IMGNAME $OSVER${NORM}" 1>%2
MYSUB=$(ip route show | grep '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/[0-9]* '); MYSUB=${MYSUB%% *}
#echo "$Debug: ipmatch $MYSUB" $IPS
IP=$(ipmatch $MYSUB $IPS)
echo -e "${YELLOW}#VM Info: $VM $NAME $FLAVOR $IMGNAME $OSVER${NORM}" $IPS 1>&2
# Fallback to old behavior
if test -z "$IP" -a -n "$IPS"; then IP=$(echo "$IPS" | tail -n1); fi

# Check VPC and use EIP if present and needed
MYVPC=$(otc.sh mds meta_data 2>/dev/null | jq .meta.vpc_id | tr -d '"')
if test -z "$MYVPC" -o "$MYVPC" == "null" || test "$(getVPC)" != "$MYVPC"; then
NPORT=$(echo "$VMINFO" | jq .interfaceAttachments[].port_id | head -n1 | tr -d '"')
EIP=$(otc.sh eip list | grep " $IP " | awk '{ print $2; }')
if test -n "$EIP"; then
echo "#Using EIP $EIP instead of IP $IP" 1>&2
IP=$EIP
fi
#NPORT=$(echo "$VMINFO" | jq .interfaceAttachments[].port_id | head -n1 | tr -d '"')
for ip in $IPS; do
EIP=$(otc.sh eip list | grep " $ip " | awk '{ print $2; }')
if test -n "$EIP"; then
echo "#Using EIP $EIP ($ip) instead of IP $IP" 1>&2
IP=$EIP
break
fi
done
fi
}

Expand Down
56 changes: 47 additions & 9 deletions ssh_otc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ getVPC()
return 1
}

ipval()
{
declare -i val=0
OLDIFS="$IFS"
IFS="."
for oct in $1; do
val=$(((val<<8)+$oct))
done
IFS="$OLDIFS"
echo "$val"
}

# $1: net/pref
# $2-: IPs to test
# echos matching IP (if any)
ipmatch()
{
PREF=${1#*/}
MASK=$((0xffffffff^(0xffffffff>>$PREF)))
NET=$(ipval ${1%%/*})
NET=$((NET&MASK))
shift
#echo "$PREF $MASK $NET"
for ip in "$@"; do
#echo "Debug: ipval $ip & $MASK == $NET: $(ipval $ip): $(($(ipval $ip)&$MASK))" 1>&2
if test $(($(ipval $ip)&$MASK)) == $NET; then echo "$ip"; return 0; fi
done
return 1
}

getvm()
{
VM=$1
Expand All @@ -70,7 +100,7 @@ getvm()
fi

VMINFO=$(otc.sh vm show $VM) || { echo "No such VM \"$VM\"" 1>&2; exit 2; }
IP=$(echo "$VMINFO" | jq '.interfaceAttachments[].fixed_ips[].ip_address' | tr -d '"' | head -n1)
IPS=$(echo "$VMINFO" | jq '.interfaceAttachments[] | select(.port_state == "ACTIVE") | .fixed_ips[].ip_address' | tr -d '"')
NAME=$(echo "$VMINFO" | jq '.server.name' | tr -d '"')
FLAVOR=$(echo "$VMINFO" | jq '.server.flavor.id' | tr -d '"')
IMGID=$(echo "$VMINFO" | jq '.server.image.id' | tr -d '"')
Expand All @@ -85,17 +115,25 @@ getvm()
OSVER=$(echo "$IMGINFO" | jq '.__os_version' | tr -d '"')
fi
if [[ "$OSVER" = "Ubuntu"* ]] && [ "$USER" == "linux" ]; then USER=ubuntu; fi
echo -e "${YELLOW}#VM Info: $VM $NAME $FLAVOR $IMGNAME $OSVER${NORM}" 1>&2
MYSUB=$(ip route show | grep '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/[0-9]* '); MYSUB=${MYSUB%% *}
#echo "$Debug: ipmatch $MYSUB" $IPS
IP=$(ipmatch $MYSUB $IPS)
echo -e "${YELLOW}#VM Info: $VM $NAME $FLAVOR $IMGNAME $OSVER${NORM}" $IPS 1>&2
# Fallback to old behavior
if test -z "$IP" -a -n "$IPS"; then IP=$(echo "$IPS" | tail -n1); fi

# Check VPC and use EIP if present and needed
MYVPC=$(otc.sh mds meta_data 2>/dev/null | jq .meta.vpc_id | tr -d '"')
if test -z "$MYVPC" -o "$MYVPC" == "null" || test "$(getVPC)" != "$MYVPC"; then
NPORT=$(echo "$VMINFO" | jq .interfaceAttachments[].port_id | head -n1 | tr -d '"')
EIP=$(otc.sh eip list | grep " $IP " | awk '{ print $2; }')
if test -n "$EIP"; then
echo "#Using EIP $EIP instead of IP $IP" 1>&2
IP=$EIP
fi
if test -z "$MYVPC" -o "$MYVPC" == "null" -o -z "$IP" || test "$(getVPC)" != "$MYVPC"; then
#NPORT=$(echo "$VMINFO" | jq '.interfaceAttachments[] | select(.port_state == "ACTIVE") | .port_id' | tr -d '"')
for ip in $IPS; do
EIP=$(otc.sh eip list | grep " $ip " | awk '{ print $2; }')
if test -n "$EIP"; then
echo "#Using EIP $EIP ($ip) instead of IP $IP" 1>&2
IP=$EIP
break
fi
done
fi
}

Expand Down

0 comments on commit 9ecbc9e

Please sign in to comment.