Note: Please go to docs.rightscale.com to access the current RightScale documentation set. Also, feel free to Chat with us!
Home > Guides > Windows User Guide > Windows Tutorials > 'Hello World' Chef Tutorial for Windows

'Hello World' Chef Tutorial for Windows

Objective

Create a Chef Recipe for a Windows server.  When this recipe is run, the user will be prompted to supply their name as a value to a Recipe Input (NAME).  The Recipe will then create a file called c:\helloworld.txt, and the contents of this file will be

Hello World! My name is [NAME]

Upon completion of this tutorial, you should be able to:

  • Use a Git client to duplicate a repository on GitHub
  • Create a new Chef Recipe in your repo
  • Add your new repository to your RightScale account
  • Launch a Windows server and run your Chef Recipe

 

Table of Contents

Prerequisities

This tutorial assumes you are familiar with the RightScale Dashboard, importing ServerTemplates from the MultiCloud Marketplace and launching Windows servers.

  • Powershell must be installed on your local computer. To download and install Powershell, go to http://www.microsoft.com/powershell.
  • If you are launching a server into a cloud that support security groups and/or SSH keys, you will need to have existing resources or create new ones when you create and launch a test server. The server will need TCP port 3306 open to allow RDP connections.

Overview

There are quite a number of distinct steps required to complete this tutorial, as outlined in the Table of Contents above.  This tutorial will step you through all the commands that you'll need to complete the task, so no previous Git experience is required.  However, if you are not familiar with Git, then there are a number of tutorials that will help you, including the following:

Also, the tutorial assumes you will be using Git in a Windows environment.  If you are using Linux or Mac OS, then the installation steps will be different.

Steps

Set up Git

The easiest way to install and configure Git on your local Windows machine is to download and install the GitHub client application.

Create a New Repository

  1. Start the GitHub client application. (Start > GitHub, Inc > GitHub)
  2. Log into the application with your GitHub account information and proceed to the dashboard. If you do not have a GitHub account, you can sign-up for a free account from the login screen.
  3. Click the Create link.
  4. Provide the required information about the repository.
    screen-NewRepository-v1.png
    • Enter a name for the new repository (e.g. cheftutorial) and a helpful description (optional).
    • Specify the location on your local computer that will act as the source location for the contents of the repository.
    • Since this tutorial is for demonstration purposes only, keep the "Push to github" option checked.
    • Select the GitHub account under which the repository will belong.
  5. Click Create.

Clone a Repository

In the first step is to populate your new repository (e.g. cheftutorial) with the contents from another repository.

  1. Open a Command Prompt and navigate to your local GitHub repositories.
C:\Users\<user>\Documents\GitHub\cheftutorial>cd ..
C:\Users\<user>\Documents\GitHub>
  1. Navigate to your local directory for your repository and run the following shell command to create a new folder (cookbooks_public_windows) with the contents of an existing repository.
  2. C:\users\<user>\documents\github>git clone https://github.com/rightsc
    ale/cookbooks_public_windows
    Cloning into 'cookbooks_public_windows'...
    remote: Counting objects: 724, done.
    remote: Compressing objects: 100% (298/298), done.
    remote: Total 724 (delta 415), receiving objects:  93% (674/724), 7.14 Mied
    Receiving objects:  9
    Receiving objects: 100% (724/724), 7.29 MiB | 791.00 KiB/s, done.
    Resolving deltas: 100% (415/415), done.
    Checking connectivity... done
    Checking out files: 100% (129/129), done.

 

  1. Keep the Command Prompt window open.
  2. Using Windows Explorer, copy all of the contents from the 'cookbooks_public_windows' directory and paste it into the 'cheftutorial' directory.

Add a New Recipe

In this tutorial, you're going to add a new recipe to an existing cookbook, which will save us some time because we can simply modify the metadata (metadata.rb) of an existing cookbook instead of creating a new cookbook and all of its components from scratch. 

  1. In the Command Prompt window, run the following command to create a new file that will become a new Chef recipe in the utilities cookbook. Navigate to the recipes folder of the utilities cookbook.
cd cheftutorial/cookbooks/utilities/recipes
touch cheftutorial.rb
  1. In Windows Explorer, locate the new file (cheftutorial.rb) and open it with Notepad, or another text editor.
  2. Copy and paste the following script example. The example code is a Ruby script that contains some Powershell code. This particular script is going to create a text file called hellowworld.txt and save it locally to the Windows server, where the value of a user-defined input will determine the contents of the created text file. Note: The spacing in the script is important. There should be no leading spaces before POWERSHELL_SCRIPT on line 7.
