|
| 1 | +#!/bin/bash |
| 2 | +[[ -f ~/.node_managerrc ]] && source ~/.node_managerrc |
| 3 | + |
| 4 | +# Help text |
| 5 | +HELP=`cat <<-EOF |
| 6 | +Usage: $0 [options] [UID]\n |
| 7 | +\n |
| 8 | +-n| --name \t\t The name of the node_group.\n |
| 9 | +\t \t\t *Required to create a new group.\n\n |
| 10 | +-x| --ensure \t\t Set to [present|absent] for existence.\n |
| 11 | +\t \t\t Default: present\n\n |
| 12 | +-d| --description \t Description of group.\n\n |
| 13 | +-e| --environment \t Puppet environment for group.\n |
| 14 | +\t \t\t Default: production\n\n |
| 15 | +-o| --override \t Set to [true|false] for environment group.\n |
| 16 | +\t \t\t Default: false\n\n |
| 17 | +-p| --parent \t\t Parent group UID.\n |
| 18 | +\t \t\t Default: 00000000-0000-4000-8000-000000000000\n\n |
| 19 | +-c| --classes \t\t Hash of classes and parameters.\n |
| 20 | +\t \t\t Example: '{ "vim": {} }'\n\n |
| 21 | +-r| --rule \t\t Array of rules for matching.\n |
| 22 | +\t \t\t Example: '["or", ["=", "name", "node.whatsaranjit.com"]]'\n\n |
| 23 | +-v| --variables \t Variables to set in the group.\n |
| 24 | +\t \t\t Example: '{ "foo": "bar" }'\n\n |
| 25 | +-h| --help \t\t Display this help message.\n |
| 26 | +EOF` |
| 27 | +
|
| 28 | +# Defaults |
| 29 | +MASTER="${MASTER:-`hostname -f`}" |
| 30 | +PORT="${PORT:-4433}" |
| 31 | +
|
| 32 | +# File to catch response headers |
| 33 | +DUMP=$(mktemp) |
| 34 | +CURL="/usr/bin/curl -s -k -D ${DUMP}" |
| 35 | +CLASSIFIER_URL="https://${MASTER}:${PORT}/classifier-api/v1/groups" |
| 36 | +
|
| 37 | +# Get all groups |
| 38 | +if [[ $# -eq 0 ]]; then |
| 39 | + $CURL -X GET \ |
| 40 | + -H "Content-Type: application/json" \ |
| 41 | + -H "X-Authentication: ${TOKEN}" \ |
| 42 | + $CLASSIFIER_URL \ |
| 43 | + | python -m json.tool 2> /dev/null \ |
| 44 | + || (>&2 echo "Unable to hit API!"; exit 1) |
| 45 | +# Get single group |
| 46 | +elif [[ $# -eq 1 ]]; then |
| 47 | + if [[ "$1" == "--help" || "$1" == "-h" ]]; then |
| 48 | + echo -e $HELP |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | + $CURL -X GET \ |
| 52 | + -H "Content-Type: application/json" \ |
| 53 | + -H "X-Authentication: ${TOKEN}" \ |
| 54 | + "${CLASSIFIER_URL}/${1}" \ |
| 55 | + | python -m json.tool 2> /dev/null \ |
| 56 | + || (>&2 echo "Unable to hit API!"; exit 1) |
| 57 | +# Create/update groups |
| 58 | +else |
| 59 | + DATA='{' |
| 60 | + while [[ $# -gt 1 ]]; do |
| 61 | + key="$1" |
| 62 | +
|
| 63 | + case $key in |
| 64 | + -h|--help) |
| 65 | + echo -e $HELP |
| 66 | + exit 0 |
| 67 | + shift |
| 68 | + ;; |
| 69 | + -x|--ensure) |
| 70 | + ENSURE="$2" |
| 71 | + shift |
| 72 | + ;; |
| 73 | + -n|--name) |
| 74 | + NAME="$2" |
| 75 | + DATA="${DATA} \"name\": \"${NAME}\"," |
| 76 | + shift |
| 77 | + ;; |
| 78 | + -d|--description) |
| 79 | + DESCRIPTION="$2" |
| 80 | + DATA="${DATA} \"description\": \"${DESCRIPTION}\"," |
| 81 | + shift |
| 82 | + ;; |
| 83 | + -e|--environment) |
| 84 | + ENVIRONMENT="$2" |
| 85 | + DATA="${DATA} \"environment\": \"${ENVIRONMENT}\"," |
| 86 | + shift |
| 87 | + ;; |
| 88 | + -o|--override) |
| 89 | + OVERRIDE="$2" |
| 90 | + DATA="${DATA} \"environment_trumps\": ${OVERRIDE}," |
| 91 | + shift |
| 92 | + ;; |
| 93 | + -p|--parent) |
| 94 | + PARENT="$2" |
| 95 | + DATA="${DATA} \"parent\": \"${PARENT}\"," |
| 96 | + shift |
| 97 | + ;; |
| 98 | + -c|--classes) |
| 99 | + CLASSES="$2" |
| 100 | + DATA="${DATA} \"classes\": ${CLASSES}," |
| 101 | + shift |
| 102 | + ;; |
| 103 | + -r|--rule) |
| 104 | + RULE="$2" |
| 105 | + DATA="${DATA} \"rule\": $RULE," |
| 106 | + shift |
| 107 | + ;; |
| 108 | + -v|--variables) |
| 109 | + VARIABLES="$2" |
| 110 | + DATA="${DATA} \"variables\": $VARIABLES," |
| 111 | + shift |
| 112 | + ;; |
| 113 | + *) |
| 114 | + (>&2 echo "Invalid options supplied!"); exit 1 |
| 115 | + ;; |
| 116 | + esac |
| 117 | + shift |
| 118 | + done |
| 119 | + # Set defaults if they weren't given |
| 120 | + if [[ -z $PARENT ]]; then |
| 121 | + DATA="${DATA} \"parent\": \"00000000-0000-4000-8000-000000000000\"," |
| 122 | + fi |
| 123 | + if [[ -z $CLASSES ]]; then |
| 124 | + DATA="${DATA} \"classes\": {}," |
| 125 | + fi |
| 126 | + # Remove trailing comma |
| 127 | + DATA=`sed 's/,$//' <<< $DATA` |
| 128 | + DATA="${DATA} }" |
| 129 | + # Last arg, if given, is the group ID |
| 130 | + ID=$1 |
| 131 | + if [[ ! -z $ID ]]; then |
| 132 | + URL="${CLASSIFIER_URL}/${ID}" |
| 133 | + else |
| 134 | + URL=$CLASSIFIER_URL |
| 135 | + fi |
| 136 | + # Do cURL with JSON data |
| 137 | + if [[ "$ENSURE" == "absent" ]]; then |
| 138 | + $CURL -X DELETE \ |
| 139 | + -H "Content-Type: application/json" \ |
| 140 | + -H "X-Authentication: ${TOKEN}" \ |
| 141 | + $URL \ |
| 142 | + || (>&2 echo "Unable to hit API!"; exit 1) |
| 143 | + else |
| 144 | + $CURL -X POST \ |
| 145 | + -H "Content-Type: application/json" \ |
| 146 | + -H "X-Authentication: ${TOKEN}" \ |
| 147 | + --data "${DATA}" \ |
| 148 | + $URL \ |
| 149 | + | python -m json.tool 2> /dev/null || ( |
| 150 | + NEWID=`grep 'Location' $DUMP | cut -d '/' -f5` |
| 151 | + if [[ ! -z $NEWID ]]; then |
| 152 | + echo "New group ID: ${NEWID}" |
| 153 | + else |
| 154 | + (>&2 echo "Unable to hit API!"); exit 1 |
| 155 | + fi |
| 156 | + ) |
| 157 | + fi |
| 158 | +fi |
| 159 | +
|
| 160 | +CODE=`grep 'HTTP/' $DUMP | cut -d ' ' -f2` |
| 161 | +
|
| 162 | +# All good |
| 163 | +if [[ "$CODE" -eq '200' ]]; then |
| 164 | + exit 0 |
| 165 | +# Successful creation |
| 166 | +elif [[ "$CODE" -eq '201' ]]; then |
| 167 | + exit 2 |
| 168 | +# Successful delete |
| 169 | +elif [[ "$CODE" -eq '204' ]]; then |
| 170 | + echo "${ID} removed" |
| 171 | + exit 2 |
| 172 | +# Successful creation |
| 173 | +elif [[ "$CODE" -eq '303' ]]; then |
| 174 | + exit 2 |
| 175 | +# Bad schema |
| 176 | +elif [[ "$CODE" -eq '400' ]]; then |
| 177 | + exit 1 |
| 178 | +# ID not found |
| 179 | +elif [[ "$CODE" -eq '404' ]]; then |
| 180 | + exit 1 |
| 181 | +# Violating uniqueness |
| 182 | +elif [[ "$CODE" -eq '422' ]]; then |
| 183 | + exit 4 |
| 184 | +# Huh? |
| 185 | +else |
| 186 | + exit 1 |
| 187 | +fi |
0 commit comments