-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack.sh
executable file
·64 lines (53 loc) · 1.44 KB
/
slack.sh
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
#!/usr/bin/env bash
set -ex
export SLACK_PATH=$(dirname $(readlink -e "$0"))
#
# Read all of the configuration variables, to set up the environment.
#
for config in "$SLACK_PATH/config/"*; do
source "$config"
done
if [ -z "$DEPLOY_SERVER" ]; then
log "ERROR: $0 requires a deploy server to execute."
exit 1
fi
if [ -z "$SLACK_URL" ]; then
log "ERROR: $0 requires a configured Slack Webhook to execute."
exit 1
fi
#
# Filename of FIFO on gamebox
#
export FIFO="/home/$DEPLOY_USER/slack"
#
# Gracefully handle ctrl+c
#
ctrl_c() {
echo "Trapped Ctrl-C"
slack_msg_custom "ssh-slack" "$ERRORCHAN" "Trapped Ctrl+C, no longer monitoring \`$FIFO\` $(cat $FILE)"
log "Trapped Ctrl+C, no longer monitoring \`$FIFO\` $(cat $FILE)"
exit
}
trap ctrl_c INT
FILE=$(mktemp)
while true; do
$DEPLOY_SSH -n "[ -p '$FIFO' ] || (mkfifo '$FIFO' && chmod 600 '$FIFO'); read line < '$FIFO'; echo \$line" > $FILE 2>&1
ERR=$?
if [[ $ERR != 0 ]] ; then
slack_msg_custom "ssh-slack" "$ERRORCHAN" "Got error $ERR, no longer monitoring \`$FIFO\` $(cat $FILE)"
log "Got error $ERR, no longer monitoring \`$FIFO\` $(cat $FILE)"
break
fi
xxd < $FILE
{
read -d " " user
read -d " " channel
message=$(cat)
} < $FILE
echo user $user
echo chan $channel
echo msg $message
slack_msg_custom "$channel" "$user" "$message"
log "#$channel: $user - $message"
mv $FILE $FILE.last
done