Note: Please go to docs.rightscale.com to access the current RightScale documentation set. Also, feel free to Chat with us!
Home > Guides > RightScale API 1.5 > Examples > Instances > Run Executable

Run Executable

Runs a RightScript or Chef Recipe on an active RightScale Server.

icon-curl_api-v1.png    icon-right_api_client-v1.png

Table of Contents

Curl

Prerequisite:  Example assumes you have previously authenticated, and your valid session cookie is in 'mycookie'.

Basic Example

Note:

  • This is similar to Multi-Run Executable, but instead of running on a multiple active servers, this script updates only one server.
  • This can only be executed on a RightScale Server with an active instance (the INSTANCEID variable is the instance ID which can be retrieved from a call such as List Servers).
  • When specifying RightScale input(s), you must declare both the name and value.
  • The example below runs a RightScript. 
#!/bin/sh -e
CLOUD="232"
INSTANCEID="9AOF4L3DC3IV4"  # Instance ID of the running instance. Obtain from the API
RIGHTSCRIPT="416366"        # RightScript ID to run.  Can get this from the API or the UI/URL.
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d right_script_href=/api/right_scripts/$RIGHTSCRIPT \
-d inputs[][name]="DNS_DOMAIN_ID" \
-d inputs[][value]="text:9623906" \
-d inputs[][name]="DNS_RECORD_ID" \
-d inputs[][value]="text:ExampleRightAPI" \
-d inputs[][name]="RACKSPACE_API_TOKEN" \
-d inputs[][value]="cred:RACKSPACE_AUTH_KEY" \
-d inputs[][name]="DNS_RECORD_IP" \
-d inputs[][value]="text:public" \
-d inputs[][name]="RAX_REGION" \
-d inputs[][value]="text:ORD(Chicago)" \
https://my.rightscale.com/api/clouds/$CLOUD/instances/$INSTANCEID/run_executable

Sample Output

HTTP/1.1 202 Accepted
Server: nginx/1.0.15
Date: Fri, 21 Dec 2012 00:05:26 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 202 Accepted
Location: /api/clouds/232/instances/9AOF4L3DC3IV4/live/tasks/ae-110022913001
X-Runtime: 2959
X-Request-Uuid: 31b3ee03337143b596fd70f457f47f82
Set-Cookie: 
Cache-Control: no-cache

Advanced Example

This example from rs_api_examples demonstrates running a recipe (also supports usage of API 1.0 from the CLI).

#!/bin/bash -e

# rs-run-recipe.sh <server_or_instance_id> <recipe_name> [[extra curl post params]]

# e.g.  rs-run-recipe.sh 516031001 "rightscale::default" "server[ignore_lock]=true"
#
#       For API 1.5:
#       rs_api_version=1.5 rs_cloud_id=1869 rs-run-recipe.sh 1SR4O53A7PLFA "rightscale::setup_timezone" "inputs[][name]=rightscale/timezone" "inputs[][value]=text:Australia/Sydney"

[[ ! $1 ]] && echo 'No server_id ID provided, exiting.' && exit 1
[[ ! $2 ]] && echo 'No recipe name provided, exiting.' && exit 1

. "$HOME/.rightscale/rs_api_config.sh"
. "$HOME/.rightscale/rs_api_creds.sh"

server_id="$1"
recipe="$2"
shift; shift
for arg in "$@"; do
	args+=(-d "$arg")
done

# needs all common public cloud IDs inserted or substitute with API call if possible
case "$rs_cloud_id" in 
  1)
    rs_cloud="AWS US-East"
  ;;
  1869)
    rs_cloud="Softlayer"
  ;;
  *)
    rs_cloud="$rs_cloud_id"
  ;;
esac

