-
-
Save thedoc31/a26345721e79c2728180 to your computer and use it in GitHub Desktop.
Shell script to trigger AEM6 BlobGC via curl and JMX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Bash script to trigger AEM Blob GC on localhost using curl via JMX | |
# Author : Jayan Kandathil (Adobe Managed Services) | |
# Version : 0.3 | |
# Version Notes: | |
# 0.1 - Initial version | |
# 0.2 - autodetect environment and adjust port accordingly; handle and report errors appropriately | |
# 0.3 - update for AEM 6.2 | |
# 0.3.1 - update for AEM 6.1 SP2 | |
# 0.4.0 - update for AEM 6.3 | |
# | |
# Last updated : May 22, 2017 | |
# Last updated by: J. Casalino (Adobe Managed Services) | |
# Instructions : Copy to CQ/AEM Linux instance's /tmp folder, make necessary changes, enable the execute bit and run. | |
# Example CRON statement to run nightly at 11pm: 0 23 * * * /tmp/cq_trigger_blobgc.sh | |
# Suggest you run Blob Garbage Collection only after an Online/Offline Revision Collection has completed. | |
# Echo on (for debugging purposes) | |
# set -x verbose | |
# Turn on error validation | |
set -o errexit | |
# TCP port CQ listens on | |
PORT="" | |
# CQ Admin user ID | |
CQ_USER=admin | |
DATE_TIME=$(date "+%Y.%m.%d-%H.%M.%S") | |
# rsync log file | |
LOG_FILE=/tmp/cq_trigger_blobgc.log | |
# ------------------------------------------------ | |
# Do not configure below this point | |
# ------------------------------------------------ | |
# Log timestamp | |
date | tee -a $LOG_FILE | |
# Check to see if pass is installed and executable | |
hash pass 2>/dev/null || { echo "I can't find the pass executable to grab the CQ Admin password. Please install it and save a CQ_Admin password then try again." | tee -a $LOG_FILE; exit 1; } | |
# Acquire CQ_Admin user password from pass vault | |
CQ_USER_PASSWORD=$(pass CQ_Admin) # Caleb Pryor is a rockstar for making pass go | |
# Detect if we're running on an author or publisher | |
echo "$HOSTNAME" | |
case "$HOSTNAME" in | |
(*author*) | |
echo "We're running on an author." | tee -a $LOG_FILE | |
PORT=4502 | |
;; | |
(*publish*) | |
echo "We're running on a publisher." | tee -a $LOG_FILE | |
PORT=4503 | |
;; | |
(*) | |
echo "I can't tell if I'm running on an author or publisher based on HOSTNAME. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
# Check to see if we're running CRX2 or CRX3 | |
OAK_VERSION=$(curl -s http://localhost:$PORT/crx/explorer/index.jsp | grep -i 'Content Repository Extreme' | awk '{print $7}' | awk 'NR==2' | cut -d'<' -f1) # Josh Hamer is a rockstar for this code | |
case $OAK_VERSION in | |
(1.0.*) | |
echo "We're running Oak $OAK_VERSION on AEM 6.0" | tee -a $LOG_FILE | |
AEMVER=60 | |
;; | |
(1.2.*) | |
echo "We're running Oak $OAK_VERSION on AEM 6.1" | tee -a $LOG_FILE | |
AEMVER=61 | |
;; | |
(1.4.*) | |
echo "We're running Oak $OAK_VERSION on AEM 6.2" | tee -a $LOG_FILE | |
AEMVER=62 | |
;; | |
(1.6.*) | |
echo "We're running Oak $OAK_VERSION on AEM 6.3" | tee -a $LOG_FILE | |
AEMVER=63 | |
;; | |
(2.*.*) | |
echo "We're running CRX2. This script only works on Oak CRX3 repositories. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "I can't tell if you're running CRX2, Oak, or an AEM version I don't understand yet. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
# Initiate Blob GC using curl | |
#echo $CQ_USER | |
#echo $CQ_USER_PASSWORD | |
HTTPRESULTCODE=-1 | |
echo "Attempting Known BlobGCs..." | tee -a $LOG_FILE | |
case $AEMVER in | |
(60) | |
echo "Attempting AEM 6.0 SP2 Command..." # uses -d command to add required header and uses ID=4 | |
HTTPRESULTCODE=$(curl -s -o /dev/null -u "$CQ_USER":"$CQ_USER_PASSWORD" -w "%{http_code}" -d X-Requested-With=XMLHttpRequest -X POST "http://localhost:$PORT/system/console/jmx/org.apache.jackrabbit.oak%3Aid%3D4%2Cname%3D%22Segment+node+store+blob+garbage+collection%22%2Ctype%3D%22BlobGarbageCollection%22/op/startBlobGC/") | |
case "$HTTPRESULTCODE" in | |
(200) | |
echo "BlobGC Successfully Triggered." | tee -a $LOG_FILE | |
;; | |
(40*) | |
echo "That didn't work. HTTP $HTTPRESULTCODE received; trying another method." | tee -a $LOG_FILE | |
HTTPRESULTCODE=-1 | |
;; | |
(50*) | |
echo "Server error. Result code $HTTPRESULTCODE received; fix the server then try again." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "$HTTPRESULTCODE received; I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
if [ "$HTTPRESULTCODE" -ne 200 ] | |
then | |
echo "Attempting AEM 6.0 SP1 Command..." # removes -d command required by SP2 and uses ID=2 | |
HTTPRESULTCODE=$(curl -s -o /dev/null -u "$CQ_USER":"$CQ_USER_PASSWORD" -w "%{http_code}" -X POST "http://localhost:$PORT/system/console/jmx/org.apache.jackrabbit.oak%3Aid%3D2%2Cname%3D%22Segment+node+store+blob+garbage+collection%22%2Ctype%3D%22BlobGarbageCollection%22/op/startBlobGC/") | |
case "$HTTPRESULTCODE" in | |
(200) | |
echo "BlobGC Successfully Triggered." | tee -a $LOG_FILE | |
;; | |
(40*) | |
echo "That didn't work. HTTP $HTTPRESULTCODE received; trying another method." | tee -a $LOG_FILE | |
HTTPRESULTCODE=-1 | |
;; | |
(50*) | |
echo "Server error. Result code $HTTPRESULTCODE received; fix the server then try again." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "$HTTPRESULTCODE received; I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
fi | |
if [ "$HTTPRESULTCODE" -ne 200 ] | |
then | |
echo "Sorry bub, none of the commands I know for AEM 6.0 worked." | tee -a $LOG_FILE | |
exit 1 | |
else | |
echo "Success." | tee -a $LOG_FILE | |
fi | |
;; | |
(61) | |
echo "Attempting AEM 6.1 SP0 Command..." # 6.1 expects a boolean parameter in the invoke POST, so we add --data to pass in a value in addition to 6.0 SP2 -d header statement. ID changes to ID=5 | |
HTTPRESULTCODE=$(curl -s -o /dev/null -u "$CQ_USER":"$CQ_USER_PASSWORD" -w "%{http_code}" --data "markOnly=false" -d X-Requested-With=XMLHttpRequest -X POST "http://localhost:$PORT/system/console/jmx/org.apache.jackrabbit.oak%3Aid%3D5%2Cname%3D%22Segment+node+store+blob+garbage+collection%22%2Ctype%3D%22BlobGarbageCollection%22/op/startBlobGC/boolean") | |
case "$HTTPRESULTCODE" in | |
(200) | |
echo "BlobGC Successfully Triggered." | tee -a $LOG_FILE | |
;; | |
(40*) | |
echo "That didn't work. HTTP $HTTPRESULTCODE received." | tee -a $LOG_FILE | |
HTTPRESULTCODE=-1 | |
;; | |
(50*) | |
echo "Server error. Result code $HTTPRESULTCODE received; fix the server then try again." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "$HTTPRESULTCODE received; I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
if [ "$HTTPRESULTCODE" -ne 200 ] | |
then | |
echo "Attempting AEM 6.1 SP2 Command..." # adds -d command required by SP2 | |
HTTPRESULTCODE=$(curl -s -o /dev/null -u "$CQ_USER":"$CQ_USER_PASSWORD" -w "%{http_code}" --data "markOnly=false" -d X-Requested-With=XMLHttpRequest -X POST "http://localhost:$PORT/system/console/jmx/org.apache.jackrabbit.oak%3Aid%3D6%2Cname%3D%22Segment+node+store+blob+garbage+collection%22%2Ctype%3D%22BlobGarbageCollection%22/op/startBlobGC/boolean") | |
case "$HTTPRESULTCODE" in | |
(200) | |
echo "BlobGC Successfully Triggered." | tee -a $LOG_FILE | |
;; | |
(40*) | |
echo "That didn't work. HTTP $HTTPRESULTCODE received; trying another method." | tee -a $LOG_FILE | |
HTTPRESULTCODE=-1 | |
;; | |
(50*) | |
echo "Server error. Result code $HTTPRESULTCODE received; fix the server then try again." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "$HTTPRESULTCODE received; I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
fi | |
if [ "$HTTPRESULTCODE" -ne 200 ] | |
then | |
echo "Sorry bub, none of the commands I know for AEM 6.1 worked." | tee -a $LOG_FILE | |
exit 1 | |
else | |
echo "Success." | tee -a $LOG_FILE | |
fi | |
;; | |
(62) | |
echo "Attempting AEM 6.2 SP0 Command..." # 6.2 expects a boolean parameter in the invoke POST, so we add --data to pass in a value in addition to 6.0 SP2 -d header statement. ID changes to ID=5 | |
HTTPRESULTCODE=$(curl -s -o /dev/null -u "$CQ_USER":"$CQ_USER_PASSWORD" -w "%{http_code}" --data "markOnly=false" -d X-Requested-With=XMLHttpRequest -X POST "http://localhost:$PORT/system/console/jmx/org.apache.jackrabbit.oak%3Aname%3DSegment+node+store+blob+garbage+collection%2Ctype%3DBlobGarbageCollection/op/startBlobGC/boolean") | |
case "$HTTPRESULTCODE" in | |
(200) | |
echo "BlobGC Successfully Triggered." | tee -a $LOG_FILE | |
;; | |
(40*) | |
echo "That didn't work. HTTP $HTTPRESULTCODE received." | tee -a $LOG_FILE | |
HTTPRESULTCODE=-1 | |
;; | |
(50*) | |
echo "Server error. Result code $HTTPRESULTCODE received; fix the server then try again." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "$HTTPRESULTCODE received; I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
if [ "$HTTPRESULTCODE" -ne 200 ] | |
then | |
echo "Sorry bub, none of the commands I know for AEM 6.2 worked." | tee -a $LOG_FILE | |
exit 1 | |
else | |
echo "Success." | tee -a $LOG_FILE | |
fi | |
;; | |
(63) | |
echo "Attempting AEM 6.3 SP0 Command..." # 6.3 expects two boolean parameters in the invoke POST, so we add --data to pass in required values in addition -d header statement. | |
HTTPRESULTCODE=$(curl -s -o /dev/null -u "$CQ_USER":"$CQ_USER_PASSWORD" -w "%{http_code}" --data "markOnly=false&forceBlobIdRetrieve=false" -d X-Requested-With=XMLHttpRequest -X POST "http://localhost:$PORT/system/console/jmx/org.apache.jackrabbit.oak%3Aname%3DSegment+node+store+blob+garbage+collection%2Ctype%3DBlobGarbageCollection/op/startBlobGC/boolean%2Cboolean") | |
case "$HTTPRESULTCODE" in | |
(200) | |
echo "BlobGC Successfully Triggered." | tee -a $LOG_FILE | |
;; | |
(40*) | |
echo "That didn't work. HTTP $HTTPRESULTCODE received." | tee -a $LOG_FILE | |
HTTPRESULTCODE=-1 | |
;; | |
(50*) | |
echo "Server error. Result code $HTTPRESULTCODE received; fix the server then try again." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
(*) | |
echo "$HTTPRESULTCODE received; I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac | |
if [ "$HTTPRESULTCODE" -ne 200 ] | |
then | |
echo "Sorry bub, none of the commands I know for AEM 6.3 worked." | tee -a $LOG_FILE | |
exit 1 | |
else | |
echo "Success." | tee -a $LOG_FILE | |
fi | |
;; | |
(*) | |
echo "I have no idea how I got here. Aborting." | tee -a $LOG_FILE | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment