Home > Guides > RightScale API 1.0 > RightScale API 1.0 Examples > Server Operations > Relaunch a Server

Relaunch a Server

Table of Contents


RestConnection has a helper to gracefully relaunch servers (recommended method). It is also possible with a quick and dirty Bash script.

Bash Example

#!/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
Sample Output
  

You must to post a comment.
Last Modified
21:57, 16 May 2013

Page Rating

Was this article helpful?

Tags

This page has no custom tags set.

Announcements

None

Glossary | 用語용어 Site Map | Site Help Community Corporate Site Get Support Dashboard Login
Doc Feedback Product Feedback Resources MultiCloud Marketplace Forums

Dashboard Status


© 2006-2014 RightScale, Inc. All rights reserved.
RightScale is a registered trademark of RightScale, Inc. All other products and services may be trademarks or servicemarks of their respective owners.