Created
November 29, 2016 04:29
-
-
Save s1061123/8ca70d63caef0b3900f672d6114d427b to your computer and use it in GitHub Desktop.
ovs-helper // ovs helper command for OPNFV Apex
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
#!/usr/bin/env bash | |
CONFIG=${CONFIG:-'/var/opt/opnfv'} | |
RESOURCES=${RESOURCES:-"$CONFIG/images"} | |
LIB=${LIB:-"$CONFIG/lib"} | |
#VALID_CMDS="undercloud overcloud opendaylight debug-stack mock-detached -h --help" | |
VALID_CMDS="getdpid getflow -h --help" | |
source $LIB/utility-functions.sh | |
SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error) | |
resolve_cmd() { | |
local given=$1 | |
shift | |
local list=($*) | |
local inv=(${list[*]##${given}*}) | |
local OIFS=$IFS; IFS='|'; local pat="${inv[*]}"; IFS=$OIFS | |
shopt -s extglob | |
echo "${list[*]##+($pat)}" | |
shopt -u extglob | |
} | |
display_usage() { | |
echo -e "Usage:\n$0 subcommand [ arguments ]\n" | |
echo -e "Arguments:\n" | |
echo -e " getdpid Listup OpenVSwitch DPID as decimal format\n" | |
echo -e " getflow [ node ] Print OVS flowtable at given node\n" | |
} | |
get_flow() { | |
local node | |
local node_output | |
local node_ip | |
if [ -z "$1" ]; then | |
echo "Missing required argument: overcloud node to login to" | |
return 1 | |
elif ! echo "$1" | grep -E "(controller|compute)[0-9]+" > /dev/null; then | |
echo "Invalid argument: overcloud node to login to must be in the format: \ | |
controller<number> or compute<number>" | |
return 1 | |
fi | |
node_output=$(undercloud_connect "stack" "source stackrc; nova list") | |
node=$(echo "$1" | sed -E 's/([a-zA-Z]+)([0-9]+)/\1-\2/') | |
node_ip=$(echo "$node_output" | grep "$node" | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") | |
if [ "$node_ip" == "" ]; then | |
echo -e "Unable to find IP for ${node} in \n${node_output}" | |
return 1 | |
fi | |
ssh ${SSH_OPTIONS[@]} -T heat-admin@${node_ip} "sudo ovs-ofctl dump-flows br-int --protocol=OpenFlow13" | |
} | |
get_dpid() { | |
node_output=$(undercloud_connect "stack" "source stackrc; openstack server list -f csv | \ | |
awk -F, '{print \$2\"/\"\$4;}' | sed -e '1,1d' -e 's/ctlplane=//g' -e 's/\"//g'") | |
for i in $node_output; do | |
hostname=$(echo $i|awk -F/ '{print $1;}') | |
ip=$(echo $i|awk -F/ '{print $2;}') | |
ovs_ofctl_out=$(ssh ${SSH_OPTIONS[@]} -T heat-admin@${ip} "sudo ovs-ofctl show br-int --protocol=OpenFlow13") | |
ovs_dpid=$(echo $ovs_ofctl_out | perl -nlE "say if s/.*dpid:(\S+) .*/\$1/g") | |
printf "%s:%d (0x%x)\n" ${hostname} 0x${ovs_dpid} 0x${ovs_dpid} | |
done | |
} | |
##translates the command line argument | |
##params: $@ the entire command line is passed | |
##usage: parse_cmd_line() "$@" | |
parse_cmdline() { | |
local match | |
match=($(resolve_cmd $1 $VALID_CMDS)) | |
if [ ${#match[*]} -gt 1 ]; then | |
echo "$1 is ambiguous, possible matches: ${match[*]}" >&2 | |
exit 1 | |
elif [ ${#match[*]} -lt 1 ]; then | |
echo "$0 is not a recognized command. Use -h to see acceptable list" >&2 | |
exit 1 | |
else | |
match=$(echo $match | tr -d ' ') | |
fi | |
case "$match" in | |
-h|--help) | |
display_usage | |
exit 0 | |
;; | |
getdpid) | |
get_dpid | |
exit 0 | |
;; | |
getflow) | |
if [ -z "$2" ]; then | |
get_flow | |
else | |
get_flow "$2" | |
fi | |
exit 0 | |
;; | |
*) | |
echo -e "\n\nThis script is used to interact with Apex deployments\n\n" | |
echo "Use -h to display help" | |
exit 1 | |
;; | |
esac | |
} | |
main() { | |
parse_cmdline "$@" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment