Skip to content

Instantly share code, notes, and snippets.

@abajwa-hw
Last active December 21, 2018 02:19
Show Gist options
  • Select an option

  • Save abajwa-hw/4bf004d0fb065d404760eaeebc15e74e to your computer and use it in GitHub Desktop.

Select an option

Save abajwa-hw/4bf004d0fb065d404760eaeebc15e74e to your computer and use it in GitHub Desktop.
Script to setup startup of HDP on boot
#curl -sSL https://gist.github.com/abajwa-hw/4bf004d0fb065d404760eaeebc15e74e/raw | sudo -E sh
#################################
## write out start_services.sh #
#################################
cat > /root/start_services.sh <<-'EOS'
export ambari_user=demokitadmin
export ambari_pass=BadPass#1
export log="/var/log/hdp_startup.log"
#detect name of cluster
output=`curl -s -u ${ambari_user}:${ambari_pass} -i -H 'X-Requested-By: ambari' http://localhost:8080/api/v1/clusters`
cluster_name=`echo $output | sed -n 's/.*"cluster_name" : "\([^\"]*\)".*/\1/p'`
echo "Starting cluster ${cluster_name}..." >> ${log}
#form start_cmd
read -r -d '' start_cmd <<EOF
curl -u ${ambari_user}:${ambari_pass} -i -H "X-Requested-By: blah" -X PUT -d '{"RequestInfo":{"context":"_PARSE_.START.ALL_SERVICES","operation_level":{"level":"CLUSTER","cluster_name":"'"${cluster_name}"'"}},"Body":{"ServiceInfo":{"state":"STARTED"}}}' http://localhost:8080/api/v1/clusters/${cluster_name}/services
EOF
#run command and get request id
echo "Requesting Ambari to start all.." >> ${log}
req_output=$(eval ${start_cmd})
req=$(echo ${req_output} | sed -n 's/.*"id" : \([0-9]*\),.*/\1/p')
#check status of request using its id
read -r -d '' status_cmd <<EOF
curl -u ${ambari_user}:${ambari_pass} -i -H "X-Requested-By: blah" -X GET http://localhost:8080/api/v1/clusters/${cluster_name}/requests/${req} | sed -n 's/.*"request_status" : "\([^\"]*\)".*/\1/p'
EOF
status=$(eval $status_cmd)
echo "status of request ${req} is ${status}"
#wait until request processing completed
until ([ "$status" != "IN_PROGRESS" ] && [ "$status" != "PENDING" ]);
do
echo "Waiting for start all operation to complete..." >> ${log}
sleep 5
status=$(eval $status_cmd)
done
#check last status: if request failed, retry once
if [ "${status}" == "FAILED" ]; then
echo "Start all failed. Retrying operation once more..." >> ${log}
sleep 5
eval ${start_cmd} >> ${log}
elif [ "${status}" == "COMPLETED" ]; then
echo "Start all operation completed. Cluster is ready" >> ${log}
else
echo "Operation did not succeed. Status was ${status}" >> ${log}
fi
EOS
chmod +x /root/start_services.sh
#####################
#append to rc.local #
#####################
cat <<'EOS' >> /etc/rc.local
echo "Sleeping for 30s..." > /var/log/hdp_startup.log
sleep 30
echo "generating new /etc/hosts that includes demo.hortonworks.com entry pointing to VMs ip, hostname..." >> /var/log/hdp_startup.log
curl -sSL https://gist.github.com/abajwa-hw/9d7d06b8d0abf705ae311393d2ecdeec/raw | sudo -E sh >> /var/log/hdp_startup.log
echo "hostname is $(hostname -f)" >> /var/log/hdp_startup.log
echo "hostnamectl output is $(hostnamectl)" >> /var/log/hdp_startup.log
if [ -e "/usr/hdp/current/knox-server/bin/gateway.sh" ]; then
echo "starting Knox..." >> /var/log/hdp_startup.log
mkdir /var/run/knox
chown knox:hadoop /var/run/knox
sudo -u knox /usr/hdp/current/knox-server/bin/gateway.sh start
sudo -u knox /usr/hdp/current/knox-server/bin/ldap.sh start
fi
echo "starting Ambari..." >> /var/log/hdp_startup.log
service ambari-server restart
service ambari-agent restart
ambari_up=0
until [ "$ambari_up" -eq "1" ];
do
echo "Waiting for Ambari to bind to port..." >> /var/log/hdp_startup.log
sleep 5
ambari_up=$(netstat -tulpn | grep 8080 | wc -l)
done
echo "Waiting 15s..." >> /var/log/hdp_startup.log
sleep 15
USER=demokitadmin
PASSWORD=BadPass#1
AMBARI_HOST=localhost
HOST=$(hostname -f)
#detect name of cluster
output=`curl -s -u ${USER}:${PASSWORD} -i -H 'X-Requested-By: ambari' http://localhost:8080/api/v1/clusters`
cluster_name=`echo $output | sed -n 's/.*"cluster_name" : "\([^\"]*\)".*/\1/p'`
# on first boot, update set Ambari admin password to accountId
if [ ! -f /root/.firstbootdone ]; then
echo "First boot detected..." >> /var/log/hdp_startup.log
#fetch accountId
eval $(curl -sSL http://169.254.169.254/latest/dynamic/instance-identity/document \
| awk -F\" '/:/ {print "export "$2"="$4}')
#if accountId was found, VM must be on AWS
if [ -n "${accountId}" ]; then
echo "AWS detected..." >> /var/log/hdp_startup.log
#Ambari admin user's password to AWS accountId
export ambari_password=${accountId}
export old_password="081339556850"
if [ "${old_password}" != "${ambari_password}" ]; then
echo "New accountid detected...resetting Ambari admin password to AWS accountId" >> /var/log/hdp_startup.log
read -r -d '' passwd_cmd <<EOF
curl -iv -u admin:${old_password} -H "X-Requested-By: blah" -X PUT -d '{ "Users": { "user_name": "admin", "old_password": "${old_password}", "password": "$ambari_password" }}' http://localhost:8080/api/v1/users/admin
EOF
eval ${passwd_cmd} >> /var/log/hdp_startup.log
#update Zeppelin admin user password to AWS accountId
#cd /var/lib/ambari-server/resources/scripts/
#zeppelinenv=$( ./configs.sh -u ${USER} -p ${PASSWORD} get localhost ${cluster_name} zeppelin-env )
#if [ $? -eq 0 ]; then
# echo "updating Zeppelin admin user password to AWS accountId..." >> /var/log/hdp_startup.log
# echo ${zeppelinenv} | sed -e '1,3d' \
# -e "/^\"shiro_ini_content\" : / s:admin = ${old_password}:admin = ${ambari_password}:" > /tmp/zeppelin-env.json
#
# #write updating configs
# ./configs.sh -u ${USER} -p ${PASSWORD} set localhost ${cluster_name} zeppelin-env /tmp/zeppelin-env.json >> /var/log/hdp_startup.log
#fi
else
echo "AWS accountid unchanged..keeping default password for ambari admin user" >> /var/log/hdp_startup.log
fi
fi
touch /root/.firstbootdone
else
echo "Instance was detected to be previously booted..skipping password reset" >> /var/log/hdp_startup.log
fi
#sleep until agent comes up
until [ $(curl -u ${USER}:${PASSWORD} -H X-Requested-By:blah http://{$AMBARI_HOST}:8080/api/v1/hosts/${HOST} | grep '"host_status"\s\+:\s\+"UNHEALTHY"' | wc -l) -eq "1" ];
do
echo "Cluster in unknown state...Sleeping for 10s while waiting for Ambari" >> /var/log/hdp_startup.log
sleep 10
done
echo "Sleeping for 25s..." >> /var/log/hdp_startup.log
sleep 25
status=$(curl -u ${USER}:${PASSWORD} -H X-Requested-By:blah http://{$AMBARI_HOST}:8080/api/v1/hosts/${HOST} | grep '"host_status"')
echo "Cluster Status: ${status}" >> /var/log/hdp_startup.log
echo "Requesting Ambari to start up HDP services..." >> /var/log/hdp_startup.log
/root/start_services.sh >> /var/log/hdp_startup.log
echo "Startup script complete." >> /var/log/hdp_startup.log
echo "Login to Ambari at http://$(curl icanhazip.com):8080 using username=admin and password=<Your AWS account number>" >> /var/log/hdp_startup.log
EOS
chmod a+x /etc/rc.local
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment