RightScale Support Wiki > References > Decommission Scripts

Decommission Scripts


There are 3 types of RightScripts:

  • Boot Scripts - RightScripts that are called when a new instance is launched and in the boot-up phase.
  • Operational Scripts - RightScripts avalable from the Dashboard  in the operational phase.
  • Decommission Scripts  - RightScripts that are called when an instance is terminated.


Decommission Scripts
are essentially operational scripts that help you safely terminate (decommission) an instance from a deployment.  Previously, when an instance was terminated, there was no way to perform any last minute actions before it was terminated and disappeared.  Decommission scripts provide a great way to gracefully "un-launch" an instance and perform important last minute actions that need to be done before an instance is permanently terminated.  However, you only have 100 seconds to perform these actions, so you must be selective when choosing which scripts/actions to run.   If you list too many actions or one of them takes a long time to complete, the remaining actions might not have enough time to be successfully executed.

NOTE: 

If the time allotted for Instance Termination is exceeded, the remaining portion of the script and all scripts that follow will not be called.

What types of Decommision Scripts are recommended?

Decommission scripts should only be used for performing critical last minute actions that do not require a long time to complete.

  • Deregister the instance's IP address.  It usually takes a second or less to perform.
  • Disconnect the instance from (HAProxy) load balancers.
What types of events are not recommended?
  • Backups to S3.  Large backups may take a very long time and partial backups may cause problems later.
  • Sending emails.  The e-mail is queued, but may never get sent in time.

 

How do I perform actions that take longer than 100 seconds before an instance is terminated?

There may be cases where 100 seconds is an insufficient amount of time to perform some necessary final actions.  Therefore, you cannot use decommissioning scripts.  In such cases, you'll need to perform these longer actions using operational scripts since there is no time limit associated with operational scripts.   Simply end the operational script with a request to EC2 to terminate this server.  After the self-termination, decommission scripts will run in the same way they would have run from the RightScale Dashboard.

Example:

Create a tar file of your whole directory (tar/home) and save it to S3 before your instance disappears.

The script below is very simple.  For instance, you may want to add more checks to see if the backup is 100% solid.  Feel free to cut and paste this code as an example and expand on the error checks as needed. 

#!/bin/bash
#
# Description: Perform a backup and then Terminate this Server.
#
source /etc/profile # Must be first, the same for the OS login.
source /var/spool/ec2/meta-data.sh
source /var/spool/ec2/user-data.sh
#
# Work in a safe place to write.
# 
cd /tmp 
#
#  At this point in the script we have all of the ENV set like a login to root.
#

#
# Backup all of the /home file tree,  this may take a while....
#
# NOTE: Assumes that we havce lots of free space. Watch out.
# 
tar czf /tmp/my_home_tar.tgz   /home
#
# Save the backup in S3 for later use.
#
#  You need an AWS S3 account for this step.
#
# NOTE: Script assumes that the bucket exists before the stript is run.
#
s3cmd put my_bucket:my_home_tar.tgz /tmp/my_home_tar.tgz
#
# Terminate this server now that the data is saved.
#
# NOTE:  $EC2_INSTANCE_ID  has the instances name for this instance.
# 
ec2-terminate-instances $EC2_INSTANCE_ID
#
# This script ends while EC2 is doing the terminate.
#
exit 0 # Leave with a smile
Tag page
You must login to post a comment.