# Cookbook Name:: utilities
# Recipe:: Chef Tutorial
powershell "Chef Tutorial" do
parameters({'MYNAME' => @node[:utilities][:MYNAME]})
  powershell_script = <<'POWERSHELL_SCRIPT'
  echo "Hello World! My name is $env:MYNAME" > c:\helloworld.txt
POWERSHELL_SCRIPT
  source(powershell_script)
end

 

  1. Save your changes and close the file.

Update the Metadata

Since you modified the contents of the utilities cookbook, the next step is to update its metadata accordingly. In this example, you made the following modifications.

  • Created a new recipe called cheftutorial
  • Created a new input called MYNAME

 

  1. In Windows Explorer, find and open the metadata.rb file of the utilities cookbook in a text editor.
  2. Add the following line to the list of recipes in the cookbook's metadata. (See line 16 in the screenshot below.)
recipe "utilities::cheftutorial", "Create a helloworld.txt file with a custom hello world message"
  1. Add the following lines of code to the list of attributes. (See lines 18-22 in the screenshot below.) You must declare Chef attributes that you want to be displayed as user-defined inputs in the RightScale Dashboard.
attribute "utilities/MYNAME",
  :display_name => "First Name",
  :description => "Please type your first name",
  :recipes => ["utilities::cheftutorial"],
  :required => "required"

 

screen-UpdateMetadata-v1.png

Add the Changes

Before you can commit and push your changes to the hosted (GitHub) repository, you must first specify which files you're going to add.

  1. In the Command Prompt, navigate to the local repository and run the following command to add all of your changes.
cd \users\<user>\documents\github\cheftutorial
git add *
git status

Commit and Publish the Changes

  1. In the Git client application, provide a helpful description of the commit action and commit the changes that you made to the local repository. 

screen-CommitChanges-v2.png

  1. Next, click the Publish button to push the commit of changes to your hosted (GitHub) repository.
  2. Go to your hosted repository to view your latest changes.

screen-NewGitHubRepo-v2.png

 

Add the Repository to the RightScale Account

  1. In the RightScale Dashboard go to Design > Repositories.
  2. Click Add Repository and provide the required information about the repository.
    screen-NewRepository-v2.png
    • Name & Description - Provide a helpful name and description.
    • Type - Specify the type of software repository that contains your cookbooks.
    • Tag/Branch - Specify the name of the tag/branch within the specified repository.
    • URL - Specify the source location of the software repository. 
    • Git SSH Key - Specify an SSH Key for authentication purposes. It's only required for private repositories. If it's a public repository, you can leave this field blank/unselected.
    • Cookbooks Path - Specify the path inside the software repository where the cookbooks can be found. For this example, add 'cookbooks'.
  3. Click OK.

 

Import the Base ServerTemplate

  1. Go to the MultiCloud Marketplace (Design > MultiCloud Marketplace > ServerTemplates) and import the following ServerTemplate. (Requires 'library' user role privileges.)
  2. Clone the ServerTemplate and rename it accordingly. (e.g. My Chef Tutorial)
  3. Go to the cloned ServerTemplate's Scripts tab and click Modify.
  4. Click Attach Cookbooks.
  5. Select and attach the utilities cookbook from the repository that you just added to RightScale. (Note: The "Source" of the cookbook should match the name of the Repository that you created in the previous step. (e.g. Chef Tutorial))
  6. Once the cookbook is attached to the ServerTemplate you can add any recipes from that cookbook into one of the ServerTemplate's boot/operational/decommission sections. Expand the view of the cookbook so that you can see its recipes. Drag and drop the utilities::cheftutorial recipe into the Operational Scripts section.

    screen-AddScript-v1.png

Launch a Test Server

The next step is to launch a server using the customized ServerTemplate that has the demo script.

  1. Create a new deployment for testing purposes.  See Create a New Deployment.
  2. Add a new server to the deployment using the customized ServerTemplate (e.g. My Chef Tutorial). See Add a Server to a Deployment.
  3. Launch the server.
  4. At the Input Confirmation screen you will need to provide values for the missing and required inputs. Provide appropriate values and click Launch.
  5. Wait for the server to become "operational" before you proceed to the next step.

Test the Script

  1. Go to the Scripts tab of the "current" operational server.
  2. Under the "Operational Scripts" section run the test script (utilities::cheftutorial)
  3. Since the script uses an input, you will need to provide your name before you can run the script.
    screen-InputName-v1.png
  4. Go to the server's Audit Entries tab to see the detailed message about the execution of the script.
  5. Click the RDP button to create a Remote Desktop Connection into the running server. Find and open the 'c:\helloworld.txt' text file that the Chef recipe created to view the custom message with your name.

    screen-RDP_HelloWorld-v1.png

 

You must to post a comment.
Last modified
17:12, 3 Dec 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.