RestConnection has a helper to gracefully relaunch servers (recommended method). It is also possible with a quick and dirty Bash script.
#!/bin/bash -e
# rs-relaunch-server
source ./rs-login # login and store cookie
# Set variables
server_id="$SERVER_ID"
if [ ! "$server_id" ]; then
echo 'No server ID provided, exiting.'
exit
fi
## functions
# get server's operational state
server_state() {
echo "Getting server's operational state."
state_xml=$(
curl -s -H "X-API-VERSION:$rs_api_version" -b "$rs_api_cookie" \
https://my.rightscale.com/api/acct/"$rs_api_account_id"/servers/"$server_id"/current | \
grep state
) || no_state=1
if [ ! "$no_state" ]; then
state_xml="${state_xml#*>}"
state="${state_xml%<*}"
echo ' state: '"$state"
else
echo 'Server is not operational.'
fi
}
# terminate the server
stop_server() {
echo 'Terminating the server.'
stop_result=$(
curl -d api_version="$rs_api_version" -b "$rs_api_cookie" -sL -w "\\n%{http_code} %{url_effective}" \
https://my.rightscale.com/api/acct/"$rs_api_account_id"/servers/"$server_id"/stop
)
stop_code=$(tail -n1 <<< $stop_result | awk '{print $1}')
[ "$stop_code" = "201" ] || ( echo "$stop_result"; echo 'Failed to stop server.' )
}
# launch the server
start_server() {
echo 'Launching the server.'
start_result=$(
curl -d api_version="$rs_api_version" -b "$rs_api_cookie" -sL -w "\\n%{http_code} %{url_effective}" \
https://my.rightscale.com/api/acct/"$rs_api_account_id"/servers/"$server_id"/start
)
echo "$start_result"
}
wait_for_terminate() {
echo 'Waiting for the server to terminate.'
while sleep 25s; do
unset state
server_state
( [ ! "$state" ] || [ "$state" = 'terminated' ] ) && break # wait for no/terminated state
echo '.'
done
}
# first, get the server's operational state
server_state
# terminate the server when operational only
case "$state" in
operational|pending)
stop_server
wait_for_terminate
start_server
;;
decommissioning|shutting-down)
wait_for_terminate
start_server
;;
terminated)
start_server # start server on no or unknown operational state
;;
*)
start_server
;;
esac
| Site Map | Community | Training | Corporate Site | Get Support | Dashboard Login | |
| Product Feedback | Resources | Forums | MultiCloud Marketplace | Support Tickets |