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

MindTouch
Copyright (c) 2006-2014 MindTouch Inc.
http://mindtouch.com

This file and accompanying files are licensed under the MindTouch Master Subscription Agreement (MSA).

At any time, you shall not, directly or indirectly: (i) sublicense, resell, rent, lease, distribute, market, commercialize or otherwise transfer rights or usage to: (a) the Software, (b) any modified version or derivative work of the Software created by you or for you, or (c) MindTouch Open Source (which includes all non-supported versions of MindTouch-developed software), for any purpose including timesharing or service bureau purposes; (ii) remove or alter any copyright, trademark or proprietary notice in the Software; (iii) transfer, use or export the Software in violation of any applicable laws or regulations of any government or governmental agency; (iv) use or run on any of your hardware, or have deployed for use, any production version of MindTouch Open Source; (v) use any of the Support Services, Error corrections, Updates or Upgrades, for the MindTouch Open Source software or for any Server for which Support Services are not then purchased as provided hereunder; or (vi) reverse engineer, decompile or modify any encrypted or encoded portion of the Software.

A complete copy of the MSA is available at http://www.mindtouch.com/msa

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

Tags

This page has no custom tags.

Classifications

This page has no classifications.

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.