David K. Bainbridge | 7ae4f19 | 2016-09-29 09:01:38 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | #D provides access to the CORD POD base metal provisioning service |
| 3 | |
| 4 | PROG=$(echo $(basename $0) | sed -e 's/^cord-/cord /g') |
| 5 | |
| 6 | usage() { |
| 7 | echo "usage: $PROG <sub-command> [options]" |
| 8 | echo " list display the provisioning status of all nodes" |
| 9 | echo " show display the provisiniong status of a single node" |
| 10 | echo " delete delete node(s) provisioning state" |
| 11 | echo " help this message" |
| 12 | } |
| 13 | |
| 14 | COMMAND=$1; shift |
| 15 | case $COMMAND in |
| 16 | list) |
| 17 | DO_JSON=0 |
| 18 | DO_MAP=0 |
| 19 | while [ $# -gt 0 ]; do |
| 20 | case $1 in |
| 21 | json|--json|-j) |
| 22 | DO_JSON=1 |
| 23 | ;; |
| 24 | map|--map|-m) |
| 25 | DO_MAP=1 |
| 26 | ;; |
| 27 | help|--help|-h) |
| 28 | echo "usage: $PROG list [--json|-j] [--map|-m]" |
| 29 | echo " json display output as JSON object" |
| 30 | echo " map map node provisioning status to state name" |
| 31 | exit 0 |
| 32 | ;; |
| 33 | *) |
| 34 | >&2 "Unknown option '$1'" |
| 35 | echo "usage: $PROG list [--json|-j] [--map|-m]" |
| 36 | echo " json display output as JSON object" |
| 37 | echo " map map node provisioning status to state name" |
| 38 | exit 1 |
| 39 | ;; |
| 40 | esac |
| 41 | shift |
| 42 | done |
| 43 | if [ $DO_JSON -eq 1 ]; then |
| 44 | if [ $DO_MAP -eq 1 ]; then |
| 45 | curl -sSL http://$CORD_HEAD_NODE:4243/provision/ | jq -c -M 'def STATUS: ["Unknown","Processing","Complete","Error"]; [ .[] | . | .status|=STATUS[.] ]' |
| 46 | else |
| 47 | curl -sSL http://$CORD_HEAD_NODE:4243/provision/ |
| 48 | fi |
| 49 | else |
| 50 | for LINE in "ID,NAME,MAC,IP,STATUS,MESSAGE" $(curl -sSL http://$CORD_HEAD_NODE:4243/provision/ | jq 'def STATUS: ["Unknown","Processing","Complete","Error"]; .[] | . | .status|=STATUS[.] | .request.Info.id+","+.request.Info.name+","+.request.Info.mac+","+.request.Info.ip+","+.status+","+.message'); do |
| 51 | echo $LINE | sed -e 's/^"//;s/"$//' |
| 52 | done | column -s , -t |
| 53 | fi |
| 54 | ;; |
| 55 | show) |
| 56 | DO_JSON=0 |
| 57 | DO_MAP=0 |
| 58 | ID= |
| 59 | while [ $# -gt 0 ]; do |
| 60 | case $1 in |
| 61 | json|--json|-j) |
| 62 | DO_JSON=1 |
| 63 | ;; |
| 64 | map|--map|-m) |
| 65 | DO_MAP=1 |
| 66 | ;; |
| 67 | help) |
| 68 | echo "usage: $PROG show [--json|-j] [--map|-m] <id>" |
| 69 | echo " json display output as JSON object" |
| 70 | echo " map map node provisioning status to state name" |
| 71 | exit 0 |
| 72 | ;; |
| 73 | *) |
| 74 | ID="$ID $1" |
| 75 | ;; |
| 76 | esac |
| 77 | shift |
| 78 | done |
| 79 | if [ $DO_JSON -eq 1 ]; then |
| 80 | if [ $DO_MAP -eq 1 ]; then |
| 81 | echo -n "[" |
| 82 | for i in $ID; do |
| 83 | curl -sSL http://$CORD_HEAD_NODE:4243/provision/$i | jq -c -M 'def STATUS: ["Unknown","Processing","Complete","Error"]; . | .status|=STATUS[.]' |
| 84 | done | awk -vORS=, '{ print }' | sed 's/,$//' |
| 85 | echo -n "]" |
| 86 | else |
| 87 | echo -n "[" |
| 88 | for i in $ID; do |
| 89 | curl -sSL http://$CORD_HEAD_NODE:4243/provision/$i | jq -c -M . |
| 90 | done | awk -vORS=, '{ print }' | sed 's/,$//' |
| 91 | echo -n "]" |
| 92 | fi |
| 93 | else |
| 94 | for i in "__TITLE__" $ID; do |
| 95 | if [ $i == "__TITLE__" ]; then |
| 96 | echo "ID,NAME,MAC,IP,STATUS,MESSAGE" |
| 97 | else |
| 98 | VALUE=$(curl --fail -sSL http://$CORD_HEAD_NODE:4243/provision/$i) |
| 99 | if [ $? -ne 0 ]; then |
| 100 | echo "$i, , , ,Not Found" |
| 101 | else |
| 102 | echo "$VALUE" | jq 'def STATUS: ["Unknown","Processing","Complete","Error"]; . | .status|=STATUS[.] | .request.Info.id+","+.request.Info.name+","+.request.Info.mac+","+.request.Info.ip+","+.status+","+.message'| sed -e 's/^"//;s/"$//' |
| 103 | fi |
| 104 | fi |
| 105 | done | column -s , -t |
| 106 | fi |
| 107 | ;; |
| 108 | delete) |
| 109 | ID= |
| 110 | while [ $# -gt 0 ]; do |
| 111 | case $1 in |
| 112 | all|-a|--all) |
| 113 | ID=$(curl -sSL http://$CORD_HEAD_NODE:4243/provision/ | jq '.[] | .request.Info.id' | sed -e 's/^"//;s/"$//') |
| 114 | ;; |
| 115 | help|-h|--help) |
| 116 | echo "$PROG delete [--all|-a] [<id> ...]" |
| 117 | exit 0 |
| 118 | ;; |
| 119 | *) |
| 120 | ID="$ID $1" |
| 121 | ;; |
| 122 | esac |
| 123 | shift |
| 124 | done |
| 125 | for i in $ID; do |
| 126 | curl --fail -sSL -XDELETE http://$CORD_HEAD_NODE:4243/provision/$i |
| 127 | if [ $? -eq 0 ]; then |
| 128 | echo "$i DELETED" |
| 129 | else |
| 130 | echo "$i FAILED" |
| 131 | fi |
| 132 | done | column -s , -t |
| 133 | ;; |
| 134 | help|-h|--help) |
| 135 | usage |
| 136 | exit 0 |
| 137 | ;; |
| 138 | *) |
| 139 | >&2 echo "Unknown subcommand '$COMMAND'" |
| 140 | usage |
| 141 | exit 1 |
| 142 | ;; |
| 143 | esac |