Last active
March 3, 2021 20:51
-
-
Save onefoursix/d7aae5a026fa83f13d910d540f48afb8 to your computer and use it in GitHub Desktop.
bash script to restart StreamSets Job that waits for Job to deactivate before starting it again
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
#!/usr/bin/env bash | |
## restart-streamsets-job.sh | |
## A bash script that stops a StreamSets Job, waits for the Job to transition | |
## to an INACTIVE state and then starts the Job. If the Job is already in an | |
## INACTIVE state it will simply be started. | |
## Dependencies: | |
## jq must be installed - see https://stedolan.github.io/jq/ | |
## jq installation commands on RHEL7: | |
## sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm | |
## sudo yum install -y jq | |
## User variables ################################################ | |
# Job ID | |
JOB_ID= | |
# Control Hub User in the form <user>@<org> | |
SCH_USER= | |
# Control Hub Password | |
SCH_PASSWORD= | |
# Control Hub Base URL | |
SCH_BASE_URL=https://cloud.streamsets.com | |
# How long to pause between status checks when | |
# waiting for Job to become INACTIVE | |
# DO NOT SET A VALUE LESS THAN 5 | |
SLEEP_SECONDS=5 | |
# Maximum wait time for Job to become INACTIVE (in seconds) | |
MAX_WAIT_SECONDS=3600 | |
## Get a Control Hub Auth Token ################################## | |
curl -X POST $SCH_BASE_URL/security/public-rest/v1/authentication/login \ | |
--header "Content-Type:application/json" --header "X-Requested-By:SDC" \ | |
-d "{\"userName\":\"$SCH_USER\", \"password\": \"$SCH_PASSWORD\"}" \ | |
-c cookie.txt | |
# Extract the auth token from the response | |
AUTH_TOKEN=$(cat cookie.txt | grep SSO | rev | grep -o '^\S*' | rev) | |
# Delete cookie | |
rm -rf cookie.txt | |
echo "" | |
JOB_STATUS="" | |
## Get the current status of the Job ############################# | |
CURRENT_STATUS_URL=$SCH_BASE_URL/jobrunner/rest/v1/job/$JOB_ID/currentStatus | |
RESPONSE=`curl -s -X GET $CURRENT_STATUS_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
| jq '.status'` | |
# Check the status returned by Control Hub | |
if [ "$RESPONSE" = "\"INACTIVE\"" ]; then | |
JOB_STATUS="INACTIVE" | |
echo "`date`: Job $JOB_ID currently INACTIVE and will be started" | |
elif [ "$RESPONSE" = "\"ACTIVE\"" ]; then | |
JOB_STATUS="ACTIVE" | |
echo "`date`: Job $JOB_ID currently ACTIVE and will be stopped" | |
else | |
echo "`date`: Job $JOB_ID has status $RESPONSE and will try to be stopped" | |
fi | |
## Stop the Job (unless its status is already INACTIVE) ########################## | |
if [ ! "$JOB_STATUS" = "INACTIVE" ]; then | |
echo "`date`: Trying to stop Job $JOB_ID..." | |
STOP_JOB_URL=$SCH_BASE_URL/jobrunner/rest/v1/job/$JOB_ID/stop | |
RESPONSE=`curl -s -X POST $STOP_JOB_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
-d '{}' \ | |
| jq '.status'` | |
# Check the status returned by Control Hub | |
if [ "$RESPONSE" = "\"DEACTIVATING\"" ]; then | |
echo "`date`: Job $JOB_ID DEACTIVATED" | |
else | |
echo "`date`: Error stopping Job $JOB_ID" | |
echo "`date`: Error message: "$RESPONSE | |
echo "`date`: Quitting without trying to start Job $JOB_ID" | |
exit -1 | |
fi | |
echo "`date`: Waiting for DEACTIVATED Job to transition to INACTIVE status..." | |
# Keep track of how long we have waited for Job to become INACTIVE | |
WAIT_SECONDS=0 | |
# Wait for stopped Job to transition to INACTIVE status | |
while [ ! "$JOB_STATUS" = "\"INACTIVE\"" ] | |
do | |
sleep $SLEEP_SECONDS | |
WAIT_SECONDS=$((WAIT_SECONDS+SLEEP_SECONDS)) | |
# Exit if we have waited too long | |
if [ "$WAIT_SECONDS" -gt "$MAX_WAIT_SECONDS" ]; then | |
echo "`date`: Error stopping Job $JOB_ID" | |
echo "`date`: Exceeded timeout of $MAX_WAIT_SECONDS seconds waiting for Job to transition to INACTIVE" | |
echo "`date`: Quitting without trying to start Job $JOB_ID" | |
exit -1 | |
fi | |
# Get Job Status from Control Hub | |
JOB_STATUS=`curl -s -X GET $CURRENT_STATUS_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
| jq '.status'` | |
done | |
echo "`date`: Job $JOB_ID has transitioned to INACTIVE status" | |
fi | |
## Start the Job ########################################################## | |
echo "`date`: Trying to start Job $JOB_ID ..." | |
START_JOB_URL=$SCH_BASE_URL/jobrunner/rest/v1/job/$JOB_ID/start | |
RESPONSE=`curl -s -X POST $START_JOB_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
-d '{}' \ | |
| jq '.status'` | |
# Check the Job status returned by Control Hub | |
if [ "$RESPONSE" = "\"ACTIVATING\"" ]; then | |
echo "`date`: Job $JOB_ID successfully ACTIVATED" | |
exit 0 | |
else | |
echo "`date`: Error activating Job $JOB_ID" | |
exit -1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment