Skip to content

Commit

Permalink
#15 Improve redis_autojoin.sh
Browse files Browse the repository at this point in the history
Handle Hipchat versions where the Redis config is stored in the Hipchat site config file.
Thanks @anx-ag for the patch.
  • Loading branch information
7-plus-t committed Sep 18, 2019
1 parent 1eb0183 commit 542672f
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions redis_autojoin.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@ set -e

# Get autojoin configuration for Hipchat users from Redis. Script is best run on a Hipchat App node.

export PGHOST=$(cat /hipchat/config/site.json | jq -r '.databases.hipchat_postgres.servers[0]' | cut -d: -f1)
export PGUSER=$(cat /hipchat/config/site.json | jq -r '.databases.hipchat_postgres.user')
export PGSCHEMA=$(cat /hipchat/config/site.json | jq -r '.databases.hipchat_postgres.schema')
export PGPASSWORD=$(cat /hipchat/config/site.json | jq -r '.databases.hipchat_postgres.pass')
SITECONFIG=/hipchat/config/site.json
OUTPUT_FILE=./autojoin.json

RHOST=$(psql -h $PGHOST -U $PGUSER -d $PGSCHEMA -t -c "SELECT value FROM configurations WHERE key='redishostname';")
RPORT=$(psql -h $PGHOST -U $PGUSER -d $PGSCHEMA -t -c "SELECT value FROM configurations WHERE key='redisport';")
RPWD=$(psql -h $PGHOST -U $PGUSER -d $PGSCHEMA -t -c "SELECT value FROM configurations WHERE key='redispass';")
# check if the redis configuration is stored directly in the site config
export RHOST=$(jq -r .redis[0].host $SITECONFIG)
if [ "$RHOST" != "null" ]; then
export RPORT=$(jq -r .redis[0].port $SITECONFIG)
export RPWD=$(jq -r .redis[0].auth $SITECONFIG)
else
export PGHOST=$(jq -r '.databases.hipchat_postgres.servers[0]' $SITECONFIG | cut -d: -f1)
export PGUSER=$(jq -r '.databases.hipchat_postgres.user' $SITECONFIG)
export PGSCHEMA=$(jq -r '.databases.hipchat_postgres.schema' $SITECONFIG)
export PGPASSWORD=$(jq -r '.databases.hipchat_postgres.pass' $SITECONFIG)

export RHOST=$(psql -h $PGHOST -U $PGUSER -d $PGSCHEMA -t -c "SELECT value FROM configurations WHERE key='redishostname';")
export RPORT=$(psql -h $PGHOST -U $PGUSER -d $PGSCHEMA -t -c "SELECT value FROM configurations WHERE key='redisport';")
export RPWD=$(psql -h $PGHOST -U $PGUSER -d $PGSCHEMA -t -c "SELECT value FROM configurations WHERE key='redispass';")
fi

if [ "$RPWD" != "null" ]; then
RPWD="-a $RPWD"
else
RPWD=""
fi

OUTPUT_FILE=./autojoin.json
echo "{\"autojoins\": [" > $OUTPUT_FILE
for key in $(redis-cli -h $RHOST -p $RPORT -a $RPWD KEYS '*' | grep "pref:autoJoin:" | cut -d" " -f2); do
for key in $(redis-cli -h $RHOST -p $RPORT $RPWD KEYS '*' | grep "pref:autoJoin:" | cut -d" " -f2); do
echo "Found key: $key"
VALUE=$(redis-cli -h $RHOST -p $RPORT -a $RPWD GET "$key")
VALUE=$(redis-cli -h $RHOST -p $RPORT $RPWD GET "$key")
[[ ${#VALUE} -eq 4 && "$VALUE" -eq "None" ]] && VALUE="[]"
USER_ID=$(echo $key | cut -d":" -f3)
echo "{\"user_id\": $USER_ID, \"rooms\": $VALUE }," >> $OUTPUT_FILE
Expand Down

0 comments on commit 542672f

Please sign in to comment.