Skip to content

Instantly share code, notes, and snippets.

@bmwh
Created May 22, 2015 00:54
Show Gist options
  • Save bmwh/a6f462f228191c9c048b to your computer and use it in GitHub Desktop.
Save bmwh/a6f462f228191c9c048b to your computer and use it in GitHub Desktop.
#!/bin/bash
# shutdown.sh
# Cleanly shut down CDH and Cloudera Manager via the REST API
# -----------------------------------------------------------------------
# Cloudera Manager credentials
USERNAME=admin
PASSWORD=admin
# Cloudera Manager connection details
CMHOST=localhost
CMPORT=7180
CMSCHEME=http
# Cluster and service name (URL encoded)
CLUSTER=cluster
# Maximum time (in seconds) before giving up
TIMEOUT=600
# -----------------------------------------------------------------------
# Stop CDH
BASEURL=$CMSCHEME://$CMHOST:$CMPORT
start=`date '+%s'`
curl -s -u "$USERNAME":"$PASSWORD" -X POST \
"$BASEURL/api/v1/clusters/$CLUSTER/commands/stop" >/dev/null
echo
echo -n Stopping $CLUSTER...
while curl -s -u "$USERNAME":"$PASSWORD" \
"$BASEURL/api/v1/clusters/$CLUSTER/services/$SERVICE" \
| grep 'serviceState' | grep -v 'STOPPED' | grep -q 'serviceState' ; do
now=`date '+%s'`
if [ $(($now - $start)) -gt $TIMEOUT ] ; then
echo timed out after $TIMEOUT seconds
exit 1
fi
echo -n .
sleep 5
done
echo done
# Stop CM services
start=`date '+%s'`
curl -s -u "$USERNAME":"$PASSWORD" -X POST \
"$BASEURL/api/v1/cm/service/commands/stop" >/dev/null
echo
echo -n Stopping CM services...
while ! curl -s -u "$USERNAME":"$PASSWORD" \
"$BASEURL/api/v1/cm/service" \
| grep 'serviceState' | grep -q 'STOPPED' ; do
now=`date '+%s'`
if [ $(($now - $start)) -gt $TIMEOUT ] ; then
echo timed out after $TIMEOUT seconds
exit 1
fi
echo -n .
sleep 5
done
echo done
echo
echo Shutting down all nodes with shutdown -h...
for i in `seq 12 16` 11 ; do
ssh 192.168.0.$i shutdown -h now
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment