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 > End-to-End Examples > 3 Tier E2E API Example > Create 3Tier Servers

Create 3Tier Servers

Objective

Use the RightScale API to create servers for a sample 3 tier deployment based on settings in configuration files.

Table of Contents

Overview

Introduction

The RightScale Support Portal tutorials and Training exercises often include a familiar production-like 3 tier architecture for running and managing your web servers in the cloud.  This tutorial uses the API to create a 3 tier architecture consisting of 6 servers:

  • 2 HA proxy load balancers
  • 2 PHP application servers
  • 2 MySQL 5.5 database servers

Use Case

A possible use case for automating this procedure is a training environment where the ability to effectively "reset" the students environment is a simple script/API call away.  Perhaps you have already trained your staff on the basics of the RightScale Dashboard.  They know how to login, navigate, create a deployment, SSH keys, security groups, etc.  However, their training environment often gets broken or muddled, so reconfiguring in an efficient manner to a "known" starting point is desirable.  Then students can configure security group rules, download/configure your application, change deployment inputs, spin up servers in the proper order, lock them, etc. 

Advantages

There are other potential advantages to using the API in a manner such as discussed in this 3 tier tutorial, such as:

  • Environment control.  Locking down and controlling your training environment, right down to the cloud and ServerTemplates used.  In a training environment, using a HEAD version of a published ServerTemplate could sneak up and surprise you.  For example, input name or default values could change or the introduction of new inputs could alter the behavior of the ServerTemplate.  Programmatically creating your environment quickly and in a repeatable fashion may suit your needs.
  • Out of band usage.  That is, ability to create this environment external to the RightScale Dashboard.  (You could even invoke the script using the API from a smart phone. For example, using a SSH client to access a host and invoke an API/script to create the environment, terminate or relaunch a server, etc.)

Steps

Configure

This script uses two very basic configuration files.  The settings are read in from these files then used within the script.  That way, you can configure one time, and run as often as required.

Warning!  There is no input validation.  The correct IDs are assumed, and a space is used for the delimiter. 

  • generic.cfg - Contains three resource IDs on one line separated by white space. They are:
    • <Cloud_ID>  <Deployment_ID>  <SecurityGroup_ID¹>
    • Example:  2117 316785001 DEU7O3167M2J4
  • 3tier.cfg - Contains the ServerTemplate, MCI and Instance Type resource IDs for each tier (load balancer tier (LB), application tier (APP) and database (DB)).  The format is also white space delimited on a single line.  (Note: Shown on three lines below for readability.)
    • <ServerTemplate_LB>  <MCI_LB>  <Itype_LB>
    • <ServerTemplate_App>  <MCI_App>  <Itype_App>
    • <ServerTemplate_DB>  <MCI_DB>  <Itype_DB>
    • Ex:  252299001 240802001 9F6N6MA39F7E9 252293001 240802001 9F6N6MA39F7E9 269641001 240802001 9F6N6MA39F7E9

Note that if a ServerTemplate in your environment changes for an reason (new image, instance type size, inputs, etc.) you can modify your configuration file and re-run your script.  No code changes are necessary once you have your script correct.

 

¹ Reminder: Not all clouds support the same cloud resources.  In our example, security groups are included.  Some clouds do not support security groups. If this is the case, simply remove this entry from your generic.cfg file, as well as references to the $SG (security group) variable in the script.

Authenticate

As always, you must be authenticated prior to using the RightScale API.

#!/bin/sh -e
email="john.doe@example.com"   # The email address for your RightScale User in the Dashboard
pswd="SomeSecurePassword"      # Your User's password
account="1234"                 # Account ID, easily obtained from navigation in the Dashboard

curl -i -H X_API_VERSION:1.5 -c mycookie -X POST --data-urlencode "email=$email" --data-urlencode "password=$pswd" -d account_href=/api/accounts/"$account" https://my.rightscale.com/api/session

