Last active
December 17, 2021 06:01
-
-
Save lvazquez/7e2e4b83c384a111e235451e1e1c40c6 to your computer and use it in GitHub Desktop.
Ambari Control script based in randerzander's control.sh
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
AMBARI_USER='admin' | |
AMBARI_PASS='admin' | |
AMBARI_AUTH='/etc/ambari-server/ambari-control.auth' | |
AMBARI_PROPS='/etc/ambari-server/conf/ambari.properties' | |
PROG=`basename $0` | |
HOST=$(hostname -f) | |
AMBARI_CLUSTER="" | |
LIMIT=10 | |
echomsg() { | |
echo "$@" 1>&2; | |
} | |
# Display usage info and exit the program | |
function usage() { | |
echo "Usage: $PROG | |
[cluster|{start|stop|maint|unmaint|check} {ALL|SERVICE}] | |
* cluster: get cluster name | |
* start/stop SERVICE: start/stop given service | |
* maint/unmaint SERVICE: turn maintenence mode on/off for given service | |
* check SERVICE: check status for given service. | |
* ... " 1>&2 | |
exit 1 | |
} | |
# Load authentication info | |
if [ -a "$AMBARI_AUTH" ]; then | |
. "$AMBARI_AUTH" | |
fi | |
# Check Ambari properties file to detect if using SSL | |
SSL=$(cat ${AMBARI_PROPS}|perl -n -e '/api.ssl\s*=\s*(\w+)/ && print $1') | |
# Define connection parameters | |
if [[ "$SSL" = "true" ]] | |
then | |
PORT=8443 | |
CURL_CMD="curl -k -s -u $AMBARI_USER:$AMBARI_PASS" | |
API_BASE="https://${HOST}:${PORT}/api/v1/clusters" | |
else | |
PORT=8080 | |
CURL_CMD="curl -s -u $AMBARI_USER:$AMBARI_PASS" | |
API_BASE="http://${HOST}:${PORT}/api/v1/clusters" | |
fi | |
# At least action argument is needed | |
if [[ "$#" -lt 1 ]]; then | |
usage | |
else | |
action=$1 | |
fi | |
################################################# | |
# Ambari Server detect and startup section | |
AMBARI_SERVER=$(which ambari-server) | |
if [[ ! -x ${AMBARI_SERVER} ]] | |
then | |
echomsg "Unable to find Ambari Server exec" | |
exit 1 | |
else | |
if ! ${AMBARI_SERVER} status >/dev/null | |
then | |
echomsg "Ambari Server is not running, starting..." | |
${AMBARI_SERVER} start 1>&2 | |
ret=$? | |
if [[ ! $ret ]]; then | |
echomsg "ERROR: failed to start ambari-server" | |
exit 1 | |
fi | |
fi | |
fi | |
################################################# | |
# Ambari REST API connection checkout | |
count=0 | |
echomsg -n "Connecting to REST API" | |
while ! response=$($CURL_CMD -H 'X-Requested-By: ambari' $API_BASE) && [[ $count -le $LIMIT ]] | |
do | |
sleep 1 && echomsg -n "." | |
count=$((count+1)) | |
done | |
echomsg | |
if [[ $count -gt $LIMIT ]] | |
then | |
echomsg "ERROR: unable to connect to Ambari API" | |
exit 1 | |
fi | |
################################################# | |
# Detect cluster name | |
AMBARI_CLUSTER=$(echo $response|perl -n -e '/cluster_name"\s+:\s+"(\w+)"/ && print $1') | |
if [ -z "$AMBARI_CLUSTER" ]; | |
then | |
echomsg "ERROR: unable to get cluster name" | |
exit -1 | |
else | |
export $AMBARI_CLUSTER; | |
echomsg "OK: Connected to cluster $AMBARI_CLUSTER" | |
fi | |
SERVICES_URI="$API_BASE/$AMBARI_CLUSTER/services" | |
################################################# | |
# Service control functions | |
################################################# | |
function startAll(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI} \ | |
-d '{"RequestInfo":{"context":"_PARSE_.START.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"$AMBARI_CLUSTER"}},"Body":{"ServiceInfo":{"state":"STARTED"}}}' | |
} | |
function stopAll(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI} \ | |
-d '{"RequestInfo":{"context":"_PARSE_.STOP.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"$AMBARI_CLUSTER"}},"Body":{"ServiceInfo":{"state":"INSTALLED"}}}' | |
} | |
function start(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI}/$1 \ | |
-d '{"RequestInfo": {"context" :"Start '"$1"' via REST"}, "Body": {"ServiceInfo": {"state": "STARTED"}}}' | |
} | |
function startWait(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI}/$1 \ | |
-d '{"RequestInfo": {"context" :"Start '"$1"' via REST"}, "Body": {"ServiceInfo": {"state": "STARTED"}}}' | |
wait $1 "STARTED" | |
} | |
function stop(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI}/$1 \ | |
-d '{"RequestInfo": {"context" :"Stop '"$1"' via REST"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}}' | |
} | |
function stopWait(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT -s ${SERVICES_URI}/$1 \ | |
-d '{"RequestInfo": {"context" :"Stop '"$1"' via REST"}, "Body": {"ServiceInfo": {"state": "INSTALLED"}}}' | |
wait $1 "INSTALLED" | |
} | |
function maintOff(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI}/$1 \ | |
-d '{"RequestInfo":{"context":"Turn Off Maintenance Mode"},"Body":{"ServiceInfo":{"maintenance_state":"OFF"}}}' | |
} | |
function maintOn(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X PUT ${SERVICES_URI}/$1 \ | |
-d '{"RequestInfo":{"context":"Turn Off Maintenance Mode"},"Body":{"ServiceInfo":{"maintenance_state":"ON"}}}' | |
} | |
function delete(){ | |
$CURL_CMD -H 'X-Requested-By: ambari' -X DELETE ${SERVICES_URI}/$1 | |
} | |
function wait(){ | |
finished=0 | |
count=0 | |
while [ $finished -ne 1 -a $count -lt $LIMIT ] | |
do | |
str=$($CURL_CMD -H 'X-Requested-By: ambari' ${SERVICES_URI}/$1) | |
if [[ $str == *"$2"* ]] || [[ $str == *"Service not found"* ]] | |
then | |
finished=1 | |
else | |
count=$((count+1)) | |
fi | |
sleep 2 | |
done | |
} | |
function checkService() { | |
output=$($CURL_CMD -H 'X-Requested-By: ambari' ${SERVICES_URI}/$1) | |
regex='\"status\"[[:space:]]+\:[[:space:]]+404' | |
if [[ -z "$output" || "$output" =~ $regex ]]; then | |
STATE="NOTFOUND" | |
RET=1 | |
else | |
STATE=`echo $output|perl -n -e '/"state"\s+:\s+"(\w+)"/ && print $1'` | |
if [[ -z "$STATE" ]]; then | |
STATE="UNKNOWN" | |
RET=1 | |
fi | |
RET=0 | |
fi | |
echo $STATE | |
return $RET | |
} | |
function checkComponent() { | |
str=$($CURL_CMD -H 'X-Requested-By: ambari' ${SERVICES_URI}/$1) | |
regex='\"component_name\"[[:space:]]+\:[[:space:]]+\"'$2'\"' | |
if [[ "$str" =~ $regex ]] | |
then | |
echo 0 | |
else | |
echo 1 | |
fi | |
} | |
ret=0 | |
if [[ $action == "cluster" ]]; then | |
echo $AMBARI_CLUSTER | |
else | |
if [[ "$#" -gt 1 ]]; then | |
SERVICE=`echo $2 | tr [a-z] [A-Z]` | |
if [[ -z "$SERVICE" ]]; then | |
echomsg "ERROR: Invalid empty service name" | |
usage | |
fi | |
else | |
echomsg "ERROR: Illegal number of parameters" | |
usage | |
fi | |
case "$1" in | |
start) | |
if [[ "$SERVICE" == "ALL" ]]; then | |
echomsg "Starting All Services" | |
startAll | |
ret=$? | |
else | |
echomsg "Starting $SERVICE via REST API" | |
start $SERVICE | |
ret=$? | |
fi | |
;; | |
stop) | |
if [[ "$SERVICE" == "ALL" ]]; then | |
echomsg "Stopping All Services" | |
stopAll | |
ret=$? | |
else | |
echomsg "Stopping $SERVICE via REST API" | |
stop $SERVICE | |
ret=$? | |
fi | |
;; | |
maint) | |
echomsg "Turn On Maintenance Mode for $SERVICE via REST API" | |
maintOn $SERVICE | |
ret=$? | |
;; | |
unmaint) | |
echomsg "Turn Off Maintenance Mode for $SERVICE via REST API" | |
maintOff $SERVICE | |
ret=$? | |
;; | |
check) | |
status=$(checkService $SERVICE) | |
ret=$? | |
echomsg "Checking Status for $SERVICE: $status" | |
echo $status | |
;; | |
*) | |
usage | |
;; | |
esac | |
fi | |
exit $ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment