There are 3 types of RightScripts:
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, there is typically a time frame of a couple minutes depending on the cloud to perform these actions. 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 successfully execute.
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.
Decommission scripts should only be used for performing critical last minute actions that do not require a long time to complete.
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.
Using RightScale-enabled images w/ RightLink >= 5.6 is recommended for this technique.
The intention of sleeping is only to simulate running a script longer than 100 seconds.
After decommission call the system's shutdown command.
Note: depending on the kernel you are using (and if the instance is instance-store or EBS) a shutdown may stop the instance instead of terminate.
To ensure termination, use the RIghtScale or appropriate cloud API to terminate the instance by name or identifier.
#!/bin/sh -ex sleep 200; service rightlink decommission && shutdown -h now &
Create a tar archive of your whole home directory (/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.
Note: Amazon EC2 API Tools is required. You can use the Install Amazon EC2 API Tools RightScript to install these.
Your AWS/EC2 credentials are also needed. Use Import AWS and EC2 credentials to copy them to ~/root on the instance.
#! /bin/bash -x # 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.
For RightImages with RightLink v5.8 or later, you can use the rs_shutdown utility to gracefully reboot/shutdown/terminate the instance.
rs_shutdown -r #reboot the server rs_shutdown -t #terminate the server rs_shutdown -s #stop the server rs_shutdown -d #Request deferred shutdown (reboot, stop or terminate) pending finishing of any remaining scripts (default)
© 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.