Advertisement
Skylighty

ONOS-bash

Aug 26th, 2021
1,531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.62 KB | None | 0 0
  1. # ONOS developer BASH profile conveniences
  2. # Simply include in your own .bash_aliases or .bash_profile
  3.  
  4. # Root of the ONOS source tree
  5. export ONOS_ROOT=${ONOS_ROOT:-~/onos}
  6.  
  7. # This is a hack to remove symlinks from the ONOS_ROOT path. To be removed when
  8. # the protobuf buck rules can handle symlinks
  9. ONOS_ROOT=$(pushd $ONOS_ROOT >/dev/null && pwd -P && popd >/dev/null)
  10.  
  11. # Rely on developer preference for automatically ignited apps
  12. # Default to drivers and gui2
  13. export ONOS_APPS=${ONOS_APPS:-drivers,openflow,gui2}
  14.  
  15. export MAVEN=${MAVEN:-~/Applications/apache-maven-3.3.9}
  16.  
  17. # Clean up Karaf environment variables to avoid issues when switching from ONOS 1.x to 2.x
  18. unset KARAF_VERSION KARAF_ROOT KARAF_LOG
  19.  
  20. # Setup a path
  21. export PATH="$PATH:$ONOS_ROOT/tools/dev/bin"
  22. export PATH="$PATH:$ONOS_ROOT/tools/test/bin:$ONOS_ROOT/tools/test/scenarios/bin"
  23. export RUN_PACK_PATH=${RUN_PACK_PATH:-$ONOS_ROOT/tools/package/runtime/bin}
  24. export PATH="$RUN_PACK_PATH:$PATH"
  25. export PATH="$PATH:$ONOS_ROOT/tools/build"
  26. export PATH="$PATH:$MAVEN/bin"
  27.  
  28. # Setup cell enviroment
  29. export ONOS_CELL=${ONOS_CELL:-local}
  30.  
  31. # Setup default web user/password
  32. export ONOS_WEB_USER=onos
  33. export ONOS_WEB_PASS=rocks
  34.  
  35. # Setup default location of test scenarios
  36. export ONOS_SCENARIOS=$ONOS_ROOT/tools/test/scenarios
  37.  
  38. # Setup path to Mininet custom scripts
  39. export ONOS_MN_PY=$ONOS_ROOT/tools/dev/mininet/onos.py
  40. export BMV2_MN_PY=$ONOS_ROOT/tools/dev/mininet/bmv2.py
  41.  
  42. # Convenience utility to warp to various ONOS source projects
  43. # e.g. 'o api', 'o dev', 'o'
  44. function o {
  45.     cd $(find $ONOS_ROOT/ -type d -and \( -name 'bazel-*' -o -name 'buck-out' -o -name '.git' -o -name 'target' -o -name 'gen-src' -o -name 'src' \) -prune -o -type d | \
  46.         egrep "${1:-$ONOS_ROOT}" | awk '{print length($1)"\t"$1}'  | sort -n | cut -f2 | head -n 1)
  47. }
  48.  
  49. # Short-hand for ONOS build, package and test.
  50. alias op="SHLVL=1 bazel build //:onos"
  51. alias ot="bazel run //:buildifier_check && bazel query 'tests(//...)' | grep -v "coverage" | SHLVL=1 xargs bazel test --test_summary=terse --test_output=errors"
  52. alias ob="op && ot"
  53. alias obd="SHLVL=1 bazel build //docs:external"
  54.  
  55. alias ok="SHLVL=1 bazel run //:onos-local --"
  56. alias oh="onos localhost halt"
  57.  
  58. alias ol='onos-log'
  59. alias ow='onos-watch'
  60. alias ocl='onos-check-logs'
  61. alias oi='setPrimaryInstance'
  62.  
  63. # Short-hand for tailing and searching the ONOS (karaf) log
  64. alias tl='$ONOS_ROOT/tools/dev/bin/onos-local-log'
  65.  
  66. function filterLocalLog {
  67.     tl | grep --colour=always -E -e "${1-org.onlab|org.onosproject}"
  68. }
  69. alias tlo='filterLocalLog'
  70. alias tle='tlo "ERROR|WARN|Exception|Error"'
  71.  
  72. function filterLog {
  73.     ol | grep --colour=always -E -e "${1-org.onlab|org.onosproject}"
  74. }
  75. alias olo='filterLog'
  76. alias ole='olo "ERROR|WARN|Exception|Error"'
  77.  
  78. # Pretty-print JSON output
  79. alias pp='python -m json.tool'
  80.  
  81. # Short-hand to launch Java API docs, REST API docs and ONOS GUI
  82. alias docs='open $ONOS_ROOT/docs/target/site/apidocs/index.html'
  83. alias rsdocs='onos-rsdocs'
  84. alias gui='onos-gui'
  85.  
  86. # Short-hand for 'mvn clean install' for us lazy folk
  87. alias mci='mvn clean install'
  88.  
  89. # Git annotated one-line log
  90. alias gil='git log --oneline --decorate=short'
  91.  
  92. # Test related conveniences
  93.  
  94. # SSH to a specified ONOS instance
  95. alias sshctl='onos-ssh'
  96. alias sshnet='onos-ssh $OCN'
  97.  
  98. # Runs a sequence of STC scenarios until the first failure.
  99. function stcs {
  100.     for s in "$@"; do
  101.         if [[ $s =~ ^[0-9]*$ ]]; then
  102.             printf "Waiting %d seconds...\n" $s
  103.             sleep $s
  104.         else
  105.             printf "Running scenario %s...\n" $s
  106.             stc $s || return 1
  107.         fi
  108.     done
  109. }
  110.  
  111. # Applies the settings in the specified topology recipe file or lists current
  112. # topo recipe definition if no topo recipe file is given.
  113. function topo {
  114.     topo=${1:-""}
  115.     case "$topo" in
  116.     "")
  117.         env | egrep "ONOS_TOPO"
  118.         env | egrep "(OTD|OTL|OTH)="
  119.         ;;
  120.  
  121.     *)
  122.         [ ! -f $ONOS_ROOT/tools/test/topos/$1.recipe ] && echo "No such topo recipe: $1" >&2 && return 1
  123.         unset ONOS_TOPO OTD OTL OTH ONOS_DEVICES ONOS_HOSTS
  124.         ONOS_DEVICES=""
  125.         ONOS_HOSTS=""
  126.         unset $(env | sed -n 's:\(^OT[DLH][0-9]\{1,\}\)=.*:\1 :g p')
  127.         export ONOS_TOPO=$1
  128.         . $ONOS_ROOT/tools/test/topos/$1.recipe
  129.         let d=1; while [ $d -le $OTD ]; do
  130.             dev="$(printf 'of:%016x' $d)"
  131.             export OTD$d=$dev; export ONOS_DEVICES="$ONOS_DEVICES $dev"
  132.             let d=d+1;
  133.         done
  134.         let h=1; while [ $h -le $OTH ]; do
  135.             host="$(printf '00:00:00:00:00:%02x/-1' $h)"
  136.             export OTH$h=$host; export ONOS_HOSTS="$ONOS_HOSTS $host"
  137.             let h=h+1
  138.         done
  139.         topo
  140.     esac
  141. }
  142.  
  143. # Lists available topo recipes
  144. function topos {
  145.     for topo in $(ls -1 $ONOS_ROOT/tools/test/topos/*.recipe); do
  146.         name=$(basename $topo .recipe)
  147.         printf "%-16s  %s\n" \
  148.             "$([ $name = $ONOS_TOPO ] && echo $name '*' || echo $name)" \
  149.             "$(grep '^#' $topo | head -n 1)"
  150.     done
  151. }
  152.  
  153. # Sets the primary instance to the specified instance number.
  154. function setPrimaryInstance {
  155.     export ONOS_INSTANCES=$(env | grep 'OC[0-9]*=' | sort | cut -d= -f2)
  156.  
  157.     if [ -z "${OCC1-}" ]; then
  158.         aux=/tmp/cell-$$.aux
  159.         let i=1
  160.         for n in $ONOS_INSTANCES; do
  161.             echo "export OCC$i=$n" >> $aux
  162.             let i=i+1
  163.         done
  164.         [ -f $aux ] && source $aux && rm $aux
  165.     fi
  166.  
  167.     export ONOS_CORES=$(env | grep 'OCC[0-9]*=' | sort | cut -d= -f2)
  168.     export OCI=$(env | egrep "OC[0-9]+" | sort | egrep OC${1:-1} | cut -d= -f2)
  169.     echo $OCI
  170. }
  171.  
  172. # Sets minority (OCMI) and majority (OCMA) variables
  173. function setMinorityMajority {
  174.     nodes=($(env | grep 'OC[0-9]*=' | sort | cut -d= -f2))
  175.     min=1
  176.     maj=1
  177.     node_count="${#nodes[@]}"
  178.     if [ $node_count -gt 1 ]; then
  179.         middle=$(expr "$node_count" / "2")
  180.         index=0
  181.         for node in "${nodes[@]}"; do
  182.             if [ "$index" -gt "$middle" ]; then
  183.                 export OCMI${min}=${node}
  184.                 min=$(expr $min + 1)
  185.             else
  186.                 export OCMA${maj}=${node}
  187.                 maj=$(expr $maj + 1)
  188.             fi
  189.             index=$(expr $index + 1)
  190.         done
  191.     fi
  192. }
  193.  
  194. # Open Networking Foundation shared test cell warden address
  195. export CELL_WARDEN="10.192.19.72"
  196. export CELL_SLAVE_1=$CELL_WARDEN
  197. export CELL_SLAVE_2="10.192.19.71"
  198. export CELL_SLAVES="$CELL_SLAVE_1 $CELL_SLAVE_2"
  199.  
  200. # Clears cell environment
  201. function clearCell {
  202.     unset ONOS_CELL ONOS_IP ONOS_BOOT_FEATURES
  203.     unset OCI OCN OCT ONOS_INSTANCES ONOS_CORES ONOS_FEATURES
  204.     unset ONOS_USER ONOS_GROUP ONOS_WEB_USER ONOS_WEB_PASS
  205.     unset $(env | sed -n 's:\(^OC[0-9]\{1,\}\)=.*:\1 :g p')
  206.     unset $(env | sed -n 's:\(^OCC[0-9]\{1,\}\)=.*:\1 :g p')
  207.     unset $(env | sed -n 's:\(^OCMI[0-9]\{1,\}\)=.*:\1 :g p')
  208.     unset $(env | sed -n 's:\(^OCMA[0-9]\{1,\}\)=.*:\1 :g p')
  209. }
  210.  
  211. # Applies the settings in the specified cell file or lists current cell definition
  212. # if no cell file is given.
  213. function cell {
  214.     cell=${1:-""}
  215.     case "$cell" in
  216.     "borrow")
  217.         clearCell
  218.         aux="/tmp/cell-$$"
  219.         duration=${2:-0}
  220.         spec=${3:-5+3+1}
  221.         spec=${spec//+/%2B}
  222.         user=${4:-$(id -un)}
  223.         hint=${5:-""}
  224.         query="duration=$duration&spec=$spec&user=$user&cellNameHint=$hint"
  225.         curl -sS -X POST "http://$CELL_WARDEN:4321/?$query" -d "$(cat ~/.ssh/id_rsa.pub)" > $aux
  226.         . $aux
  227.         rm -f $aux
  228.         setPrimaryInstance 1 >/dev/null
  229.         setMinorityMajority >/dev/null
  230.         onos-verify-cell
  231.         topo default >/dev/null
  232.         ;;
  233.     "return")
  234.         curl -sS -X DELETE "http://$CELL_WARDEN:4321/?user=${2:-$(id -un)}"
  235.         clearCell
  236.         ;;
  237.  
  238.     "status")
  239.         curl -sS "http://$CELL_WARDEN:4321/" | sort
  240.         ;;
  241.  
  242.     "")
  243.         env | egrep "^ONOS_CELL"
  244.         env | egrep "^OCC[0-9]+" | sort
  245.         env | egrep "^OC[0-9]+" | sort
  246.         env | egrep "^OC[INT]" | sort
  247.         env | egrep "^ONOS_" | egrep -v 'ONOS_ROOT|ONOS_CELL|ONOS_INSTANCES|ONOS_CORES|ONOS_DEVICES|ONOS_HOSTS' | sort
  248.         ;;
  249.  
  250.     *)
  251.         [ ! -f $ONOS_ROOT/tools/test/cells/$1 ] && echo "No such cell: $1" >&2 && return 1
  252.         clearCell
  253.         export ONOS_USER=sdn
  254.         export ONOS_GROUP=sdn
  255.         export ONOS_WEB_USER=onos
  256.         export ONOS_WEB_PASS=rocks
  257.         export ONOS_CELL=$1
  258.         . $ONOS_ROOT/tools/test/cells/$1
  259.         setPrimaryInstance 1 >/dev/null
  260.         cell
  261.         topo default >/dev/null
  262.     esac
  263. }
  264.  
  265. # Lists available cells
  266. function cells {
  267.     for cell in $(ls -1 $ONOS_ROOT/tools/test/cells); do
  268.         printf "%-16s  %s\n" \
  269.             "$([ ! -z $ONOS_CELL ] && [ $cell = $ONOS_CELL ] && echo $cell '*' || echo $cell)" \
  270.             "$(grep '^#' $ONOS_ROOT/tools/test/cells/$cell | head -n 1)"
  271.     done
  272. }
  273.  
  274. # Find a process by regex
  275. function spy {
  276.     ps -ef | egrep "$@" | grep -v egrep
  277. }
  278.  
  279. # Kill a process by regex
  280. function nuke {
  281.     spy "$@" | cut -c7-11 | xargs kill
  282. }
  283.  
  284. # Edit a cell file by providing a cell name; opens the cell file in $EDITOR.
  285. function vicell {
  286.     local apply=false
  287.     local create=false
  288.     local ${cdf:=$ONOS_CELL}
  289.     local cpath="${ONOS_ROOT}/tools/test/cells/"
  290.  
  291.     if [ "$1" = "-h" ] ; then
  292.         printf "usage: vicell [file] [options]\n\noptions:\n"
  293.         printf "\t[file]: cell name (default: current cell)\n"
  294.         printf "\t-a: apply the cell after editing\n"
  295.         printf "\t-e: [editor] set EDITOR to [editor] (default: *vi*)\n"
  296.         printf "\t-c: create cell file if none exist\n\n"
  297.         return 1
  298.     fi
  299.  
  300.     while [ $# -gt 0 ]; do
  301.         case "$1" in
  302.             -a) apply=true ;;
  303.             -e) EDITOR=$2; shift ;;
  304.             -c) create=true ;;
  305.             *) cdf="$1" ;;
  306.         esac
  307.         shift
  308.     done
  309.  
  310.     if [ ! -e "${cpath}${cdf}" ] && ! ($create) ; then
  311.         printf "${cdf} : no such cell\n" && return 1
  312.     fi
  313.  
  314.     if [ -z "${EDITOR}" ] || [ -x "$(which ${EDITOR})" ]; then
  315.         unset EDITOR && vi ${cpath}${cdf}
  316.     else
  317.         $EDITOR ${cpath}${cdf}
  318.     fi
  319.     ($apply) && cell ${cdf}
  320. }
  321.  
  322. # Autocomplete for certain utilities
  323. . ${ONOS_ROOT}/tools/test/bin/ogroup-opts
  324.  
  325.  
  326. # Load AT&T MPLS topo GEO data
  327. alias atttopo='onos-netcfg $OCI $ONOS_ROOT/tools/test/topos/attmpls-cfg.json'
  328.  
  329. # Load UK topo GEO data
  330. alias uktopo='onos-netcfg $OCI $ONOS_ROOT/tools/test/topos/uk-cfg.json'
  331.  
  332. # Mininet command that uses BMv2 instead of OVS
  333. alias mn-bmv2='sudo -E mn --custom $BMV2_MN_PY --switch onosbmv2 --controller remote,ip=$OCI'
  334. alias mn-stratum='sudo -E mn --custom $BMV2_MN_PY --switch stratum --controller remote,ip=$OCI'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement