forked from TheJumpCloud/SystemContextAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance-shutdown-initd
64 lines (50 loc) · 1.56 KB
/
instance-shutdown-initd
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
#! /bin/bash
#
# instance-shutdown Run commands at system shutdown.
#
# chkconfig: 01 95 05
#
### BEGIN INIT INFO
# Provides: instance-shutdown
# Required-Start: $network $syslog
# Required-Stop:
# Default-Start:
# Default-Stop: 0
# Short-Description: instance shutdown
# Description: Run commands at system shutdown.
### END INIT INFO
#
case "$1" in
start)
touch /var/lock/subsys/instance-shutdown
exit 0
;;
stop)
##
## Example of moving a system to the "stopped instances" tag upon system shut down.
##
## Replace the following with any System Context Api call upon shutdown.
## System Context Api Docs: https://github.com/TheJumpCloud/SystemContextAPI
##
conf="`cat /opt/jc/jcagent.conf`"
regex="systemKey\":\"(\w+)\""
if [[ $conf =~ $regex ]] ; then
systemKey="${BASH_REMATCH[1]}"
fi
now=`date -u "+%a, %d %h %Y %H:%M:%S GMT"`;
signstr="PUT /api/systems/${systemKey} HTTP/1.1\ndate: ${now}"
signature=`printf "$signstr" | openssl dgst -sha256 -sign /opt/jc/client.key | openssl enc -e -a | tr -d '\n'` ;
tmpD=`date +%s`
curl -iq \
-d "{ \"tags\" : [\"stopped servers\"], \"displayName\" : \"stopped-${tmpD}\"}" \
-X "PUT" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Date: ${now}" \
-H "Authorization: Signature keyId=\"system/${systemKey}\",headers=\"request-line date\",algorithm=\"rsa-sha256\",signature=\"${signature}\"" \
--url https://console.jumpcloud.com/api/systems/${systemKey}
;;
*)
exit 0
;;
esac