-
Notifications
You must be signed in to change notification settings - Fork 1
/
pe-edit-group
executable file
·106 lines (87 loc) · 2.09 KB
/
pe-edit-group
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
#
# Filename: pe-edit-group
# Created: Wed Jul 8 19:11:05 2015 (+1000)
# Version: 0.1.0
# Author: Ade
# Description:
# edit a group
# Change Log:
#
set -eu
fetchonly=0
while getopts :f OPT; do
case $OPT in
f)
fetchonly=1
;;
*)
echo "usage: ${0##*/} [-f] [--] ARGS..."
exit 2
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
HOST=$(grep server /etc/puppetlabs/puppet/classifier.yaml|awk '{print $2}')
PORT=$(grep port /etc/puppetlabs/puppet/classifier.yaml|awk '{print $2}')
ENDPOINT="https://${HOST}:${PORT}/classifier-api/v1"
if [ "$EDITOR" = "" ]; then
EDITOR=vi
fi
if [[ ! -f ~/.puppetlabs/puppet-access.conf ]]
then
mkdir -p ~/.puppetlabs
echo '{"service-url":"https://'$HOST':'$PORT'/rbac-api"}' > ~/.puppetlabs/puppet-access.conf
fi
# TODO: somehow test if we need to do this
puppet-access login
TOKEN=$(puppet-access show)
TEMPFILE=/tmp/pe-rule-$$
curl -s -k -X GET \
-H 'Content-Type: application/json' \
-H "X-Authentication:$TOKEN" \
${ENDPOINT}/groups/${1} | jq . > $TEMPFILE
if (( fetchonly ))
then
echo "data is in $TEMPFILE"
exit
fi
BEFORESUM="$(sha256sum $TEMPFILE)"
keepediting=1
while (( keepediting ))
do
${EDITOR} $TEMPFILE
if jq -e . < $TEMPFILE > /dev/null
then
keepediting=0
else
echo "Syntax check returned non-zero exit status." \
"Press ENTER to re-edit." | fmt
read junk
fi
if (( $? != 0 ))
then
echo "Editor $EDITOR returned non-zero exit status." \
"Not sending changed file, but" \
"I saved it in $TEMPFILE" | fmt
exit 1
fi
done
AFTERSUM="$(sha256sum $TEMPFILE)"
if [[ "$BEFORESUM" == "$AFTERSUM" ]]
then
echo "No change in $TEMPFILE. Not sending."
exit 1
fi
curl -s -k -X POST \
-H 'Content-Type: application/json' \
-H "X-Authentication:$TOKEN" \
${ENDPOINT}/groups/${1} \
-d @$TEMPFILE | jq .
if (( $? == 0 ))
then
rm $TEMPFILE
else
echo "Error when sending changed file." \
"Saved it in $TEMPFILE" | fmt
fi