Note: Please go to docs.rightscale.com to access the current RightScale documentation set. Also, feel free to Chat with us!
Home > Guides > Chef Cookbooks Developer Guide > Developer > ServerTemplate Development > Common Development Tasks > Using RightScripts with Chef Recipes

Using RightScripts with Chef Recipes

Objective

To understand how to effectively use RightScripts with Chef cookbooks and recipes. 

Overview

Even though you may be using a Chef-based ServerTemplate that uses only Chef recipes, you can still safely use RightScripts with Chef recipes.

In this tutorial, you will perform the following actions:

  • Create a simple RightScript and add it to a Chef-based ServerTemplate that only contains cookbooks and recipes
  • Run the script on a server
  • SSH into the server for further investigation

In this example, you will execute a RightScript as an operational script. However, it's important to understand that RightScripts are always executed in the correct sequential order if they are listed as a boot or decommission script. For example, if a RightScript is placed inbetween Chef recipes of the boot script list, it will still be executed in the proper order. See Breakdown of a Server launched with a Chef-based ServerTemplate for details.

Steps

Create a RightScript

Use the sample code below to create a new RightScript. This particular bash script contains one input ($FIRSTNAME).

Name the RightScript accordingly. (e.g. HelloChef)

echo "Hello! My name is $FIRSTNAME"

Import a ServerTemplate

In this tutorial, the "base" ServerTemplate will be used for performing our test, however you could also use another Chef-based ServerTemplate of your choice and perform the same steps.

Go to the MultiCloud Marketplace (Design -> ServerTemplates) and import the following Chef-based ServerTemplate:

  • Base ServerTemplate for Linux (Chef)


Clone the ServerTemplate to create an editable HEAD version and name it accordingly.

Create and Launch a Server

Create a new deployment.  See Create a New Deployment.

Add a server into the deployment using the HEAD version of the ServerTemplate so that you can add a script to it later in this tutorial.  

Launch the server.

SSH into the Server

Once the server becomes operational, SSH into the server. Navigate to the following path and use the list command to view its contents. Since the ServerTemplate's list of boot scripts only uses Chef cookbooks and recipes, the directory is currently empty.

# cd /var/cache/rightscale/right_scripts/right_scripts_cookbook/recipes
# ls

Later, when you run the simple RightScript you created earlier, the 'right_scripts' directory will be populated with the files that are required to run the script.

Run the RightScript

Go to the ServerTemplate's HEAD version. Under the Scripts tab, add the simple "Hello" RightScript as an operational script.

Go to the server that you created and launched with the HEAD version of the ServerTemplate. (Tip: Use the ServerTemplate's Xref tab to find a shortcut link to the server.)

Go to the server's Scripts tab and run the script. (e.g. HelloChef) If you used our sample code, you will be prompted to provide a name for the FIRSTNAME input.

SSH into the Server

Wait for the script to be completed. 

SSH into the server once again. Navigate to the following path and use the list command to view its contents.  You should find two files that represent your RightScript in Chef.

# cd /var/cache/rightscale/right_scripts/right_scripts_cookbook/recipes
# ls
HelloChef  HelloChef.rb

 

Let's view the contents of both files to understand how they're used. 

# vi HelloChef

The first file is the 'source_file' and contains the original code of the RightScript.

echo "Hello! My name is $FIRSTNAME"

 

Exit the VI editor (Type 'Esc' and then 'Shift + q' to exit editing mode, type 'q' to quit.) and view the second file, which shows the ruby file that was auto-generated and executed by Chef.

# vi HelloChef.rb
right_script 'HelloChef' do
  parameters(node["HelloChef"]["parameters"])
  cache_dir  '/var/cache/rightscale/right_scripts_content/rs_attach23643247508960'
  source_file '/var/cache/rightscale/right_scripts/right_scripts_cookbook/recipes/HelloChef'
end

As you can see in the code above, Chef executes the ruby file in the run list, which uses the source file to execute the actual code. 

You can also go to the server's Audit Entries tab to view the audit entry.

*RS>                  RightScript: 'HelloChef'                   ****
17:54:51: Hello! My name is Dean
17:54:51: Script exit status: 0
17:54:51: Script duration: 0.38663
*RS> Duration: 2.77 seconds
17:54:51: Chef Run complete in 0.399483 seconds
*RS> completed: HelloChef

 

You must to post a comment.
Last modified
22:04, 16 May 2013

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.