Skip to content

Commit 42fce36

Browse files
authored
Merge pull request #31 from WhatsARanjit/scripts
Scripts
2 parents 8909475 + 17d7128 commit 42fce36

File tree

2 files changed

+273
-0
lines changed

2 files changed

+273
-0
lines changed

scripts/README.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Node_manager scripts
2+
3+
#### Table of Contents
4+
5+
1. [Overview](#overview)
6+
1. [Example](#example)
7+
1. [Setup](#setup)
8+
1. [Usage](#usage)
9+
10+
## Overview
11+
12+
Manipulate node_groups from a system without Puppet installed.
13+
14+
## Example
15+
16+
```
17+
[root@server ~/node_manager/scripts]# ./node_group.sh -n 'Example Group' \
18+
--rule '["or", ["=", "name", "node.whatsaranjit.com"]]' \
19+
--classes '{"vim": {}}' --variables '{"foo": "bar"}'
20+
New group ID: 15e0c815-e3ca-48e3-a467-e86e5b9d025e
21+
```
22+
23+
## Setup
24+
25+
Place a file at `~/.node_managerrc` following this example:
26+
27+
```
28+
MASTER=master.whatsaranjit.com # Defaults to hostname -f
29+
PORT=4433 # Defaults to 4433
30+
TOKEN='<your_token>'
31+
```
32+
33+
## Usage
34+
35+
```
36+
Usage: ./node_group.sh [options] [UID]
37+
38+
-n| --name The name of the node_group.
39+
*Required to create a new group.
40+
41+
-x| --ensure Set to [present|absent] for existence.
42+
Default: present
43+
44+
-d| --description Description of group.
45+
46+
-e| --environment Puppet environment for group.
47+
Default: production
48+
49+
-o| --override Set to [true|false] for environment group.
50+
Default: false
51+
52+
-p| --parent Parent group UID.
53+
Default: 00000000-0000-4000-8000-000000000000
54+
55+
-c| --classes Hash of classes and parameters.
56+
Example: '{ "vim": {} }'
57+
58+
-r| --rule Array of rules for matching.
59+
Example: '["or", ["=", "name", "node.whatsaranjit.com"]]'
60+
61+
-v| --variables Variables to set in the group.
62+
Example: '{ "foo": "bar" }'
63+
64+
-h| --help Display this help message.
65+
```
66+
67+
### Create a new group
68+
69+
```
70+
[root@server ~/node_manager/scripts]# ./node_group.sh -n 'Example Group'
71+
```
72+
The `--name` flag is required. All other flags are optional.
73+
74+
### Update an existing group
75+
76+
```
77+
[root@server ~/node_manager/scripts]# ./node_group.sh -d 'New description' 15e0c815-e3ca-48e3-a467-e86e5b9d025e
78+
```
79+
You must edit a group by giving the UID.
80+
81+
### Delete a group
82+
83+
```
84+
[root@server ~/node_manager/scripts]# ./node_group.sh --ensure absent 15e0c815-e3ca-48e3-a467-e86e5b9d025e
85+
```
86+
You must delete a group by giving the UID.

scripts/node_group.sh

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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

Comments
 (0)