case $rs_api_version in
	*1.0*)
    api_url="https://my.rightscale.com/api/acct/$rs_api_account_id/servers/$server_id/current/run_executable"
    echo "Running Chef recipe [$recipe] on Server [$server_id]."
    echo "[API $rs_api_version] POST: $api_url"
    api_result=$(curl -s -S -i -X POST -b "$rs_api_cookie" -H X-API-VERSION:"$rs_api_version" -d server[recipe]="$recipe" "${args[@]}" "$api_url")
    case "$api_result" in 
      *Status:\ 201*)
        echo "$api_result" | awk '/^Location:/ { print $2 }'
      ;;
      *)
        echo "FAILED: $api_result"
      ;;
    esac
	;;
  *1.5*)
    instance_id="$server_id"
    api_url="https://my.rightscale.com/api/clouds/$rs_cloud_id/instances/$instance_id/run_executable"
    echo "Running Chef recipe [$recipe] on Instance [$server_id] in cloud [$rs_cloud]."
    echo "[API $rs_api_version] POST: $api_url"
    api_result=$(curl -s -S -i -X POST -b "$rs_api_cookie" -H X-API-VERSION:"$rs_api_version" -d recipe_name="$recipe" "${args[@]}" "$api_url")
    case "$api_result" in 
      *Status:\ 202\ Accepted*)
        echo "$api_result" | awk '/^Location:/ { print $2 }'
      ;;
      *)
        echo "FAILED: $api_result"
      ;;
    esac
	;;
esac

Sample Output

(includes the command issued)

$ rs_api_version=1.5 rs_cloud_id=1869 ./rs-run-recipe.sh 1SR4O53A7PLFA "rightscale::setup_timezone" "inputs[][name]=rightscale/timezone" "inputs[][value]=text:Australia/Sydney"
Running Chef recipe [rightscale::setup_timezone] on Instance [1SR4O53A7PLFA] in cloud [Softlayer].
[API 1.5] POST: https://my.rightscale.com/api/clouds/1869/instances/1SR4O53A7PLFA/run_executable
/api/clouds/1869/instances/1SR4O53A7PLFA/live/tasks/ae-123025717001

right_api_client

Prerequisite:  Example assumes you have previously authenticated using right_api_client.

Basic Example

Note:

  • This can only be executed on a RightScale Server with an active instance (the SERVERID variable is the server ID which can be retrieved from a call such as List Servers or by viewing the dashboard URL of the server).
  • The example below runs a RightScript.
  • When specifying RightScale input(s), you must declare both the name and value.

 

## Sets necessary variables for right_script_href and server ID and sets input names and values
script_href = "right_script_href=/api/right_scripts/500272003"
serverid = 920583003
## replace "INPUT1/2" and "VALUE1/2" with the name/value of your input accordingly
inputs = "&inputs[][name]=INPUT1&inputs[][value]=text:VALUE1&inputs[][name]=INPUT2&inputs[][value]=text:VALUE2"

## Runs the actual script/executable designated in script_href variable on the server
task = @client.servers(:id => serverid).show.current_instance.show.run_executable(script_href + inputs)

## Shows the task summary, which is the same as the audit entry summary in the dashboard (failed, completed, etc.)
task.show.summary

Sample Output

Note:

  • Sample output below comes from interactive Ruby shell (IRB) and uses sample script and server IDs. Your script HREF and server ID would be different in your own output.
  • task.show.summary is run 3 times to show the various "phases" of the script's execution on the server
script_href = "right_script_href=/api/right_scripts/500272003"
=> "right_script_href=/api/right_scripts/500272003"
serverid = 920583003
=> 920583003
inputs = "&inputs[][name]=INPUT1&inputs[][value]=text:VALUE1"
=> "&inputs[][name]=INPUT1&inputs[][value]=text:VALUE1"
task = @client.servers(:id => serverid).show.current_instance.show.run_executable(script_href)
=> #<RightApi::Resource resource_type="task">
task.show.summary
=> "Querying tags"
task.show.summary
=> "Preparing execution"
task.show.summary
=> "completed: GB API Test"

 

See also

You must to post a comment.
Last modified
17:19, 12 Nov 2013

Tags

This page has no custom tags.

Classifications

This page has no classifications.

Announcements

None


© 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.