Note Although OAuth is preferred for production environments, the Examples section of the API guide uses standard user/pass authentication for simplicity sake.

If you have not registered with RightScale, sign up for FREE!  

Create Script

Although the contents of our example script to create all the cloud resources is a bit long, the logic used is quite simple.  The trick is configuring it correctly, and constructing the URLs appropriately. 

#!/bin/sh -e
#
if [ $# -ne "1" ]; then
        echo "Usage:  3tier ServerNamePrefix"
        echo "ServerNamePrefix will get prepended to all created servers"
        echo "Exiting . . ."
        exit 0
fi
# CONFIGURATION
echo "Reading configuration files . . . "
read CLOUD DEPLOYMENT SG < generic.cfg
read ST_LB MCI_LB ITYPE_LB ST_APP MCI_APP ITYPE_APP ST_DB MCI_DB ITYPE_DB < 3tier.cfg
echo "Done (config)"
echo "Values set: $ST_LB $MCI_LB $ITYPE_LB $ST_APP $MCI_APP $ITYPE_APP $ST_DB $MCI_DB $ITYPE_DB"

# Create Load Balancers
echo "Create Load Balancer #1"
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d server[name]=$1_load_balancer1 \
-d server[description]=$1_load_balancer1 \
-d server[deployment_href]=/api/deployments/$DEPLOYMENT \
-d server[instance][cloud_href]=/api/clouds/$CLOUD \
-d server[instance][server_template_href]=/api/server_templates/$ST_LB \
-d server[instance][multi_cloud_image_href]=/api/multi_cloud_images/$MCI_LB \
-d server[instance][instance_type_href]=/api/clouds/$CLOUD/instance_types/$ITYPE_LB \
-d server[instance][security_group_hrefs][]=/api/clouds/$CLOUD/security_groups/$SG \
https://my.rightscale.com/api/servers
echo "Done (load bal 1)"

echo "Create Load Balancer #2"
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d server[name]=$1_load_balancer2 \
-d server[description]=$1_load_balancer2 \
-d server[deployment_href]=/api/deployments/$DEPLOYMENT \
-d server[instance][cloud_href]=/api/clouds/$CLOUD \
-d server[instance][server_template_href]=/api/server_templates/$ST_LB \
-d server[instance][multi_cloud_image_href]=/api/multi_cloud_images/$MCI_LB \
-d server[instance][instance_type_href]=/api/clouds/$CLOUD/instance_types/$ITYPE_LB \
-d server[instance][security_group_hrefs][]=/api/clouds/$CLOUD/security_groups/$SG \
https://my.rightscale.com/api/servers
echo "Done (load bal 2)"

echo "Create App Server #1"
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d server[name]=$1_app_server1 \
-d server[description]=$1_app_server1 \
-d server[deployment_href]=/api/deployments/$DEPLOYMENT \
-d server[instance][cloud_href]=/api/clouds/$CLOUD \
-d server[instance][server_template_href]=/api/server_templates/$ST_APP \
-d server[instance][multi_cloud_image_href]=/api/multi_cloud_images/$MCI_APP \
-d server[instance][instance_type_href]=/api/clouds/$CLOUD/instance_types/$ITYPE_APP \
-d server[instance][security_group_hrefs][]=/api/clouds/$CLOUD/security_groups/$SG \
https://my.rightscale.com/api/servers
echo "Done (app server 1)"

echo "Create App Server #2"
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d server[name]=$1_app_server2 \
-d server[description]=$1_app_server2 \
-d server[deployment_href]=/api/deployments/$DEPLOYMENT \
-d server[instance][cloud_href]=/api/clouds/$CLOUD \
-d server[instance][server_template_href]=/api/server_templates/$ST_APP \
-d server[instance][multi_cloud_image_href]=/api/multi_cloud_images/$MCI_APP \
-d server[instance][instance_type_href]=/api/clouds/$CLOUD/instance_types/$ITYPE_APP \
-d server[instance][security_group_hrefs][]=/api/clouds/$CLOUD/security_groups/$SG \
https://my.rightscale.com/api/servers
echo "Done (app server 2)"

echo "Create DB Server #1"
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d server[name]=$1_db_server1 \
-d server[description]=$1_db_server1 \
-d server[deployment_href]=/api/deployments/$DEPLOYMENT \
-d server[instance][cloud_href]=/api/clouds/$CLOUD \
-d server[instance][server_template_href]=/api/server_templates/$ST_DB \
-d server[instance][multi_cloud_image_href]=/api/multi_cloud_images/$MCI_DB \
-d server[instance][instance_type_href]=/api/clouds/$CLOUD/instance_types/$ITYPE_DB \
-d server[instance][security_group_hrefs][]=/api/clouds/$CLOUD/security_groups/$SG \
https://my.rightscale.com/api/servers
echo "Done (db server #1)"

echo "Create DB Server #2"
curl -i -H X_API_VERSION:1.5 -b mycookie -X POST \
-d server[name]=$1_db_server2 \
-d server[description]=$1_db_server2 \
-d server[deployment_href]=/api/deployments/$DEPLOYMENT \
-d server[instance][cloud_href]=/api/clouds/$CLOUD \
-d server[instance][server_template_href]=/api/server_templates/$ST_DB \
-d server[instance][multi_cloud_image_href]=/api/multi_cloud_images/$MCI_DB \
-d server[instance][instance_type_href]=/api/clouds/$CLOUD/instance_types/$ITYPE_DB \
-d server[instance][security_group_hrefs][]=/api/clouds/$CLOUD/security_groups/$SG \
https://my.rightscale.com/api/servers
echo "Done (db server #2)"

Run Script

Running the script is simple.  It does require one command line argument however.  Whatever you specify will be prepended to every server's name in your deployment.  (This could be useful if working in a classroom environment.  For example, prepend with real names or simply Student1, Student2, etc.)

Reminder:  Don't forget to change your script's permissions so that it is executable.  Further, if the current directory "." is not in your environment's PATH variable you must invoke your script explicitly.  (As shown below.)

Before running your script:

  • Navigate to the Deployment you are adding your 3 tier servers too
  • Note what servers exist

Now run your script, providing the name to prepend.  (Note:  Example output truncated.)

$ ./3tier Bethany
Reading configuration file. . .
Done (config)
Values set: 252299001 240802001 9F6N6MA39F7E9 252293001 240802001 9F6N6MA39F7E9 269641001 240802001 9F6N6MA39F7E9
Create Load Balancer #1
HTTP/1.1 201 Created
Server: nginx/1.0.15
Date: Mon, 05 Nov 2012 16:40:17 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
Location: /api/servers/571162001
X-Runtime: 688
X-Request-Uuid: f591fd74f1674457964586ee258265af
Set-Cookie:
Cache-Control: no-cache

Done (load bal 1)
Create Load Balancer #2
HTTP/1.1 201 Created
Server: nginx/1.0.15
Date: Mon, 05 Nov 2012 16:40:18 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
Location: /api/servers/571163001
X-Runtime: 531
X-Request-Uuid: 3ab82dfdc84d45079b656ec605d6484a
Set-Cookie:
Cache-Control: no-cache

Done (load bal 2)

. . . . 
Done (db server #2)

Now refresh your browser.  You should see all servers created based on the cloud resources you specified in your configuration files.  In our example:

screen-e2e create servers via API.png

Post Tutorial Steps

There are many ways to configure and accomplish the steps in this tutorial. It is meant as a learning tool and provide ideas that could work in your specific use case.  Some may want to explore further or implement slightly different based on their needs.

  • Convert the script above to a RightScript that uses inputs not configuration files
  • Locate a client for your smart phone so you can invoke an API script remotely
  • Add more elaborate configuration files and error checking/validation to your script
You must to post a comment.
Last modified
14:11, 19 Jun 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.