purge_EBS_Snapshots now more portable - works with both OS X/BSD Date and GNU/Linux Date command.

This commit is contained in:
Colin Johnson 2012-11-09 07:26:38 +00:00
parent e76a6743b4
commit c02743304a

View File

@ -68,23 +68,45 @@ create_EBS_Snapshot_Tags()
fi fi
} }
date_command_get() date_binary_get()
{ {
#finds full path to date binary #`uname -o (operating system) would be ideal, but OS X / Darwin does not support to -o option`
date_binary_full_path=`which date` #`uname` on OS X defaults to `uname -s` and `uname` on GNU/Linux defaults to `uname -s`
#command below is used to determine if date binary is gnu, macosx or other uname_result=`uname`
date_binary_file_result=`file -b $date_binary_full_path` case $uname_result in
case $date_binary_file_result in Darwin) date_binary="osx-posix" ;;
"Mach-O 64-bit executable x86_64") date_binary="macosx" ;; Linux) date_binary="linux-gnu" ;;
"ELF 64-bit LSB executable, x86-64, version 1 (SYSV)"*) date_binary="gnu" ;;
*) date_binary="unknown" ;; *) date_binary="unknown" ;;
esac esac
#based on the installed date binary the case statement below will determine the method to use to determine "purge_after_days" in the future }
get_purge_after_date()
{
#based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_days" in the future
case $date_binary in case $date_binary in
gnu) date_command="date -d +${purge_after_days}days -u +%Y-%m-%d" ;; linux-gnu) echo `date -d +${purge_after_days}days -u +%Y-%m-%d` ;;
macosx) date_command="date -v+${purge_after_days}d -u +%Y-%m-%d" ;; osx-posix) echo `date -v+${purge_after_days}d -u +%Y-%m-%d` ;;
unknown) date_command="date -d +${purge_after_days}days -u +%Y-%m-%d" ;; *) echo `date -d +${purge_after_days}days -u +%Y-%m-%d` ;;
*) date_command="date -d +${purge_after_days}days -u +%Y-%m-%d" ;; esac
}
get_purge_after_date_epoch()
{
#based on the date_binary variable, the case statement below will determine the method to use to determine "purge_after_date_epoch" in the future
case $date_binary in
linux-gnu) echo `date -d $purge_after_date +%s` ;;
osx-posix) echo `date -j -f "%Y-%m-%d" $purge_after_date "+%s"` ;;
*) echo `date -d $purge_after_date +%s` ;;
esac
}
get_date_current_epoch()
{
#based on the date_binary variable, the case statement below will determine the method to use to determine "date_current_epoch" in the future
case $date_binary in
linux-gnu) echo `date -d $date_current +%s` ;;
osx-posix) echo `date -j -f "%Y-%m-%d" $date_current "+%s"` ;;
*) echo `date -d $date_current +%s` ;;
esac esac
} }
@ -105,8 +127,8 @@ purge_EBS_Snapshots()
then echo "A Snapshot with the Snapshot ID $snapshot_id_evaluated has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfter=YYYY-MM-DD\" date. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2 then echo "A Snapshot with the Snapshot ID $snapshot_id_evaluated has the tag \"PurgeAllow=true\" but does not have a \"PurgeAfter=YYYY-MM-DD\" date. $app_name is unable to determine if $snapshot_id_evaluated should be purged." 1>&2
else else
#convert both the date_current and purge_after_date into epoch time to allow for comparison #convert both the date_current and purge_after_date into epoch time to allow for comparison
date_current_epoch=`date -j -f "%Y-%m-%d" "$date_current" "+%s"` date_current_epoch=`get_date_current_epoch`
purge_after_date_epoch=`date -j -f "%Y-%m-%d" "$purge_after_date" "+%s"` purge_after_date_epoch=`get_purge_after_date_epoch`
#perform compparison - if $purge_after_date_epoch is a lower number than $date_current_epoch than the PurgeAfter date is earlier than the current date - and the snapshot can be safely removed #perform compparison - if $purge_after_date_epoch is a lower number than $date_current_epoch than the PurgeAfter date is earlier than the current date - and the snapshot can be safely removed
if [[ $purge_after_date_epoch < $date_current_epoch ]] if [[ $purge_after_date_epoch < $date_current_epoch ]]
then then
@ -152,11 +174,11 @@ date_current=`date -u +%Y-%m-%d`
#sets the PurgeAfter tag to the number of days that a snapshot should be retained #sets the PurgeAfter tag to the number of days that a snapshot should be retained
if [[ -n $purge_after_days ]] if [[ -n $purge_after_days ]]
then then
#if the date_binary is not set, call the date_command_get function #if the date_binary is not set, call the date_binary_get function
if [[ -z $date_binary ]] if [[ -z $date_binary ]]
then date_command_get then date_binary_get
fi fi
purge_after_date=`$date_command` purge_after_date=`get_purge_after_date`
echo "Snapshots taken by $app_name will be eligible for purging after the following date: $purge_after_date." echo "Snapshots taken by $app_name will be eligible for purging after the following date: $purge_after_date."
fi fi